Skip to content

a8m/azure-documentdb-node

 
 

Repository files navigation

Microsoft Azure DocumentDB nodejs SDK

DocumentDB is a purpose built NoSQL JSON document database designed for modern mobile and web applications. DocumentDB supports rich queries over JSON data as well as transactional execution of JavaScript based application logic.
DocumentDB is built with a deep commitment to the JSON data model enabling it to offer differentiated query and data processing capabilities that scale to meet the needs of the most demanding modern applications.

The Nodejs sdk has two API models:
Callback model: where all calls use callbacks for responses and this is exposed in DocumentClient.
Promises model: where all calls use Q promises for responses ( this one is built on top of the callback model and this is exposed in DocumentClientWrapper.

Installing the library using npm

npm install documentdb

Hello world example code using callbacks

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);
    });
});

});

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

Hello world example code using Q promises

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

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!" };

var database, collection, document; client.createDatabaseAsync(databaseDefinition) .then(function(databaseResponse) { database = databaseResponse.resource; return client.createCollectionAsync(database._self, collectionDefinition); }) .then(function(collectionResponse) { collection = collectionResponse.resource;

    return client.createDocumentAsync(collection._self, documentDefinition);
})
.then(function(documentResponse) {
	var document = documentResponse.resource;
	console.log('Created Document with content: ', document.content);
    cleanup(client, database);
})
.fail(function(error) {
    console.log("An error occured", error);
});

function cleanup(client, database) { client.deleteDatabaseAsync(database._self) .then(function(response) { console.log('clean up completed'); }) .fail(function(error){ console.log(error); }); }

Running the tests

  
First You need to install mocha and q
> npm install mocha
> npm install q

From the nodejsclient folder run > mocha -t 50000 -R spec

About

No description, website, or topics provided.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • JavaScript 100.0%