ts-arch-kit
v2.4.0
Published
```sh npm install ts-arch-kit ```
Readme
Installation
npm install ts-arch-kitor yarn
yarn add ts-arch-kitAbstractModel
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: "[email protected]" });
console.log(newUser.pk); // Automatically generated ID
console.log(newUser.isNew); // trueTracking Changes
const user = new User({ pk: "123", name: "John Doe", email: "[email protected]" }, 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)); // trueCreating 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
