@libs-for-dev/nestjs-ddd-library 1.1.1 → 1.2.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/README.md +124 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -10,6 +10,9 @@ A comprehensive library providing Domain-Driven Design (DDD) building blocks for
|
|
|
10
10
|
- **Value Objects**: Immutable objects that contain attributes but have no conceptual identity with examples:
|
|
11
11
|
- **UUID**: Built-in UUID v7 generation and validation
|
|
12
12
|
- **Email**: Email value object with validation
|
|
13
|
+
- **Money**: Money value object with amount and currency (ISO 4217) validation
|
|
14
|
+
- **PhoneNumber**: Phone number value object with mobile phone validation
|
|
15
|
+
- **Url**: URL value object with protocol validation
|
|
13
16
|
|
|
14
17
|
## Installation
|
|
15
18
|
|
|
@@ -74,8 +77,65 @@ if (emailResult.isOk()) {
|
|
|
74
77
|
}
|
|
75
78
|
```
|
|
76
79
|
|
|
80
|
+
#### Money
|
|
81
|
+
|
|
82
|
+
```typescript
|
|
83
|
+
import { Money } from '@libs-for-dev/nestjs-ddd-library';
|
|
84
|
+
|
|
85
|
+
// Create a Money value object
|
|
86
|
+
const moneyResult = Money.create(100.50, 'USD');
|
|
87
|
+
|
|
88
|
+
if (moneyResult.isOk()) {
|
|
89
|
+
const money = moneyResult.unwrap();
|
|
90
|
+
console.log(money.amount); // 100.50
|
|
91
|
+
console.log(money.currency); // USD
|
|
92
|
+
console.log(money.value); // { amount: 100.50, currency: 'USD' }
|
|
93
|
+
} else {
|
|
94
|
+
// Handle error
|
|
95
|
+
console.error(moneyResult.unwrapErr());
|
|
96
|
+
}
|
|
97
|
+
```
|
|
98
|
+
|
|
99
|
+
#### PhoneNumber
|
|
100
|
+
|
|
101
|
+
```typescript
|
|
102
|
+
import { PhoneNumber } from '@libs-for-dev/nestjs-ddd-library';
|
|
103
|
+
|
|
104
|
+
// Create a PhoneNumber value object
|
|
105
|
+
const phoneResult = PhoneNumber.create('+1234567890');
|
|
106
|
+
|
|
107
|
+
if (phoneResult.isOk()) {
|
|
108
|
+
const phone = phoneResult.unwrap();
|
|
109
|
+
console.log(phone.value); // +1234567890
|
|
110
|
+
} else {
|
|
111
|
+
// Handle error
|
|
112
|
+
console.error(phoneResult.unwrapErr());
|
|
113
|
+
}
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
#### Url
|
|
117
|
+
|
|
118
|
+
```typescript
|
|
119
|
+
import { Url } from '@libs-for-dev/nestjs-ddd-library';
|
|
120
|
+
|
|
121
|
+
// Create a Url value object
|
|
122
|
+
const urlResult = Url.create('https://example.com');
|
|
123
|
+
|
|
124
|
+
if (urlResult.isOk()) {
|
|
125
|
+
const url = urlResult.unwrap();
|
|
126
|
+
console.log(url.value); // https://example.com
|
|
127
|
+
} else {
|
|
128
|
+
// Handle error
|
|
129
|
+
console.error(urlResult.unwrapErr());
|
|
130
|
+
}
|
|
131
|
+
```
|
|
132
|
+
|
|
77
133
|
### Entities
|
|
78
134
|
|
|
135
|
+
`AbstractEntity` provides immutable properties through `DeepReadonly`. All properties, including nested objects, are readonly and cannot be modified directly. Changes must be made through entity domain methods.
|
|
136
|
+
|
|
137
|
+
#### Basic Usage
|
|
138
|
+
|
|
79
139
|
```typescript
|
|
80
140
|
import { AbstractEntity, Uuid } from '@libs-for-dev/nestjs-ddd-library';
|
|
81
141
|
|
|
@@ -118,6 +178,70 @@ user.updateName('Jane Doe');
|
|
|
118
178
|
console.log(user.props.name); // Jane Doe
|
|
119
179
|
```
|
|
120
180
|
|
|
181
|
+
#### Readonly Properties with Nested Objects
|
|
182
|
+
|
|
183
|
+
Properties returned by `props` getter are deeply readonly, including nested objects. This ensures immutability and prevents accidental modifications:
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
import { AbstractEntity, Uuid } from '@libs-for-dev/nestjs-ddd-library';
|
|
187
|
+
|
|
188
|
+
interface UserProfileProps {
|
|
189
|
+
personal: {
|
|
190
|
+
firstName: string;
|
|
191
|
+
lastName: string;
|
|
192
|
+
};
|
|
193
|
+
settings: {
|
|
194
|
+
theme: string;
|
|
195
|
+
};
|
|
196
|
+
}
|
|
197
|
+
|
|
198
|
+
class UserProfile extends AbstractEntity<Uuid, UserProfileProps> {
|
|
199
|
+
public static create(id: Uuid, props: UserProfileProps): UserProfile {
|
|
200
|
+
return new UserProfile(id, props);
|
|
201
|
+
}
|
|
202
|
+
|
|
203
|
+
public updateFirstName(firstName: string): void {
|
|
204
|
+
this.propsData = {
|
|
205
|
+
props: {
|
|
206
|
+
...this.props,
|
|
207
|
+
personal: {
|
|
208
|
+
...this.props.personal,
|
|
209
|
+
firstName
|
|
210
|
+
}
|
|
211
|
+
}
|
|
212
|
+
};
|
|
213
|
+
}
|
|
214
|
+
}
|
|
215
|
+
|
|
216
|
+
// Usage
|
|
217
|
+
const profile = UserProfile.create(Uuid.generate(), {
|
|
218
|
+
personal: {
|
|
219
|
+
firstName: 'John',
|
|
220
|
+
lastName: 'Doe'
|
|
221
|
+
},
|
|
222
|
+
settings: {
|
|
223
|
+
theme: 'dark'
|
|
224
|
+
}
|
|
225
|
+
});
|
|
226
|
+
|
|
227
|
+
// All properties are readonly, including nested objects
|
|
228
|
+
// This will throw an error at runtime:
|
|
229
|
+
// profile.props.personal.firstName = 'Jane'; // ❌ Error: Cannot assign to read-only property
|
|
230
|
+
|
|
231
|
+
// This will also throw an error:
|
|
232
|
+
// profile.props.settings.theme = 'light'; // ❌ Error: Cannot assign to read-only property
|
|
233
|
+
|
|
234
|
+
// Changes must be made through domain methods
|
|
235
|
+
profile.updateFirstName('Jane');
|
|
236
|
+
console.log(profile.props.personal.firstName); // Jane
|
|
237
|
+
|
|
238
|
+
// Previous references remain unchanged (immutability)
|
|
239
|
+
const oldProps = profile.props;
|
|
240
|
+
profile.updateFirstName('Bob');
|
|
241
|
+
console.log(oldProps.personal.firstName); // Jane (unchanged)
|
|
242
|
+
console.log(profile.props.personal.firstName); // Bob (updated)
|
|
243
|
+
```
|
|
244
|
+
|
|
121
245
|
## Domain Events
|
|
122
246
|
|
|
123
247
|
The library integrates with NestJS CQRS to support domain events:
|