@ts-ddd/entity
TypeScript icon, indicating that this package has built-in type declarations

1.0.0 • Public • Published

Entity Utilities


DDD Entity classes and utilities

Installation

npm i @ts-ddd/entity
yarn add @ts-ddd/entity
pnpm install @ts-ddd/entity
bun add @ts-ddd/entity

Usage

import { Identifier, Entity } from '@ts-ddd/entity';

interface IBook {
  id: Identifier;
  name: string;
  year: number;
  publisher: string;
}
  
class Book extends Entity<IBook> {
  public static create(payload: IBook) {
    return new Book(payload);
  }
  
  public static createToPrimitives(payload: Omit<IBook, 'id'>) {
    return new Book({
      id: uuidGenerator(),
      ...payload
    })
  }
  
  public static createFromPrimitives(payload: IBook): Book {
    return new Book(payload)
  }
}

Create an entity from primitives (create from any data source)

const book = Book.createFromPrimitives({
  id: '88d37945-cc6e-43b0-8872-9d378d4bb171',
  name: "Harry Potter",
  year: 1997,
  publisher: "Bloomsbury"
});

Create an entity to put in a data store

const book = Book.createToPrimitives({
  name: `Harry Potter and the Sorcerer's Stone`,
  year: 1997,
  publisher: "Bloomsbury"
});

Get property from entity

book.getProperty('id') // 88d37945-cc6e-43b0-8872-9d378d4bb171

Set property to entity

book.setProperty('name', `Harry Potter and the Sorcerer's Stone`)

Get primitives values

book.toPrimitives()
/*
  {
    id: '88d37945-cc6e-43b0-8872-9d378d4bb171',
    name: 'Harry Potter and the Sorcerer's Stone',
    year: 1997,
    publisher: 'Bloomsbury'
}
*/


---

Readme

Keywords

none

Package Sidebar

Install

npm i @ts-ddd/entity

Weekly Downloads

2

Version

1.0.0

License

MIT

Unpacked Size

5.98 kB

Total Files

11

Last publish

Collaborators

  • fdorantesm