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

cosmonauts

v1.0.6

Published

cosmosDB odm

Downloads

21

Readme

Cosmonauts

Cosmonauts is a module that provides a high-level abstraction layer for interacting with Azure Cosmos DB. The module includes a set of files that allow developers to easily create, read, update, and delete documents in their Cosmos DB collections.

modelBase.js serves as a base class that other models can inherit from, providing a set of common methods and properties such as the ability to define custom validation rules for model data, and support for defining virtual properties. This base class helps in reducing code duplication and promotes code reuse across different models.

DocumentDBRepository.js, on the other hand, is a utility module that provides a set of methods for performing common CRUD (Create, Read, Update, Delete) operations on DocumentDB, as well as support for pagination, querying, and transactional consistency. This module helps to abstract away the complexity of working directly with DocumentDB's API, providing a simplified and consistent interface for accessing and manipulating data stored in the database.

Together, these files provide a powerful set of tools for working with Cosmos DB, making it easier for developers to build scalable, performant, and reliable applications on the Azure platform.


How to use ModelBase class

The ModelBase class is a base class that can be extended to create models in a project. It provides basic properties and methods that can be used to manage data in a database.

Usage

To create a new model, extend the ModelBase class and add any additional properties and methods as needed. For example:

import { ModelBase } from 'cosmonauts'

class Person extends ModelBase {
  userName: string

  email: string

  password: string
  
  gender: string
}

The ModelBase class provides the following properties that can be accessed or set by the derived class:

  • id: The unique identifier for the model.
  • documentType: The type of the document, which is automatically generated based on the name of the derived class.
  • documentNamespace: The namespace of the document, which is automatically generated based on the name of the derived class.
  • partitionKey: The partition key for the document.
  • createdDate: The date the document was created.
  • createdBy: The user who created the document.
  • lastUpdatedDate: The date the document was last updated.
  • lastUpdatedBy: The user who last updated the document.
  • activeFlag: A flag indicating whether the document is active or not.
  • etag: The entity tag for the document.

For example, to get the id property of a MyModel object, you can use the id getter method:

const myModel = new MyModel();
const id = myModel.id;

How to use DocumentDBRepository

This is a Node.js module that provides an interface for interacting with a Microsoft Azure Cosmos DB database using the SQL API.

Usage

To use this module, first create unitOfWork.js file and import the module into your code like this:

import { DocumentDBRepository } from 'cosmonauts'
const DB = 'MyDB'
const client = new CosmosClient('your cosmosDB connection string)

export default class UnitOfWork {
  ModelRepository

  constructor() {
    this.ModelRepository = new DocumentDBRepository(DB, client, new MyModel(), partitionKeyProperties)
  }
}

Create a new document

To create a new document, call the CreateAsync method:

import UnitOfWork from 'repositories'

const unitOfWork = new UnitOfWork()
const item = { /* document data */ };
const createdBy = '[email protected]';
const activeFlag = 'Y';
const document = await unitOfWork.ModelRepository.CreateAsync(item, createdBy, activeFlag);

Retrieve a document by ID

To retrieve a document by ID, call the GetByIdAsync method:

import UnitOfWork from 'repositories'

const unitOfWork = new UnitOfWork()
const id = '12345';
const partitionKey = 'examplePartitionKey';
const document = await unitOfWork.ModelRepository.GetByIdAsync(id, partitionKey);

Update a document

To update a document, call the UpdateAsync method:

import UnitOfWork from 'repositories'

const unitOfWork = new UnitOfWork()
const id = '12345';
const lastUpdatedBy = '[email protected]';
const updated = await unitOfWork.ModelRepository.UpdateAsync(id, item, lastUpdatedBy);

Delete a document

To delete a document, call the DeleteAsync method:

import UnitOfWork from 'repositories'

const unitOfWork = new UnitOfWork()
const id = '12345';
const partitionKey = 'examplePartitionKey';
const deleted = await unitOfWork.ModelRepository.DeleteAsync(id, partitionKey);

Query documents

To query documents, call the GetAsync method:

import UnitOfWork from 'repositories'

const unitOfWork = new UnitOfWork()
const options = {
  partitionKey: 'examplePartitionKey',
  usePaging: true,
  predicate: (item) => item.userName === 'example',
  selector: ['userName'],
  orderBy: { property: 'userName', descending: true },
  pageSize: 10
};
const documents = await unitOfWork.Modelrepository.GetAsync(options);

Where options is an object that contains the following properties:

  • partitionKey (optional) - The partition key to use for the query.
  • usePaging (optional) - Whether or not to use paging.
  • predicate (optional) - A function that returns a boolean indicating whether or not an item should be included in the query results.
  • selector (optional) - A string or array of an object properties to desired. (keyof Model or Array[keyof Model])
  • orderBy (optional) - An object that specifies the property to sort by and whether to sort in ascending or descending order.
  • pageSize (optional) - The maximum number of items to return per page.