{"version":3,"file":"base.entity-Cg5l6Krp.cjs","names":["#id","#createdAt","#updatedAt","other: IEntity","other: Readonly<SerializedEntity>"],"sources":["../src/domain/base.entity.ts"],"sourcesContent":["import { DateTime, UUID } from \"./refined.types.js\";\n\nexport interface IEntity {\n  readonly id: UUID;\n  readonly createdAt: DateTime;\n  readonly updatedAt: DateTime;\n}\n\nexport interface SerializedEntity {\n  readonly id: string;\n  readonly createdAt: Date;\n  readonly updatedAt: Date;\n}\n\nexport type IEntityForUpdate = Pick<IEntity, \"updatedAt\">;\n\nexport abstract class BaseEntity implements IEntity {\n  #id: UUID;\n  #createdAt: DateTime;\n  #updatedAt: DateTime;\n\n  protected constructor() {\n    this.#id = UUID.init();\n    this.#createdAt = DateTime.now();\n    this.#updatedAt = this.#createdAt;\n  }\n\n  get id(): UUID {\n    return this.#id;\n  }\n\n  get createdAt(): DateTime {\n    return this.#createdAt;\n  }\n\n  get updatedAt(): DateTime {\n    return this.#updatedAt;\n  }\n\n  protected markUpdated(): void {\n    this.#updatedAt = DateTime.now();\n  }\n\n  protected forUpdate(): IEntityForUpdate {\n    return { updatedAt: this.#updatedAt };\n  }\n\n  // for construction within safe boundaries of the domain\n  protected _copyBaseProps(other: IEntity): void {\n    this.#id = other.id;\n    this.#createdAt = DateTime.from(other.createdAt);\n    this.#updatedAt = DateTime.from(other.updatedAt);\n  }\n\n  protected _fromSerialized(other: Readonly<SerializedEntity>): this {\n    this.#id = UUID.fromTrusted(other.id); // only to simplify fromSerialized implementation on developer side\n    this.#createdAt = DateTime.from(other.createdAt);\n    this.#updatedAt = DateTime.from(other.updatedAt);\n\n    return this;\n  }\n\n  protected _serialize(): SerializedEntity {\n    return {\n      id: this.id,\n      createdAt: this.createdAt,\n      updatedAt: this.updatedAt,\n    };\n  }\n\n  //@ts-ignore\n  abstract serialize();\n}\n\nexport type SimpleSerialized<\n  EntityInterface extends IEntity,\n  T extends keyof EntityInterface = keyof IEntity,\n> = SerializedEntity & Omit<EntityInterface, T | keyof IEntity>;\n"],"mappings":";;;;AAgBA,IAAsB,aAAtB,MAAoD;CAClD;CACA;CACA;CAEA,cAAwB;AACtB,OAAKA,MAAM,2BAAK,MAAM;AACtB,OAAKC,aAAa,+BAAS,KAAK;AAChC,OAAKC,aAAa,KAAKD;CACxB;CAED,IAAI,KAAW;AACb,SAAO,KAAKD;CACb;CAED,IAAI,YAAsB;AACxB,SAAO,KAAKC;CACb;CAED,IAAI,YAAsB;AACxB,SAAO,KAAKC;CACb;CAED,cAA8B;AAC5B,OAAKA,aAAa,+BAAS,KAAK;CACjC;CAED,YAAwC;AACtC,SAAO,EAAE,WAAW,KAAKA,WAAY;CACtC;CAGD,eAAyBC,OAAsB;AAC7C,OAAKH,MAAM,MAAM;AACjB,OAAKC,aAAa,+BAAS,KAAK,MAAM,UAAU;AAChD,OAAKC,aAAa,+BAAS,KAAK,MAAM,UAAU;CACjD;CAED,gBAA0BE,OAAyC;AACjE,OAAKJ,MAAM,2BAAK,YAAY,MAAM,GAAG;AACrC,OAAKC,aAAa,+BAAS,KAAK,MAAM,UAAU;AAChD,OAAKC,aAAa,+BAAS,KAAK,MAAM,UAAU;AAEhD,SAAO;CACR;CAED,aAAyC;AACvC,SAAO;GACL,IAAI,KAAK;GACT,WAAW,KAAK;GAChB,WAAW,KAAK;EACjB;CACF;AAIF"}