@archi-code/ddd 0.0.0-stage → 3.0.0-alpha.1
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 +46 -0
- package/README.md +2693 -2
- package/SKILL.md +104 -0
- package/ddd-skills/archi-code-ddd/imports.md +84 -0
- package/ddd-skills/archi-code-ddd/layers.md +299 -0
- package/ddd-skills/archi-code-ddd/patterns.md +676 -0
- package/ddd-skills/archi-code-ddd/what-not-to-do.md +189 -0
- package/ddd-skills/archi-code-ddd/where-each-class-goes.md +63 -0
- package/ddd-skills/ddd-agregados/GUIA.md +114 -0
- package/ddd-skills/ddd-agregados/references/consistencia-eventual.md +159 -0
- package/ddd-skills/ddd-agregados/references/ejemplo-ecommerce.md +52 -0
- package/ddd-skills/ddd-agregados/references/fabricas.md +196 -0
- package/ddd-skills/ddd-agregados/references/implementacion-ts.md +711 -0
- package/ddd-skills/ddd-arquitectura/GUIA.md +80 -0
- package/ddd-skills/ddd-arquitectura/assets/dependency-cruiser.cjs +92 -0
- package/ddd-skills/ddd-arquitectura/references/estilos-adicionales.md +51 -0
- package/ddd-skills/ddd-arquitectura/references/estructura-hexagonal-ts.md +352 -0
- package/ddd-skills/ddd-arquitectura/references/modulos.md +48 -0
- package/ddd-skills/ddd-auditoria-refactorizacion/GUIA.md +85 -0
- package/ddd-skills/ddd-auditoria-refactorizacion/references/ejemplo-auditoria.md +316 -0
- package/ddd-skills/ddd-auditoria-refactorizacion/references/lista-comprobacion.md +54 -0
- package/ddd-skills/ddd-auditoria-refactorizacion/references/plan-migracion.md +73 -0
- package/ddd-skills/ddd-auditoria-refactorizacion/scripts/scan_ddd_smells.py +264 -0
- package/ddd-skills/ddd-context-mapping/GUIA.md +83 -0
- package/ddd-skills/ddd-context-mapping/references/anticorrupcion-ts.md +404 -0
- package/ddd-skills/ddd-context-mapping/references/estilos-integracion.md +155 -0
- package/ddd-skills/ddd-event-sourcing-cqrs/GUIA.md +92 -0
- package/ddd-skills/ddd-event-sourcing-cqrs/references/event-sourcing-ts.md +471 -0
- package/ddd-skills/ddd-event-sourcing-cqrs/references/proyecciones-cqrs-ts.md +285 -0
- package/ddd-skills/ddd-eventos-dominio/GUIA.md +106 -0
- package/ddd-skills/ddd-eventos-dominio/references/diseno-eventos.md +131 -0
- package/ddd-skills/ddd-eventos-dominio/references/ejemplo-ecommerce.md +240 -0
- package/ddd-skills/ddd-eventos-dominio/references/publicacion-ts.md +526 -0
- package/ddd-skills/ddd-orquestador/GUIA.md +40 -0
- package/ddd-skills/ddd-orquestador/references/chuleta-reglas.md +35 -0
- package/ddd-skills/ddd-repositorios-persistencia/GUIA.md +121 -0
- package/ddd-skills/ddd-repositorios-persistencia/references/implementacion-ts.md +296 -0
- package/ddd-skills/ddd-repositorios-persistencia/references/tests-repositorios.md +177 -0
- package/ddd-skills/ddd-repositorios-persistencia/references/transacciones-ts.md +294 -0
- package/ddd-skills/ddd-servicios-capa-aplicacion/GUIA.md +99 -0
- package/ddd-skills/ddd-servicios-capa-aplicacion/references/capa-aplicacion-ts.md +264 -0
- package/ddd-skills/ddd-servicios-capa-aplicacion/references/ejemplo-ecommerce.md +324 -0
- package/ddd-skills/ddd-servicios-capa-aplicacion/references/servicios-dominio-ts.md +191 -0
- package/ddd-skills/ddd-value-objects-entidades/GUIA.md +118 -0
- package/ddd-skills/ddd-value-objects-entidades/references/ejemplo-ecommerce.md +87 -0
- package/ddd-skills/ddd-value-objects-entidades/references/entidades-ts.md +220 -0
- package/ddd-skills/ddd-value-objects-entidades/references/persistencia-valores.md +88 -0
- package/ddd-skills/ddd-value-objects-entidades/references/specification-y-diseno-flexible.md +119 -0
- package/ddd-skills/ddd-value-objects-entidades/references/validacion.md +96 -0
- package/ddd-skills/ddd-value-objects-entidades/references/value-objects-ts.md +377 -0
- package/dist/cjs/application/event/event-bus.d.ts +4 -0
- package/dist/cjs/application/event/event-bus.js +2 -0
- package/dist/cjs/application/event/event-listener.d.ts +4 -0
- package/dist/cjs/application/event/event-listener.js +6 -0
- package/dist/cjs/application/event/listens-to.d.ts +15 -0
- package/dist/cjs/application/event/listens-to.js +28 -0
- package/dist/cjs/application/index.d.ts +7 -0
- package/dist/cjs/application/index.js +9 -0
- package/dist/cjs/application/ports/clock.d.ts +3 -0
- package/dist/cjs/application/ports/clock.js +2 -0
- package/dist/cjs/application/ports/logger.d.ts +8 -0
- package/dist/cjs/application/ports/logger.js +2 -0
- package/dist/cjs/application/use-case/use-case.d.ts +6 -0
- package/dist/cjs/application/use-case/use-case.js +24 -0
- package/dist/cjs/domain/aggregate/aggregate-root.d.ts +14 -0
- package/dist/cjs/domain/aggregate/aggregate-root.js +33 -0
- package/dist/cjs/domain/entity/entity.d.ts +9 -0
- package/dist/cjs/domain/entity/entity.js +19 -0
- package/dist/cjs/domain/event/event-base.d.ts +29 -0
- package/dist/cjs/domain/event/event-base.js +51 -0
- package/dist/cjs/domain/index.d.ts +10 -0
- package/dist/cjs/domain/index.js +24 -0
- package/dist/cjs/domain/primitive/primitive-types.d.ts +22 -0
- package/dist/cjs/domain/primitive/primitive-types.js +2 -0
- package/dist/cjs/domain/repository/aggregate-finder.d.ts +4 -0
- package/dist/cjs/domain/repository/aggregate-finder.js +2 -0
- package/dist/cjs/domain/repository/aggregate-remover.d.ts +4 -0
- package/dist/cjs/domain/repository/aggregate-remover.js +2 -0
- package/dist/cjs/domain/repository/aggregate-saver.d.ts +4 -0
- package/dist/cjs/domain/repository/aggregate-saver.js +2 -0
- package/dist/cjs/domain/specification/specification.d.ts +9 -0
- package/dist/cjs/domain/specification/specification.js +44 -0
- package/dist/cjs/domain/type/abstract-type.d.ts +20 -0
- package/dist/cjs/domain/type/abstract-type.js +69 -0
- package/dist/cjs/domain/type/array-type.d.ts +16 -0
- package/dist/cjs/domain/type/array-type.js +65 -0
- package/dist/cjs/domain/type/boolean-type.d.ts +4 -0
- package/dist/cjs/domain/type/boolean-type.js +16 -0
- package/dist/cjs/domain/type/composite/composite-field.d.ts +19 -0
- package/dist/cjs/domain/type/composite/composite-field.js +63 -0
- package/dist/cjs/domain/type/composite/composite.d.ts +26 -0
- package/dist/cjs/domain/type/composite/composite.js +55 -0
- package/dist/cjs/domain/type/date-type.d.ts +18 -0
- package/dist/cjs/domain/type/date-type.js +35 -0
- package/dist/cjs/domain/type/enum-type.d.ts +7 -0
- package/dist/cjs/domain/type/enum-type.js +30 -0
- package/dist/cjs/domain/type/equality/value-equality.d.ts +7 -0
- package/dist/cjs/domain/type/equality/value-equality.js +41 -0
- package/dist/cjs/domain/type/id-type.d.ts +7 -0
- package/dist/cjs/domain/type/id-type.js +17 -0
- package/dist/cjs/domain/type/immutable/immutable-date.d.ts +4 -0
- package/dist/cjs/domain/type/immutable/immutable-date.js +37 -0
- package/dist/cjs/domain/type/immutable/immutable.d.ts +4 -0
- package/dist/cjs/domain/type/immutable/immutable.js +19 -0
- package/dist/cjs/domain/type/index.d.ts +17 -0
- package/dist/cjs/domain/type/index.js +33 -0
- package/dist/cjs/domain/type/json-type.d.ts +5 -0
- package/dist/cjs/domain/type/json-type.js +17 -0
- package/dist/cjs/domain/type/number-type.d.ts +4 -0
- package/dist/cjs/domain/type/number-type.js +16 -0
- package/dist/cjs/domain/type/required/required-mark.d.ts +9 -0
- package/dist/cjs/domain/type/required/required-mark.js +13 -0
- package/dist/cjs/domain/type/required/required.d.ts +7 -0
- package/dist/cjs/domain/type/required/required.js +21 -0
- package/dist/cjs/domain/type/string-type.d.ts +4 -0
- package/dist/cjs/domain/type/string-type.js +16 -0
- package/dist/cjs/domain/type/uuid-type.d.ts +7 -0
- package/dist/cjs/domain/type/uuid-type.js +27 -0
- package/dist/cjs/domain/type/value-objects/value-objects.d.ts +16 -0
- package/dist/cjs/domain/type/value-objects/value-objects.js +70 -0
- package/dist/cjs/exceptions/abstract.exception.d.ts +23 -0
- package/dist/cjs/exceptions/abstract.exception.js +66 -0
- package/dist/cjs/exceptions/application/aggregate-not-found.exception.d.ts +5 -0
- package/dist/cjs/exceptions/application/aggregate-not-found.exception.js +17 -0
- package/dist/cjs/exceptions/application/application.exception.d.ts +6 -0
- package/dist/cjs/exceptions/application/application.exception.js +17 -0
- package/dist/cjs/exceptions/application/concurrency.exception.d.ts +5 -0
- package/dist/cjs/exceptions/application/concurrency.exception.js +17 -0
- package/dist/cjs/exceptions/application/not-allowed.exception.d.ts +5 -0
- package/dist/cjs/exceptions/application/not-allowed.exception.js +17 -0
- package/dist/cjs/exceptions/brand/exception-brand.d.ts +6 -0
- package/dist/cjs/exceptions/brand/exception-brand.js +29 -0
- package/dist/cjs/exceptions/domain/domain.exception.d.ts +6 -0
- package/dist/cjs/exceptions/domain/domain.exception.js +17 -0
- package/dist/cjs/exceptions/domain/required-value.exception.d.ts +4 -0
- package/dist/cjs/exceptions/domain/required-value.exception.js +21 -0
- package/dist/cjs/exceptions/domain/type-primitive.exception.d.ts +4 -0
- package/dist/cjs/exceptions/domain/type-primitive.exception.js +21 -0
- package/dist/cjs/exceptions/domain/validation.exception.d.ts +14 -0
- package/dist/cjs/exceptions/domain/validation.exception.js +23 -0
- package/dist/cjs/exceptions/exception-code.d.ts +15 -0
- package/dist/cjs/exceptions/exception-code.js +19 -0
- package/dist/cjs/exceptions/index.d.ts +14 -0
- package/dist/cjs/exceptions/index.js +30 -0
- package/dist/cjs/exceptions/infrastructure/event-delivery.exception.d.ts +5 -0
- package/dist/cjs/exceptions/infrastructure/event-delivery.exception.js +20 -0
- package/dist/cjs/exceptions/infrastructure/infrastructure.exception.d.ts +6 -0
- package/dist/cjs/exceptions/infrastructure/infrastructure.exception.js +17 -0
- package/dist/cjs/exceptions/infrastructure/internal-error.exception.d.ts +5 -0
- package/dist/cjs/exceptions/infrastructure/internal-error.exception.js +17 -0
- package/dist/cjs/exceptions/infrastructure/unknown-event.exception.d.ts +4 -0
- package/dist/cjs/exceptions/infrastructure/unknown-event.exception.js +19 -0
- package/dist/cjs/exceptions/received/received-value.d.ts +4 -0
- package/dist/cjs/exceptions/received/received-value.js +13 -0
- package/dist/cjs/infrastructure/clock/fixed-clock.d.ts +8 -0
- package/dist/cjs/infrastructure/clock/fixed-clock.js +19 -0
- package/dist/cjs/infrastructure/clock/system-clock.d.ts +4 -0
- package/dist/cjs/infrastructure/clock/system-clock.js +9 -0
- package/dist/cjs/infrastructure/event/domain-event-bus.d.ts +16 -0
- package/dist/cjs/infrastructure/event/domain-event-bus.js +60 -0
- package/dist/cjs/infrastructure/event/event-channel.d.ts +9 -0
- package/dist/cjs/infrastructure/event/event-channel.js +2 -0
- package/dist/cjs/infrastructure/event/event-registry.d.ts +9 -0
- package/dist/cjs/infrastructure/event/event-registry.js +43 -0
- package/dist/cjs/infrastructure/event/in-memory/emitter.d.ts +11 -0
- package/dist/cjs/infrastructure/event/in-memory/emitter.js +54 -0
- package/dist/cjs/infrastructure/event/in-memory/in-memory-event-channel.d.ts +16 -0
- package/dist/cjs/infrastructure/event/in-memory/in-memory-event-channel.js +51 -0
- package/dist/cjs/infrastructure/http/http-error-mapper.d.ts +23 -0
- package/dist/cjs/infrastructure/http/http-error-mapper.js +63 -0
- package/dist/cjs/infrastructure/index.d.ts +16 -0
- package/dist/cjs/infrastructure/index.js +21 -0
- package/dist/cjs/infrastructure/logging/console-logger.d.ts +17 -0
- package/dist/cjs/infrastructure/logging/console-logger.js +37 -0
- package/dist/cjs/infrastructure/logging/in-memory-logger.d.ts +15 -0
- package/dist/cjs/infrastructure/logging/in-memory-logger.js +27 -0
- package/dist/cjs/infrastructure/logging/log-serializer.d.ts +7 -0
- package/dist/cjs/infrastructure/logging/log-serializer.js +59 -0
- package/dist/cjs/infrastructure/logging/silent-logger.d.ts +7 -0
- package/dist/cjs/infrastructure/logging/silent-logger.js +18 -0
- package/dist/esm/application/event/event-bus.d.ts +4 -0
- package/dist/esm/application/event/event-bus.js +1 -0
- package/dist/esm/application/event/event-listener.d.ts +4 -0
- package/dist/esm/application/event/event-listener.js +2 -0
- package/dist/esm/application/event/listens-to.d.ts +15 -0
- package/dist/esm/application/event/listens-to.js +25 -0
- package/dist/esm/application/index.d.ts +7 -0
- package/dist/esm/application/index.js +3 -0
- package/dist/esm/application/ports/clock.d.ts +3 -0
- package/dist/esm/application/ports/clock.js +1 -0
- package/dist/esm/application/ports/logger.d.ts +8 -0
- package/dist/esm/application/ports/logger.js +1 -0
- package/dist/esm/application/use-case/use-case.d.ts +6 -0
- package/dist/esm/application/use-case/use-case.js +20 -0
- package/dist/esm/domain/aggregate/aggregate-root.d.ts +14 -0
- package/dist/esm/domain/aggregate/aggregate-root.js +29 -0
- package/dist/esm/domain/entity/entity.d.ts +9 -0
- package/dist/esm/domain/entity/entity.js +15 -0
- package/dist/esm/domain/event/event-base.d.ts +29 -0
- package/dist/esm/domain/event/event-base.js +47 -0
- package/dist/esm/domain/index.d.ts +10 -0
- package/dist/esm/domain/index.js +6 -0
- package/dist/esm/domain/primitive/primitive-types.d.ts +22 -0
- package/dist/esm/domain/primitive/primitive-types.js +1 -0
- package/dist/esm/domain/repository/aggregate-finder.d.ts +4 -0
- package/dist/esm/domain/repository/aggregate-finder.js +1 -0
- package/dist/esm/domain/repository/aggregate-remover.d.ts +4 -0
- package/dist/esm/domain/repository/aggregate-remover.js +1 -0
- package/dist/esm/domain/repository/aggregate-saver.d.ts +4 -0
- package/dist/esm/domain/repository/aggregate-saver.js +1 -0
- package/dist/esm/domain/specification/specification.d.ts +9 -0
- package/dist/esm/domain/specification/specification.js +40 -0
- package/dist/esm/domain/type/abstract-type.d.ts +20 -0
- package/dist/esm/domain/type/abstract-type.js +65 -0
- package/dist/esm/domain/type/array-type.d.ts +16 -0
- package/dist/esm/domain/type/array-type.js +61 -0
- package/dist/esm/domain/type/boolean-type.d.ts +4 -0
- package/dist/esm/domain/type/boolean-type.js +12 -0
- package/dist/esm/domain/type/composite/composite-field.d.ts +19 -0
- package/dist/esm/domain/type/composite/composite-field.js +59 -0
- package/dist/esm/domain/type/composite/composite.d.ts +26 -0
- package/dist/esm/domain/type/composite/composite.js +52 -0
- package/dist/esm/domain/type/date-type.d.ts +18 -0
- package/dist/esm/domain/type/date-type.js +29 -0
- package/dist/esm/domain/type/enum-type.d.ts +7 -0
- package/dist/esm/domain/type/enum-type.js +26 -0
- package/dist/esm/domain/type/equality/value-equality.d.ts +7 -0
- package/dist/esm/domain/type/equality/value-equality.js +37 -0
- package/dist/esm/domain/type/id-type.d.ts +7 -0
- package/dist/esm/domain/type/id-type.js +13 -0
- package/dist/esm/domain/type/immutable/immutable-date.d.ts +4 -0
- package/dist/esm/domain/type/immutable/immutable-date.js +33 -0
- package/dist/esm/domain/type/immutable/immutable.d.ts +4 -0
- package/dist/esm/domain/type/immutable/immutable.js +15 -0
- package/dist/esm/domain/type/index.d.ts +17 -0
- package/dist/esm/domain/type/index.js +14 -0
- package/dist/esm/domain/type/json-type.d.ts +5 -0
- package/dist/esm/domain/type/json-type.js +13 -0
- package/dist/esm/domain/type/number-type.d.ts +4 -0
- package/dist/esm/domain/type/number-type.js +12 -0
- package/dist/esm/domain/type/required/required-mark.d.ts +9 -0
- package/dist/esm/domain/type/required/required-mark.js +9 -0
- package/dist/esm/domain/type/required/required.d.ts +7 -0
- package/dist/esm/domain/type/required/required.js +18 -0
- package/dist/esm/domain/type/string-type.d.ts +4 -0
- package/dist/esm/domain/type/string-type.js +12 -0
- package/dist/esm/domain/type/uuid-type.d.ts +7 -0
- package/dist/esm/domain/type/uuid-type.js +23 -0
- package/dist/esm/domain/type/value-objects/value-objects.d.ts +16 -0
- package/dist/esm/domain/type/value-objects/value-objects.js +66 -0
- package/dist/esm/exceptions/abstract.exception.d.ts +23 -0
- package/dist/esm/exceptions/abstract.exception.js +62 -0
- package/dist/esm/exceptions/application/aggregate-not-found.exception.d.ts +5 -0
- package/dist/esm/exceptions/application/aggregate-not-found.exception.js +13 -0
- package/dist/esm/exceptions/application/application.exception.d.ts +6 -0
- package/dist/esm/exceptions/application/application.exception.js +13 -0
- package/dist/esm/exceptions/application/concurrency.exception.d.ts +5 -0
- package/dist/esm/exceptions/application/concurrency.exception.js +13 -0
- package/dist/esm/exceptions/application/not-allowed.exception.d.ts +5 -0
- package/dist/esm/exceptions/application/not-allowed.exception.js +13 -0
- package/dist/esm/exceptions/brand/exception-brand.d.ts +6 -0
- package/dist/esm/exceptions/brand/exception-brand.js +25 -0
- package/dist/esm/exceptions/domain/domain.exception.d.ts +6 -0
- package/dist/esm/exceptions/domain/domain.exception.js +13 -0
- package/dist/esm/exceptions/domain/required-value.exception.d.ts +4 -0
- package/dist/esm/exceptions/domain/required-value.exception.js +17 -0
- package/dist/esm/exceptions/domain/type-primitive.exception.d.ts +4 -0
- package/dist/esm/exceptions/domain/type-primitive.exception.js +17 -0
- package/dist/esm/exceptions/domain/validation.exception.d.ts +14 -0
- package/dist/esm/exceptions/domain/validation.exception.js +19 -0
- package/dist/esm/exceptions/exception-code.d.ts +15 -0
- package/dist/esm/exceptions/exception-code.js +16 -0
- package/dist/esm/exceptions/index.d.ts +14 -0
- package/dist/esm/exceptions/index.js +14 -0
- package/dist/esm/exceptions/infrastructure/event-delivery.exception.d.ts +5 -0
- package/dist/esm/exceptions/infrastructure/event-delivery.exception.js +16 -0
- package/dist/esm/exceptions/infrastructure/infrastructure.exception.d.ts +6 -0
- package/dist/esm/exceptions/infrastructure/infrastructure.exception.js +13 -0
- package/dist/esm/exceptions/infrastructure/internal-error.exception.d.ts +5 -0
- package/dist/esm/exceptions/infrastructure/internal-error.exception.js +13 -0
- package/dist/esm/exceptions/infrastructure/unknown-event.exception.d.ts +4 -0
- package/dist/esm/exceptions/infrastructure/unknown-event.exception.js +15 -0
- package/dist/esm/exceptions/received/received-value.d.ts +4 -0
- package/dist/esm/exceptions/received/received-value.js +9 -0
- package/dist/esm/infrastructure/clock/fixed-clock.d.ts +8 -0
- package/dist/esm/infrastructure/clock/fixed-clock.js +15 -0
- package/dist/esm/infrastructure/clock/system-clock.d.ts +4 -0
- package/dist/esm/infrastructure/clock/system-clock.js +5 -0
- package/dist/esm/infrastructure/event/domain-event-bus.d.ts +16 -0
- package/dist/esm/infrastructure/event/domain-event-bus.js +56 -0
- package/dist/esm/infrastructure/event/event-channel.d.ts +9 -0
- package/dist/esm/infrastructure/event/event-channel.js +1 -0
- package/dist/esm/infrastructure/event/event-registry.d.ts +9 -0
- package/dist/esm/infrastructure/event/event-registry.js +39 -0
- package/dist/esm/infrastructure/event/in-memory/emitter.d.ts +11 -0
- package/dist/esm/infrastructure/event/in-memory/emitter.js +50 -0
- package/dist/esm/infrastructure/event/in-memory/in-memory-event-channel.d.ts +16 -0
- package/dist/esm/infrastructure/event/in-memory/in-memory-event-channel.js +47 -0
- package/dist/esm/infrastructure/http/http-error-mapper.d.ts +23 -0
- package/dist/esm/infrastructure/http/http-error-mapper.js +59 -0
- package/dist/esm/infrastructure/index.d.ts +16 -0
- package/dist/esm/infrastructure/index.js +9 -0
- package/dist/esm/infrastructure/logging/console-logger.d.ts +17 -0
- package/dist/esm/infrastructure/logging/console-logger.js +33 -0
- package/dist/esm/infrastructure/logging/in-memory-logger.d.ts +15 -0
- package/dist/esm/infrastructure/logging/in-memory-logger.js +23 -0
- package/dist/esm/infrastructure/logging/log-serializer.d.ts +7 -0
- package/dist/esm/infrastructure/logging/log-serializer.js +55 -0
- package/dist/esm/infrastructure/logging/silent-logger.d.ts +7 -0
- package/dist/esm/infrastructure/logging/silent-logger.js +14 -0
- package/dist/esm/package.json +3 -0
- package/package.json +106 -4
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.EventBase = void 0;
|
|
4
|
+
const date_type_1 = require("../type/date-type");
|
|
5
|
+
const id_type_1 = require("../type/id-type");
|
|
6
|
+
const uuid_type_1 = require("../type/uuid-type");
|
|
7
|
+
const index_1 = require("../../exceptions/index");
|
|
8
|
+
class EventBase {
|
|
9
|
+
constructor(aggregateId, metadata = {}) {
|
|
10
|
+
const { eventName, eventVersion = 1 } = this.constructor;
|
|
11
|
+
if (typeof eventName !== 'string' || eventName.trim() === '') {
|
|
12
|
+
throw new index_1.InternalErrorException(`${this.constructor.name} must declare a static eventName`);
|
|
13
|
+
}
|
|
14
|
+
if (!Number.isSafeInteger(eventVersion) || eventVersion < 1) {
|
|
15
|
+
throw new index_1.InternalErrorException(`${this.constructor.name}.eventVersion must be a whole number from 1`);
|
|
16
|
+
}
|
|
17
|
+
if (aggregateId === null ||
|
|
18
|
+
aggregateId === undefined ||
|
|
19
|
+
(typeof aggregateId === 'string' && aggregateId.trim() === '')) {
|
|
20
|
+
throw new index_1.RequiredValueException(`${this.constructor.name}.aggregateId`, aggregateId);
|
|
21
|
+
}
|
|
22
|
+
this.aggregateId = aggregateId;
|
|
23
|
+
this.eventId = new id_type_1.IdType(metadata.eventId ?? uuid_type_1.UuidType.random()).value;
|
|
24
|
+
this.occurredOn =
|
|
25
|
+
metadata.occurredOn === undefined
|
|
26
|
+
? date_type_1.CreatedAt.now(metadata.clock).value
|
|
27
|
+
: new date_type_1.CreatedAt(metadata.occurredOn).value;
|
|
28
|
+
}
|
|
29
|
+
eventName() {
|
|
30
|
+
return this.constructor.eventName;
|
|
31
|
+
}
|
|
32
|
+
eventVersion() {
|
|
33
|
+
return this.constructor.eventVersion ?? 1;
|
|
34
|
+
}
|
|
35
|
+
toJSON() {
|
|
36
|
+
const serialized = {
|
|
37
|
+
eventName: this.eventName(),
|
|
38
|
+
eventVersion: this.eventVersion(),
|
|
39
|
+
eventId: this.eventId,
|
|
40
|
+
aggregateId: this.aggregateId,
|
|
41
|
+
occurredOn: this.occurredOn.toISOString(),
|
|
42
|
+
};
|
|
43
|
+
for (const [key, value] of Object.entries(this)) {
|
|
44
|
+
if (!(key in serialized)) {
|
|
45
|
+
serialized[key] = value;
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
return serialized;
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
exports.EventBase = EventBase;
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
export * from './event/event-base';
|
|
2
|
+
export * from './entity/entity';
|
|
3
|
+
export * from './aggregate/aggregate-root';
|
|
4
|
+
export * from './type';
|
|
5
|
+
export * from './primitive/primitive-types';
|
|
6
|
+
export type { AggregateFinder } from './repository/aggregate-finder';
|
|
7
|
+
export type { AggregateSaver } from './repository/aggregate-saver';
|
|
8
|
+
export type { AggregateRemover } from './repository/aggregate-remover';
|
|
9
|
+
export { Specification } from './specification/specification';
|
|
10
|
+
export type { SatisfiedBy } from './specification/specification';
|
|
@@ -0,0 +1,24 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __exportStar = (this && this.__exportStar) || function(m, exports) {
|
|
14
|
+
for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
|
|
15
|
+
};
|
|
16
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
17
|
+
exports.Specification = void 0;
|
|
18
|
+
__exportStar(require("./event/event-base"), exports);
|
|
19
|
+
__exportStar(require("./entity/entity"), exports);
|
|
20
|
+
__exportStar(require("./aggregate/aggregate-root"), exports);
|
|
21
|
+
__exportStar(require("./type"), exports);
|
|
22
|
+
__exportStar(require("./primitive/primitive-types"), exports);
|
|
23
|
+
var specification_1 = require("./specification/specification");
|
|
24
|
+
Object.defineProperty(exports, "Specification", { enumerable: true, get: function () { return specification_1.Specification; } });
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import type { AggregateRoot } from '../aggregate/aggregate-root';
|
|
2
|
+
import type { AbstractType } from '../type/abstract-type';
|
|
3
|
+
export type Methods<T> = {
|
|
4
|
+
[P in keyof T]-?: T[P] extends Function ? P : never;
|
|
5
|
+
}[keyof T];
|
|
6
|
+
export type MethodsAndProperties<T> = {
|
|
7
|
+
[key in keyof T]: T[key];
|
|
8
|
+
};
|
|
9
|
+
export type Properties<T> = Omit<MethodsAndProperties<T>, Methods<T>>;
|
|
10
|
+
export type MutablePropertiesData<T> = Mutable<Required<Properties<T>>>;
|
|
11
|
+
export type PropertiesData<T> = Required<Properties<T>>;
|
|
12
|
+
type RemoveUnderscore<S extends string> = S extends `_${infer R}` ? R : S;
|
|
13
|
+
type Mutable<T> = {
|
|
14
|
+
-readonly [P in keyof T]: T[P];
|
|
15
|
+
};
|
|
16
|
+
type AggregateBookkeeping<T> = T extends AggregateRoot<any, any> ? 'version' : never;
|
|
17
|
+
export type PrimitiveTypes<T> = {
|
|
18
|
+
[key in keyof Mutable<Properties<T>> as key extends AggregateBookkeeping<T> ? never : RemoveUnderscore<string & key>]: PrimitiveType<T[key]>;
|
|
19
|
+
};
|
|
20
|
+
export type DataTypes<T> = Partial<PrimitiveTypes<T>>;
|
|
21
|
+
export type PrimitiveType<T> = T extends (infer U)[] ? PrimitiveType<U>[] : T extends AbstractType<any> ? T['value'] : T extends null ? null : T extends Date ? never : T extends object ? PrimitiveTypes<T> : never;
|
|
22
|
+
export {};
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
export interface SatisfiedBy<Candidate> {
|
|
2
|
+
isSatisfiedBy(candidate: Candidate): boolean;
|
|
3
|
+
}
|
|
4
|
+
export declare abstract class Specification<Candidate> implements SatisfiedBy<Candidate> {
|
|
5
|
+
abstract isSatisfiedBy(candidate: Candidate): boolean;
|
|
6
|
+
and(other: SatisfiedBy<Candidate>): Specification<Candidate>;
|
|
7
|
+
or(other: SatisfiedBy<Candidate>): Specification<Candidate>;
|
|
8
|
+
not(): Specification<Candidate>;
|
|
9
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Specification = void 0;
|
|
4
|
+
class Specification {
|
|
5
|
+
and(other) {
|
|
6
|
+
return new BothSpecification(this, other);
|
|
7
|
+
}
|
|
8
|
+
or(other) {
|
|
9
|
+
return new EitherSpecification(this, other);
|
|
10
|
+
}
|
|
11
|
+
not() {
|
|
12
|
+
return new NegatedSpecification(this);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
15
|
+
exports.Specification = Specification;
|
|
16
|
+
class BothSpecification extends Specification {
|
|
17
|
+
constructor(left, right) {
|
|
18
|
+
super();
|
|
19
|
+
this.left = left;
|
|
20
|
+
this.right = right;
|
|
21
|
+
}
|
|
22
|
+
isSatisfiedBy(candidate) {
|
|
23
|
+
return this.left.isSatisfiedBy(candidate) && this.right.isSatisfiedBy(candidate);
|
|
24
|
+
}
|
|
25
|
+
}
|
|
26
|
+
class EitherSpecification extends Specification {
|
|
27
|
+
constructor(left, right) {
|
|
28
|
+
super();
|
|
29
|
+
this.left = left;
|
|
30
|
+
this.right = right;
|
|
31
|
+
}
|
|
32
|
+
isSatisfiedBy(candidate) {
|
|
33
|
+
return this.left.isSatisfiedBy(candidate) || this.right.isSatisfiedBy(candidate);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
class NegatedSpecification extends Specification {
|
|
37
|
+
constructor(negated) {
|
|
38
|
+
super();
|
|
39
|
+
this.negated = negated;
|
|
40
|
+
}
|
|
41
|
+
isSatisfiedBy(candidate) {
|
|
42
|
+
return !this.negated.isSatisfiedBy(candidate);
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,20 @@
|
|
|
1
|
+
import type { Validatable, ValidationResult } from '@archi-code/validation';
|
|
2
|
+
import type { ValueOf } from './required/required-mark';
|
|
3
|
+
import type { ValidationIssue } from '../../exceptions/index';
|
|
4
|
+
type JsonValue<T> = T extends Date ? string : T;
|
|
5
|
+
export declare abstract class AbstractType<T, Input = T> implements Validatable<T | null> {
|
|
6
|
+
protected readonly _value: T | null;
|
|
7
|
+
static [Symbol.hasInstance](candidate: unknown): boolean;
|
|
8
|
+
constructor(value?: Input | null);
|
|
9
|
+
get value(): ValueOf<T, this>;
|
|
10
|
+
get isNull(): boolean;
|
|
11
|
+
toString(): string;
|
|
12
|
+
toJSON(): ValueOf<JsonValue<T>, this>;
|
|
13
|
+
equals(other: unknown): boolean;
|
|
14
|
+
validate(): ValidationResult<ValueOf<T, this>>;
|
|
15
|
+
protected brokenRules(value: unknown): ValidationIssue[];
|
|
16
|
+
protected abstract filter(value: unknown): T | null;
|
|
17
|
+
private acceptInput;
|
|
18
|
+
private static isMissingInput;
|
|
19
|
+
}
|
|
20
|
+
export {};
|
|
@@ -0,0 +1,69 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var _a;
|
|
3
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
4
|
+
exports.AbstractType = void 0;
|
|
5
|
+
const validation_1 = require("@archi-code/validation");
|
|
6
|
+
const value_equality_1 = require("./equality/value-equality");
|
|
7
|
+
const required_mark_1 = require("./required/required-mark");
|
|
8
|
+
const index_1 = require("../../exceptions/index");
|
|
9
|
+
const VALUE_OBJECT = Symbol.for('@archi-code/ddd/value-object.v1');
|
|
10
|
+
class AbstractType {
|
|
11
|
+
static [Symbol.hasInstance](candidate) {
|
|
12
|
+
return (Function.prototype[Symbol.hasInstance].call(this, candidate) ||
|
|
13
|
+
(this === _a &&
|
|
14
|
+
typeof candidate === 'object' &&
|
|
15
|
+
candidate !== null &&
|
|
16
|
+
candidate[VALUE_OBJECT] === true));
|
|
17
|
+
}
|
|
18
|
+
constructor(value = null) {
|
|
19
|
+
this._value = this.acceptInput(value);
|
|
20
|
+
const brokenRules = this.brokenRules(this._value);
|
|
21
|
+
if (brokenRules.length > 0) {
|
|
22
|
+
throw new index_1.ValidationException(brokenRules);
|
|
23
|
+
}
|
|
24
|
+
}
|
|
25
|
+
get value() {
|
|
26
|
+
return this._value;
|
|
27
|
+
}
|
|
28
|
+
get isNull() {
|
|
29
|
+
return this._value === null;
|
|
30
|
+
}
|
|
31
|
+
toString() {
|
|
32
|
+
return this.isNull ? '' : (0, validation_1.anyToString)(this._value);
|
|
33
|
+
}
|
|
34
|
+
toJSON() {
|
|
35
|
+
const value = this._value;
|
|
36
|
+
return (value instanceof Date ? value.toISOString() : value);
|
|
37
|
+
}
|
|
38
|
+
equals(other) {
|
|
39
|
+
return (typeof other === 'object' &&
|
|
40
|
+
other !== null &&
|
|
41
|
+
other.constructor === this.constructor &&
|
|
42
|
+
value_equality_1.ValueEquality.equals(this._value, other._value));
|
|
43
|
+
}
|
|
44
|
+
validate() {
|
|
45
|
+
return (0, validation_1.runValidations)(this.constructor, this._value);
|
|
46
|
+
}
|
|
47
|
+
brokenRules(value) {
|
|
48
|
+
return (0, validation_1.runValidations)(this.constructor, value).errors.map((error) => ({
|
|
49
|
+
field: null,
|
|
50
|
+
code: 'validator' in error ? error.validator : error.custom,
|
|
51
|
+
message: `${this.constructor.name}: ${error.message}`,
|
|
52
|
+
}));
|
|
53
|
+
}
|
|
54
|
+
acceptInput(value) {
|
|
55
|
+
const accepted = _a.isMissingInput(value) ? null : this.filter(value);
|
|
56
|
+
if (accepted === null && required_mark_1.RequiredMark.isMarked(this)) {
|
|
57
|
+
throw new index_1.RequiredValueException(this.constructor.name, value);
|
|
58
|
+
}
|
|
59
|
+
return accepted;
|
|
60
|
+
}
|
|
61
|
+
static isMissingInput(value) {
|
|
62
|
+
return (value === null || value === undefined || (typeof value === 'string' && value.trim() === ''));
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.AbstractType = AbstractType;
|
|
66
|
+
_a = AbstractType;
|
|
67
|
+
(() => {
|
|
68
|
+
Object.defineProperty(_a.prototype, VALUE_OBJECT, { value: true });
|
|
69
|
+
})();
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { ValidationResult } from '@archi-code/validation';
|
|
2
|
+
import { AbstractType } from './abstract-type';
|
|
3
|
+
import type { ValueOf } from './required/required-mark';
|
|
4
|
+
type ItemValue<Item extends AbstractType<any>> = Item['value'];
|
|
5
|
+
export declare abstract class ArrayType<Item extends AbstractType<any>> extends AbstractType<ItemValue<Item>[]> {
|
|
6
|
+
protected abstract createItem(value: unknown): Item;
|
|
7
|
+
get items(): Item[];
|
|
8
|
+
toString(): string;
|
|
9
|
+
hasItem(value: ItemValue<Item>): boolean;
|
|
10
|
+
withItem(value: ItemValue<Item>): this;
|
|
11
|
+
withoutItem(value: ItemValue<Item>): this;
|
|
12
|
+
protected filter(value: unknown): ItemValue<Item>[];
|
|
13
|
+
validate(): ValidationResult<ValueOf<ItemValue<Item>[], this>>;
|
|
14
|
+
private copyWith;
|
|
15
|
+
}
|
|
16
|
+
export {};
|
|
@@ -0,0 +1,65 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ArrayType = void 0;
|
|
4
|
+
const validation_1 = require("@archi-code/validation");
|
|
5
|
+
const abstract_type_1 = require("./abstract-type");
|
|
6
|
+
const immutable_1 = require("./immutable/immutable");
|
|
7
|
+
const value_equality_1 = require("./equality/value-equality");
|
|
8
|
+
const value_objects_1 = require("./value-objects/value-objects");
|
|
9
|
+
const index_1 = require("../../exceptions/index");
|
|
10
|
+
class ArrayType extends abstract_type_1.AbstractType {
|
|
11
|
+
get items() {
|
|
12
|
+
return (this._value ?? []).map((value) => this.createItem(value));
|
|
13
|
+
}
|
|
14
|
+
toString() {
|
|
15
|
+
return this.items.map((item) => item.toString()).join(', ');
|
|
16
|
+
}
|
|
17
|
+
hasItem(value) {
|
|
18
|
+
const itemValue = this.createItem(value).value;
|
|
19
|
+
return (this._value ?? []).some((current) => value_equality_1.ValueEquality.equals(current, itemValue));
|
|
20
|
+
}
|
|
21
|
+
withItem(value) {
|
|
22
|
+
return this.copyWith([...(this._value ?? []), value]);
|
|
23
|
+
}
|
|
24
|
+
withoutItem(value) {
|
|
25
|
+
if (this._value === null) {
|
|
26
|
+
return this;
|
|
27
|
+
}
|
|
28
|
+
const itemValue = this.createItem(value).value;
|
|
29
|
+
return this.copyWith(this._value.filter((current) => !value_equality_1.ValueEquality.equals(current, itemValue)));
|
|
30
|
+
}
|
|
31
|
+
filter(value) {
|
|
32
|
+
const converted = (0, validation_1.toArray)(value);
|
|
33
|
+
if (!converted.ok) {
|
|
34
|
+
throw new index_1.TypePrimitiveException('Array', value);
|
|
35
|
+
}
|
|
36
|
+
try {
|
|
37
|
+
return immutable_1.Immutable.freeze(value_objects_1.ValueObjects.buildList(converted.value, (item) => this.createItem(item).value));
|
|
38
|
+
}
|
|
39
|
+
catch (error) {
|
|
40
|
+
if (error instanceof index_1.ValidationException) {
|
|
41
|
+
throw new index_1.ValidationException([...this.brokenRules(converted.value), ...error.errors]);
|
|
42
|
+
}
|
|
43
|
+
throw error;
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
validate() {
|
|
47
|
+
const own = super.validate();
|
|
48
|
+
const itemErrors = (this._value ?? []).flatMap((itemValue, index) => this.createItem(itemValue)
|
|
49
|
+
.validate()
|
|
50
|
+
.errors.map((error) => Object.freeze({ ...error, message: `Item ${index + 1}: ${error.message}` })));
|
|
51
|
+
if (itemErrors.length === 0) {
|
|
52
|
+
return own;
|
|
53
|
+
}
|
|
54
|
+
return Object.freeze({
|
|
55
|
+
ok: false,
|
|
56
|
+
value: own.value,
|
|
57
|
+
errors: Object.freeze([...own.errors, ...itemErrors]),
|
|
58
|
+
});
|
|
59
|
+
}
|
|
60
|
+
copyWith(values) {
|
|
61
|
+
const SameType = this.constructor;
|
|
62
|
+
return new SameType(values);
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
exports.ArrayType = ArrayType;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.BooleanType = void 0;
|
|
4
|
+
const validation_1 = require("@archi-code/validation");
|
|
5
|
+
const abstract_type_1 = require("./abstract-type");
|
|
6
|
+
const index_1 = require("../../exceptions/index");
|
|
7
|
+
class BooleanType extends abstract_type_1.AbstractType {
|
|
8
|
+
filter(value) {
|
|
9
|
+
const converted = (0, validation_1.toBoolean)(value);
|
|
10
|
+
if (!converted.ok) {
|
|
11
|
+
throw new index_1.TypePrimitiveException('Boolean', value);
|
|
12
|
+
}
|
|
13
|
+
return converted.value;
|
|
14
|
+
}
|
|
15
|
+
}
|
|
16
|
+
exports.BooleanType = BooleanType;
|
|
@@ -0,0 +1,19 @@
|
|
|
1
|
+
import type { AbstractType } from '../abstract-type';
|
|
2
|
+
export type ValueClass = new (value: any) => AbstractType<any>;
|
|
3
|
+
export interface CompositeClass {
|
|
4
|
+
readonly name: string;
|
|
5
|
+
readonly prototype: object;
|
|
6
|
+
new (props: any): object;
|
|
7
|
+
fromPrimitives(primitives: any): object;
|
|
8
|
+
}
|
|
9
|
+
export type FieldClass = ValueClass | CompositeClass;
|
|
10
|
+
export type FieldSpec = FieldClass | readonly [FieldClass];
|
|
11
|
+
export declare class CompositeField {
|
|
12
|
+
static brand(prototype: object): void;
|
|
13
|
+
static isComposite(value: unknown): boolean;
|
|
14
|
+
static accept(owner: string, field: string, spec: FieldSpec, value: unknown): unknown;
|
|
15
|
+
static fromPrimitive(spec: FieldSpec, primitive: unknown): unknown;
|
|
16
|
+
static equals(left: unknown, right: unknown): boolean;
|
|
17
|
+
static primitive(value: unknown): unknown;
|
|
18
|
+
private static acceptOne;
|
|
19
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.CompositeField = void 0;
|
|
4
|
+
const value_objects_1 = require("../value-objects/value-objects");
|
|
5
|
+
const index_1 = require("../../../exceptions/index");
|
|
6
|
+
const COMPOSITE = Symbol.for('@archi-code/ddd/composite.v1');
|
|
7
|
+
class CompositeField {
|
|
8
|
+
static brand(prototype) {
|
|
9
|
+
Object.defineProperty(prototype, COMPOSITE, { value: true });
|
|
10
|
+
}
|
|
11
|
+
static isComposite(value) {
|
|
12
|
+
return (typeof value === 'object' &&
|
|
13
|
+
value !== null &&
|
|
14
|
+
value[COMPOSITE] === true);
|
|
15
|
+
}
|
|
16
|
+
static accept(owner, field, spec, value) {
|
|
17
|
+
if (!Array.isArray(spec)) {
|
|
18
|
+
return CompositeField.acceptOne(owner, field, spec, value);
|
|
19
|
+
}
|
|
20
|
+
if (!Array.isArray(value)) {
|
|
21
|
+
throw new index_1.TypePrimitiveException(`list for ${owner}.${field}`, value);
|
|
22
|
+
}
|
|
23
|
+
return Object.freeze(value.map((item, index) => CompositeField.acceptOne(owner, `${field}.${index}`, spec[0], item)));
|
|
24
|
+
}
|
|
25
|
+
static fromPrimitive(spec, primitive) {
|
|
26
|
+
if (Array.isArray(spec)) {
|
|
27
|
+
if (!Array.isArray(primitive)) {
|
|
28
|
+
throw new index_1.TypePrimitiveException('Array', primitive);
|
|
29
|
+
}
|
|
30
|
+
return value_objects_1.ValueObjects.buildList(primitive, (item) => CompositeField.fromPrimitive(spec[0], item));
|
|
31
|
+
}
|
|
32
|
+
const fieldClass = spec;
|
|
33
|
+
return CompositeField.isComposite(fieldClass.prototype)
|
|
34
|
+
? fieldClass.fromPrimitives(primitive)
|
|
35
|
+
: new fieldClass(primitive);
|
|
36
|
+
}
|
|
37
|
+
static equals(left, right) {
|
|
38
|
+
if (Array.isArray(left)) {
|
|
39
|
+
return (Array.isArray(right) &&
|
|
40
|
+
left.length === right.length &&
|
|
41
|
+
left.every((item, index) => CompositeField.equals(item, right[index])));
|
|
42
|
+
}
|
|
43
|
+
return left.equals(right);
|
|
44
|
+
}
|
|
45
|
+
static primitive(value) {
|
|
46
|
+
if (Array.isArray(value)) {
|
|
47
|
+
return value.map((item) => CompositeField.primitive(item));
|
|
48
|
+
}
|
|
49
|
+
return CompositeField.isComposite(value)
|
|
50
|
+
? value.toPrimitives()
|
|
51
|
+
: value.value;
|
|
52
|
+
}
|
|
53
|
+
static acceptOne(owner, field, fieldClass, value) {
|
|
54
|
+
if (value === null || value === undefined) {
|
|
55
|
+
throw new index_1.RequiredValueException(`${owner}.${field}`, value);
|
|
56
|
+
}
|
|
57
|
+
if (!(value instanceof fieldClass)) {
|
|
58
|
+
throw new index_1.TypePrimitiveException(`${fieldClass.name} for ${owner}.${field}`, value);
|
|
59
|
+
}
|
|
60
|
+
return value;
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
exports.CompositeField = CompositeField;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
import type { FieldSpec } from './composite-field';
|
|
2
|
+
import type { PrimitiveType } from '../../primitive/primitive-types';
|
|
3
|
+
export type CompositeSchema = Record<string, FieldSpec>;
|
|
4
|
+
type FieldOf<Spec> = Spec extends readonly [infer Item] ? FieldOf<Item>[] : Spec extends abstract new (...args: any[]) => infer Instance ? Instance : never;
|
|
5
|
+
export type CompositeProps<Schema extends CompositeSchema> = {
|
|
6
|
+
readonly [Field in keyof Schema]: FieldOf<Schema[Field]>;
|
|
7
|
+
};
|
|
8
|
+
export type CompositePrimitives<Schema extends CompositeSchema> = {
|
|
9
|
+
[Field in keyof Schema]: PrimitiveType<FieldOf<Schema[Field]>>;
|
|
10
|
+
};
|
|
11
|
+
type InputOf<Spec> = Spec extends readonly [infer Item] ? InputOf<Item>[] : Spec extends {
|
|
12
|
+
fromPrimitives(this: any, primitives: infer Primitives): unknown;
|
|
13
|
+
} ? Primitives : Spec extends abstract new (value: infer Value) => unknown ? Exclude<Value, undefined> : never;
|
|
14
|
+
export type CompositeInput<Schema extends CompositeSchema> = {
|
|
15
|
+
[Field in keyof Schema]: InputOf<Schema[Field]>;
|
|
16
|
+
};
|
|
17
|
+
export interface CompositeValue<Schema extends CompositeSchema> {
|
|
18
|
+
equals(other: unknown): boolean;
|
|
19
|
+
toPrimitives(): CompositePrimitives<Schema>;
|
|
20
|
+
}
|
|
21
|
+
export interface CompositeType<Schema extends CompositeSchema> {
|
|
22
|
+
new (props: CompositeProps<Schema>): CompositeProps<Schema> & CompositeValue<Schema>;
|
|
23
|
+
fromPrimitives<Composed>(this: new (props: CompositeProps<Schema>) => Composed, primitives: CompositeInput<Schema>): Composed;
|
|
24
|
+
}
|
|
25
|
+
export declare function Composite<Schema extends CompositeSchema>(schema: Schema): CompositeType<Schema>;
|
|
26
|
+
export {};
|
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.Composite = Composite;
|
|
4
|
+
const validation_1 = require("@archi-code/validation");
|
|
5
|
+
const composite_field_1 = require("./composite-field");
|
|
6
|
+
const value_objects_1 = require("../value-objects/value-objects");
|
|
7
|
+
const index_1 = require("../../../exceptions/index");
|
|
8
|
+
function Composite(schema) {
|
|
9
|
+
const fields = Object.keys(schema);
|
|
10
|
+
class CompositeValueObject {
|
|
11
|
+
static fromPrimitives(primitives) {
|
|
12
|
+
if (typeof primitives !== 'object' || primitives === null || Array.isArray(primitives)) {
|
|
13
|
+
throw new index_1.TypePrimitiveException(this.name, primitives);
|
|
14
|
+
}
|
|
15
|
+
const record = primitives;
|
|
16
|
+
const props = value_objects_1.ValueObjects.build(Object.fromEntries(fields.map((field) => [
|
|
17
|
+
field,
|
|
18
|
+
() => composite_field_1.CompositeField.fromPrimitive(schema[field], record[field]),
|
|
19
|
+
])));
|
|
20
|
+
return new this(props);
|
|
21
|
+
}
|
|
22
|
+
constructor(props) {
|
|
23
|
+
const owner = this.constructor.name;
|
|
24
|
+
for (const field of fields) {
|
|
25
|
+
Object.defineProperty(this, field, {
|
|
26
|
+
value: composite_field_1.CompositeField.accept(owner, field, schema[field], props?.[field]),
|
|
27
|
+
enumerable: true,
|
|
28
|
+
});
|
|
29
|
+
}
|
|
30
|
+
const brokenRules = (0, validation_1.runValidations)(this.constructor, this).errors;
|
|
31
|
+
if (brokenRules.length > 0) {
|
|
32
|
+
throw new index_1.ValidationException(brokenRules.map((error) => ({
|
|
33
|
+
field: null,
|
|
34
|
+
code: 'custom' in error ? error.custom : error.validator,
|
|
35
|
+
message: `${owner}: ${error.message}`,
|
|
36
|
+
})));
|
|
37
|
+
}
|
|
38
|
+
Object.freeze(this);
|
|
39
|
+
}
|
|
40
|
+
equals(other) {
|
|
41
|
+
return (typeof other === 'object' &&
|
|
42
|
+
other !== null &&
|
|
43
|
+
other.constructor === this.constructor &&
|
|
44
|
+
fields.every((field) => composite_field_1.CompositeField.equals(this[field], other[field])));
|
|
45
|
+
}
|
|
46
|
+
toPrimitives() {
|
|
47
|
+
return Object.fromEntries(fields.map((field) => [
|
|
48
|
+
field,
|
|
49
|
+
composite_field_1.CompositeField.primitive(this[field]),
|
|
50
|
+
]));
|
|
51
|
+
}
|
|
52
|
+
}
|
|
53
|
+
composite_field_1.CompositeField.brand(CompositeValueObject.prototype);
|
|
54
|
+
return CompositeValueObject;
|
|
55
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { AbstractType } from './abstract-type';
|
|
2
|
+
export declare class DateType extends AbstractType<Date, Date | string> {
|
|
3
|
+
protected filter(value: unknown): Date;
|
|
4
|
+
toString(): string;
|
|
5
|
+
}
|
|
6
|
+
declare const CreatedAt_base: import("./required/required").RequiredType<typeof DateType>;
|
|
7
|
+
export declare class CreatedAt extends CreatedAt_base {
|
|
8
|
+
static now(clock?: {
|
|
9
|
+
now(): Date;
|
|
10
|
+
}): CreatedAt;
|
|
11
|
+
}
|
|
12
|
+
declare const UpdatedAt_base: import("./required/required").RequiredType<typeof DateType>;
|
|
13
|
+
export declare class UpdatedAt extends UpdatedAt_base {
|
|
14
|
+
static now(clock?: {
|
|
15
|
+
now(): Date;
|
|
16
|
+
}): UpdatedAt;
|
|
17
|
+
}
|
|
18
|
+
export {};
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.UpdatedAt = exports.CreatedAt = exports.DateType = void 0;
|
|
4
|
+
const validation_1 = require("@archi-code/validation");
|
|
5
|
+
const abstract_type_1 = require("./abstract-type");
|
|
6
|
+
const immutable_1 = require("./immutable/immutable");
|
|
7
|
+
const required_1 = require("./required/required");
|
|
8
|
+
const index_1 = require("../../exceptions/index");
|
|
9
|
+
const TIME_WITHOUT_ZONE = /^\d{4}-\d{2}-\d{2}[T ]\d{2}:\d{2}:\d{2}(?:\.\d+)?$/;
|
|
10
|
+
class DateType extends abstract_type_1.AbstractType {
|
|
11
|
+
filter(value) {
|
|
12
|
+
const converted = (0, validation_1.toDate)(value, { iso: true });
|
|
13
|
+
if (!converted.ok || (typeof value === 'string' && TIME_WITHOUT_ZONE.test(value))) {
|
|
14
|
+
throw new index_1.TypePrimitiveException('Date', value);
|
|
15
|
+
}
|
|
16
|
+
return immutable_1.Immutable.date(converted.value);
|
|
17
|
+
}
|
|
18
|
+
toString() {
|
|
19
|
+
return this._value === null ? '' : this._value.toISOString();
|
|
20
|
+
}
|
|
21
|
+
}
|
|
22
|
+
exports.DateType = DateType;
|
|
23
|
+
const SYSTEM_TIME = { now: () => new Date() };
|
|
24
|
+
class CreatedAt extends (0, required_1.Required)(DateType) {
|
|
25
|
+
static now(clock = SYSTEM_TIME) {
|
|
26
|
+
return new CreatedAt(clock.now());
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
exports.CreatedAt = CreatedAt;
|
|
30
|
+
class UpdatedAt extends (0, required_1.Required)(DateType) {
|
|
31
|
+
static now(clock = SYSTEM_TIME) {
|
|
32
|
+
return new UpdatedAt(clock.now());
|
|
33
|
+
}
|
|
34
|
+
}
|
|
35
|
+
exports.UpdatedAt = UpdatedAt;
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
import { AbstractType } from './abstract-type';
|
|
2
|
+
export declare abstract class EnumType<T extends string | number> extends AbstractType<T> {
|
|
3
|
+
protected abstract getEnum(): Record<string, string | number>;
|
|
4
|
+
protected filter(value: unknown): T;
|
|
5
|
+
private static matches;
|
|
6
|
+
private options;
|
|
7
|
+
}
|