/flexiblePersistence

A CQRS and Event Sourcing platform

Primary LanguageTypeScriptMIT LicenseMIT

Flexible Persistence

Publish npm version npm downloads

A CQRS and Event Sourcing platform

// Init Journaly as a observer platform for using as a message broker
const journaly = Journaly.newJournaly() as SubjectObserver<any>;

// config read database
read = new MongoDB(
  new PersistenceInfo(
    {
      database: 'read',
      host: process.env.MONGO_HOST || 'localhost',
      port: process.env.MONGO_PORT,
    },
    journaly
  )
);

// config write database
write = new MongoDB(
  new PersistenceInfo(
    {
      database: 'write',
      host: process.env.MONGO_HOST || 'localhost',
      port: process.env.MONGO_PORT,
    },
    journaly
  )
);

// init Flexible Persistence handler with write and read databases
const handler = new Handler(write, read);

// sample object
const obj = {};
obj['test'] = 'test';

// create an event to create an object
const persistencePromise = await handler.addEvent(
  new Event({ operation: Operation.create, name: 'object', content: obj })
);

// prints create event
console.log(persistencePromise);

{
  receivedItem: {
    __v: generated,
    id: generated,
    test: 'test',
  },
  result: undefined,
  selectedItem: undefined,
  sentItem: {
    test: 'test',
  },
}

Overview

Installation

This is a Node.js module available through the npm registry.

Before installing, download and install Node.js.

If this is a brand new project, make sure to create a package.json first with the npm init command or yarn init command.

Installation is done using the npm install command or yarn add command:

$ npm install flexiblepersistence

or

$ yarn add flexiblepersistence

Features

  • Ready to use event-sourcing/CQRS design pattern

Settings

PersistenceInfo Settings:

uri?: string;
database?: string;
host?: string;
port?: number | string;
username?: string;
password?: string;
options?: string;
connectionType?: string;
ssl?: ConnectionOptions | tls.ConnectionOptions | boolean | undefined;

Handler

// use it to add event to databases
public addEvent(event: Event): Promise<Output<any>>;

// use it to read multiple elements from database
public readArray(
  scheme: string,
  selectedItem: any
): Promise<Output<any>>;

// use it to read a single element from database
public readItem(
  scheme: string,
  selectedItem: any
): Promise<Output<any>>;

// use it to read a single element from database using id
public readItemById(scheme: string, id): Promise<Output<any>>;

Event

Event Fields:

operation?: Operation;
name?: string;
selection?: unknown;
single?: boolean;
content?: any | any[];
timestamp?: string;
_id?: unknown;
__v?: unknown;

Operation: Similar to CRUD

// existent, => create + flag correct
//Similar to create, but used to fix database.
//Use it when a data is missing
create,
//Use it to create a new element
read,
//Use it to read from database
// correct, => update + flag correct
//Similar to update, but used to fix database.
//Use it when a data is wrong
update,
//Use it to update an element
// nonexistent, => delete + flag correct
//Similar to delete, but used to fix database.
//Use it when an element is not supposed to exist
delete,
//Use it to delete an element
other,

Persistence Promise

Event Response:

receivedItem: Output;
result: any;
selectedItem: any;
sentItem: any;

IPersistence

It's possible to use diferent databases or services implementing IPersistence interface, like MongoDB does (MongoPersistence).

Other implementations:

Tests

To run the test suite, first install Docker and dependencies, then run docker-compose up -d and npm test:

$ docker-compose up -d
$ npm install
$ npm test

or

$ docker-compose up -d
$ yarn
$ yarn test

People

The original author of Journaly is Judah Lima

List of all contributors