Skip to content

Commit

Permalink
docs: ✏️ add comment
Browse files Browse the repository at this point in the history
  • Loading branch information
waynewyang committed Dec 14, 2023
1 parent 40f7d4e commit d5b26bc
Show file tree
Hide file tree
Showing 9 changed files with 384 additions and 45 deletions.
24 changes: 12 additions & 12 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
"homepage": "https://github.com/unipackage/ddd#readme",
"dependencies": {
"@types/lodash": "^4.14.199",
"@unipackage/utils": "^0.1.5",
"@unipackage/utils": "^1.0.0",
"fp-ts": "^2.16.1",
"immutable": "^5.0.0-beta.4"
},
Expand Down
101 changes: 95 additions & 6 deletions src/aggregate/index.ts
Original file line number Diff line number Diff line change
@@ -1,8 +1,31 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import { Entity } from "../entity"
import { Entities } from "../entities"

/**
* Properties of the aggregate.
*/
export interface AggregateProperties {
id: string | number
readonly id: string | number
name: string
entities: {
[key: string]: Entity<Object>
Expand All @@ -15,59 +38,125 @@ export interface AggregateProperties {
}
}

/**
* Represents an aggregate.
* @template T - Type of the aggregate properties.
*/
export class Aggregate<T extends AggregateProperties> {
private properties: T
/**
* Properties of the aggregate.
*/
private readonly properties: T

/**
* Creates an instance of the aggregate.
* @param properties - The initial properties for the aggregate.
*/
constructor(properties: T) {
this.properties = properties
}

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

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

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

const entityCollectionsCount = Object.values(
this.properties.entityCollections
).reduce(
(count, entityCollection) => count + entityCollection.count(),
0
)

return entitiesCount + entityCollectionsCount
}

/**
* Gets an entity by key.
* @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 {
return this.properties.entities[key]
}

/**
* Gets entity collections by key.
* @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 {
return this.properties.entityCollections[key]
}

/**
* Adds an entity to the aggregate.
* @param key - The key of the entity.
* @param entity - The entity to add.
*/
addEntity(key: string, entity: Entity<Object>): void {
this.properties.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]
}

/**
* Adds entity collections to the aggregate.
* @param key - The key of the entity collections.
* @param entity - The entity collections to add.
*/
addEntityCollections(key: string, entity: Entities<Entity<Object>>): void {
this.properties.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]
}

/**
* Gets extra information by key.
* @param key - The key of the extra information.
* @returns The extra information with the specified key, or undefined if not found.
*/
getExtraInfo(key: string): any {
return this.properties.extra[key]
}

/**
* Sets extra information by key.
* @param key - The key of the extra information.
* @param value - The value of the extra information.
*/
setExtraInfo(key: string, value: any): void {
this.properties.extra[key] = value
}
Expand Down
60 changes: 60 additions & 0 deletions src/entities/index.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,88 @@
/*******************************************************************************
* (c) 2023 unipackage
*
* Licensed under either the MIT License (the "MIT License") or the Apache License, Version 2.0
* (the "Apache License"). You may not use this file except in compliance with one of these
* licenses. You may obtain a copy of the MIT License at
*
* https://opensource.org/licenses/MIT
*
* Or the Apache License, Version 2.0 at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the MIT License or the Apache License for the specific language governing permissions and
* limitations under the respective licenses.
********************************************************************************/

import { Entity } from "../entity"

/**
* Abstract class representing a collection of entities.
* @template T - The type of entities in the collection, extending Entity<Object>.
*/
export abstract class Entities<T extends Entity<Object>> {
/**
* The array of entities in the collection.
*/
protected entities: T[]

/**
* Creates an instance of the Entities class.
* @param entities - An array of entities to initialize the collection.
*/
constructor(entities: T[]) {
this.entities = entities
}

/**
* Gets the array of entities in the collection.
* @returns An array of entities.
*/
getEntities(): T[] {
return this.entities
}

/**
* Adds an entity to the collection.
* @param entity - The entity to add.
*/
add(entity: T): void {
this.entities.push(entity)
}

/**
* Clears all entities from the collection.
*/
clear(): void {
this.entities = []
}

/**
* Gets the count of entities in the collection.
* @returns The number of entities.
*/
count(): number {
return this.entities.length
}

/**
* Finds an entity in the collection based on the provided find function.
* @param findFn - The function used to find the entity.
* @returns The found entity or undefined if not found.
*/
find(findFn: (entity: T) => boolean): T | undefined {
return this.entities.find((entity) => findFn(entity))
}

/**
* Removes the specified entity from the collection.
* @param entity - The entity to remove.
* @throws Error if the entity is not found in the collection.
*/
remove(entity: T): void {
const index = this.entities.indexOf(entity)
if (index === -1) {
Expand All @@ -35,6 +91,10 @@ export abstract class Entities<T extends Entity<Object>> {
this.entities.splice(index, 1)
}

/**
* Sorts the entities in the collection based on the provided compare function.
* @param compareFn - The function used to compare entities for sorting.
*/
sort(compareFn: (a: T, b: T) => number): void {
this.entities.sort(compareFn)
}
Expand Down
Loading

0 comments on commit d5b26bc

Please sign in to comment.