Skip to content

Commit

Permalink
feat: 🎸 update Aggregate
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 19, 2023
1 parent e3da6bd commit 2ccc054
Showing 1 changed file with 24 additions and 25 deletions.
49 changes: 24 additions & 25 deletions src/aggregate/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,18 +20,19 @@

import { Entity } from "../entity"
import { Entities } from "../entities"
import { ValueFields } from "@unipackage/utils"

/**
* Properties of the aggregate.
* interface of the aggregate.
*/
export interface AggregateProperties {
export interface Aggregate<T extends Object> {
readonly id: string | number
name: string
entities: {
[key: string]: Entity<Object>
[key: string]: Entity<T>
}
entityCollections: {
[key: string]: Entities<Entity<Object>>
[key: string]: Entities<Entity<T>>
}
extra: {
[key: string]: any
Expand All @@ -42,45 +43,43 @@ export interface AggregateProperties {
* Represents an aggregate.
* @template T - Type of the aggregate properties.
*/
export class Aggregate<T extends AggregateProperties> {
/**
* Properties of the aggregate.
*/
private readonly properties: T

export class Aggregate<T extends Object> {
/**
* Creates an instance of the aggregate.
* @param properties - The initial properties for the aggregate.
* @param data - The initial data for the entity.
*/
constructor(properties: T) {
this.properties = properties
constructor(data: ValueFields<T>) {
if (!data || typeof data !== "object") {
throw new Error("Invalid data provided to the constructor")
}
Object.assign(this, data)
}

/**
* Gets the ID of the aggregate.
* @returns The ID of the aggregate.
*/
getId(): string | number {
return this.properties.id
return this.id
}

/**
* Gets the name of the aggregate.
* @returns The name of the aggregate.
*/
getName(): string {
return this.properties.name
return this.name
}

/**
* Gets the total count of entity types in the aggregate.
* @returns The total count of entity types.
*/
getEntityTypeCount(): number {
const entitiesCount = Object.keys(this.properties.entities).length
const entitiesCount = Object.keys(this.entities).length

const entityCollectionsCount = Object.values(
this.properties.entityCollections
this.entityCollections
).reduce(
(count, entityCollection) => count + entityCollection.count(),
0
Expand All @@ -95,7 +94,7 @@ export class Aggregate<T extends AggregateProperties> {
* @returns The entity with the specified key, or undefined if not found.
*/
getEntityByKey(key: string): Entity<Object> | undefined {
return this.properties.entities[key]
return this.entities[key]
}

/**
Expand All @@ -106,7 +105,7 @@ export class Aggregate<T extends AggregateProperties> {
getEntityCollectionsByKey(
key: string
): Entities<Entity<Object>> | undefined {
return this.properties.entityCollections[key]
return this.entityCollections[key]
}

/**
Expand All @@ -115,15 +114,15 @@ export class Aggregate<T extends AggregateProperties> {
* @param entity - The entity to add.
*/
addEntity(key: string, entity: Entity<Object>): void {
this.properties.entities[key] = entity
this.entities[key] = entity
}

/**
* Removes an entity from the aggregate.
* @param key - The key of the entity to remove.
*/
removeEntity(key: string): void {
delete this.properties.entities[key]
delete this.entities[key]
}

/**
Expand All @@ -132,15 +131,15 @@ export class Aggregate<T extends AggregateProperties> {
* @param entity - The entity collections to add.
*/
addEntityCollections(key: string, entity: Entities<Entity<Object>>): void {
this.properties.entityCollections[key] = entity
this.entityCollections[key] = entity
}

/**
* Removes entity collections from the aggregate.
* @param key - The key of the entity collections to remove.
*/
removeEntityCollections(key: string): void {
delete this.properties.entityCollections[key]
delete this.entityCollections[key]
}

/**
Expand All @@ -149,7 +148,7 @@ export class Aggregate<T extends AggregateProperties> {
* @returns The extra information with the specified key, or undefined if not found.
*/
getExtraInfo(key: string): any {
return this.properties.extra[key]
return this.extra[key]
}

/**
Expand All @@ -158,6 +157,6 @@ export class Aggregate<T extends AggregateProperties> {
* @param value - The value of the extra information.
*/
setExtraInfo(key: string, value: any): void {
this.properties.extra[key] = value
this.extra[key] = value
}
}

0 comments on commit 2ccc054

Please sign in to comment.