Skip to content

A set of classes and utilities to standardize the architecture of a Node.js application

License

Notifications You must be signed in to change notification settings

JSoaress/ts-arch-kit

Repository files navigation

Installation

npm install ts-arch-kit

or yarn

yarn add ts-arch-kit

AbstractModel

AbstractModel is a base class for domain entities that provides primary key management, state tracking, and equality comparison.

Importing

import { AbstractModel, AbstractModelProps } from "ts-arch-kit/dist/core";

Usage

Creating a Model Class

To use AbstractModel, extend it in your entity class:

type UserProps = AbstractModelProps & {
    name: string;
    email: string;
};

class User extends AbstractModel<UserProps> {
    constructor(props: UserProps, isNew = true) {
        super(props, isNew);
    }

    getName(): string {
        return this.props.name;
    }
}

Instantiating an Entity

const newUser = new User({ name: "John Doe", email: "john@example.com" });
console.log(newUser.pk); // Automatically generated ID
console.log(newUser.isNew); // true

Tracking Changes

const user = new User({ pk: "123", name: "John Doe", email: "john@example.com" }, false);
console.log(user.isDirty()); // false

user.props.name = "John Smith";
console.log(user.isDirty()); // true
console.log(user.getDirtyProps()); // ["name"]

Checking Equality

const user1 = new User({ pk: "123", name: "Alice" }, false);
const user2 = new User({ pk: "123", name: "Alice" }, false);

console.log(user1.equals(user2)); // true

Creating a Model Class with Numeric ID Generation

To use AbstractModel, extend it in your entity class and provide a custom numeric ID generator:

let idCounter = 1;
function numericIdGenerator() {
    return idCounter++;
}

type UserProps = {
    pk?: number;
    name: string;
    email: string;
};

class User extends AbstractModel<UserProps> {
    constructor(props: UserProps, isNew = true) {
        super(props, isNew, numericIdGenerator);
    }

    getName(): string {
        return this.props.name;
    }
}

API

AbstractModel<Props>

Constructor

constructor(props: Props, isNew?: boolean, idGenerator?: () => PrimaryKey)
  • props: Model properties.
  • isNew: Indicates if the entity is new (default: true).
  • idGenerator: Custom function for ID generation (default: UUID).

Methods

  • getDirtyProps(): string[] - Returns modified property names.
  • checkDirtyProps(prop: string): boolean - Checks if a property has changed.
  • isDirty(): boolean - Checks if any property has changed.
  • equals(entity?: AbstractModel<Props>): boolean - Compares two entities by primary key.

License

MIT

About

A set of classes and utilities to standardize the architecture of a Node.js application

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages