UNPKG

rich-domain

Version:

This package provide utils file and interfaces to assistant build a complex application with domain driving design

75 lines 4.29 kB
import { Adapter, AutoMapperSerializer, EntityMapperPayload, EntityProps, _Adapter, _Entity, Settings, UID } from "../types"; import { ReadonlyDeep } from "../types-util"; import AutoMapper from "./auto-mapper"; import GettersAndSetters from "./entity-getters-and-setters"; import Result from "./result"; /** * @description Represents a domain entity identified by a unique identifier (ID). * Extends basic entity functionalities, ensuring the presence of `createdAt` and `updatedAt` timestamps and providing * utility methods such as equality checks, object transformations, and instance creation. */ export declare class Entity<Props extends EntityProps> extends GettersAndSetters<Props> implements _Entity<Props> { protected _id: UID<string>; protected autoMapper: AutoMapper<Props>; constructor(props: Props, config?: Settings); /** * @description Determines if the current entity has the same properties (except `createdAt` and `updatedAt`) and the same ID as another entity. * @param other The entity instance to compare against. * @returns `true` if both entities have identical properties and IDs; otherwise, `false`. */ isEqual(other: this): boolean; /** * @description Converts the current entity instance into a plain object representation. * @param adapter An optional adapter or builder that transforms the entity into another object format. * @returns If an adapter is provided, returns the adapted object. Otherwise, returns a deeply frozen object * representing the entity properties along with entity metadata (`AutoMapperSerializer<Props> & EntityMapperPayload`). */ toObject<T>(adapter?: Adapter<this, T> | _Adapter<this, T>): T extends {} ? T & EntityMapperPayload : ReadonlyDeep<AutoMapperSerializer<Props> & EntityMapperPayload>; /** * @description Retrieves the unique identifier (ID) of the entity. * @returns An instance of `UID<string>` representing the entity's ID. */ get id(): UID<string>; /** * @description Generates a "hash code" like identifier for the entity, combining its class name and ID. * @summary The format is `[Entity@ClassName]:UUID`. The ClassName is derived from the entity's prototype. * This helps uniquely identify the entity instance in logs or debugging. * @returns A `UID<string>` representing the hash code of the entity. */ hashCode(): UID<string>; /** * @description Checks if the entity is newly created and not yet persisted or saved externally (e.g., to a database). * @returns `true` if the entity is considered new (ID marked as new); otherwise, `false`. */ isNew(): boolean; /** * @description Creates a new entity instance based on the current entity. * Allows overriding some properties. If no `id` is provided in the new props, a new one will be generated. * @param props Optional partial properties to override when creating the new entity instance. * @returns A new instance of the entity with updated properties. */ clone(props?: Props extends object ? Partial<Props> : never): this; /** * @description Validates if a given value is suitable for creating or representing an entity. * @param value The value to validate. * @returns `true` if the value is considered valid for the entity; otherwise, `false`. */ static isValid(value: any): boolean; /** * @description Validates the provided properties to check if they can be used to create a valid entity instance. * @param props The properties object to validate. * @returns `true` if the props are valid; `false` otherwise. */ static isValidProps(props: any): boolean; /** * @description Initializes a new entity instance from the given properties. * @summary This method should be implemented in subclasses. By default, it throws an error. * @param props The properties to initialize the entity. * @returns The newly created entity instance or throws an error if not implemented. * @throws An error indicating the method is not implemented. */ static init(props: any): any; static create(props: any): Result<any, any, any>; } export default Entity; //# sourceMappingURL=entity.d.ts.map