npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

documentdb-util

v2.6.1

Published

A little wrapper utility around document db to perform simple operations

Downloads

36

Readme

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