This repository has been archived on 2026-01-22. You can view files and clone it, but cannot push or open issues or pull requests.
factuges_2025/packages/rdx-ddd-domain/src/domain-entity.ts
2025-04-27 22:47:47 +02:00

34 lines
898 B
TypeScript

import { UniqueID } from "./value-objects/unique-id";
export abstract class DomainEntity<T extends object> {
protected readonly props: T;
public readonly id: UniqueID;
protected constructor(props: T, id?: UniqueID) {
this.id = id ? id : UniqueID.generateNewID().data;
this.props = props;
}
protected _flattenProps(props: T): { [s: string]: any } {
return Object.entries(props).reduce((result: any, [key, valueObject]) => {
console.log(key, valueObject.value);
result[key] = valueObject.value;
return result;
}, {});
}
equals(other: DomainEntity<T>): boolean {
return other instanceof DomainEntity && this.id.equals(other.id);
}
toString(): { [s: string]: string } {
const flattenProps = this._flattenProps(this.props);
return {
id: this.id.toString(),
...flattenProps.map((prop: any) => String(prop)),
};
}
}