diff --git a/src/entity/index.ts b/src/entity/index.ts index 993bc35..f5e67eb 100644 --- a/src/entity/index.ts +++ b/src/entity/index.ts @@ -1,21 +1,16 @@ -import { ValueObject } from "../valueObject" import { isEqual } from "lodash" import { TypeFromProperties } from "@unipackage/utils" export interface EntityInterface { clone(): this - compareProperties(entity: this): boolean - deserialize(data: string): void equal(other: this, fields?: Array): boolean getName(): string getType(): string getId(): any - serialize(): string } export abstract class Entity implements EntityInterface { - id?: any; - [key: string]: any + id?: any constructor(data: TypeFromProperties) { if (!data || typeof data !== "object") { @@ -30,6 +25,10 @@ export abstract class Entity implements EntityInterface { return Object.keys(this) } + protected validate(): boolean { + return true + } + equal(other: this, fields?: Array): boolean { const properties = fields ?? (Object.keys(this) as Array) for (const property of properties) { @@ -48,56 +47,12 @@ export abstract class Entity implements EntityInterface { return `${this.constructor.name}` } - protected validate(): boolean { - return true - } - getId(): any { return this.id } - serialize(): string { - return JSON.stringify(this) - } - - deserialize(data: string): void { - try { - this.properties = JSON.parse(data) - } catch (error: any) { - throw new Error("Failed to deserialize the data. " + error.message) - } - } - clone(): this { const clonedData = JSON.parse(JSON.stringify(this)) return new (this.constructor as any)(clonedData) } - - //no use - compareProperties(entity: this): boolean { - const thisKeys = Object.keys(this) - const entityKeys = Object.keys(entity) - - if (thisKeys.length !== entityKeys.length) { - return false - } - - for (const key of thisKeys) { - const thisValue = this[key] - const entityValue = entity[key] - - if ( - thisValue instanceof ValueObject && - entityValue instanceof ValueObject - ) { - if (!thisValue.equals(entityValue.immutable())) { - return false - } - } else if (thisValue !== entityValue) { - return false - } - } - - return true - } }