Giter Site home page Giter Site logo

documentdb-util's Introduction

Npm Version Npm Downloads

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

List Collections

let collections = await dbUtil.listCollections(database);

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:"summer",
    serverScript: function(a,b){
        var context = getContext();
        var response = context.getResponse();
        let sum = a + b;
        response.setBody(sum);
    }
}

let procInstance = await dbUtil.createStoredProcedure(collection, proc);
let result = await dbUtil.executeStoredProcedure(procInstance,[1,2]);

Create & execute Triggers

let database = await dbUtil.database('test');
let collection = await dbUtil.collection(database, 'people');

let superTimeTrigger = {
    id: "validateDocumentContents",
    serverScript: function validate() {
        var context = getContext();
        var request = context.getRequest();
        var documentToCreate = request.getBody();
        var ts = new Date();
        documentToCreate["supertime"] = ts.getTime();
        request.setBody(documentToCreate);
    },
    triggerType: 'Pre',
    triggerOperation: 'Create'
}

let triggerInstance = await dbUtil.trigger(collection, superTimeTrigger);

await dbUtil.insert(collection, {
    name: 'penguin',
    profession: 'better guy',
    income: 200
}, { preTriggerInclude: [superTimeTrigger.id] });

Create & execute User Defined Functions

var taxUdf = {
    id: "tax",
    serverScript: function tax(income) {

        if (income == undefined)
            throw 'no input';

        if (income < 1000)
            return income * 0.1;
        else if (income < 10000)
            return income * 0.2;
        else
            return income * 0.4;
    }
}

let udf = await dbUtil.userDefinedFunction(collection, taxUdf);
await dbUtil.insert(collection, {
    name: 'boomer',
    profession: 'rich guy',
    income: 10000
});

let spec2 = {
    query: 'Select * from c WHERE udf.tax(c.income) > @taxAmount',
    parameters: [
        {
            name: '@taxAmount',
            value: 3000
        }
    ]
}

let doc2 = await dbUtil.query(collection, spec2);

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

documentdb-util's People

Contributors

spatney avatar

Stargazers

 avatar

Watchers

 avatar

Forkers

shelleysu84

documentdb-util's Issues

Unexpected identifier on "await dbUtil.database('mydatabase');"

Hi, just wondering if anyone knows what's wrong here?

With simply the following it's failing with 'Unexpected identifier' on the dbUtil object. I tested on node version 8.9 and 8.9.1 with documentdb and documentdb-util installed.

var DocumentDbUtility = require("documentdb-util");
var dbUtil = new DocumentDbUtility({authKey: 'xxx' host: 'xxx'});
let database = await dbUtil.database('newzible-test');
                     ^^^^^

SyntaxError: Unexpected identifier
at createScript (vm.js:80:10)
at Object.runInThisContext (vm.js:139:10)
at Module._compile (module.js:599:28)
at Object.Module._extensions..js (module.js:646:10)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.