Document DB Utility
Here are some examples on how to use this Utility. Note, await is only avaliable as an experimental feature in Node. Each function returns a promise, so you can simple chain promises instead of using await.
Initialize the library
var DocumentDbUtility = require('documentdb-util');
var dbUtil = new DocumentDbUtility({authKey:'KEY', host:'URL TO DOCDB'}));
Get or Create Database
await dbUtil.database('test');
Get or Create Collection
let collection = await dbUtil.collection(database, 'people');
Insert Document
await dbUtil.insert(collection, {
name:'penguin',
profession: 'good guy'
});
Query for Documents
let spec = {
query: 'Select * from c where c.name = @name',
parameters:[
{
name:'@name',
value: 'penguin'
}
]
}
let docs = await dbUtil.query(collection, spec);
Update Document
let doc = (await dbUtil.query(collection, spec))[0];
doc.profession = "bad guy";
let docLink = dbUtil.createDocumentLink(database.id, collection.id, doc.id);
await dbUtil.update(docLink, doc);
Create & execute Stored Procedure
let proc = {
id:"jello",
serverScript: function() {
var context = getContext();
var response = context.getResponse();
response.setBody("Hello from Proc");
}
}
let procInstance = await dbUtil.storedProcedure(collection, proc);
let result = await dbUtil.executeStoredProcedure(procInstance);
Delete Document
let docLink = dbUtil.createDocumentLink(database.id, collection.id, doc.id);
await dbUtil.delete(docLink);
Delete Database
await dbUtil.deleteDatabase('name');
Delete Collection
await dbUtil.deleteCollection('dbName','collectionName');