0.0.4 • Published 4 years ago

@flexiblepersistence/postgres v0.0.4

Weekly downloads
-
License
BSD-3-Clause
Repository
github
Last release
4 years ago

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<PersistencePromise<any>>;

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

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

// use it to read a single element from database using id
public readItemById(scheme: string, id): Promise<PersistencePromise<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,
//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,
//Similar to update, but used to fix database.
//Use it when a data is wrong
update,
//Use it to update an element
nonexistent,
//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

It's not obligatory to use existent, correct and nonexistent. It's possible to use just CRUD operations.

Persistence Promise

Event Response:

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

PersistenceAdapter

It's possible to use diferent databases or services implementing PersistenceAdapter interface, like MongoDB does.

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

0.0.4

4 years ago

0.0.3

4 years ago

0.0.2

4 years ago

0.0.1

4 years ago

0.0.1-t

4 years ago