1.0.2 • Published 4 years ago

sengi-cosmosdb v1.0.2

Weekly downloads
5
License
MIT
Repository
github
Last release
4 years ago

Sengi-CosmosDB

npm.io npm JavaScript Style Guide

A wrapper for Azure Cosmos DB that implements the Sengi document store interface.

Installation

npm install sengi-cosmosdb

Usage

To instantiate a CosmosDbDocStore you have to provide the following parameters:

  • cosmosUrl - A url that identifies your Cosmos DB instance.

  • cosmosKey - A master key with read and write access to your Cosmos DB instance.

  • config - A configuration object

    • getDatabaseName - A function (docTypeName, docTypePluralName, options) that returns the name of the database to connect to.

    • getContainerName - A function getContainerName (databaseName, docTypeName, docTypePluralName, options) that returns the name of the container to edit.

    • getPartitionKey - A function (databaseName, containerName, docTypeName, docTypePluralName, id, options) that returns the value of the containers partition key. Typically this will return the id or something from the options object that has been populated by the Sengi interface. For example, the Sengi-Express library will place the additional path components (for example the tenant ID) into the options for use here.

const { createCosmosDbDocStore } = require('sengi-cosmosdb')

const cosmosDbDocStore = createCosmosDbDocStore({
  cosmosUrl: 'https://documents.azure...',
  cosmosKey: 'ABCD_KEY_FROM_AZURE_PORTAL',
  config: {
    getDatabaseName: (docTypeName, docTypePluralName, options) => 'myDatabase',
    getContainerName: (databaseName, docTypeName, docTypePluralName, options) => 'myContainer',
    getPartitionKey: (databaseName, containerName, docTypeName, docTypePluralName, id, options) => id
  }
)

cosmosDbDocStore.upsert('test', 'tests', {
  id: '0001',
  docType: 'test',
  docOps: [],
  hello: 'world'
})

Development

Code base adheres to the rules chosen by https://standardjs.com/. Code is formatted with 2 spaces.

Tests are written using Jest with 100% coverage.

To run the tests locally you will need an Azure Cosmos Account with:

  • A database named sengi
  • A container namd mixed with a partition key of /id.
  • A container named shows with a partition key of /city.

You will also need to set the following environment variables on your local machine:

  • SENGI_COSMOS_URL: https://<your-account-name>.documents.azure.com
  • SENGI_COSMOS_KEY: abcdabcdabcdabcdabcdabcd==
npm test

Note that the tests run sequentially (jest --runInBand) so that only one test can access the database at a time.

Continuous Deployment

Any pushes or pull-requests on non-master branches will trigger the test runner.

Any pushes to master will cause the library to be re-published.