3.2.1 • Published 3 years ago

pip-services3-data-node v3.2.1

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

Persistence components for Node.js

This module is a part of the Pip.Services polyglot microservices toolkit. It contains generic interfaces for data access components as well as abstract implementations for in-memory and file persistence.

The persistence components come in two kinds. The first kind is a basic persistence that can work with any object types and provides only minimal set of operations. The second kind is so called "identifieable" persistence with works with "identifable" data objects, i.e. objects that have unique ID field. The identifiable persistence provides a full set or CRUD operations that covers most common cases.

The module contains the following packages:

  • Core - generic interfaces for data access components.
  • Persistence - in-memory and file persistence components, as well as JSON persister class.

Quick links:

Use

Install the NPM package as

npm install pip-services3-data-node --save

For example, you need to implement persistence for a data object defined as following.

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

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

Our 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 in-memory persistence component you shall inherit IdentifiableMemoryPersistence. 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 { IdentifiableMemoryPersistence } from 'pip-services3-data-node';

export class MyMemoryPersistence extends IdentifableMemoryPersistence {
  public constructor() {
    super();
  }

  private composeFilter(filter: FilterParams): any {
    filter = filter || new FilterParams();
    
    let id = filter.getAsNullableString("id");
    let tempIds = filter.getAsNullableString("ids");
    let ids = tempIds != null ? tempIds.split(",") : null;
    let key = filter.getAsNullableString("key");

    return (item) => {
        if (id != null && item.id != id)
            return false;
        if (ids != null && ids.indexOf(item.id) < 0)
            return false;
        if (key != null && item.key != key)
            return false;
        return true;
    };
  }
  
  public getPageByFilter(correlationId: string, filter: FilterParams, paging: PagingParams,
    callback: (err: any, page: DataPage<MyObject>) => void): void {
    super.getPageByFilter(correlationId, this.composeFilter(filter), paging, null, null, callback);
  }  
  
  public getOneByKey(correlationId: string, key: string,
    callback: (err: any, item: MyObject) => void): void {
    
    let item = _.find(this._items, (item) => item.key == key);
    
    if (item != null) {
      this._logger.trace(correlationId, "Found object by key=%s", key);
    } else {
      this._logger.trace(correlationId, "Cannot find by key=%s", key);
    }
    
    callback(null, item);
  }

}

It is easy to create file persistence by adding a persister object to the implemented in-memory persistence component.

import { ConfigParams } from 'pip-services3-commons-node';
import { JsonFilePersister } from 'pip-services3-data-node';

export class MyFilePersistence extends MyMemoryPersistence {
  protected _persister: JsonFilePersister<MyObject>;

  constructor(path?: string) {
      super();
      this._persister = new JsonFilePersister<MyObject>(path);
      this._loader = this._persister;
      this._saver = this._persister;
  }

  public configure(config: ConfigParams) {
      super.configure(config);
      this._persister.configure(config);
  }
}

Develop

For development you shall install the following prerequisites:

  • Node.js 10+
  • 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 Node.js version of Pip.Services is created and maintained by:

  • Volodymyr Tkachenko
  • Sergey Seroukhov

The documentation is written by:

  • Mark Makarychev
@everything-registry/sub-chunk-2439pip-services-accounts-nodepip-services-activities-nodepip-services-announcements-nodepip-services-applications-nodepip-services-attachments-nodepip-services-beacons-nodepip-services-blobs-nodepip-services-clusters-nodepip-services-comments-nodepip-services-creditcards-nodepip-services-dashboards-nodepip-services-email-nodepip-services-emailsettings-nodepip-services-eventlog-nodepip-services-feedbacks-nodepip-services-guides-nodepip-services-help-nodepip-services-imagesets-nodepip-services-invitations-nodepip-services-jobs-nodepip-services-logging-nodepip-services-settings-nodepip-services-sms-nodepip-services-smssettings-nodepip-services-statistics-nodepip-services-tags-nodepip-services-tips-nodepip-services-transducerdata-nodepip-services-paymentmethods-nodepip-services-perfmon-nodepip-services-positions-nodepip-services-purchaseorders-nodepip-services-quotes-nodepip-services-roles-nodepip-services-routeanalysis-nodepip-services-routes-nodepip-services-msgdistribution-nodepip-services-msgtemplates-nodepip-services-organizations-nodepip-services-orgroles-nodepip-services-passwords-nodepip-services-metrics-nodepip-services-microfrontends-nodepip-services-sessions-nodepip-templates-microservice-nodepip-clients-comments-nodepip-services3-mongodb-nodepip-services3-sqlite-nodepip-services3-sqlserver-nodepip-services3-couchbase-nodepip-services3-mongoose-nodepip-services3-mysql-nodepip-services3-postgres-nodeiqs-services-agreements-nodeiqs-services-attendance-nodeiqs-services-cloudwatch-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-resolutions-nodeiqs-services-restgateway-nodeiqs-services-rosters-nodeiqs-services-shifts-nodeiqs-services-signals-nodeiqs-services-stateupdates-nodeiqs-services-teltonikagateway-nodeiqs-services-zones-nodeiqs-bindles-infrastructure-nodeiqs-bundles-configuration-nodeiqs-bundles-content-node@infinitebrahmanuniverse/nolb-pip
3.2.1

3 years ago

3.2.0

4 years ago

3.1.0

4 years ago

3.0.4

4 years ago

3.0.3

5 years ago