@globalart/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/dist/index.js ADDED
@@ -0,0 +1,1044 @@
1
+ //#region rolldown:runtime
2
+ var __create = Object.create;
3
+ var __defProp = Object.defineProperty;
4
+ var __getOwnPropDesc = Object.getOwnPropertyDescriptor;
5
+ var __getOwnPropNames = Object.getOwnPropertyNames;
6
+ var __getProtoOf = Object.getPrototypeOf;
7
+ var __hasOwnProp = Object.prototype.hasOwnProperty;
8
+ var __copyProps = (to, from, except, desc) => {
9
+ if (from && typeof from === "object" || typeof from === "function") for (var keys = __getOwnPropNames(from), i = 0, n = keys.length, key; i < n; i++) {
10
+ key = keys[i];
11
+ if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, {
12
+ get: ((k) => from[k]).bind(null, key),
13
+ enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable
14
+ });
15
+ }
16
+ return to;
17
+ };
18
+ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__getProtoOf(mod)) : {}, __copyProps(isNodeMode || !mod || !mod.__esModule ? __defProp(target, "default", {
19
+ value: mod,
20
+ enumerable: true
21
+ }) : target, mod));
22
+
23
+ //#endregion
24
+ let uuid = require("uuid");
25
+ uuid = __toESM(uuid);
26
+ let zod = require("zod");
27
+ zod = __toESM(zod);
28
+ let lodash = require("lodash");
29
+ lodash = __toESM(lodash);
30
+ let oxide_ts = require("oxide.ts");
31
+ oxide_ts = __toESM(oxide_ts);
32
+ let ts_pattern = require("ts-pattern");
33
+ ts_pattern = __toESM(ts_pattern);
34
+ let dequal = require("dequal");
35
+ dequal = __toESM(dequal);
36
+ let date_fns = require("date-fns");
37
+ date_fns = __toESM(date_fns);
38
+ let nanoid = require("nanoid");
39
+ nanoid = __toESM(nanoid);
40
+
41
+ //#region src/aggregate-root.ts
42
+ var AggregateRoot = class {
43
+ #domainEvents = [];
44
+ get domainEvents() {
45
+ return this.#domainEvents;
46
+ }
47
+ set domainEvents(events) {
48
+ this.#domainEvents = events;
49
+ }
50
+ addDomainEvent(event) {
51
+ this.#domainEvents.push(event);
52
+ }
53
+ removeEvents(events) {
54
+ this.#domainEvents = this.#domainEvents.filter((event) => !events.includes(event));
55
+ }
56
+ };
57
+
58
+ //#endregion
59
+ //#region src/command.ts
60
+ var Command = class {
61
+ commandId;
62
+ correlationId;
63
+ causationId;
64
+ constructor(props) {
65
+ this.correlationId = props.correlationId ?? (0, uuid.v4)();
66
+ this.commandId = props.commandId ?? (0, uuid.v4)();
67
+ }
68
+ };
69
+
70
+ //#endregion
71
+ //#region src/event.ts
72
+ const eventSchema = (name, payload, meta) => zod.z.object({
73
+ id: zod.z.string().uuid(),
74
+ name: zod.z.literal(name),
75
+ operatorId: zod.z.string().optional(),
76
+ payload,
77
+ timestamp: zod.z.coerce.date(),
78
+ meta
79
+ });
80
+ var BaseEvent = class {
81
+ constructor(payload, operatorId, meta, id = (0, uuid.v4)(), timestamp = /* @__PURE__ */ new Date()) {
82
+ this.payload = payload;
83
+ this.operatorId = operatorId;
84
+ this.meta = meta;
85
+ this.id = id;
86
+ this.timestamp = timestamp;
87
+ }
88
+ toJSON() {
89
+ return {
90
+ id: this.id,
91
+ name: this.name,
92
+ operatorId: this.operatorId,
93
+ timestamp: this.timestamp.toISOString(),
94
+ payload: this.payload,
95
+ meta: this.meta
96
+ };
97
+ }
98
+ };
99
+
100
+ //#endregion
101
+ //#region src/exception.base.ts
102
+ var ExceptionBase = class extends Error {
103
+ correlationId;
104
+ /**
105
+ *
106
+ * @param message
107
+ * @param correlationId
108
+ * @param cause
109
+ * @param metadata
110
+ */
111
+ constructor(message, correlationId, cause, metadata) {
112
+ super(message);
113
+ this.message = message;
114
+ this.cause = cause;
115
+ this.metadata = metadata;
116
+ Error.captureStackTrace(this, this.constructor);
117
+ this.correlationId = correlationId;
118
+ }
119
+ toJSON() {
120
+ return {
121
+ message: this.message,
122
+ code: this.code,
123
+ stack: this.stack,
124
+ correlationId: this.correlationId,
125
+ cause: JSON.stringify(this.cause),
126
+ metadata: this.metadata
127
+ };
128
+ }
129
+ };
130
+
131
+ //#endregion
132
+ //#region src/filter/base.filter.ts
133
+ const baseFilter = zod.z.object({
134
+ field: zod.z.string().min(1),
135
+ relation: zod.z.string().min(1).optional()
136
+ });
137
+
138
+ //#endregion
139
+ //#region src/filter/conjunction.ts
140
+ const $and = zod.z.literal("$and");
141
+ const $or = zod.z.literal("$or");
142
+ const $not = zod.z.literal("$not");
143
+ const conjunctions = zod.z.union([
144
+ $and,
145
+ $or,
146
+ $not
147
+ ]);
148
+
149
+ //#endregion
150
+ //#region src/utils.ts
151
+ function convertPropsToObject(props) {
152
+ const propsCopy = { ...props };
153
+ for (const prop in propsCopy) {
154
+ if (Array.isArray(propsCopy[prop])) propsCopy[prop] = propsCopy[prop].map((item) => {
155
+ return convertToPlainObject(item);
156
+ });
157
+ propsCopy[prop] = convertToPlainObject(propsCopy[prop]);
158
+ }
159
+ return propsCopy;
160
+ }
161
+ function convertToPlainObject(item) {
162
+ if (ValueObject.isValueObject(item)) return item.unpack();
163
+ return item;
164
+ }
165
+
166
+ //#endregion
167
+ //#region src/value-objects/value-object.ts
168
+ var ValueObject = class ValueObject {
169
+ constructor(props) {
170
+ this.props = props;
171
+ }
172
+ equals(vo) {
173
+ if (vo === null || vo === void 0) return false;
174
+ return (0, dequal.dequal)(vo, this);
175
+ }
176
+ static isValueObject(obj) {
177
+ return obj instanceof ValueObject;
178
+ }
179
+ unpack() {
180
+ if (this.isDomainPrimitive(this.props)) return this.props.value;
181
+ const propsCopy = convertPropsToObject(this.props);
182
+ return Object.freeze(propsCopy);
183
+ }
184
+ isDomainPrimitive(obj) {
185
+ if (Object.prototype.hasOwnProperty.call(obj, "value")) return true;
186
+ return false;
187
+ }
188
+ };
189
+
190
+ //#endregion
191
+ //#region src/filter/fields/field-value.base.ts
192
+ var FieldValueBase = class extends ValueObject {};
193
+
194
+ //#endregion
195
+ //#region src/filter/fields/date/date-field-value.ts
196
+ var DateFieldValue = class extends FieldValueBase {
197
+ constructor(value) {
198
+ super({ value });
199
+ }
200
+ accept(visitor) {
201
+ visitor.date(this);
202
+ }
203
+ static fromNullableString(str) {
204
+ if (!str) return new this(null);
205
+ return new this(new Date(str));
206
+ }
207
+ };
208
+
209
+ //#endregion
210
+ //#region src/filter/operators.ts
211
+ const $eq = zod.z.literal("$eq");
212
+ const $neq = zod.z.literal("$neq");
213
+ const $contains = zod.z.literal("$contains");
214
+ const $not_contains = zod.z.literal("$not_contains");
215
+ const $starts_with = zod.z.literal("$starts_with");
216
+ const $ends_with = zod.z.literal("$ends_with");
217
+ const $regex = zod.z.literal("$regex");
218
+ const $is_true = zod.z.literal("$is_true");
219
+ const $is_false = zod.z.literal("$is_false");
220
+ const $in = zod.z.literal("$in");
221
+ const $nin = zod.z.literal("$nin");
222
+ const $gt = zod.z.literal("$gt");
223
+ const $lt = zod.z.literal("$lt");
224
+ const $gte = zod.z.literal("$gte");
225
+ const $lte = zod.z.literal("$lte");
226
+ const $start_eq = zod.z.literal("$start_eq");
227
+ const $start_neq = zod.z.literal("$start_neq");
228
+ const $start_gt = zod.z.literal("$start_gt");
229
+ const $start_lt = zod.z.literal("$start_lt");
230
+ const $start_gte = zod.z.literal("$start_gte");
231
+ const $start_lte = zod.z.literal("$start_lte");
232
+ const $end_eq = zod.z.literal("$end_eq");
233
+ const $end_neq = zod.z.literal("$end_neq");
234
+ const $end_gt = zod.z.literal("$end_gt");
235
+ const $end_lt = zod.z.literal("$end_lt");
236
+ const $end_gte = zod.z.literal("$end_gte");
237
+ const $end_lte = zod.z.literal("$end_lte");
238
+ const $is_empty = zod.z.literal("$is_empty");
239
+ const $is_not_empty = zod.z.literal("$is_not_empty");
240
+ const $is_today = zod.z.literal("$is_today");
241
+ const $is_not_today = zod.z.literal("$is_not_today");
242
+ const $is_tomorrow = zod.z.literal("$is_tomorrow");
243
+ const $is_yesterday = zod.z.literal("$is_yesterday");
244
+ const $between = zod.z.literal("$between");
245
+ const $has_file_type = zod.z.literal("$has_file_type");
246
+ const $has_file_extension = zod.z.literal("$has_file_extension");
247
+ const $is_root = zod.z.literal("$is_root");
248
+ const $is_me = zod.z.literal("$is_me");
249
+ const $is_not_me = zod.z.literal("$is_not_me");
250
+ const operatorsWihtoutValue = zod.z.union([
251
+ $is_empty,
252
+ $is_not_empty,
253
+ $is_today,
254
+ $is_not_today,
255
+ $is_tomorrow,
256
+ $is_yesterday,
257
+ $is_root,
258
+ $is_me,
259
+ $is_not_me
260
+ ]);
261
+ const isOperatorWithoutValue = (value) => operatorsWihtoutValue.safeParse(value).success;
262
+
263
+ //#endregion
264
+ //#region src/filter/fields/date/date.filter.ts
265
+ const dateFilterOperators = zod.z.union([
266
+ $eq,
267
+ $neq,
268
+ $gt,
269
+ $gte,
270
+ $lt,
271
+ $lte,
272
+ $between,
273
+ $is_today,
274
+ $is_tomorrow,
275
+ $is_yesterday,
276
+ $is_not_today
277
+ ]);
278
+ const dateFilterValue = zod.z.string().nullable().or(zod.z.tuple([zod.z.string(), zod.z.string()]));
279
+ const dateFilter = zod.z.object({
280
+ type: zod.z.literal("date"),
281
+ operator: dateFilterOperators,
282
+ value: dateFilterValue
283
+ }).merge(baseFilter);
284
+
285
+ //#endregion
286
+ //#region src/filter/fields/number/number-field-value.ts
287
+ var NumberFieldValue = class extends FieldValueBase {
288
+ constructor(value) {
289
+ super({ value });
290
+ }
291
+ accept(visitor) {
292
+ visitor.number(this);
293
+ }
294
+ };
295
+
296
+ //#endregion
297
+ //#region src/filter/fields/number/number.filter.ts
298
+ const numberFilterOperators = zod.z.union([
299
+ $eq,
300
+ $neq,
301
+ $gt,
302
+ $gte,
303
+ $lt,
304
+ $lte,
305
+ $is_empty,
306
+ $is_not_empty
307
+ ]);
308
+ const numberFilterValue = zod.z.number().nullable();
309
+ const numberFilter = zod.z.object({
310
+ type: zod.z.literal("number"),
311
+ operator: numberFilterOperators,
312
+ value: numberFilterValue
313
+ }).merge(baseFilter);
314
+
315
+ //#endregion
316
+ //#region src/filter/fields/string/string-field-value.ts
317
+ var StringFieldValue = class extends FieldValueBase {
318
+ constructor(value) {
319
+ super({ value });
320
+ }
321
+ accept(visitor) {
322
+ visitor.string(this);
323
+ }
324
+ };
325
+
326
+ //#endregion
327
+ //#region src/filter/fields/string/string.filter.ts
328
+ const stringFilterOperators = zod.z.union([
329
+ $eq,
330
+ $neq,
331
+ $contains,
332
+ $not_contains,
333
+ $starts_with,
334
+ $ends_with,
335
+ $regex,
336
+ $is_empty,
337
+ $is_not_empty
338
+ ]);
339
+ const stringFilterValue = zod.z.string().nullable();
340
+ const stringFilter = zod.z.object({
341
+ type: zod.z.literal("string"),
342
+ operator: stringFilterOperators,
343
+ value: stringFilterValue
344
+ }).merge(baseFilter);
345
+
346
+ //#endregion
347
+ //#region src/specification.ts
348
+ var CompositeSpecification = class {
349
+ and(s) {
350
+ return new And(this, s);
351
+ }
352
+ or(s) {
353
+ return new Or(this, s);
354
+ }
355
+ not() {
356
+ return new Not(this);
357
+ }
358
+ };
359
+ var And = class extends CompositeSpecification {
360
+ constructor(left, right) {
361
+ super();
362
+ this.left = left;
363
+ this.right = right;
364
+ }
365
+ isSatisfiedBy(t) {
366
+ return this.left.isSatisfiedBy(t) && this.right.isSatisfiedBy(t);
367
+ }
368
+ mutate(t) {
369
+ return this.left.mutate(t).and(this.right.mutate(t));
370
+ }
371
+ accept(v) {
372
+ return this.left.accept(v).and(this.right.accept(v));
373
+ }
374
+ };
375
+ var Or = class extends CompositeSpecification {
376
+ constructor(left, right) {
377
+ super();
378
+ this.left = left;
379
+ this.right = right;
380
+ }
381
+ isSatisfiedBy(t) {
382
+ return this.left.isSatisfiedBy(t) || this.right.isSatisfiedBy(t);
383
+ }
384
+ mutate(t) {
385
+ return this.left.mutate(t).orElse(() => this.right.mutate(t));
386
+ }
387
+ accept(v) {
388
+ v.or(this.left, this.right);
389
+ return (0, oxide_ts.Ok)(void 0);
390
+ }
391
+ };
392
+ var Not = class extends CompositeSpecification {
393
+ constructor(spec) {
394
+ super();
395
+ this.spec = spec;
396
+ }
397
+ isSatisfiedBy(t) {
398
+ return !this.spec.isSatisfiedBy(t);
399
+ }
400
+ mutate() {
401
+ throw new Error("[Not.mutate] Method not implemented.");
402
+ }
403
+ accept(v) {
404
+ return this.spec.accept(v.not());
405
+ }
406
+ };
407
+ const and = (...specs) => {
408
+ if (!specs.length) return oxide_ts.None;
409
+ let s = specs[0];
410
+ for (const spec of specs.slice(1)) s = s.and(spec);
411
+ return (0, oxide_ts.Some)(s);
412
+ };
413
+ const andOptions = (...specs) => {
414
+ return and(...specs.filter((spec) => spec.isSome()).map((spec) => spec.unwrap()));
415
+ };
416
+ const or = (...specs) => {
417
+ if (!specs.length) return oxide_ts.None;
418
+ let s = specs[0];
419
+ for (const spec of specs.slice(1)) s = s.or(spec);
420
+ return (0, oxide_ts.Some)(s);
421
+ };
422
+
423
+ //#endregion
424
+ //#region src/filter/filter-specification.base.ts
425
+ var BaseFilterSpecification = class extends CompositeSpecification {
426
+ constructor(field, value, relation) {
427
+ super();
428
+ this.field = field;
429
+ this.value = value;
430
+ this.relation = relation;
431
+ }
432
+ mutate(t) {
433
+ throw new Error("Method not implemented.");
434
+ }
435
+ };
436
+
437
+ //#endregion
438
+ //#region src/filter/specifications/date.specification.ts
439
+ var DateEqual = class extends BaseFilterSpecification {
440
+ isSatisfiedBy(value) {
441
+ return value instanceof DateFieldValue && value.equals(this.value);
442
+ }
443
+ accept(v) {
444
+ v.dateEqual(this);
445
+ return (0, oxide_ts.Ok)(void 0);
446
+ }
447
+ };
448
+ var DateGreaterThan = class extends BaseFilterSpecification {
449
+ isSatisfiedBy(value) {
450
+ if (!(value instanceof DateFieldValue)) return false;
451
+ const d1 = value.unpack();
452
+ const d2 = this.value.unpack();
453
+ return !!d1 && !!d2 && (0, date_fns.isAfter)(d1, d2);
454
+ }
455
+ accept(v) {
456
+ v.dateGreaterThan(this);
457
+ return (0, oxide_ts.Ok)(void 0);
458
+ }
459
+ };
460
+ var DateLessThan = class extends BaseFilterSpecification {
461
+ isSatisfiedBy(value) {
462
+ if (!(value instanceof DateFieldValue)) return false;
463
+ const d1 = value.unpack();
464
+ const d2 = this.value.unpack();
465
+ return !!d1 && !!d2 && (0, date_fns.isBefore)(d1, d2);
466
+ }
467
+ accept(v) {
468
+ v.dateLessThan(this);
469
+ return (0, oxide_ts.Ok)(void 0);
470
+ }
471
+ };
472
+ var DateGreaterThanOrEqual = class extends BaseFilterSpecification {
473
+ isSatisfiedBy(value) {
474
+ if (!(value instanceof DateFieldValue)) return false;
475
+ const d1 = value.unpack();
476
+ const d2 = this.value.unpack();
477
+ return !!d1 && !!d2 && ((0, date_fns.isEqual)(d1, d2) || (0, date_fns.isAfter)(d1, d2));
478
+ }
479
+ accept(v) {
480
+ v.dateGreaterThanOrEqual(this);
481
+ return (0, oxide_ts.Ok)(void 0);
482
+ }
483
+ };
484
+ var DateLessThanOrEqual = class extends BaseFilterSpecification {
485
+ isSatisfiedBy(value) {
486
+ if (!(value instanceof DateFieldValue)) return false;
487
+ const d1 = value.unpack();
488
+ const d2 = this.value.unpack();
489
+ return !!d1 && !!d2 && ((0, date_fns.isEqual)(d1, d2) || (0, date_fns.isBefore)(d1, d2));
490
+ }
491
+ accept(v) {
492
+ v.dateLessThanOrEqual(this);
493
+ return (0, oxide_ts.Ok)(void 0);
494
+ }
495
+ };
496
+ var DateIsToday = class extends BaseFilterSpecification {
497
+ isSatisfiedBy(value) {
498
+ if (!(value instanceof DateFieldValue)) return false;
499
+ const date = value.unpack();
500
+ return !!date && (0, date_fns.isToday)(date);
501
+ }
502
+ accept(v) {
503
+ v.dateIsToday(this);
504
+ return (0, oxide_ts.Ok)(void 0);
505
+ }
506
+ };
507
+ var DateIsTomorrow = class extends BaseFilterSpecification {
508
+ isSatisfiedBy(value) {
509
+ if (!(value instanceof DateFieldValue)) return false;
510
+ const date = value.unpack();
511
+ return !!date && (0, date_fns.isTomorrow)(date);
512
+ }
513
+ accept(v) {
514
+ v.dateIsTomorrow(this);
515
+ return (0, oxide_ts.Ok)(void 0);
516
+ }
517
+ };
518
+ var DateIsYesterday = class extends BaseFilterSpecification {
519
+ isSatisfiedBy(value) {
520
+ if (!(value instanceof DateFieldValue)) return false;
521
+ const date = value.unpack();
522
+ return !!date && (0, date_fns.isYesterday)(date);
523
+ }
524
+ accept(v) {
525
+ v.dateIsYesterday(this);
526
+ return (0, oxide_ts.Ok)(void 0);
527
+ }
528
+ };
529
+ var DateBetween = class extends BaseFilterSpecification {
530
+ constructor(field, dateStart, dateEnd, relation) {
531
+ super(field, null, relation);
532
+ this.field = field;
533
+ this.dateStart = dateStart;
534
+ this.dateEnd = dateEnd;
535
+ this.relation = relation;
536
+ }
537
+ isSatisfiedBy(value) {
538
+ if (!(value instanceof DateFieldValue)) return false;
539
+ const date = value.unpack();
540
+ return !!date && (0, date_fns.isWithinInterval)(date, {
541
+ start: this.dateStart,
542
+ end: this.dateEnd
543
+ });
544
+ }
545
+ accept(v) {
546
+ v.dateBetween(this);
547
+ return (0, oxide_ts.Ok)(void 0);
548
+ }
549
+ };
550
+
551
+ //#endregion
552
+ //#region src/filter/specifications/number.specification.ts
553
+ var NumberEqual = class extends BaseFilterSpecification {
554
+ isSatisfiedBy(value) {
555
+ return value instanceof NumberFieldValue && value.equals(this.value);
556
+ }
557
+ accept(v) {
558
+ v.numberEqual(this);
559
+ return (0, oxide_ts.Ok)(void 0);
560
+ }
561
+ };
562
+ var NumberGreaterThan = class extends BaseFilterSpecification {
563
+ isSatisfiedBy(value) {
564
+ if (!(value instanceof NumberFieldValue)) return false;
565
+ const n1 = value.unpack();
566
+ const n2 = this.value.unpack();
567
+ if (n1 === null && (0, lodash.isNumber)(n2)) return true;
568
+ return n1 !== null && n2 !== null && n1 > n2;
569
+ }
570
+ accept(v) {
571
+ v.numberGreaterThan(this);
572
+ return (0, oxide_ts.Ok)(void 0);
573
+ }
574
+ };
575
+ var NumberLessThan = class extends BaseFilterSpecification {
576
+ isSatisfiedBy(value) {
577
+ if (!(value instanceof NumberFieldValue)) return false;
578
+ const n1 = value.unpack();
579
+ const n2 = this.value.unpack();
580
+ return n1 !== null && n2 !== null && n1 < n2;
581
+ }
582
+ accept(v) {
583
+ v.numberLessThan(this);
584
+ return (0, oxide_ts.Ok)(void 0);
585
+ }
586
+ };
587
+ var NumberGreaterThanOrEqual = class extends BaseFilterSpecification {
588
+ isSatisfiedBy(value) {
589
+ if (!(value instanceof NumberFieldValue)) return false;
590
+ const n1 = value.unpack();
591
+ const n2 = this.value.unpack();
592
+ if (n1 === null && (0, lodash.isNumber)(n2)) return true;
593
+ return n1 !== null && n2 !== null && n1 >= n2;
594
+ }
595
+ accept(v) {
596
+ v.numberGreaterThanOrEqual(this);
597
+ return (0, oxide_ts.Ok)(void 0);
598
+ }
599
+ };
600
+ var NumberLessThanOrEqual = class extends BaseFilterSpecification {
601
+ isSatisfiedBy(value) {
602
+ if (!(value instanceof NumberFieldValue)) return false;
603
+ const n1 = value.unpack();
604
+ const n2 = this.value.unpack();
605
+ return n1 !== null && n2 !== null && n1 <= n2;
606
+ }
607
+ accept(v) {
608
+ v.numberLessThanOrEqual(this);
609
+ return (0, oxide_ts.Ok)(void 0);
610
+ }
611
+ };
612
+ var NumberEmpty = class extends BaseFilterSpecification {
613
+ constructor(field) {
614
+ super(field, new NumberFieldValue(null));
615
+ this.field = field;
616
+ }
617
+ isSatisfiedBy(value) {
618
+ return value instanceof NumberFieldValue && (0, lodash.isNil)(value.unpack());
619
+ }
620
+ accept(v) {
621
+ v.numberEmpty(this);
622
+ return (0, oxide_ts.Ok)(void 0);
623
+ }
624
+ };
625
+
626
+ //#endregion
627
+ //#region src/filter/specifications/string.specification.ts
628
+ var StringEqual = class extends BaseFilterSpecification {
629
+ isSatisfiedBy(value) {
630
+ return value instanceof StringFieldValue && this.value.equals(value);
631
+ }
632
+ accept(v) {
633
+ v.stringEqual(this);
634
+ return (0, oxide_ts.Ok)(void 0);
635
+ }
636
+ };
637
+ var StringNotEqual = class extends BaseFilterSpecification {
638
+ isSatisfiedBy(value) {
639
+ return value instanceof StringFieldValue && this.value.equals(value);
640
+ }
641
+ accept(v) {
642
+ v.stringNotEqual(this);
643
+ return (0, oxide_ts.Ok)(void 0);
644
+ }
645
+ };
646
+ var StringContain = class extends BaseFilterSpecification {
647
+ isSatisfiedBy(value) {
648
+ if (!(value instanceof StringFieldValue)) return false;
649
+ const s1 = value.unpack();
650
+ const s2 = this.value.unpack();
651
+ return !!s1 && !!s2 && s1.includes(s2);
652
+ }
653
+ accept(v) {
654
+ v.stringContain(this);
655
+ return (0, oxide_ts.Ok)(void 0);
656
+ }
657
+ };
658
+ var StringStartsWith = class extends BaseFilterSpecification {
659
+ isSatisfiedBy(value) {
660
+ if (!(value instanceof StringFieldValue)) return false;
661
+ const s1 = value.unpack();
662
+ const s2 = this.value.unpack();
663
+ return !!s1 && !!s2 && s1.startsWith(s2);
664
+ }
665
+ accept(v) {
666
+ v.stringStartsWith(this);
667
+ return (0, oxide_ts.Ok)(void 0);
668
+ }
669
+ };
670
+ var StringEndsWith = class extends BaseFilterSpecification {
671
+ isSatisfiedBy(value) {
672
+ if (!(value instanceof StringFieldValue)) return false;
673
+ const s1 = value.unpack();
674
+ const s2 = this.value.unpack();
675
+ return !!s1 && !!s2 && s1.endsWith(s2);
676
+ }
677
+ accept(v) {
678
+ v.stringEndsWith(this);
679
+ return (0, oxide_ts.Ok)(void 0);
680
+ }
681
+ };
682
+ var StringRegex = class extends BaseFilterSpecification {
683
+ isSatisfiedBy(value) {
684
+ if (!(value instanceof StringFieldValue)) return false;
685
+ const s1 = value.unpack();
686
+ const s2 = this.value.unpack();
687
+ return !!s1 && !!s2 && new RegExp(s2).test(s1);
688
+ }
689
+ accept(v) {
690
+ v.stringRegex(this);
691
+ return (0, oxide_ts.Ok)(void 0);
692
+ }
693
+ };
694
+ var StringEmpty = class extends BaseFilterSpecification {
695
+ constructor(field) {
696
+ super(field, new StringFieldValue(null));
697
+ }
698
+ isSatisfiedBy(value) {
699
+ if (value instanceof StringFieldValue) return !value.unpack();
700
+ return !value;
701
+ }
702
+ accept(v) {
703
+ v.stringEmpty(this);
704
+ return (0, oxide_ts.Ok)(void 0);
705
+ }
706
+ };
707
+
708
+ //#endregion
709
+ //#region src/filter/filter.ts
710
+ const filterRoorFilter = (filters) => {
711
+ const filterTuple = [filters[0], ...filters.slice(1)];
712
+ const filter$1 = zod.z.union(filterTuple);
713
+ const group$1 = zod.z.lazy(() => zod.z.object({
714
+ conjunction: conjunctions,
715
+ children: zod.z.union([group$1, filter$1]).array().nonempty().optional()
716
+ }));
717
+ const filterOrGroup$1 = filter$1.or(group$1);
718
+ const filterOrGroupList$1 = filterOrGroup$1.array();
719
+ return group$1.or(filterOrGroupList$1);
720
+ };
721
+ const filter = zod.z.discriminatedUnion("type", [
722
+ numberFilter,
723
+ stringFilter,
724
+ dateFilter
725
+ ]);
726
+ const group = zod.z.lazy(() => zod.z.object({
727
+ conjunction: conjunctions,
728
+ children: zod.z.union([group, filter]).array().nonempty().optional()
729
+ }));
730
+ const filterOrGroup = filter.or(group);
731
+ const filterOrGroupList = filterOrGroup.array();
732
+ const rootFilter = filterOrGroup.or(filterOrGroupList);
733
+ const isGroup = (filterOrGroup$1) => {
734
+ return Reflect.has(filterOrGroup$1, "conjunction");
735
+ };
736
+ const isFilter = (filterOrGroup$1) => {
737
+ return Reflect.has(filterOrGroup$1, "type") && Reflect.has(filterOrGroup$1, "operator");
738
+ };
739
+ const operators = zod.z.union([numberFilterOperators, stringFilterOperators]);
740
+ const operatorsMap = { number: numberFilterOperators.options.map((v) => v.value) };
741
+ const convertStringFilter = (filter$1) => {
742
+ if (filter$1.value === void 0) return oxide_ts.None;
743
+ switch (filter$1.operator) {
744
+ case "$eq": return (0, oxide_ts.Some)(new StringEqual(filter$1.field, new StringFieldValue(filter$1.value), filter$1.relation));
745
+ case "$neq": return (0, oxide_ts.Some)(new StringNotEqual(filter$1.field, new StringFieldValue(filter$1.value), filter$1.relation));
746
+ case "$contains": return (0, oxide_ts.Some)(new StringContain(filter$1.field, new StringFieldValue(filter$1.value)));
747
+ case "$not_contains": return (0, oxide_ts.Some)(new StringContain(filter$1.field, new StringFieldValue(filter$1.value)).not());
748
+ case "$starts_with": return (0, oxide_ts.Some)(new StringStartsWith(filter$1.field, new StringFieldValue(filter$1.value)));
749
+ case "$ends_with": return (0, oxide_ts.Some)(new StringEndsWith(filter$1.field, new StringFieldValue(filter$1.value)));
750
+ case "$regex": return (0, oxide_ts.Some)(new StringRegex(filter$1.field, new StringFieldValue(filter$1.value)));
751
+ case "$is_empty": return (0, oxide_ts.Some)(new StringEmpty(filter$1.field));
752
+ case "$is_not_empty": return (0, oxide_ts.Some)(new StringEmpty(filter$1.field).not());
753
+ default: return oxide_ts.None;
754
+ }
755
+ };
756
+ const convertNumberFilter = (filter$1) => {
757
+ if (filter$1 === void 0) return oxide_ts.None;
758
+ switch (filter$1.operator) {
759
+ case "$eq": return (0, oxide_ts.Some)(new NumberEqual(filter$1.field, new NumberFieldValue(filter$1.value)));
760
+ case "$neq": return (0, oxide_ts.Some)(new NumberEqual(filter$1.field, new NumberFieldValue(filter$1.value)).not());
761
+ case "$gt": return (0, oxide_ts.Some)(new NumberGreaterThan(filter$1.field, new NumberFieldValue(filter$1.value)));
762
+ case "$gte": return (0, oxide_ts.Some)(new NumberGreaterThanOrEqual(filter$1.field, new NumberFieldValue(filter$1.value)));
763
+ case "$lt": return (0, oxide_ts.Some)(new NumberLessThan(filter$1.field, new NumberFieldValue(filter$1.value)));
764
+ case "$lte": return (0, oxide_ts.Some)(new NumberLessThanOrEqual(filter$1.field, new NumberFieldValue(filter$1.value)));
765
+ case "$is_empty": return (0, oxide_ts.Some)(new NumberEmpty(filter$1.field));
766
+ case "$is_not_empty": return (0, oxide_ts.Some)(new NumberEmpty(filter$1.field).not());
767
+ default: return oxide_ts.None;
768
+ }
769
+ };
770
+ const convertDateFilter = (filter$1) => {
771
+ if (filter$1 === void 0) return oxide_ts.None;
772
+ switch (filter$1.operator) {
773
+ case "$eq": return (0, oxide_ts.Some)(new DateEqual(filter$1.field, DateFieldValue.fromNullableString(filter$1.value), filter$1.relation));
774
+ case "$between": return (0, oxide_ts.Some)(new DateBetween(filter$1.field, new Date(filter$1.value[0]), new Date(filter$1.value[1])));
775
+ }
776
+ };
777
+ const convertFilter = (filter$1) => {
778
+ return (0, ts_pattern.match)(filter$1).returnType().with({ type: "number" }, (f) => convertNumberFilter(f)).with({ type: "string" }, (f) => convertStringFilter(f)).with({ type: "date" }, (f) => convertDateFilter(f)).otherwise(() => oxide_ts.None);
779
+ };
780
+ const convertFilterOrGroup = (filterOrGroup$1) => {
781
+ if (isGroup(filterOrGroup$1)) return convertFilterOrGroupList(filterOrGroup$1.children, filterOrGroup$1.conjunction);
782
+ else if (isFilter(filterOrGroup$1)) return convertFilter(filterOrGroup$1);
783
+ return oxide_ts.None;
784
+ };
785
+ const convertFilterOrGroupList = (filterOrGroupList$1 = [], conjunction = "$and") => {
786
+ let spec = oxide_ts.None;
787
+ for (const filter$1 of filterOrGroupList$1) if (spec.isNone()) {
788
+ spec = convertFilterOrGroup(filter$1);
789
+ if (conjunction === "$not") return spec.map((s) => s.not());
790
+ } else if (isFilter(filter$1)) spec = spec.map((left) => {
791
+ const right = convertFilterOrGroup(filter$1);
792
+ if (right.isSome()) {
793
+ if (conjunction === "$and") return left.and(right.unwrap());
794
+ else if (conjunction === "$or") return left.or(right.unwrap());
795
+ return left.and(right.unwrap().not());
796
+ }
797
+ return left;
798
+ });
799
+ else if (isGroup(filter$1)) spec = convertFilterOrGroupList(filter$1.children, filter$1.conjunction);
800
+ return spec;
801
+ };
802
+ const convertFilterSpec = (filter$1) => {
803
+ if (Array.isArray(filter$1)) return convertFilterOrGroupList(filter$1);
804
+ return convertFilterOrGroup(filter$1);
805
+ };
806
+ const isEmptyFilter = (filter$1) => (0, lodash.isEmpty)(filter$1);
807
+
808
+ //#endregion
809
+ //#region src/filter/fields/date/date-field.type.ts
810
+ const dateFieldValue = zod.z.date().nullable();
811
+
812
+ //#endregion
813
+ //#region src/filter/fields/number/number-field.type.ts
814
+ const numberFieldValue = zod.z.number().or(zod.z.null());
815
+
816
+ //#endregion
817
+ //#region src/filter/fields/string/string-field.type.ts
818
+ const stringFieldValue = zod.z.string().nullable();
819
+
820
+ //#endregion
821
+ //#region src/filter/root-filter.ts
822
+ var RootFilter = class extends ValueObject {
823
+ get value() {
824
+ return this.props;
825
+ }
826
+ get group() {
827
+ if (Array.isArray(this.value)) return {
828
+ conjunction: "$and",
829
+ children: this.value
830
+ };
831
+ if (isGroup(this.value)) return this.value;
832
+ return {
833
+ conjunction: "$and",
834
+ children: [this.value]
835
+ };
836
+ }
837
+ getSpec() {
838
+ return convertFilterSpec(this.value);
839
+ }
840
+ toJSON() {
841
+ return this.props;
842
+ }
843
+ };
844
+
845
+ //#endregion
846
+ //#region src/pagination.ts
847
+ const paginationSchema = zod.z.object({
848
+ limit: zod.z.coerce.number().positive().int().optional(),
849
+ offset: zod.z.coerce.number().nonnegative().int().optional()
850
+ });
851
+ const paginatedResponseSchema = paginationSchema.extend({ total: zod.z.number().nonnegative().int() });
852
+
853
+ //#endregion
854
+ //#region src/query.ts
855
+ var Query = class {};
856
+
857
+ //#endregion
858
+ //#region src/sort.ts
859
+ const sortingSchema = zod.z.record(zod.z.string(), zod.z.enum(["ASC", "DESC"]));
860
+
861
+ //#endregion
862
+ //#region src/value-objects/boolean.vo.ts
863
+ var BoolVO = class BoolVO extends ValueObject {
864
+ constructor(value) {
865
+ super({ value });
866
+ }
867
+ get value() {
868
+ return this.props.value;
869
+ }
870
+ static True() {
871
+ return new BoolVO(true);
872
+ }
873
+ static False() {
874
+ return new BoolVO(false);
875
+ }
876
+ };
877
+
878
+ //#endregion
879
+ //#region src/value-objects/date.vo.ts
880
+ var DateVO = class DateVO extends ValueObject {
881
+ constructor(value) {
882
+ const date = new Date(value);
883
+ super({ value: date });
884
+ }
885
+ get value() {
886
+ return this.props.value;
887
+ }
888
+ static now() {
889
+ return new DateVO(Date.now());
890
+ }
891
+ };
892
+
893
+ //#endregion
894
+ //#region src/value-objects/id.vo.ts
895
+ var ID = class extends ValueObject {
896
+ constructor(value) {
897
+ super({ value });
898
+ }
899
+ get value() {
900
+ return this.props.value;
901
+ }
902
+ };
903
+
904
+ //#endregion
905
+ //#region src/value-objects/nanoid.vo.ts
906
+ var NanoID = class NanoID extends ID {
907
+ static ALPHABETS = "0123456789abcdefghijklmnopqrstuvwxyz";
908
+ static createId(prefix = "", size = 5) {
909
+ const id = (0, nanoid.customAlphabet)(NanoID.ALPHABETS, size)();
910
+ return prefix + id;
911
+ }
912
+ get value() {
913
+ return this.props.value;
914
+ }
915
+ };
916
+
917
+ //#endregion
918
+ //#region src/value-objects/string.vo.ts
919
+ var StringVO = class StringVO extends ValueObject {
920
+ constructor(value) {
921
+ super({ value });
922
+ }
923
+ get value() {
924
+ return this.props.value;
925
+ }
926
+ static empty() {
927
+ return new StringVO("");
928
+ }
929
+ };
930
+
931
+ //#endregion
932
+ exports.$between = $between;
933
+ exports.$contains = $contains;
934
+ exports.$end_eq = $end_eq;
935
+ exports.$end_gt = $end_gt;
936
+ exports.$end_gte = $end_gte;
937
+ exports.$end_lt = $end_lt;
938
+ exports.$end_lte = $end_lte;
939
+ exports.$end_neq = $end_neq;
940
+ exports.$ends_with = $ends_with;
941
+ exports.$eq = $eq;
942
+ exports.$gt = $gt;
943
+ exports.$gte = $gte;
944
+ exports.$has_file_extension = $has_file_extension;
945
+ exports.$has_file_type = $has_file_type;
946
+ exports.$in = $in;
947
+ exports.$is_empty = $is_empty;
948
+ exports.$is_false = $is_false;
949
+ exports.$is_me = $is_me;
950
+ exports.$is_not_empty = $is_not_empty;
951
+ exports.$is_not_me = $is_not_me;
952
+ exports.$is_not_today = $is_not_today;
953
+ exports.$is_root = $is_root;
954
+ exports.$is_today = $is_today;
955
+ exports.$is_tomorrow = $is_tomorrow;
956
+ exports.$is_true = $is_true;
957
+ exports.$is_yesterday = $is_yesterday;
958
+ exports.$lt = $lt;
959
+ exports.$lte = $lte;
960
+ exports.$neq = $neq;
961
+ exports.$nin = $nin;
962
+ exports.$not_contains = $not_contains;
963
+ exports.$regex = $regex;
964
+ exports.$start_eq = $start_eq;
965
+ exports.$start_gt = $start_gt;
966
+ exports.$start_gte = $start_gte;
967
+ exports.$start_lt = $start_lt;
968
+ exports.$start_lte = $start_lte;
969
+ exports.$start_neq = $start_neq;
970
+ exports.$starts_with = $starts_with;
971
+ exports.AggregateRoot = AggregateRoot;
972
+ exports.BaseEvent = BaseEvent;
973
+ exports.BaseFilterSpecification = BaseFilterSpecification;
974
+ exports.BoolVO = BoolVO;
975
+ exports.Command = Command;
976
+ exports.CompositeSpecification = CompositeSpecification;
977
+ exports.DateBetween = DateBetween;
978
+ exports.DateEqual = DateEqual;
979
+ exports.DateFieldValue = DateFieldValue;
980
+ exports.DateGreaterThan = DateGreaterThan;
981
+ exports.DateGreaterThanOrEqual = DateGreaterThanOrEqual;
982
+ exports.DateIsToday = DateIsToday;
983
+ exports.DateIsTomorrow = DateIsTomorrow;
984
+ exports.DateIsYesterday = DateIsYesterday;
985
+ exports.DateLessThan = DateLessThan;
986
+ exports.DateLessThanOrEqual = DateLessThanOrEqual;
987
+ exports.DateVO = DateVO;
988
+ exports.ExceptionBase = ExceptionBase;
989
+ exports.FieldValueBase = FieldValueBase;
990
+ exports.ID = ID;
991
+ exports.NanoID = NanoID;
992
+ exports.NumberEmpty = NumberEmpty;
993
+ exports.NumberEqual = NumberEqual;
994
+ exports.NumberFieldValue = NumberFieldValue;
995
+ exports.NumberGreaterThan = NumberGreaterThan;
996
+ exports.NumberGreaterThanOrEqual = NumberGreaterThanOrEqual;
997
+ exports.NumberLessThan = NumberLessThan;
998
+ exports.NumberLessThanOrEqual = NumberLessThanOrEqual;
999
+ exports.Query = Query;
1000
+ exports.RootFilter = RootFilter;
1001
+ exports.StringContain = StringContain;
1002
+ exports.StringEmpty = StringEmpty;
1003
+ exports.StringEndsWith = StringEndsWith;
1004
+ exports.StringEqual = StringEqual;
1005
+ exports.StringFieldValue = StringFieldValue;
1006
+ exports.StringNotEqual = StringNotEqual;
1007
+ exports.StringRegex = StringRegex;
1008
+ exports.StringStartsWith = StringStartsWith;
1009
+ exports.StringVO = StringVO;
1010
+ exports.ValueObject = ValueObject;
1011
+ exports.and = and;
1012
+ exports.andOptions = andOptions;
1013
+ exports.baseFilter = baseFilter;
1014
+ exports.conjunctions = conjunctions;
1015
+ exports.convertFilterSpec = convertFilterSpec;
1016
+ exports.convertPropsToObject = convertPropsToObject;
1017
+ exports.dateFieldValue = dateFieldValue;
1018
+ exports.dateFilter = dateFilter;
1019
+ exports.dateFilterOperators = dateFilterOperators;
1020
+ exports.dateFilterValue = dateFilterValue;
1021
+ exports.eventSchema = eventSchema;
1022
+ exports.filterOrGroupList = filterOrGroupList;
1023
+ exports.filterRoorFilter = filterRoorFilter;
1024
+ exports.isEmptyFilter = isEmptyFilter;
1025
+ exports.isFilter = isFilter;
1026
+ exports.isGroup = isGroup;
1027
+ exports.isOperatorWithoutValue = isOperatorWithoutValue;
1028
+ exports.numberFieldValue = numberFieldValue;
1029
+ exports.numberFilter = numberFilter;
1030
+ exports.numberFilterOperators = numberFilterOperators;
1031
+ exports.numberFilterValue = numberFilterValue;
1032
+ exports.operators = operators;
1033
+ exports.operatorsMap = operatorsMap;
1034
+ exports.operatorsWihtoutValue = operatorsWihtoutValue;
1035
+ exports.or = or;
1036
+ exports.paginatedResponseSchema = paginatedResponseSchema;
1037
+ exports.paginationSchema = paginationSchema;
1038
+ exports.rootFilter = rootFilter;
1039
+ exports.sortingSchema = sortingSchema;
1040
+ exports.stringFieldValue = stringFieldValue;
1041
+ exports.stringFilter = stringFilter;
1042
+ exports.stringFilterOperators = stringFilterOperators;
1043
+ exports.stringFilterValue = stringFilterValue;
1044
+ //# sourceMappingURL=index.js.map