# @flexiblepersistence/dao

> A DAO implementation for Flexible Persistence's PersistenceAdapter

Latest version **3.0.2** (published 2023-04-10) · MIT license · 0 weekly downloads

## Install

```sh
npm install @flexiblepersistence/dao
pnpm add @flexiblepersistence/dao
yarn add @flexiblepersistence/dao
bun add @flexiblepersistence/dao
```

## Health

**Score 15/100 (F)** — status: abandoned.

Positive: no vulnerabilities.

Warnings: low downloads; no types; no esm support.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 3.0.2 |
| Published | 2023-04-10 |
| First published | 2020-11-14 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | none |
| Module format | CommonJS |
| Dependencies | 2 |
| Unpacked size | 254.2 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 4 |
| Author | Judah Holanda Correia Lima |
| Maintainers | judahh |
| Keywords | dao, flexiblepersistence, nodejs, typescript, javascript |

## Links

- npm: https://www.npmjs.com/package/@flexiblepersistence/dao
- Repository: https://github.com/Judahh/flexiblePersistence
- Homepage: https://github.com/Judahh/flexiblePersistence#readme
- Issues: https://github.com/Judahh/flexiblePersistence/issues
- npm.io page: https://npm.io/package/@flexiblepersistence/dao

## Dependencies (2)

- [journaly](https://npm.io/package/journaly.md) latest
- [bignumber.js](https://npm.io/package/bignumber.js.md) latest

## Alternatives

- [@openai/codex-sdk](https://npm.io/package/@openai/codex-sdk.md) — 731.4K weekly downloads
- [babel-plugin-transform-react-jsx](https://npm.io/package/babel-plugin-transform-react-jsx.md) — 565.0K weekly downloads
- [babel-helper-remove-or-void](https://npm.io/package/babel-helper-remove-or-void.md) — 508.5K weekly downloads
- [@pnpm/store-controller-types](https://npm.io/package/@pnpm/store-controller-types.md) — 186.9K weekly downloads
- [react-native-signature-canvas](https://npm.io/package/react-native-signature-canvas.md) — 155.6K weekly downloads

## Recent versions

- 3.0.2 (latest) — 2023-04-10
- 3.0.1 — 2023-04-07
- 3.0.0 — 2023-03-22
- 3.0.0-beta2 — 2023-03-22
- 3.0.0-beta — 2023-03-22
- 2.4.1 — 2023-03-22
- 2.4.0 — 2023-03-22
- 2.3.5 — 2023-03-17
- 2.3.4 — 2023-03-16
- 2.3.3 — 2023-03-16
- 2.3.2 — 2023-03-15
- 2.3.1 — 2023-03-15
- 2.3.0 — 2023-03-15
- 2.2.2 — 2023-02-28
- 2.2.1 — 2023-02-11
- … 75 more at https://npm.io/package/@flexiblepersistence/dao/versions

## README

# Flexible Persistence

![Publish](https://github.com/Judahh/flexiblePersistence/workflows/Publish/badge.svg)
[![npm version](https://badge.fury.io/js/flexiblepersistence.svg)](https://badge.fury.io/js/flexiblepersistence)
[![npm downloads](https://img.shields.io/npm/dt/flexiblepersistence.svg)](https://img.shields.io/npm/dt/flexiblepersistence.svg)

A CQRS and Event Sourcing platform

```js
// 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](./doc/overview.svg)

## Installation

This is a [Node.js](https://nodejs.org/en/) module available through the
[npm registry](https://www.npmjs.com/).

Before installing,
[download and install Node.js](https://nodejs.org/en/download/).

If this is a brand new project, make sure to create a `package.json` first with
the [`npm init` command](https://docs.npmjs.com/creating-a-package-json-file) or
[`yarn init` command](https://classic.yarnpkg.com/en/docs/cli/init/).

Installation is done using the
[`npm install` command](https://docs.npmjs.com/getting-started/installing-npm-packages-locally)
or [`yarn add` command](https://classic.yarnpkg.com/en/docs/cli/add):

```bash
$ npm install flexiblepersistence
```

or

```bash
$ yarn add flexiblepersistence
```

## Features

- Ready to use event-sourcing/CQRS design pattern

## Settings

PersistenceInfo Settings:

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

## Handler

```js
// 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:

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

Operation: Similar to CRUD

```js
// 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:

```js
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:

- [DAO](https://github.com/Judahh/dAOPersistence)
- [Sequelize](https://github.com/Judahh/sequelizePersistence)
- [Service](https://github.com/Judahh/servicePersistence)

## Tests

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

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

or

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

## People

The original author of Journaly is [Judah Lima](https://github.com/Judahh)

[List of all contributors](https://github.com/Judahh/flexiblepersistence/graphs/contributors)

---
_Source: https://npm.io/package/@flexiblepersistence/dao · Machine-readable twin of the npm.io package page. Health data is recomputed on every publish._
