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

cosmosdb-node

v1.0.4

Published

The Node API for interacting with the CosmosDB REST API

Downloads

11

Readme

Note: This is a community created library and is not officially supported by the CosmosDB team.

CosmosDB API

Getting started

To get started and use this API, you first have to create a CosmosDB account in the Azure portal. Once this is done, just copy this example to get started:

// Your AccountName, this is part of your endpoint 
// (Example: https://my-cosmosdb-account.documents.azure.com" gives "my-cosmosdb-account" as AccountName)
const accountName = "my-cosmosdb-account";

// Find this in the Azure Portal under Keys for your CosmosDB Resource
const primaryKey = '<yourPrimaryKey>'; 

// Initialize the library and connection
const API = require('./CosmosDB')(accountName, primaryKey);

// An example API call
let database = await API.database.createIfNotExists(databaseName);

For more calls, see below or on: https://docs.microsoft.com/en-us/rest/api/documentdb/

Resources

Database

api.database.get(dbName)

NodeJS Call

await api.database.get(<dbName>)

Response
{ id: 'testDB',
  _rid: 'ogYeAA==',
  _self: 'dbs/ogYeAA==/',
  _etag: '"00004e00-0000-0000-0000-5ab64e0b0000"',
  _colls: 'colls/',
  _users: 'users/',
  _ts: 1521896971 }

api.database.create(dbName)

NodeJS Call

await api.database.create(<dbName>)

Response
{ id: 'testDB',
  _rid: 'ogYeAA==',
  _self: 'dbs/ogYeAA==/',
  _etag: '"00004e00-0000-0000-0000-5ab64e0b0000"',
  _colls: 'colls/',
  _users: 'users/',
  _ts: 1521896971 }

api.database.list()

NodeJS Call

await api.database.list()

Response
{ _rid: '',
  Databases: 
   [ { id: 'testDB',
       _rid: 'ogYeAA==',
       _self: 'dbs/ogYeAA==/',
       _etag: '"00004e00-0000-0000-0000-5ab64e0b0000"',
       _colls: 'colls/',
       _users: 'users/',
       _ts: 1521896971 } ],
  _count: 1 }

api.database.createIfNotExists(dbName)

NodeJS Call

await api.database.createIfNotExists(<dbName>)

Response
{ id: 'testDB',
  _rid: 'ogYeAA==',
  _self: 'dbs/ogYeAA==/',
  _etag: '"00004e00-0000-0000-0000-5ab64e0b0000"',
  _colls: 'colls/',
  _users: 'users/',
  _ts: 1521896971 }

api.database.delete(dbName)

NodeJS Call

await api.database.delete(<dbName>)

Response

No response will be returned if successful, only an error will be thrown if problems arise.

Collection

api.collection.get(dbName, collectionName)

NodeJS Call

await api.collection.get(<dbName>, <collectionName>)

Response
{ id: 'testColl',
  indexingPolicy: 
   { indexingMode: 'consistent',
     automatic: true,
     includedPaths: [ [Object] ],
     excludedPaths: [] },
  _rid: 'ogYeAMaxxwE=',
  _ts: 1521896971,
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/',
  _etag: '"00004f00-0000-0000-0000-5ab64e0b0000"',
  _docs: 'docs/',
  _sprocs: 'sprocs/',
  _triggers: 'triggers/',
  _udfs: 'udfs/',
  _conflicts: 'conflicts/' }

api.collection.create(dbName, collectionName)

NodeJS Call

await api.collection.create(<dbName>, <collectionName>)

Response
{ id: 'testColl',
  indexingPolicy: 
   { indexingMode: 'consistent',
     automatic: true,
     includedPaths: [ [Object] ],
     excludedPaths: [] },
  _rid: 'ogYeAMaxxwE=',
  _ts: 1521896971,
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/',
  _etag: '"00004f00-0000-0000-0000-5ab64e0b0000"',
  _docs: 'docs/',
  _sprocs: 'sprocs/',
  _triggers: 'triggers/',
  _udfs: 'udfs/',
  _conflicts: 'conflicts/' }

api.collection.list(dbName)

NodeJS Call

await api.collection.list(<dbName>)

Response
{ _rid: 'ogYeAA==',
  DocumentCollections: 
   [ { id: 'testColl',
       indexingPolicy: [Object],
       _rid: 'ogYeAMaxxwE=',
       _ts: 1521896971,
       _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/',
       _etag: '"00004f00-0000-0000-0000-5ab64e0b0000"',
       _docs: 'docs/',
       _sprocs: 'sprocs/',
       _triggers: 'triggers/',
       _udfs: 'udfs/',
       _conflicts: 'conflicts/' } ],
  _count: 1 }

api.collection.createIfNotExists(dbName, collectionName)

NodeJS Call

await api.collection.createIfNotExists(<dbName>, <collectionName>)

Response
{ id: 'testColl',
  indexingPolicy: 
   { indexingMode: 'consistent',
     automatic: true,
     includedPaths: [ [Object] ],
     excludedPaths: [] },
  _rid: 'ogYeAMaxxwE=',
  _ts: 1521896971,
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/',
  _etag: '"00004f00-0000-0000-0000-5ab64e0b0000"',
  _docs: 'docs/',
  _sprocs: 'sprocs/',
  _triggers: 'triggers/',
  _udfs: 'udfs/',
  _conflicts: 'conflicts/' }

api.collection.delete(dbName, collectionName)

NodeJS Call

await api.collection.delete(<dbName>, <collectionName>)

Response

No response will be returned if successful, only an error will be thrown if problems arise.

api.collection.truncate(dbName, collectionName)

Truncates the collection.

Note: This will delete and recreate the collection, so the ID will change

NodeJS Call

await api.collection.truncate(<dbName>, <collectionName>)

Response

No response will be returned if successful, only an error will be thrown if problems arise.

Document

api.document.get(dbName, collectionName, documentId)

NodeJS Call

await api.document.get(<dbName>, <collectionName>, <documentId>)

Response
{ id: 'test-doc',
  hello: 'world',
  obj: { test: 'doc' },
  arr: [ 'el1', 'el2' ],
  _rid: 'ogYeAMaxxwEBAAAAAAAAAA==',
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/docs/ogYeAMaxxwEBAAAAAAAAAA==/',
  _etag: '"00006a59-0000-0000-0000-5ab64e0c0000"',
  _attachments: 'attachments/',
  _ts: 1521896972 }

api.document.create(dbName, collectionName, document)

NodeJS Call

await api.document.create(<dbName>, <collectionName>, <document>)

Example
const newDoc = await api.document.create('testDB', 'testColl', {
  id: 'test-doc',
  hello: 'world',
  obj: {
      test: 'doc'
  },
  arr: [ "el1", "el2" ]
})
Response
{ id: 'test-doc',
  hello: 'world',
  obj: { test: 'doc' },
  arr: [ 'el1', 'el2' ],
  _rid: 'ogYeAMaxxwEBAAAAAAAAAA==',
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/docs/ogYeAMaxxwEBAAAAAAAAAA==/',
  _etag: '"00006a59-0000-0000-0000-5ab64e0c0000"',
  _attachments: 'attachments/',
  _ts: 1521896972 }

api.document.list(dbName, collectionName)

NodeJS Call

await api.document.list(<dbName>, <collectionName>)

Response
{ _rid: 'ogYeAMaxxwE=',
  Documents: 
   [ { id: 'test-doc',
       hello: 'world',
       obj: [Object],
       arr: [Array],
       _rid: 'ogYeAMaxxwEBAAAAAAAAAA==',
       _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/docs/ogYeAMaxxwEBAAAAAAAAAA==/',
       _etag: '"00006a59-0000-0000-0000-5ab64e0c0000"',
       _attachments: 'attachments/',
       _ts: 1521896972 } ],
  _count: 1 }

api.document.query(dbName, collectionName, )

NodeJS Call

await api.document.query(<dbName>, <collectionName>, <query>)

Example
const myDoc = await api.document.query('testDB', 'testColl', {
  query: `SELECT * FROM ${collectionName} c WHERE c.id = @id`,
  parameters: [
    {
      name: '@id',
      value: doc.id
    }
  ]
})
Response
{ _rid: 'oQVCAKobrAE=',
  Documents:
   [ { id: 'test-doc',
       hello: 'world',
       obj: [Object],
       arr: [Array],
       _rid: 'ogYeAMaxxwEBAAAAAAAAAA==',
       _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/docs/ogYeAMaxxwEBAAAAAAAAAA==/',
       _etag: '"00006a59-0000-0000-0000-5ab64e0c0000"',
       _attachments: 'attachments/',
       _ts: 1521897637 } ],
  _count: 1 }

api.document.createIfNotExists(dbName, collectionName, document)

NodeJS Call

await api.document.createIfNotExists(<dbName>, <collectionName>, <document>)

Example
const newDoc = await api.document.create('testDB', 'testColl', {
  id: 'test-doc',
  hello: 'world',
  obj: {
      test: 'doc'
  },
  arr: [ "el1", "el2" ]
})
Response
{ id: 'test-doc',
  hello: 'world',
  obj: { test: 'doc' },
  arr: [ 'el1', 'el2' ],
  _rid: 'ogYeAMaxxwEBAAAAAAAAAA==',
  _self: 'dbs/ogYeAA==/colls/ogYeAMaxxwE=/docs/ogYeAMaxxwEBAAAAAAAAAA==/',
  _etag: '"00006a59-0000-0000-0000-5ab64e0c0000"',
  _attachments: 'attachments/',
  _ts: 1521896972 }

api.document.delete(dbName, collectionName, documentId)

NodeJS Call

await api.document.delete(<dbName>, <collectionName>, <documentId>)

Response

No response will be returned if successful, only an error will be thrown if problems arise.