Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions packages/ddd-kit/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
"name": "@packages/ddd-kit",
"version": "0.0.0",
"private": true,
"main": "index.ts",
"types": "index.ts",
"main": "src/index.ts",
"types": "src/index.ts",
"scripts": {
"build": "tsc"
},
Expand Down
16 changes: 10 additions & 6 deletions packages/ddd-kit/src/core/Entity.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,21 @@ import { UUID } from "./UUID";
* Interface for domain entities.
* Entities are objects defined by their identity rather than their attributes.
*/
export interface IEntity {
export interface IEntity<T> {
/**
* Unique identifier of the entity.
*/
readonly _id: UUID<string | number>;
/**
* Properties of the entity.
*/
readonly _props: T;
/**
* Checks if another object is equal to this entity.
* @param object The object to compare.
* @returns True if the objects are equal, false otherwise.
*/
equals(object?: IEntity): boolean;
equals(object?: IEntity<T>): boolean;
/**
* Returns the properties of the entity.
*/
Expand All @@ -31,7 +35,7 @@ export interface IEntity {
* @param v The value to check.
* @returns True if the value is an entity, false otherwise.
*/
function isEntity(v: unknown): v is IEntity {
function isEntity(v: unknown): v is IEntity<unknown> {
return v instanceof Entity;
}

Expand All @@ -40,8 +44,8 @@ function isEntity(v: unknown): v is IEntity {
* Entities are defined by their unique identity and encapsulate domain logic.
* @template T The type of the entity's properties.
*/
export abstract class Entity<T> implements IEntity {
protected readonly _props: T;
export abstract class Entity<T> implements IEntity<T> {
public readonly _props: T;
public readonly _id: UUID<string | number>;

/**
Expand All @@ -59,7 +63,7 @@ export abstract class Entity<T> implements IEntity {
* @param object The object to compare.
* @returns True if the objects are equal, false otherwise.
*/
public equals(object?: IEntity): boolean {
public equals(object?: IEntity<T>): boolean {
if (!object || this === object || !isEntity(object)) return false;

return this._id.equals(object._id);
Expand Down
2 changes: 1 addition & 1 deletion packages/ddd-kit/src/core/ValueObject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ export abstract class ValueObject<T> {
* Creates a new ValueObject instance.
* @param value The value to encapsulate.
*/
protected constructor(value: T) {
public constructor(value: T) {
this._value = Object.freeze(value);
}

Expand Down
7 changes: 4 additions & 3 deletions packages/ddd-kit/src/types/BaseRepository.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import type { IEntity } from "@/core/Entity";
import type { Option } from "@/core/Option";
import type { Result } from "@/core/Result";

Expand All @@ -6,7 +7,7 @@ import type { Result } from "@/core/Result";
* Repositories abstract the persistence layer for aggregates/entities.
* @template T The type of entity managed by the repository.
*/
export interface BaseRepository<T> {
export interface BaseRepository<T extends IEntity<T>> {
/**
* Persists a new entity.
* @param entity The entity to create.
Expand All @@ -24,7 +25,7 @@ export interface BaseRepository<T> {
* @param entity The entity to delete.
* @returns A Result indicating success or failure.
*/
delete(entity: T): Promise<Result<void>>;
delete(entity: T["_id"]): Promise<Result<void>>;
/**
* Finds an entity by its unique identifier.
* @param id The unique identifier.
Expand All @@ -41,7 +42,7 @@ export interface BaseRepository<T> {
* @param props Partial properties to match.
* @returns An Option containing the entity or None.
*/
findBy(props: Partial<T>): Promise<Option<T>>;
findBy(props: Partial<T["_props"]>): Promise<Option<T>>;
/**
* Checks if an entity exists by its unique identifier.
* @param id The unique identifier.
Expand Down