@woltz/rich-domain 0.2.2 → 1.1.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/CHANGELOG.md +23 -75
- package/LICENSE +20 -20
- package/README.md +37 -20
- package/dist/base-entity.d.ts +2 -2
- package/dist/base-entity.d.ts.map +1 -1
- package/dist/base-entity.js +6 -4
- package/dist/base-entity.js.map +1 -1
- package/dist/criteria.d.ts +5 -11
- package/dist/criteria.d.ts.map +1 -1
- package/dist/criteria.js +4 -3
- package/dist/criteria.js.map +1 -1
- package/dist/deep-proxy.d.ts +3 -1
- package/dist/deep-proxy.d.ts.map +1 -1
- package/dist/deep-proxy.js +116 -29
- package/dist/deep-proxy.js.map +1 -1
- package/dist/domain-event-bus.d.ts +5 -6
- package/dist/domain-event-bus.d.ts.map +1 -1
- package/dist/domain-event-bus.js +3 -11
- package/dist/domain-event-bus.js.map +1 -1
- package/dist/domain-event.d.ts +1 -31
- package/dist/domain-event.d.ts.map +1 -1
- package/dist/domain-event.js +2 -1
- package/dist/domain-event.js.map +1 -1
- package/dist/entity.d.ts +2 -2
- package/dist/entity.js +1 -1
- package/dist/exceptions.d.ts +251 -0
- package/dist/exceptions.d.ts.map +1 -0
- package/dist/exceptions.js +321 -0
- package/dist/exceptions.js.map +1 -0
- package/dist/id.d.ts +3 -3
- package/dist/id.d.ts.map +1 -1
- package/dist/id.js +15 -4
- package/dist/id.js.map +1 -1
- package/dist/index.d.ts +2 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -8
- package/dist/index.js.map +1 -1
- package/dist/paginated-result.d.ts.map +1 -1
- package/dist/paginated-result.js +12 -1
- package/dist/paginated-result.js.map +1 -1
- package/dist/repository/index.d.ts +2 -39
- package/dist/repository/index.d.ts.map +1 -1
- package/dist/repository/index.js +2 -39
- package/dist/repository/index.js.map +1 -1
- package/dist/repository/unit-of-work.d.ts +0 -11
- package/dist/repository/unit-of-work.d.ts.map +1 -1
- package/dist/repository/unit-of-work.js +0 -35
- package/dist/repository/unit-of-work.js.map +1 -1
- package/dist/types/criteria.d.ts +6 -2
- package/dist/types/criteria.d.ts.map +1 -1
- package/dist/types/criteria.js +1 -1
- package/dist/types/criteria.js.map +1 -1
- package/dist/types/domain-event.d.ts +32 -0
- package/dist/types/domain-event.d.ts.map +1 -0
- package/dist/types/domain-event.js +2 -0
- package/dist/types/domain-event.js.map +1 -0
- package/dist/types/domain.d.ts +2 -2
- package/dist/types/domain.d.ts.map +1 -1
- package/dist/types/history-tracker.d.ts +1 -1
- package/dist/types/history-tracker.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/index.js +1 -0
- package/dist/types/index.js.map +1 -1
- package/dist/value-object.d.ts +1 -1
- package/dist/value-object.d.ts.map +1 -1
- package/dist/value-object.js +2 -5
- package/dist/value-object.js.map +1 -1
- package/eslint.config.js +3 -3
- package/jest.config.js +1 -1
- package/package.json +14 -20
- package/src/base-entity.ts +6 -5
- package/src/criteria.ts +11 -11
- package/src/deep-proxy.ts +447 -339
- package/src/domain-event-bus.ts +152 -166
- package/src/domain-event.ts +53 -90
- package/src/entity.ts +16 -16
- package/src/exceptions.ts +435 -0
- package/src/id.ts +107 -94
- package/src/index.ts +26 -9
- package/src/paginated-result.ts +14 -1
- package/src/repository/index.ts +2 -44
- package/src/repository/unit-of-work.ts +1 -44
- package/src/types/criteria.ts +7 -2
- package/src/types/domain-event.ts +38 -0
- package/src/types/domain.ts +2 -3
- package/src/types/history-tracker.ts +1 -1
- package/src/types/index.ts +1 -0
- package/src/validation-error.ts +97 -97
- package/src/value-object.ts +3 -6
- package/tests/criteria.test.ts +8 -0
- package/tests/domain-events.test.ts +431 -445
- package/tests/entity-validation.test.ts +2 -2
- package/tests/entity.test.ts +33 -33
- package/tests/history-tracker.spec.ts +57 -17
- package/tests/id.test.ts +341 -341
- package/tests/repository.test.ts +8 -4
- package/tests/to-json.test.ts +103 -91
- package/tests/utils.ts +254 -151
- package/tests/value-object-validation.test.ts +0 -9
- package/tests/value-objects.test.ts +52 -52
- package/tsconfig.json +2 -24
- package/.github/workflows/ci.yml +0 -40
- package/.husky/commit-msg +0 -1
- package/.husky/pre-commit +0 -1
- package/.vscode/settings.json +0 -3
- package/commitlint.config.js +0 -23
- package/dist/filtering.d.ts +0 -107
- package/dist/filtering.d.ts.map +0 -1
- package/dist/filtering.js +0 -202
- package/dist/filtering.js.map +0 -1
- package/dist/ordering.d.ts +0 -93
- package/dist/ordering.d.ts.map +0 -1
- package/dist/ordering.js +0 -154
- package/dist/ordering.js.map +0 -1
- package/dist/pagination.d.ts +0 -218
- package/dist/pagination.d.ts.map +0 -1
- package/dist/pagination.js +0 -281
- package/dist/pagination.js.map +0 -1
- package/dist/repository/in-memory-repository.d.ts +0 -50
- package/dist/repository/in-memory-repository.d.ts.map +0 -1
- package/dist/repository/in-memory-repository.js +0 -93
- package/dist/repository/in-memory-repository.js.map +0 -1
- package/dist/repository/mapper.d.ts +0 -56
- package/dist/repository/mapper.d.ts.map +0 -1
- package/dist/repository/mapper.js +0 -15
- package/dist/repository/mapper.js.map +0 -1
- package/dist/repository/types.d.ts +0 -87
- package/dist/repository/types.d.ts.map +0 -1
- package/dist/repository/types.js +0 -6
- package/dist/repository/types.js.map +0 -1
- package/dist/repository.d.ts +0 -2
- package/dist/repository.d.ts.map +0 -1
- package/dist/repository.js +0 -21
- package/dist/repository.js.map +0 -1
- package/dist/specification.d.ts +0 -102
- package/dist/specification.d.ts.map +0 -1
- package/dist/specification.js +0 -187
- package/dist/specification.js.map +0 -1
- package/dist/types/repository.d.ts +0 -43
- package/dist/types/repository.d.ts.map +0 -1
- package/dist/types/repository.js +0 -2
- package/dist/types/repository.js.map +0 -1
- package/dist/types.d.ts +0 -88
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -12
- package/dist/types.js.map +0 -1
- package/src/repository/in-memory-repository.ts +0 -116
package/src/base-entity.ts
CHANGED
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
import { Id } from "./id";
|
|
6
6
|
import { DeepProxy } from "./deep-proxy";
|
|
7
7
|
import { ValidationError } from "./validation-error";
|
|
8
|
-
import { IDomainEvent } from "
|
|
8
|
+
import { IDomainEvent } from ".";
|
|
9
9
|
import {
|
|
10
10
|
BaseProps,
|
|
11
11
|
SubscriptionConfig,
|
|
@@ -18,6 +18,7 @@ import {
|
|
|
18
18
|
} from "./types";
|
|
19
19
|
import { DomainEventBus } from "./domain-event-bus";
|
|
20
20
|
import { DEFAULT_VALIDATION_CONFIG } from "./constants";
|
|
21
|
+
import { DomainError } from "./exceptions";
|
|
21
22
|
|
|
22
23
|
// Helper to get static properties from constructor
|
|
23
24
|
function getStaticProperty<T>(
|
|
@@ -60,7 +61,6 @@ export abstract class BaseEntity<T extends BaseProps> {
|
|
|
60
61
|
...validation?.config,
|
|
61
62
|
};
|
|
62
63
|
|
|
63
|
-
// Apply defaultValues
|
|
64
64
|
let finalProps = { ...props } as T;
|
|
65
65
|
|
|
66
66
|
// Generate ID if not provided
|
|
@@ -102,7 +102,7 @@ export abstract class BaseEntity<T extends BaseProps> {
|
|
|
102
102
|
const result = this.entitySchema["~standard"].validate(props);
|
|
103
103
|
|
|
104
104
|
if (result instanceof Promise) {
|
|
105
|
-
throw new
|
|
105
|
+
throw new DomainError(
|
|
106
106
|
"Async validation not supported in constructor. Use sync validation schema."
|
|
107
107
|
);
|
|
108
108
|
}
|
|
@@ -278,8 +278,8 @@ export abstract class BaseEntity<T extends BaseProps> {
|
|
|
278
278
|
return this._props.id;
|
|
279
279
|
}
|
|
280
280
|
|
|
281
|
-
|
|
282
|
-
return this._props.id.isNew;
|
|
281
|
+
public isNew(): boolean {
|
|
282
|
+
return this._props.id.isNew();
|
|
283
283
|
}
|
|
284
284
|
|
|
285
285
|
/**
|
|
@@ -386,6 +386,7 @@ export abstract class BaseEntity<T extends BaseProps> {
|
|
|
386
386
|
private deepToJson(obj: any): any {
|
|
387
387
|
if (obj === null || obj === undefined) return obj;
|
|
388
388
|
if (obj instanceof Id) return obj.value;
|
|
389
|
+
if (obj instanceof Date) return obj.toISOString();
|
|
389
390
|
if (Array.isArray(obj)) return obj.map((item) => this.deepToJson(item));
|
|
390
391
|
if (obj instanceof BaseEntity) return obj.toJson();
|
|
391
392
|
if (obj && typeof obj.toJson === "function") return obj.toJson();
|
package/src/criteria.ts
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
|
+
import { InvalidCriteriaError } from "./exceptions";
|
|
1
2
|
import {
|
|
2
3
|
FieldPath,
|
|
3
4
|
Filter,
|
|
5
|
+
FILTER_OPERATORS,
|
|
4
6
|
FilterOperator,
|
|
5
7
|
FilterValueFor,
|
|
6
8
|
Order,
|
|
7
9
|
OrderDirection,
|
|
8
10
|
Pagination,
|
|
9
11
|
PathValue,
|
|
12
|
+
Search,
|
|
10
13
|
TypedFilter,
|
|
11
14
|
} from "./types";
|
|
12
15
|
|
|
@@ -14,21 +17,18 @@ import {
|
|
|
14
17
|
// Filter Types
|
|
15
18
|
// ============================================================================
|
|
16
19
|
|
|
17
|
-
export class Criteria<T =
|
|
20
|
+
export class Criteria<T = any> {
|
|
18
21
|
private _filters: Filter<FieldPath<T>, any>[] = [];
|
|
19
22
|
private _orders: Order[] = [];
|
|
20
23
|
private _pagination: Pagination = { page: 1, limit: 20, offset: 0 };
|
|
21
|
-
private _search?:
|
|
22
|
-
fields: FieldPath<T>[];
|
|
23
|
-
value: string;
|
|
24
|
-
};
|
|
24
|
+
private _search?: Search<T>
|
|
25
25
|
|
|
26
26
|
private constructor() {}
|
|
27
27
|
|
|
28
28
|
/**
|
|
29
29
|
* Create a new Criteria instance
|
|
30
30
|
*/
|
|
31
|
-
static create<T =
|
|
31
|
+
static create<T = any>(): Criteria<T> {
|
|
32
32
|
return new Criteria<T>();
|
|
33
33
|
}
|
|
34
34
|
|
|
@@ -221,9 +221,9 @@ export class Criteria<T = unknown> {
|
|
|
221
221
|
const [field, operatorRaw] = key.split(":");
|
|
222
222
|
|
|
223
223
|
if (!operatorRaw || !field) continue;
|
|
224
|
-
|
|
225
224
|
const operator = isOperator(operatorRaw) ? operatorRaw : null;
|
|
226
|
-
if (!operator)
|
|
225
|
+
if (!operator)
|
|
226
|
+
throw new InvalidCriteriaError(`Invalid filter operator`, operatorRaw);
|
|
227
227
|
|
|
228
228
|
let parsedValue: any = value;
|
|
229
229
|
|
|
@@ -243,7 +243,7 @@ export class Criteria<T = unknown> {
|
|
|
243
243
|
continue;
|
|
244
244
|
}
|
|
245
245
|
|
|
246
|
-
criteria.where(field as
|
|
246
|
+
criteria.where(field as FieldPath<T>, operator, parseQueryValue(value));
|
|
247
247
|
}
|
|
248
248
|
|
|
249
249
|
// Pagination
|
|
@@ -259,7 +259,7 @@ export class Criteria<T = unknown> {
|
|
|
259
259
|
const sortParts = query.orderBy.split(",");
|
|
260
260
|
sortParts.forEach((part: string) => {
|
|
261
261
|
const [field, direction] = part.split(":");
|
|
262
|
-
criteria.orderBy(field as
|
|
262
|
+
criteria.orderBy(field as FieldPath<T>, (direction as OrderDirection) || "asc");
|
|
263
263
|
});
|
|
264
264
|
}
|
|
265
265
|
|
|
@@ -287,5 +287,5 @@ function parseQueryValue(value: string): any {
|
|
|
287
287
|
}
|
|
288
288
|
|
|
289
289
|
function isOperator(value: string): value is FilterOperator {
|
|
290
|
-
return
|
|
290
|
+
return FILTER_OPERATORS.includes(value as FilterOperator);
|
|
291
291
|
}
|