@hmpsn/ts-ddd 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE.md +21 -0
- package/README.md +194 -0
- package/dist/aggregate-root.d.ts +22 -0
- package/dist/aggregate-root.js +34 -0
- package/dist/domain-event.d.ts +28 -0
- package/dist/domain-event.js +44 -0
- package/dist/entity.d.ts +19 -0
- package/dist/entity.js +29 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +17 -0
- package/dist/repository.d.ts +19 -0
- package/dist/repository.js +2 -0
- package/dist/value-object.d.ts +17 -0
- package/dist/value-object.js +51 -0
- package/package.json +49 -0
package/LICENSE.md
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2026 Thomas Hampson <thomas@hmpsn.me>
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,194 @@
|
|
|
1
|
+
# ts-ddd
|
|
2
|
+
|
|
3
|
+
[](https://www.npmjs.com/package/ts-ddd)
|
|
4
|
+
[](https://opensource.org/licenses/MIT)
|
|
5
|
+
|
|
6
|
+
Base classes for implementing Domain-Driven Design (DDD) applications in TypeScript.
|
|
7
|
+
|
|
8
|
+
## Installation
|
|
9
|
+
|
|
10
|
+
```bash
|
|
11
|
+
npm install ts-ddd
|
|
12
|
+
```
|
|
13
|
+
|
|
14
|
+
## Features
|
|
15
|
+
|
|
16
|
+
This package provides the following DDD building blocks:
|
|
17
|
+
|
|
18
|
+
- **Entity**: Base class for entities with unique identifiers
|
|
19
|
+
- **ValueObject**: Base class for immutable value objects
|
|
20
|
+
- **AggregateRoot**: Base class for aggregate roots with domain event support
|
|
21
|
+
- **DomainEvent**: Base class for domain events
|
|
22
|
+
- **Repository**: Interface for repository pattern
|
|
23
|
+
- **UniqueEntityID**: Utility class for generating unique identifiers
|
|
24
|
+
|
|
25
|
+
## Usage
|
|
26
|
+
|
|
27
|
+
### Entity
|
|
28
|
+
|
|
29
|
+
Entities are objects that have a unique identity that runs through time and different representations.
|
|
30
|
+
|
|
31
|
+
```typescript
|
|
32
|
+
import { Entity } from 'ts-ddd';
|
|
33
|
+
|
|
34
|
+
interface UserProps {
|
|
35
|
+
name: string;
|
|
36
|
+
email: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
class User extends Entity<string> {
|
|
40
|
+
get name(): string {
|
|
41
|
+
return this.props.name;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
get email(): string {
|
|
45
|
+
return this.props.email;
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
public updateEmail(newEmail: string): void {
|
|
49
|
+
this.props.email = newEmail;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
// Create a new user
|
|
54
|
+
const user = new User('user-123', { name: 'John Doe', email: 'john@example.com' });
|
|
55
|
+
console.log(user.id); // 'user-123'
|
|
56
|
+
console.log(user.name); // 'John Doe'
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
### ValueObject
|
|
60
|
+
|
|
61
|
+
Value Objects are immutable objects that are defined by their attributes rather than a unique identity.
|
|
62
|
+
|
|
63
|
+
```typescript
|
|
64
|
+
import { ValueObject } from 'ts-ddd';
|
|
65
|
+
|
|
66
|
+
interface AddressProps {
|
|
67
|
+
street: string;
|
|
68
|
+
city: string;
|
|
69
|
+
zipCode: string;
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
class Address extends ValueObject<AddressProps> {
|
|
73
|
+
get street(): string {
|
|
74
|
+
return this.props.street;
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
get city(): string {
|
|
78
|
+
return this.props.city;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
get zipCode(): string {
|
|
82
|
+
return this.props.zipCode;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
// Create addresses
|
|
87
|
+
const address1 = new Address({ street: '123 Main St', city: 'Springfield', zipCode: '12345' });
|
|
88
|
+
const address2 = new Address({ street: '123 Main St', city: 'Springfield', zipCode: '12345' });
|
|
89
|
+
|
|
90
|
+
// Value objects with same properties are equal
|
|
91
|
+
console.log(address1.equals(address2)); // true
|
|
92
|
+
```
|
|
93
|
+
|
|
94
|
+
### AggregateRoot
|
|
95
|
+
|
|
96
|
+
An Aggregate Root is an entity that is the root of an aggregate, which is a cluster of domain objects that can be treated as a single unit.
|
|
97
|
+
|
|
98
|
+
```typescript
|
|
99
|
+
import { AggregateRoot, DomainEvent, UniqueEntityID } from 'ts-ddd';
|
|
100
|
+
|
|
101
|
+
class OrderPlacedEvent extends DomainEvent {
|
|
102
|
+
constructor(aggregateId: UniqueEntityID, public readonly total: number) {
|
|
103
|
+
super(aggregateId);
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
|
|
107
|
+
interface OrderProps {
|
|
108
|
+
total: number;
|
|
109
|
+
status: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
class Order extends AggregateRoot<string> {
|
|
113
|
+
get total(): number {
|
|
114
|
+
return this.props.total;
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
get status(): string {
|
|
118
|
+
return this.props.status;
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
public placeOrder(): void {
|
|
122
|
+
this.props.status = 'placed';
|
|
123
|
+
const event = new OrderPlacedEvent(new UniqueEntityID(this.id), this.total);
|
|
124
|
+
this.addDomainEvent(event);
|
|
125
|
+
}
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
// Create and place an order
|
|
129
|
+
const order = new Order({ total: 100, status: 'pending' }, 'order-123');
|
|
130
|
+
order.placeOrder();
|
|
131
|
+
|
|
132
|
+
// Access domain events
|
|
133
|
+
console.log(order.domainEvents.length); // 1
|
|
134
|
+
console.log(order.domainEvents[0] instanceof OrderPlacedEvent); // true
|
|
135
|
+
|
|
136
|
+
// Clear events after dispatching
|
|
137
|
+
order.clearEvents();
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
### Repository Interface
|
|
141
|
+
|
|
142
|
+
The Repository interface provides a contract for data access objects.
|
|
143
|
+
|
|
144
|
+
```typescript
|
|
145
|
+
import { IRepository } from 'ts-ddd';
|
|
146
|
+
|
|
147
|
+
class UserRepository implements IRepository<User> {
|
|
148
|
+
async findById(id: string): Promise<User | null> {
|
|
149
|
+
// Implementation
|
|
150
|
+
return null;
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
async save(entity: User): Promise<void> {
|
|
154
|
+
// Implementation
|
|
155
|
+
}
|
|
156
|
+
|
|
157
|
+
async delete(entity: User): Promise<void> {
|
|
158
|
+
// Implementation
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
```
|
|
162
|
+
|
|
163
|
+
## Development
|
|
164
|
+
|
|
165
|
+
```bash
|
|
166
|
+
# Install dependencies
|
|
167
|
+
npm install
|
|
168
|
+
|
|
169
|
+
# Build the package
|
|
170
|
+
npm run build
|
|
171
|
+
|
|
172
|
+
# Run tests
|
|
173
|
+
npm test
|
|
174
|
+
|
|
175
|
+
# Lint code
|
|
176
|
+
npm run lint
|
|
177
|
+
|
|
178
|
+
# Format code
|
|
179
|
+
npm run format
|
|
180
|
+
```
|
|
181
|
+
|
|
182
|
+
## Contributing
|
|
183
|
+
|
|
184
|
+
Contributions are welcome! Please feel free to submit a Pull Request.
|
|
185
|
+
|
|
186
|
+
1. Fork the repository
|
|
187
|
+
2. Create your feature branch (`git checkout -b feature/amazing-feature`)
|
|
188
|
+
3. Commit your changes (`git commit -m 'Add some amazing feature'`)
|
|
189
|
+
4. Push to the branch (`git push origin feature/amazing-feature`)
|
|
190
|
+
5. Open a Pull Request
|
|
191
|
+
|
|
192
|
+
## License
|
|
193
|
+
|
|
194
|
+
MIT
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import { Entity } from './entity';
|
|
2
|
+
import { DomainEvent } from './domain-event';
|
|
3
|
+
/**
|
|
4
|
+
* Base class for Aggregate Roots in DDD.
|
|
5
|
+
* An Aggregate Root is an entity that is the root of an aggregate,
|
|
6
|
+
* which is a cluster of domain objects that can be treated as a single unit.
|
|
7
|
+
*/
|
|
8
|
+
export declare abstract class AggregateRoot<TId, TProps = Record<string, unknown>> extends Entity<TId, TProps> {
|
|
9
|
+
private _events;
|
|
10
|
+
/**
|
|
11
|
+
* Gets the domain events for this aggregate.
|
|
12
|
+
*/
|
|
13
|
+
get events(): DomainEvent[];
|
|
14
|
+
/**
|
|
15
|
+
* Adds a domain event to be dispatched.
|
|
16
|
+
*/
|
|
17
|
+
protected publishEvent(event: DomainEvent): void;
|
|
18
|
+
/**
|
|
19
|
+
* Clears all domain events.
|
|
20
|
+
*/
|
|
21
|
+
clearEvents(): void;
|
|
22
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AggregateRoot = void 0;
|
|
4
|
+
const entity_1 = require("./entity");
|
|
5
|
+
/**
|
|
6
|
+
* Base class for Aggregate Roots in DDD.
|
|
7
|
+
* An Aggregate Root is an entity that is the root of an aggregate,
|
|
8
|
+
* which is a cluster of domain objects that can be treated as a single unit.
|
|
9
|
+
*/
|
|
10
|
+
class AggregateRoot extends entity_1.Entity {
|
|
11
|
+
constructor() {
|
|
12
|
+
super(...arguments);
|
|
13
|
+
this._events = [];
|
|
14
|
+
}
|
|
15
|
+
/**
|
|
16
|
+
* Gets the domain events for this aggregate.
|
|
17
|
+
*/
|
|
18
|
+
get events() {
|
|
19
|
+
return this._events;
|
|
20
|
+
}
|
|
21
|
+
/**
|
|
22
|
+
* Adds a domain event to be dispatched.
|
|
23
|
+
*/
|
|
24
|
+
publishEvent(event) {
|
|
25
|
+
this._events.push(event);
|
|
26
|
+
}
|
|
27
|
+
/**
|
|
28
|
+
* Clears all domain events.
|
|
29
|
+
*/
|
|
30
|
+
clearEvents() {
|
|
31
|
+
this._events = [];
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
exports.AggregateRoot = AggregateRoot;
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Unique identifier for domain events.
|
|
3
|
+
*/
|
|
4
|
+
export declare class UniqueEntityID {
|
|
5
|
+
private value;
|
|
6
|
+
constructor(id?: string);
|
|
7
|
+
private generateId;
|
|
8
|
+
toString(): string;
|
|
9
|
+
toValue(): string;
|
|
10
|
+
equals(id?: UniqueEntityID): boolean;
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Interface for domain events.
|
|
14
|
+
*/
|
|
15
|
+
export interface IDomainEvent {
|
|
16
|
+
dateTimeOccurred: Date;
|
|
17
|
+
getAggregateId(): UniqueEntityID;
|
|
18
|
+
}
|
|
19
|
+
/**
|
|
20
|
+
* Base class for Domain Events in DDD.
|
|
21
|
+
* Domain Events represent something that happened in the domain that domain experts care about.
|
|
22
|
+
*/
|
|
23
|
+
export declare abstract class DomainEvent implements IDomainEvent {
|
|
24
|
+
dateTimeOccurred: Date;
|
|
25
|
+
private aggregateId;
|
|
26
|
+
constructor(aggregateId: UniqueEntityID);
|
|
27
|
+
getAggregateId(): UniqueEntityID;
|
|
28
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.DomainEvent = exports.UniqueEntityID = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Unique identifier for domain events.
|
|
6
|
+
*/
|
|
7
|
+
class UniqueEntityID {
|
|
8
|
+
constructor(id) {
|
|
9
|
+
this.value = id || this.generateId();
|
|
10
|
+
}
|
|
11
|
+
generateId() {
|
|
12
|
+
return `${Date.now()}-${Math.random().toString(36).substring(2, 11)}`;
|
|
13
|
+
}
|
|
14
|
+
toString() {
|
|
15
|
+
return this.value;
|
|
16
|
+
}
|
|
17
|
+
toValue() {
|
|
18
|
+
return this.value;
|
|
19
|
+
}
|
|
20
|
+
equals(id) {
|
|
21
|
+
if (id === null || id === undefined) {
|
|
22
|
+
return false;
|
|
23
|
+
}
|
|
24
|
+
if (!(id instanceof UniqueEntityID)) {
|
|
25
|
+
return false;
|
|
26
|
+
}
|
|
27
|
+
return id.toValue() === this.value;
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
exports.UniqueEntityID = UniqueEntityID;
|
|
31
|
+
/**
|
|
32
|
+
* Base class for Domain Events in DDD.
|
|
33
|
+
* Domain Events represent something that happened in the domain that domain experts care about.
|
|
34
|
+
*/
|
|
35
|
+
class DomainEvent {
|
|
36
|
+
constructor(aggregateId) {
|
|
37
|
+
this.dateTimeOccurred = new Date();
|
|
38
|
+
this.aggregateId = aggregateId;
|
|
39
|
+
}
|
|
40
|
+
getAggregateId() {
|
|
41
|
+
return this.aggregateId;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
exports.DomainEvent = DomainEvent;
|
package/dist/entity.d.ts
ADDED
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Interface for entities that have a unique identifier.
|
|
3
|
+
*/
|
|
4
|
+
export interface IEntity<TId, TProps> {
|
|
5
|
+
equals(object?: Entity<TId, TProps>): boolean;
|
|
6
|
+
}
|
|
7
|
+
/**
|
|
8
|
+
* Base class for Entities in DDD.
|
|
9
|
+
* Entities are objects that have a unique identity that runs through time and different representations.
|
|
10
|
+
*/
|
|
11
|
+
export declare abstract class Entity<TId, TProps = Record<string, unknown>> implements IEntity<TId, TProps> {
|
|
12
|
+
readonly id: TId;
|
|
13
|
+
readonly props: TProps;
|
|
14
|
+
constructor(id: TId, props: TProps);
|
|
15
|
+
/**
|
|
16
|
+
* Checks equality between entities based on their identity.
|
|
17
|
+
*/
|
|
18
|
+
equals(object?: Entity<TId, TProps>): boolean;
|
|
19
|
+
}
|
package/dist/entity.js
ADDED
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Entity = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base class for Entities in DDD.
|
|
6
|
+
* Entities are objects that have a unique identity that runs through time and different representations.
|
|
7
|
+
*/
|
|
8
|
+
class Entity {
|
|
9
|
+
constructor(id, props) {
|
|
10
|
+
this.id = id;
|
|
11
|
+
this.props = props;
|
|
12
|
+
}
|
|
13
|
+
/**
|
|
14
|
+
* Checks equality between entities based on their identity.
|
|
15
|
+
*/
|
|
16
|
+
equals(object) {
|
|
17
|
+
if (object == null || object == undefined) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (this === object) {
|
|
21
|
+
return true;
|
|
22
|
+
}
|
|
23
|
+
if (!(object instanceof Entity)) {
|
|
24
|
+
return false;
|
|
25
|
+
}
|
|
26
|
+
return this.id === object.id;
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.Entity = Entity;
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* ts-ddd - Base classes for implementing Domain-Driven Design (DDD) applications in TypeScript
|
|
3
|
+
* Copyright (c) 2026 Thomas Hampson <thomas@hmpsn.me>
|
|
4
|
+
* Licensed under the MIT License - see LICENSE.md for details
|
|
5
|
+
*/
|
|
6
|
+
export { ValueObject } from './value-object';
|
|
7
|
+
export { Entity, IEntity } from './entity';
|
|
8
|
+
export { AggregateRoot } from './aggregate-root';
|
|
9
|
+
export { DomainEvent, IDomainEvent, UniqueEntityID } from './domain-event';
|
|
10
|
+
export { IRepository } from './repository';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
/**
|
|
3
|
+
* ts-ddd - Base classes for implementing Domain-Driven Design (DDD) applications in TypeScript
|
|
4
|
+
* Copyright (c) 2026 Thomas Hampson <thomas@hmpsn.me>
|
|
5
|
+
* Licensed under the MIT License - see LICENSE.md for details
|
|
6
|
+
*/
|
|
7
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
8
|
+
exports.UniqueEntityID = exports.DomainEvent = exports.AggregateRoot = exports.Entity = exports.ValueObject = void 0;
|
|
9
|
+
var value_object_1 = require("./value-object");
|
|
10
|
+
Object.defineProperty(exports, "ValueObject", { enumerable: true, get: function () { return value_object_1.ValueObject; } });
|
|
11
|
+
var entity_1 = require("./entity");
|
|
12
|
+
Object.defineProperty(exports, "Entity", { enumerable: true, get: function () { return entity_1.Entity; } });
|
|
13
|
+
var aggregate_root_1 = require("./aggregate-root");
|
|
14
|
+
Object.defineProperty(exports, "AggregateRoot", { enumerable: true, get: function () { return aggregate_root_1.AggregateRoot; } });
|
|
15
|
+
var domain_event_1 = require("./domain-event");
|
|
16
|
+
Object.defineProperty(exports, "DomainEvent", { enumerable: true, get: function () { return domain_event_1.DomainEvent; } });
|
|
17
|
+
Object.defineProperty(exports, "UniqueEntityID", { enumerable: true, get: function () { return domain_event_1.UniqueEntityID; } });
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import { Entity } from './entity';
|
|
2
|
+
/**
|
|
3
|
+
* Generic repository interface for DDD.
|
|
4
|
+
* Repositories provide the illusion of an in-memory collection of all objects of a certain type.
|
|
5
|
+
*/
|
|
6
|
+
export interface IRepository<T extends Entity<unknown, unknown>> {
|
|
7
|
+
/**
|
|
8
|
+
* Finds an entity by its unique identifier.
|
|
9
|
+
*/
|
|
10
|
+
findById(id: T['id']): Promise<T | null>;
|
|
11
|
+
/**
|
|
12
|
+
* Saves an entity to the repository.
|
|
13
|
+
*/
|
|
14
|
+
save(entity: T): Promise<void>;
|
|
15
|
+
/**
|
|
16
|
+
* Removes an entity from the repository.
|
|
17
|
+
*/
|
|
18
|
+
delete(entity: T): Promise<void>;
|
|
19
|
+
}
|
|
@@ -0,0 +1,17 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Base class for Value Objects in DDD.
|
|
3
|
+
* Value Objects are immutable objects that are defined by their attributes rather than a unique identity.
|
|
4
|
+
*/
|
|
5
|
+
export declare abstract class ValueObject<T> {
|
|
6
|
+
protected readonly props: T;
|
|
7
|
+
constructor(props: T);
|
|
8
|
+
/**
|
|
9
|
+
* Checks equality between value objects based on their properties.
|
|
10
|
+
* Uses deep comparison of properties.
|
|
11
|
+
*/
|
|
12
|
+
equals(vo?: ValueObject<T>): boolean;
|
|
13
|
+
/**
|
|
14
|
+
* Deep equality comparison for objects.
|
|
15
|
+
*/
|
|
16
|
+
private deepEquals;
|
|
17
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ValueObject = void 0;
|
|
4
|
+
/**
|
|
5
|
+
* Base class for Value Objects in DDD.
|
|
6
|
+
* Value Objects are immutable objects that are defined by their attributes rather than a unique identity.
|
|
7
|
+
*/
|
|
8
|
+
class ValueObject {
|
|
9
|
+
constructor(props) {
|
|
10
|
+
this.props = Object.freeze(props);
|
|
11
|
+
}
|
|
12
|
+
/**
|
|
13
|
+
* Checks equality between value objects based on their properties.
|
|
14
|
+
* Uses deep comparison of properties.
|
|
15
|
+
*/
|
|
16
|
+
equals(vo) {
|
|
17
|
+
if (vo === null || vo === undefined) {
|
|
18
|
+
return false;
|
|
19
|
+
}
|
|
20
|
+
if (vo.props === undefined) {
|
|
21
|
+
return false;
|
|
22
|
+
}
|
|
23
|
+
return this.deepEquals(this.props, vo.props);
|
|
24
|
+
}
|
|
25
|
+
/**
|
|
26
|
+
* Deep equality comparison for objects.
|
|
27
|
+
*/
|
|
28
|
+
deepEquals(a, b) {
|
|
29
|
+
if (a === b)
|
|
30
|
+
return true;
|
|
31
|
+
if (a === null || b === null || a === undefined || b === undefined) {
|
|
32
|
+
return false;
|
|
33
|
+
}
|
|
34
|
+
if (typeof a !== typeof b)
|
|
35
|
+
return false;
|
|
36
|
+
if (typeof a !== 'object')
|
|
37
|
+
return a === b;
|
|
38
|
+
const keysA = Object.keys(a);
|
|
39
|
+
const keysB = Object.keys(b);
|
|
40
|
+
if (keysA.length !== keysB.length)
|
|
41
|
+
return false;
|
|
42
|
+
for (const key of keysA) {
|
|
43
|
+
if (!keysB.includes(key))
|
|
44
|
+
return false;
|
|
45
|
+
if (!this.deepEquals(a[key], b[key]))
|
|
46
|
+
return false;
|
|
47
|
+
}
|
|
48
|
+
return true;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.ValueObject = ValueObject;
|
package/package.json
ADDED
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@hmpsn/ts-ddd",
|
|
3
|
+
"version": "1.0.0",
|
|
4
|
+
"description": "Base classes for implementing Domain-Driven Design (DDD) applications in TypeScript",
|
|
5
|
+
"main": "dist/index.js",
|
|
6
|
+
"types": "dist/index.d.ts",
|
|
7
|
+
"scripts": {
|
|
8
|
+
"build": "tsc",
|
|
9
|
+
"test": "jest",
|
|
10
|
+
"lint": "eslint src --ext .ts",
|
|
11
|
+
"format": "prettier --write \"src/**/*.ts\"",
|
|
12
|
+
"prepublishOnly": "npm run build"
|
|
13
|
+
},
|
|
14
|
+
"keywords": [
|
|
15
|
+
"ddd",
|
|
16
|
+
"domain-driven-design",
|
|
17
|
+
"typescript",
|
|
18
|
+
"entity",
|
|
19
|
+
"value-object",
|
|
20
|
+
"aggregate-root",
|
|
21
|
+
"domain-event"
|
|
22
|
+
],
|
|
23
|
+
"author": "Thomas Hampson <thomas@hmpsn.me>",
|
|
24
|
+
"license": "MIT",
|
|
25
|
+
"files": [
|
|
26
|
+
"dist"
|
|
27
|
+
],
|
|
28
|
+
"repository": {
|
|
29
|
+
"type": "git",
|
|
30
|
+
"url": "https://github.com/mrhammo/ts-ddd.git"
|
|
31
|
+
},
|
|
32
|
+
"publishConfig": {
|
|
33
|
+
"access": "public"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@semantic-release/changelog": "^6.0.3",
|
|
37
|
+
"@semantic-release/git": "^10.0.1",
|
|
38
|
+
"@types/jest": "^30.0.0",
|
|
39
|
+
"@types/node": "^25.0.10",
|
|
40
|
+
"@typescript-eslint/eslint-plugin": "^8.53.1",
|
|
41
|
+
"@typescript-eslint/parser": "^8.53.1",
|
|
42
|
+
"eslint": "^9.39.2",
|
|
43
|
+
"jest": "^30.2.0",
|
|
44
|
+
"prettier": "^3.8.1",
|
|
45
|
+
"semantic-release": "^24.2.0",
|
|
46
|
+
"ts-jest": "^29.4.6",
|
|
47
|
+
"typescript": "^5.9.3"
|
|
48
|
+
}
|
|
49
|
+
}
|