Skip to content

Commit b3fb774

Browse files
Merge pull request #378 from 4lessandrodev/feat/update-core-dep
[3.9.0] - 2024-04-28 - Update (Break Change)
2 parents f874d7e + 7a1d343 commit b3fb774

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

50 files changed

+1515
-1115
lines changed

CHANGELOG.md

Lines changed: 158 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,164 @@ All notable changes to this project will be documented in this file.
44

55
## Unreleased
66

7+
---
8+
9+
## Released
10+
11+
### [3.9.0] - 2024-04-28
12+
13+
### Update (Break Change)
14+
15+
- Update core to v1.23.0
16+
- check [Core Changelog](https://github.com/4lessandrodev/rich-domain/blob/main/CHANGELOG.md)
17+
18+
---
19+
20+
## Released
21+
22+
### [3.8.3] - 2024-04-13
23+
24+
### Update
25+
26+
- Update core
27+
- Added support to multi context name
28+
- Details [Commit](https://github.com/4lessandrodev/rich-domain/commit/00db292f0604469c8bf2f2fddf6460901a084cc6)
29+
30+
```ts
31+
32+
// Example Usage
33+
const context = Context.events();
34+
35+
const handler = (event) => console.log(event);
36+
37+
// Subscribing to events under different contexts
38+
context.subscribe('Context-A:SIGNUP', handler);
39+
context.subscribe('Context-B:SIGNUP', handler);
40+
context.subscribe('Context-C:SIGNUP', handler);
41+
context.subscribe('Context-B:NOTIFY', handler);
42+
context.subscribe('Context-B:SEND-EMAIL', handler);
43+
44+
// Dispatching events to specific contexts
45+
// Dispatches the SIGNUP event to Context-B
46+
context.dispatchEvent('Context-B:SIGNUP');
47+
// Dispatches the SIGNUP event to all contexts
48+
context.dispatchEvent('*:SIGNUP');
49+
// Dispatches all events to all contexts. Not recommended
50+
context.dispatchEvent('*:*');
51+
// Dispatches all events under Context-B
52+
context.dispatchEvent('Context-B:*');
53+
54+
```
55+
56+
---
57+
58+
### [3.8.2] - 2024-04-12
59+
60+
### Update
61+
62+
- Update core
63+
- Added support to global events [ChangeLog](https://github.com/4lessandrodev/rich-domain/pull/139)
64+
65+
---
66+
67+
### [3.8.1] - 2024-03-18
68+
69+
### Update
70+
71+
- Update core
72+
- Fix logger messages
73+
74+
---
75+
76+
### [3.8.0] - 2024-03-18
77+
78+
### Update
79+
80+
- update deps core to v1.20.0 see [changes](https://github.com/4lessandrodev/rich-domain/blob/main/CHANGELOG.md)
81+
82+
---
83+
84+
### [3.7.2] - 2024-03-15
85+
86+
### Update
87+
88+
- update deps
89+
- added support to nodejs v21
90+
91+
---
92+
93+
### [3.7.1] - 2023-12-15
94+
95+
### Update
96+
97+
- update deps
98+
99+
---
100+
101+
### [3.7.0] - 2023-09-30
102+
103+
### Update
104+
105+
- Update core
106+
- update deps
107+
- rich-domain: update lib core to 1.19.0
108+
- remove support for deprecated history method
109+
- improve performance and save memory usage
110+
111+
---
112+
113+
### [3.6.4] - 2023-08-24
114+
115+
### Update
116+
117+
- Update core
118+
- update deps
119+
- rich-domain: update lib core to 1.18.4
120+
121+
---
122+
123+
### [3.6.3] - 2023-07-30
124+
125+
### Update
126+
127+
- Update core
128+
- rich-domain: update lib core to 1.18.3 #272 #282
129+
130+
---
131+
132+
### [3.6.2] - 2023-07-09
133+
134+
### Update
135+
136+
- Update core
137+
- rich-domain: update lib core to 1.18.2 #272
138+
139+
---
140+
141+
---
142+
### [3.6.1] - 2023-06-30
143+
144+
### Update
145+
146+
- Update core
147+
- rich-domain: update lib core to 1.18.1
148+
149+
---
150+
### [3.6.0] - 2023-04-21
151+
152+
### Update
153+
154+
- Update core
155+
- rich-domain: update lib core to 1.18.0 [More](https://github.com/4lessandrodev/rich-domain/blob/main/CHANGELOG.md)
156+
157+
158+
---
159+
### [3.5.3] - 2023-02-18
160+
161+
### Update
162+
163+
- Update core
164+
- rich-domain: update lib core to 1.17.3
7165

8166
---
9167

README.md

Lines changed: 168 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -367,9 +367,175 @@ console.log(result.toObject());
367367

368368
```
369369

370-
---
371370

372-
### See also how to use Aggregate.
371+
### Aggregate
372+
373+
Encapsulate and are composed of entity classes and value objects that change together in a business transaction
374+
375+
#### Create an aggregate to compose your context.
376+
377+
In my example, let's use the context of payment. All payment transactions are encapsulated by an order (payment order) that represents a user's purchasing context.
378+
379+
```ts
380+
381+
import { Aggregate, Ok, Fail, Result, UID, EventHandler } from 'types-ddd';
382+
383+
// Entities and VO that encapsulate context.
384+
interface Props {
385+
id?: UID;
386+
payment: Payment;
387+
items: List<Item>;
388+
status: OrderStatus;
389+
customer: Customer;
390+
}
391+
392+
// Simple example of an order aggregate encapsulating entities and
393+
// value objects for context.
394+
export default class Order extends Aggregate<Props> {
395+
396+
// Private constructor to ensure instances creation through static methods.
397+
private constructor(props: Props){
398+
super(props);
399+
}
400+
401+
// Static method to begin a new order.
402+
// Takes a customer as parameter and returns an instance of Order.
403+
public static begin(customer: Customer): Order {
404+
// Initialize the status of the order as "begin".
405+
const status = OrderStatus.begin();
406+
// Initialize the list of items as empty.
407+
const items: List<Item> = List.empty();
408+
// Initialize the payment as zero, since the order hasn't been paid yet.
409+
const payment = Payment.none();
410+
// Create a new instance of Order with the provided parameters.
411+
const order = new Order({ status, payment, items, customer });
412+
413+
// Add an event to indicate that the order has begun.
414+
order.addEvent('ORDER_HAS_BEGUN', (order) => {
415+
// Perform some important operation when the order begins.
416+
console.log('Do something important...');
417+
});
418+
419+
// Alternatively, add an event by creating an
420+
// instance of a class that extends EventHandler.
421+
order.addEvent(new OrderBeganEventHandler());
422+
423+
// Return the created order instance.
424+
return order;
425+
}
426+
427+
// Method to add an item to the order.
428+
// Takes an item as parameter and returns the Order instance.
429+
addItem(item: Item): Order {
430+
// Add the item to the order's items list.
431+
this.props.items.add(item);
432+
// Sum item price to payment amount
433+
this.props.payment.sum(item.price);
434+
// Return the Order instance itself to allow chained calls.
435+
return this;
436+
}
437+
438+
// Method to perform the payment of the order.
439+
// Takes a payment object as parameter.
440+
pay(payment: Payment): Order {
441+
// Set the status of the order to "paid".
442+
this.props.status = OrderStatus.paid();
443+
// Set the provided payment object.
444+
this.props.payment = payment;
445+
// Add an event to indicate that the order has been paid.
446+
// Assuming OrderPaidEvent is a class representing
447+
// the event of order payment.
448+
this.addEvent(new OrderPaidEventHandler());
449+
return this;
450+
}
451+
452+
// Static method to create an instance of Order.
453+
// Returns a Result, which can be Ok (success) or Fail (failure).
454+
// The value of the Result is an instance of Order,
455+
// if creation is successful.
456+
public static create(props: Props): Result<Order> {
457+
return Ok(new Order(props));
458+
}
459+
}
460+
461+
```
462+
463+
#### How to use events
464+
465+
Event Handler
466+
467+
```ts
468+
469+
import { Context, EventHandler } from 'rich-domain';
470+
471+
472+
class OrderCreatedEvent extends EventHandler<Order> {
473+
474+
constructor() {
475+
super({ eventName: 'OrderCreated' });
476+
}
477+
478+
dispatch(order: Order): void {
479+
// dispatch event to another context
480+
order.context().dispatchEvent('Context:Event', order.toObject());
481+
};
482+
}
483+
484+
```
485+
486+
Aggregates domain events
487+
488+
489+
```ts
490+
491+
order.addEvent('Event', (...args) => {
492+
console.log(args);
493+
});
494+
495+
// Or add an EventHandler instance
496+
order.addEvent(new OrderCreatedEvent());
497+
498+
order.dispatchEvent('OrderBegun');
499+
500+
// dispatch with args
501+
order.dispatchEvent('Event', { info: 'custom_args' });
502+
503+
// OR call all added events
504+
await order.dispatchAll();
505+
506+
507+
```
508+
509+
#### How to subscribe to a global event
510+
511+
```ts
512+
513+
import { Context } from 'rich-domain';
514+
515+
const context = Context.events();
516+
517+
context.subscribe('Context:Event', (event) => {
518+
const [model] = event.detail;
519+
console.log(model);
520+
});
521+
522+
// dispatch an event to a context with args
523+
context.dispatchEvent('Context:Event', { name: 'Jane' });
524+
525+
// Dispatching events to specific contexts
526+
// Dispatches the SIGNUP event to Context-X
527+
context.dispatchEvent('Context-X:Signup');
528+
529+
// Dispatches the SIGNUP event to all contexts
530+
context.dispatchEvent('*:Signup');
531+
532+
// Dispatches all events to all contexts. Not recommended
533+
context.dispatchEvent('*:*');
534+
535+
// Dispatches all events under Context-Y
536+
context.dispatchEvent('Context-Y:*');
537+
538+
```
373539

374540
## Lib Full Documentation
375541

SECURITY.md

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Security Policy
2+
3+
## Supported Versions
4+
5+
Versions of types-ddd are currently being supported with security updates.
6+
7+
| Version | Supported |
8+
| ------- | ------------------ |
9+
| 3.8.x | :white_check_mark: |
10+
| < 3.8 | :x: |
11+
12+
## Reporting a Vulnerability
13+
14+
If you find some vulnerability please report as issue.
15+
16+
Every month security updates are published as 0.0.x version.

docs/README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1351,7 +1351,7 @@ Now we can dispatch the event whenever we want.
13511351

13521352
```ts
13531353

1354-
DomainEvents.dispatch({ id: product.id, eventName: "ProductCreated" });
1354+
product.dispatch("ProductCreated");
13551355

13561356
> "EVENT DISPATCH: [Aggregate@Product]:6039756f-d3bc-452e-932a-ec89ff536dda"
13571357

lib/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,3 +32,4 @@ export * from './patterns/index';
3232
export * from './core/index';
3333
export * from 'rich-domain/core';
3434
export * from 'rich-domain/utils';
35+
export * from 'rich-domain/types';

lib/patterns/proxy/proxy.pattern.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ import { IProxyContext } from '../../types/types';
9191
*/
9292
export abstract class TSProxy<Data, Payload, Error = string> {
9393
constructor(
94-
private readonly context: IProxyContext<Data, Payload, Error>
94+
private readonly context: IProxyContext<Data, Payload, Error>,
9595
) {}
9696

9797
private async canExecute(data: Data): Promise<Result<boolean, Error>> {
@@ -109,7 +109,7 @@ export abstract class TSProxy<Data, Payload, Error = string> {
109109
}
110110

111111
private async afterExecute(
112-
data: Result<Payload, Error>
112+
data: Result<Payload, Error>,
113113
): Promise<Result<Payload, Error>> {
114114
if (!this.context.afterExecute) {
115115
return Result.Ok(data.value());

0 commit comments

Comments
 (0)