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

@br-js/node-cosmosdb

v0.0.13

Published

A light weight azure cosmosdb client aiming at ease of use

Downloads

17

Readme

node-cosmos - A light weight Azure CosmosDB Client for nodejs

node-cosmos is a client for Azure CosmosDB 's SQL API (also called documentdb formerly). Which is an opinionated library aimed at ease of use for CRUD and find (aka. query).

Background

  • Microsoft's official nodejs CosmosDB client is verbose to use

Disclaimer

  • This is an alpha version, and features are focused to CRUD and find at present.

Quickstart

Start programming

import { Cosmos } from "node-cosmos"

const db = await new Cosmos("YOUR_CONNECTION_STRING").getDatabase("my-awesome-db");

await db.upsert("my-awesome-coll", { id:"id011", firstName: "Anony", lastName: "Nobody"} );
  
const users = await db.find("Collection1", {
    filter: {
        id : "id010", // id equals "id010"
        "lastName%" : "Nobo" // lastName starts with Nobo
    },
    sort: ["firstName", "ASC"],
    offset: 0,
    limit: 100
});

Examples

Work with partitions

// Save user into Coll:Collection1, partition:Users.
// If you do not specify the partition. It will default to coll name.

const db = await new Cosmos("YOUR_CONNECTION_STRING").getDatabase("Database1");

await db.upsert("Collection1", {id: "id011", firstName: "Tom", lastName: "Banks"}, "Users");

// The default partition key is "_partition", so we'll get a json like this:
// {
//    "id": "id011",
//    "firstName": "Tom",
//    "lastName": "Banks",
//    "_partition": "Users"
// }

const users = await db.find("Collection1", {
    filter: {
        id : ["id010", "id011"] // id equals "id010" or "id011"
        lastName : "Nobody" 
    },
    sort: ["firstName", "ASC"],
    offset: 0,
    limit: 100
}, "Users");

Create database and collection from zero

//get db(create if not exist)
const db = await new Cosmos(process.env.COSMOSDB_CONNECTION_STRING).getDatabase("Database1");

//create a collection(if not exist)
await db.createCollection("Collection1");

CRUD


const db = await new Cosmos(process.env.COSMOSDB_CONNECTION_STRING).getDatabase("Database1");

// Read
const user1 = await db.read("Collection1", "id001", "Users");

// Update
user1.lastName = "Updated";
await db.update("Collection1", user1, "Users");

// Upsert
await db.upsert("Collection1", user1, "Users");

// Delete
await db.delete("Collection1", user1.id, "Users");

Partial Update


// update the lastName field only
await db.update("Collection", {id: "id001", lastName:"LastNameUpdated"}, , "Users");

// if you want to override the item without partial update feature, you can use `upsert` instead, which does not perform partial updating.

Complex queries


const cond = {
  filter: {
    id: "id010", // id equal to 'id010'
    "lastName%": "Ban", // last name STARTSWITH "Ban"
    "firstName !=": "Andy", // not equal
    location:  ["New York", "Paris"], // location is 'New York' or 'Paris'. see cosmosdb IN 
    "age >=": 20, // see cosmosdb compare operators
    "desciption CONTAINS": "Project manager",// see cosmosdb CONTAINS
    "skill ARRAY_CONTAINS": "Java", // see cosmosdb ARRAY_CONTAINS
  },
  sort: ["lastName", "ASC"], //optional sort order
  offset: 0, //optional offset
  limit: 100 //optional limit
}

const users = await db.find("Collection1", cond, "Users");