Coder Social home page Coder Social logo

dkildar / immutable-entity-manager Goto Github PK

View Code? Open in Web Editor NEW
4.0 1.0 0.0 117 KB

This initiative enables developers to leverage entities in Node.js projects, whether on the client side or in any other Node.js environment.

License: Other

TypeScript 99.09% JavaScript 0.91%
builder builder-pattern entities entity entity-manager javascript manager typescript

immutable-entity-manager's Introduction

Alt npm version

Immutable entities manager

cover-image

This initiative enables developers to leverage entities in Node.js projects, whether on the client side or in any other Node.js environment.

๐Ÿ”จ Installation

npm install immutable-entity-manager
yarn add immutable-entity-manager

๐Ÿ—’๏ธ Usage/Examples

This package is built on the foundation of the manager and builder patterns. Each immutable entity has its own dedicated manager, designed to assist in data parsing. The manager, in turn, yields a builder class responsible for handling various tasks such as processing decorators, applying properties, and more.

This package uses Typescript under-the-hood which means that it generates return type, properties name, etc.

@ImmutableEntity()
class Person {
  readonly firstName!: string
  readonly lastName!: string
  @ImmutableEntityTyped(Date) readonly birthDate!: Date
}

const person = ImmutableEntityManager
    .getUniqueManager(Person)
    .parseFromJson({
        first_name: 'hello',
        last_name: 'world',
        birth_date: '2023-12-15T11:01:22.041Z'
    })
    .build()

expect(person.firstName).toBe('hello')
expect(person.lastName).toBe('world')
expect(person.birthDate).toBeInstanceOf(Date)

๐Ÿ“‘ API Reference

Decorators

  • @ImmutableEntity โ€“ mark a class as immutable entity;
  • @ImmutableEntityTransient โ€“ mark a class property as transient. It means that property won't be handled by manager and builder, also value won't be set;
  • @ImmutableEntityDefaultValue(0) โ€“ sets the default value for property if JSON data don't have value;
  • @ImmutableEntityTyped(Date) โ€“ mark a property as typed. Argument could be any class or other one immutable entity. In this case, it will apply immutable entity rules for this property otherwise it will create a new class with value as argument;

Manager

Manager represents the parser of entity.

ImmutableEntityManager
    .getUniqueManager(Person)
    .parseFromJson({
        ...
    })

Builder

Builder represents the standard entity builder pattern. Builder is exported and could be created directly but the best way is using of the manager parsing result object.

const builder = ImmutableEntityManager
    .getUniqueManager(Person)
    .parseFromJson({
        ...
    })

Property setting

Builder allows to set the values for properties. It will override data given by manager in a parsing stage.

  • Arguments: propertyName: string, value: any, applyDecorators: boolean. Apply decorators allow to set: should the builder apply this package decorators before setting the value.
builder
    .withProperty('someProperty', someValue)
    .withProperty('someAnotherProperty', someAnotherValue)

Options setting

Builder allow to choose conversion of properties: camel to snake or viceversa. snakeToCamelCase is default.

builder.withOptions({ camelToSnakeCase: true })

๐Ÿ™Œ Roadmap

  • Add transformation of property before parsing or in withProperty;

  • Allow to developers set custom constructor for entity which couldn't be initialized with @ImmutableEntityTyped decorator;

  • Add entity cloning;

  • Add React helper hooks for managing entities reactively;

  • Add different parser sources like Buffer, string etc.;

  • Add global singleton managers for each entity;

  • Allow to build entities w/o case conversion or with custom conversion function;

Alt

immutable-entity-manager's People

Contributors

dkildar avatar

Stargazers

 avatar  avatar  avatar  avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.