# @node-ddd/persistence

> Persistence layer for DDD applications

Latest version **0.0.12** (published 2022-01-01) · MIT license · 0 weekly downloads

## Install

```sh
npm install @node-ddd/persistence
pnpm add @node-ddd/persistence
yarn add @node-ddd/persistence
bun add @node-ddd/persistence
```

## Health

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

Positive: has types; esm support; no vulnerabilities; high quality score.

Warnings: low downloads; pre 1.0.

Negative: abandoned; low maintenance score.

## Facts

| | |
|---|---|
| Version | 0.0.12 |
| Published | 2022-01-01 |
| First published | 2021-10-03 |
| Weekly downloads | 0 |
| License | MIT |
| TypeScript types | bundled |
| Module format | ESM + CommonJS |
| Dependencies | 1 |
| Unpacked size | 197.5 KB |
| Known vulnerabilities | 0 |
| Install scripts | no |
| GitHub stars | 2 |
| Author | Samuel Briole |
| Maintainers | samuelbriole |

## Links

- npm: https://www.npmjs.com/package/@node-ddd/persistence
- Repository: https://github.com/node-ddd/persistence
- npm.io page: https://npm.io/package/@node-ddd/persistence

## Dependencies (1)

- [lodash.clonedeep](https://npm.io/package/lodash.clonedeep.md) ^4.5.0

## Recent versions

- 0.0.12 (latest) — 2022-01-01
- 0.0.11 — 2021-12-29
- 0.0.10 — 2021-12-29
- 0.0.9 — 2021-12-28
- 0.0.8 — 2021-12-27
- 0.0.7 — 2021-12-25
- 0.0.6 — 2021-12-25
- 0.0.5 — 2021-10-03
- 0.0.4 — 2021-10-03
- 0.0.1 — 2021-10-03

## README

# About

A library for building a persistence layer with NodeJS

## Getting Started

### 1. Installation

```bash
$ npm install @node-ddd/persistence
# OR
$ yarn add @node-ddd/persistence
```

### 2. Create a Repository interface for your aggregate

```ts
import { Repository } from '@node-ddd/persistence';
import { User } from './User';

export interface UserRepository extends Repository<User> {
  findByEmail(email: string): Promise<User | null>;
}
```

The `Repository` interface contains a set of basic methods, but you can add your own methods by extending the interface

### 3. Create an InMemory implementation

```ts
import { InMemoryRepository } from '@node-ddd/persistence';
import { UserRepository } from 'domain';
import { User } from './User';

export class InMemoryUserRepository
  extends InMemoryRepository<User>
  implements UserRepository
{
  constructor(aggregates?: ReadonlyArray<User>) {
    super({ aggregateName: 'User', aggregates });
  }
  findByEmail = (email: string): Promise<User | null> =>
    this.findOneBy(user => user.email === email);
}
```

### 4. Create a Typeorm implementation

```ts
import { TypeormRepository } from '@node-ddd/persistence';
import { getConnection } from 'typeorm';
import { User, UserRepository } from 'domain';
import { UserEntity } from './UserEntity';

export class TypeormUserRepository
  extends TypeormRepository<User, UserEntity>
  implements UserRepository
{
  constructor() {
    super({
      connection: getConnection(),
      deserializer: user =>
        new User({
          id: toUserId(user.id),
          fullName: user.fullName,
          email: user.email,
        }),
      serializer: user => ({
        id: user.id,
        fullName: user.fullName,
        email: user.email,
      }),
      entity: UserEntity,
    });
  }

  findByEmail = (email: string): Promise<User | null> =>
    this.findFromQuery(() => this.repository.findOne({ email }));
}
```

## Roadmap

- [ ] Create a functional programming interface and implementation (with `fp-ts`)
- [ ] Create a `prisma` implementation

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