Détail du package

motorq-documentdb

Azure Cosmos DB Service Node.js SDK for SQL API

cosmosdb, cosmos db, documentdb, document database

readme

🚨Deprecated 🚨

We recently announced deprecation of JS v1 SDK. Starting September 2020 Microsoft will not provide support for this library. Existing applications using this library will continue to work as-is. We strongly recommend upgrading to @azure/cosmos library. Please file any issues in the Azure SDK for JS repo https://github.com/Azure/azure-sdk-for-js

Microsoft Azure Cosmos DB Node.js SDK v1

This project provides Node.js SDK library for SQL API of Azure Cosmos DB Database Service. This project also includes samples, tools, and utilities.

Useful links:

Installation

Prerequisites

Install Node.js and npm https://docs.npmjs.com/getting-started/installing-node

Node SDK can be consumed in two ways.

Install Core Module Published to NPM

The core module uses the callbacks model for responses, exposed through the DocumentClient

npm install documentdb

Install Core Module From Github

  1. Clone Azure/azure-documentdb-node repository Please clone the source and tests from https://github.com/Azure/azure-documentdb-node

  2. Install documentdb

     npm install azure-documentdb-node\source

Prerequisites

To use the SDK, first create an account and follow tutorial.

Note:

When connecting to the emulator from the SDK, SSL verification is disabled.

Follow these instructions to run the tests locally.

Traces

The documentdb module support tracing via the debug module. Traces will go to stderr by default. To enable tracing, you can set the DEBUG environment variable in a variety of ways.

  • documentdb:* will output all logs. This can be verbose, so it's helpful to filter on log level.
  • documentdb:<log level>:* will output all traces for a given <log level> value. The valid levels are error, warn, info, and debug.
  • documentdb:<log level>:<component> will output all traces for a given <log level> and <component>. The valid components are request and query.

You can combine filters via ,. So if you wanted to have error info for all components, but only debug info for query, then you'd use documentdb:error:*,documentdb:debug:query.

Tests

Prerequisites

  1. Clone Azure/azure-documentdb-node repository Please clone the source and tests from https://github.com/Azure/azure-documentdb-node

  2. Install Node.js and npm https://docs.npmjs.com/getting-started/installing-node

  3. Install mocha package globally

     npm install -g mocha

Running the tests

Using your command-line tool, from the root of your local copy of azure-documentdb-node repository: (If you are contributing changes and submitting PR then you need to ensure that you run the tests against your local copy of the source, and not the published npm package.)

  1. Remove documentdb, if previously installed

     npm remove documentdb
  2. Install documentdb

     npm install source
  3. Change to test directory

     cd source\test
  4. Run the tests

     mocha -t 0 -R spec

If you just want to run the tests against the published npm package then skip steps #1 & #2 proceed directly to step #3

Examples

Hello World using Callbacks via the Core Module

var DocumentClient = require('documentdb').DocumentClient;

var host = "[hostendpoint]";                     // Add your endpoint
var masterKey = "[database account masterkey]";  // Add the masterkey of the endpoint
var client = new DocumentClient(host, {masterKey: masterKey});

var databaseDefinition = { id: "sample database" };
var collectionDefinition = { id: "sample collection" };
var documentDefinition = { id: "hello world doc", content: "Hello World!" };

client.createDatabase(databaseDefinition, function(err, database) {
    if(err) return console.log(err);
    console.log('created db');

    client.createCollection(database._self, collectionDefinition, function(err, collection) {
        if(err) return console.log(err);
        console.log('created collection');

        client.createDocument(collection._self, documentDefinition, function(err, document) {
            if(err) return console.log(err);
            console.log('Created Document with content: ', document.content);

            cleanup(client, database);
        });
    });
});

function cleanup(client, database) {
    client.deleteDatabase(database._self, function(err) {
        if(err) console.log(err);
    })
}

Youtube Videos

Getting started with Node.js SDK:

Azure Demo: Getting started with Document Node.js SDK

Need Help?

Be sure to check out the Microsoft Azure Developer Forums on MSDN or the Developer Forums on Stack Overflow if you have trouble with the provided code.

Contribute Code or Provide Feedback

If you would like to become an active contributor to this project please follow the instructions provided in Azure Projects Contribution Guidelines.

If you encounter any bugs with the library please file an issue in the Issues section of the project.

changelog

Changes in 1.15.3

  • Bug with undefined variable during partition key refresh

Changes in 1.15.2

  • Adds additional logging and hardening. This includes additional error/warn/info logs which have information about the https.agent state + all errors are now logged
  • Adds a separate watchdog timeout on https requests to handle cases where https.request/socket timeouts themselves get bogged down. This time produces a scary looking error as you'll only see it when running under too much load for a given client/app.
  • Adds isTimedout and duration to error messages that come from request pipeline. These indicate whether a timeout occurred for the error and what the duration of the request is as measure from the creation of the request callback itself (not based on socket time/etc.)

Changes in 1.15.1

  • Adds additional logging and hardening

Changes in 1.15.0

  • Adds logging support
  • Fixes cross partition aggregate support
  • Fixes some cases where cross partition query silently failed

Changes in 1.14.5 :

  • Added endpoint retry on ECONNREFUSED errors to help with failover

Changes in 1.14.4 :

  • npm documentation fixed.

Changes in 1.14.3 :

  • Added support for default retries on connection issues.
  • Added support to read collection change feed.
  • Fixed session consistency bug that intermittently caused "read session not available".
  • Added support for query metrics.
  • Modified http Agent's maximum number of connections.

Changes in 1.14.2 :

  • Updated documentation to use Azure Cosmos DB.
  • Added Support for proxyUrl setting in ConnectionPolicy.

Changes in 1.14.1 :

  • Minor fix for case sensitive file systems.

Changes in 1.14.0 :

  • Adds support for Session Consistency.
  • This SDK version requires the latest version of Azure Cosmos DB Emulator available for download from https://aka.ms/cosmosdb-emulator.

Changes in 1.13.0 :

  • Splitproofed cross partition queries.
  • Adds supports for resource link with leading and trailing slashes (and corresponding tests).

Changes in 1.12.2 :

  • npm documentation fixed.

Changes in 1.12.1 :

  • Fixed bug in executeStoredProcedure where documents involved had special unicode characters (LS, PS).
  • Fixed bug in handling documents with unicode characters in partition key.
  • Fixed support for creating collection with name media (github #114).
  • Fixed support for permission authorization token (github #178).

Changes in 1.12.0 :

  • Added support for Request Unit per Minute (RU/m) feature.
  • Added support for a new consistency level called ConsistentPrefix.
  • Added support for UriFactory.
  • Fixed the unicode support bug (github #171)

Changes in 1.11.0 :

  • Added the support for aggregation queries (COUNT, MIN, MAX, SUM, and AVG).
  • Added the option for controlling degree of parallelism for cross partition queries.
  • Added the option for disabling SSL verification when running against Emulator.
  • Lowered minimum throughput on partitioned collections from 10,100 RU/s to 2500 RU/s.
  • Fixed the continuation token bug for single partition collection (github #107).
  • Fixed the executeStoredProcedure bug in handling 0 as single param (github #155).

Changes in 1.10.2 :

  • Fixed user-agent header to include the SDK version.
  • Minor code cleanup.

Changes in 1.10.1 :

  • Disabling SSL verification when using the SDK to target the emulator(hostname=localhost).
  • Added support for enabling script logging during stored procedure execution.

Changes in 1.10.0 :

  • Added support for cross partition parallel queries.
  • Added support for TOP/ORDER BY queries for partitioned collections.

Changes in 1.9.0 :

  • Added retry policy support for throttled requests. (Throttled requests receive a request rate too large exception, error code 429.) By default, DocumentClient retries nine times for each request when error code 429 is encountered, honoring the retryAfter time in the response header. A fixed retry interval time can now be set as part of the RetryOptions property on the ConnectionPolicy object if you want to ignore the retryAfter time returned by server between the retries. DocumentClient now waits for a maximum of 30 seconds for each request that is being throttled (irrespective of retry count) and returns the response with error code 429. This time can also be overriden in the RetryOptions property on ConnectionPolicy object.

  • DocumentClient now returns x-ms-throttle-retry-count and x-ms-throttle-retry-wait-time-ms as the response headers in every request to denote the throttle retry count and the cummulative time the request waited between the retries.

  • The RetryOptions class was added, exposing the RetryOptions property on the ConnectionPolicy class that can be used to override some of the default retry options.

Changes in 1.8.0 :

  • Added the support for geo-replicated database accounts.

Changes in 1.7.0 :

  • Added the support for TimeToLive(TTL) feature for documents.

Changes in 1.6.0 :

  • Added support for Partitioned Collections.
  • Added support for new offer types.

Changes in 1.5.6 :

  • Fixed RangePartitionResolver.resolveForRead bug where it was not returning links due to a bad concat of results.
  • Move compareFunction from Range class to RangePartitionResolver class.

Changes in 1.5.5 :

  • Fixed hashParitionResolver resolveForRead(): When no partition key supplied was throwing exception, instead of returning a list of all registered links.

Changes in 1.5.4 :

  • Dedicated HTTPS Agent: Avoid modifying the global. Use a dedicated agent for all of the lib’s requests.

Changes in 1.5.3 :

  • Properly handle dashes in the mediaIds.

Changes in 1.5.2 :

  • Fix memory leak.

Changes in 1.5.1 :

  • Renamed "Hash" directory to "hash".

Changes in 1.5.0 :

  • Added client-side sharding support.
  • Added hash partition resolver implementation.
  • Added range partitoin resolver implementation.

Changes in 1.4.0 :

  • Implement Upsert. New upsertXXX methods on documentClient.

Changes in 1.3.0 :

  • Skipped to bring version numbers in alignment with other SDKs.

Changes in 1.2.2 :

  • Split Q promises wrapper to new repository.
  • Update to package file for npm registry.

Changes in 1.2.1 :

  • Implements ID Based Routing.
  • Fixes Issue #49 - current property conflicts with method current().

Changes in 1.2.0 :

  • Added support for GeoSpatial index.
  • Validates id property for all resources. Ids for resources cannot contain ?, /, #, \, characters or end with a space.
  • Adds new header "index transformation progress" to ResourceResponse.

Changes in 1.1.0 :

  • Implements V2 indexing policy.

Changes in 1.0.3 :

Changes in 1.0.2 :

  • Issue #45 - Promises wrapper does not include header with error.

Changes in 1.0.1 :

  • Implemented ability to query for conflicts by adding readConflicts, readConflictAsync, queryConflicts.
  • Updated API documentation.
  • Issue #41 - client.createDocumentAsync error.