Skip to content

Commit

Permalink
update to version0.1.2
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Oct 18, 2023
1 parent be727bf commit 55eb7bd
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 40 deletions.
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@unipackage/ddd",
"version": "0.1.1",
"version": "0.1.2",
"description": "ddd",
"main": "dist/index.js",
"types": "dist/index.d.ts",
Expand Down
17 changes: 7 additions & 10 deletions src/aggregate/index.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
import { Entity, EntityProperties } from "../entity"
import { Entity } from "../entity"
import { Entities } from "../entities"

export interface AggregateProperties {
id: string | number
name: string
entities: {
[key: string]: Entity<EntityProperties>
[key: string]: Entity<Object>
}
entityCollections: {
[key: string]: Entities<Entity<EntityProperties>>
[key: string]: Entities<Entity<Object>>
}
extra: {
[key: string]: any
Expand Down Expand Up @@ -38,28 +38,25 @@ export class Aggregate<T extends AggregateProperties> {
)
}

getEntityByKey(key: string): Entity<EntityProperties> | undefined {
getEntityByKey(key: string): Entity<Object> | undefined {
return this.properties.entities[key]
}

getEntityCollectionsByKey(
key: string
): Entities<Entity<EntityProperties>> | undefined {
): Entities<Entity<Object>> | undefined {
return this.properties.entityCollections[key]
}

addEntity(key: string, entity: Entity<EntityProperties>): void {
addEntity(key: string, entity: Entity<Object>): void {
this.properties.entities[key] = entity
}

removeEntity(key: string): void {
delete this.properties.entities[key]
}

addEntityCollections(
key: string,
entity: Entities<Entity<EntityProperties>>
): void {
addEntityCollections(key: string, entity: Entities<Entity<Object>>): void {
this.properties.entityCollections[key] = entity
}

Expand Down
4 changes: 2 additions & 2 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import { Entity, EntityProperties } from "../entity"
import { Entity } from "../entity"

export abstract class Entities<T extends Entity<EntityProperties>> {
export abstract class Entities<T extends Entity<Object>> {
protected entities: T[]

constructor(entities: T[]) {
Expand Down
54 changes: 28 additions & 26 deletions src/entity/index.ts
Original file line number Diff line number Diff line change
@@ -1,82 +1,84 @@
import { ValueObject } from "../valueObject"
import { MemberVariables } from "@unipackage/utils"
import { isEqual } from "lodash"

export interface EntityProperties {
id: any
export abstract class Entity<T extends Object> {
[key: string]: any
}

export abstract class Entity<T extends EntityProperties> {
protected properties: T

constructor(properties: T) {
constructor(properties: MemberVariables<T> & { id: any }) {
if (
!properties ||
typeof properties !== "object" ||
properties.id === undefined
) {
throw new Error("Invalid data provided to the constructor")
}
this.properties = properties
Object.assign(this, properties)
}

protected initialize() {}

protected getKeys(): string[] {
return Object.keys(this.properties)
return Object.keys(this)
}

equal(other: T, fields?: Array<keyof T & keyof this>): boolean {
const properties =
fields ?? (Object.keys(this) as Array<keyof T & keyof this>)
for (const property of properties) {
if (!isEqual(this[property], other[property])) {
return false
}
}
return true
}

getName(): string {
return `${this.constructor.name} - ID: ${this.getId()}`
}

getType(): string {
return `${this.constructor.name}}`
return `${this.constructor.name}`
}

protected validate(): boolean {
return true
}

getId(): any {
return this.properties.id
}

getProperties(): T {
return this.properties
}

setProperties(data: T): void {
this.properties = data
return this.id
}

serialize(): string {
return JSON.stringify(this.properties)
return JSON.stringify(this)
}

deserialize(data: string): void {
try {
this.properties = JSON.parse(data)
const parsedData = JSON.parse(data)
Object.assign(this, parsedData)
} catch (error: any) {
throw new Error("Failed to deserialize the data. " + error.message)
}
}

clone(): Entity<T> {
const clonedData = JSON.parse(JSON.stringify(this.properties))
const clonedData = JSON.parse(JSON.stringify(this))
return new (this.constructor as any)(clonedData)
}

//no use
compareProperties(entity: Entity<T>): boolean {
const thisKeys = Object.keys(this.properties)
const entityKeys = Object.keys(entity.properties)
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.properties[key]
const entityValue = entity.properties[key]
const thisValue = this[key]
const entityValue = entity[key]

if (
thisValue instanceof ValueObject &&
Expand Down
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
export { ValueObject } from "./valueObject"
export { Entities } from "./entities"
export { Entity, EntityProperties } from "./entity"
export { Entity } from "./entity"
export { Aggregate, AggregateProperties } from "./aggregate"

0 comments on commit 55eb7bd

Please sign in to comment.