3.5.0 • Published 3 years ago

pip-services3-mongodb-node v3.5.0

Weekly downloads
132
License
MIT
Repository
github
Last release
3 years ago

MongoDB components for Node.js

This module is a part of the Pip.Services polyglot microservices toolkit. It provides a set of components to implement MongoDB persistence.

The module contains the following packages:

  • Build - Factory to create MongoDB persistence components.
  • Connect - Connection component to configure MongoDB connection to database.
  • Persistence - abstract persistence components to perform basic CRUD operations.

Quick links:

Use

Install the NPM package as

npm install pip-services3-mongodb-node --save

As an example, lets create persistence for the following data object.

import { IIdentifiable } from 'pip-services3-commons-node';

export class MyObject implements IIdentifiable {
  public id: string;
  public key: string;
  public value: number;
}

The persistence component shall implement the following interface with a basic set of CRUD operations.

export interface IMyPersistence {
    getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams,
      callback: (err: any, page: DataPage<MyObject>) => void): void;
    
    getOneById(correlationId: string, id: string, callback: (err: any, item: MyObject) => void): void;
    
    getOneByKey(correlationId: string, key: string, callback: (err: any, item: MyObject) => void): void;
    
    create(correlationId: string, item: MyObject, callback?: (err: any, item: MyObject) => void): void;
    
    update(correlationId: string, item: MyObject, callback?: (err: any, item: MyObject) => void): void;
    
    deleteById(correlationId: string, id: string, callback?: (err: any, item: MyObject) => void): void;
}

To implement mongodb persistence component you shall inherit IdentifiableMongoDbPersistence. Most CRUD operations will come from the base class. You only need to override getPageByFilter method with a custom filter function. And implement a getOneByKey custom persistence method that doesn't exist in the base class.

import { IdentifiableMongoDbPersistence } from 'pip-services3-mongodb-node';

export class MyMongoDbPersistence extends IdentifableMongoDbPersistence {
  public constructor() {
    super("myobjects");
    this.ensureIndex({ key: 1 }, { unique: true });
  }

  private composeFilter(filter: FilterParams): any {
    filter = filter || new FilterParams();
    
    let criteria = [];

    let id = filter.getAsNullableString('id');
    if (id != null)
        criteria.push({ _id: id });

    let tempIds = filter.getAsNullableString("ids");
    if (tempIds != null) {
        let ids = tempIds.split(",");
        criteria.push({ _id: { $in: ids } });
    }

    let key = filter.getAsNullableString("key");
    if (key != null)
        criteria.push({ key: key });

    return criteria.length > 0 ? { $and: criteria } : null;
  }
  
  public getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams,
    callback: (err: any, page: DataPage<MyObject>) => void): void {
    super.getPageByFilter(correlationId, this.composeFilter(filter), paging, "_id", null, callback);
  }  
  
  public getOneByKey(correlationId: string, key: string,
    callback: (err: any, item: MyObject) => void): void {
    
    let filter = { key: key };

    this._collection.findOne(filter, (err, item) => {
      if (item == null)
        this._logger.trace(correlationId, "Nothing found from %s with key = %s", this._collectionName, key);
      else
        this._logger.trace(correlationId, "Retrieved from %s with key = %s", this._collectionName, key);

      item = this.convertToPublic(item);
      callback(err, item);
    });
  }

}

Configuration for your microservice that includes mongodb persistence may look the following way.

...
{{#if MONGODB_ENABLED}}
- descriptor: pip-services:connection:mongodb:con1:1.0
  connection:
    uri: {{{MONGO_SERVICE_URI}}}
    host: {{{MONGO_SERVICE_HOST}}}{{#unless MONGO_SERVICE_HOST}}localhost{{/unless}}
    port: {{MONGO_SERVICE_PORT}}{{#unless MONGO_SERVICE_PORT}}27017{{/unless}}
    database: {{MONGO_DB}}{{#unless MONGO_DB}}app{{/unless}}
  credential:
    username: {{MONGO_USER}}
    password: {{MONGO_PASS}}
    
- descriptor: myservice:persistence:mongodb:default:1.0
  dependencies:
    connection: pip-services:connection:mongodb:con1:1.0
  collection: {{MONGO_COLLECTION}}{{#unless MONGO_COLLECTION}}myobjects{{/unless}}
{{/if}}
...

Develop

For development you shall install the following prerequisites:

  • Node.js 8+
  • Visual Studio Code or another IDE of your choice
  • Docker
  • Typescript

Install dependencies:

npm install

Compile the code:

tsc

Run automated tests:

npm test

Generate API documentation:

./docgen.ps1

Before committing changes run dockerized build and test as:

./build.ps1
./test.ps1
./clear.ps1

Contacts

The library is created and maintained by Sergey Seroukhov.

The documentation is written by Mark Makarychev.

@everything-registry/sub-chunk-2439service-pushnotifications-nodeiqs-services-resolutions-nodeiqs-bindles-infrastructure-nodeiqs-bundles-configuration-nodeiqs-bundles-content-nodeiqs-services-agreements-nodeiqs-services-attendance-nodeiqs-services-controlobjects-nodeiqs-services-corrections-nodeiqs-services-currdevicestates-nodeiqs-services-currobjectstates-nodeiqs-services-dataprofiles-nodeiqs-services-deviceconfigs-nodeiqs-services-deviceprofiles-nodeiqs-services-devices-nodeiqs-services-emergencyplans-nodeiqs-services-eventgeneration-nodeiqs-services-eventrules-nodeiqs-services-eventtemplates-nodeiqs-services-gateways-nodeiqs-services-incidents-nodeiqs-services-locations-nodeiqs-services-mqttgateway-nodeiqs-services-objectgroups-nodeiqs-services-objectstates-nodeiqs-services-opevents-nodeiqs-services-zones-nodeiqs-services-rosters-nodeiqs-services-shifts-nodeiqs-services-signals-nodeiqs-services-stateupdates-nodepip-templates-microservice-nodepip-services-invitations-nodepip-services-jobs-nodepip-services-logging-nodepip-services-metrics-nodepip-services-microfrontends-nodepip-services-msgtemplates-nodepip-services-purchaseorders-nodepip-services-quotes-nodepip-services-roles-nodepip-services-routeanalysis-nodepip-services-routes-nodepip-services-tags-nodepip-services-tips-nodepip-services-transducerdata-nodepip-services-accounts-nodepip-services-activities-nodepip-services-dashboards-nodepip-services-emailsettings-nodepip-services-eventlog-nodepip-services-help-nodepip-services-imagesets-nodepip-services-organizations-nodepip-services-passwords-nodepip-services-paymentmethods-nodepip-services-perfmon-nodepip-services-positions-nodepip-services-sessions-nodepip-services-settings-nodepip-services-smssettings-nodepip-services-statistics-nodepip-services-comments-nodepip-services-creditcards-nodepip-services-announcements-nodepip-services-applications-nodepip-services-attachments-nodepip-services-beacons-nodepip-services-clusters-nodepip-services-feedbacks-nodepip-services-guides-nodepip-clients-comments-node@infinitebrahmanuniverse/nolb-pip
3.5.0

3 years ago

3.4.0

4 years ago

3.3.0

4 years ago

3.2.7

4 years ago

3.2.6

4 years ago

3.2.5

4 years ago

3.2.4

4 years ago

3.2.3

4 years ago

3.2.2

4 years ago

3.2.1

4 years ago

3.2.0

4 years ago

3.1.0

5 years ago

3.0.4

5 years ago

3.0.3

5 years ago