Skip to content

Commit

Permalink
feat: 🎸 release version 1
Browse files Browse the repository at this point in the history
BREAKING CHANGE: 🧨 release version 1
  • Loading branch information
waynewyang committed Dec 25, 2023
1 parent 2ccc054 commit 6de9684
Show file tree
Hide file tree
Showing 3 changed files with 22 additions and 6 deletions.
10 changes: 4 additions & 6 deletions src/aggregate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ export class Aggregate<T extends Object> {
* @param key - The key of the entity.
* @returns The entity with the specified key, or undefined if not found.
*/
getEntityByKey(key: string): Entity<Object> | undefined {
getEntityByKey(key: string): Entity<T> | undefined {
return this.entities[key]
}

Expand All @@ -102,9 +102,7 @@ export class Aggregate<T extends Object> {
* @param key - The key of the entity collections.
* @returns The entity collections with the specified key, or undefined if not found.
*/
getEntityCollectionsByKey(
key: string
): Entities<Entity<Object>> | undefined {
getEntityCollectionsByKey(key: string): Entities<Entity<T>> | undefined {
return this.entityCollections[key]
}

Expand All @@ -113,7 +111,7 @@ export class Aggregate<T extends Object> {
* @param key - The key of the entity.
* @param entity - The entity to add.
*/
addEntity(key: string, entity: Entity<Object>): void {
addEntity(key: string, entity: Entity<T>): void {
this.entities[key] = entity
}

Expand All @@ -130,7 +128,7 @@ export class Aggregate<T extends Object> {
* @param key - The key of the entity collections.
* @param entity - The entity collections to add.
*/
addEntityCollections(key: string, entity: Entities<Entity<Object>>): void {
addEntityCollections(key: string, entity: Entities<Entity<T>>): void {
this.entityCollections[key] = entity
}

Expand Down
16 changes: 16 additions & 0 deletions src/entity/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -156,4 +156,20 @@ export abstract class Entity<T extends Object> implements EntityInterface {
setId(id: any) {
this.id = id
}

/**
* Gets the value of the entity.
* @returns ValueFields<T>.
*/
value(): ValueFields<T> {
const result: Record<string, any> = {}

Object.entries(this).forEach(([key, value]) => {
if (this.hasOwnProperty(key) && typeof value !== "function") {
result[key] = value instanceof Entity ? value.clone() : value
}
})

return result as ValueFields<T>
}
}
2 changes: 2 additions & 0 deletions test/entity/index.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import { expect } from "chai"
import { it } from "mocha"
import { Entity } from "../../src/entity"
import { ValueFields } from "@unipackage/utils"
import assert from "assert"

// Sample class extending Entity for testing
class SampleEntity extends Entity<{
Expand Down Expand Up @@ -53,6 +54,7 @@ describe("Entity", () => {
}
const entity = new SampleEntity(data)
expect(entity).to.be.an.instanceOf(SampleEntity)
assert.deepStrictEqual(entity.value(), data)
})

it("[clone test]: should clone the entity", () => {
Expand Down

0 comments on commit 6de9684

Please sign in to comment.