ddd-ts-kit 0.8.0 → 0.10.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +12 -2
- package/dist/check/catalogue/architecture-rules.d.ts.map +1 -1
- package/dist/check/catalogue/architecture-rules.js +14 -0
- package/dist/check/catalogue/architecture-rules.js.map +1 -1
- package/dist/check/errors/kit-error-codes.d.ts.map +1 -1
- package/dist/check/errors/kit-error-codes.js +5 -0
- package/dist/check/errors/kit-error-codes.js.map +1 -1
- package/dist/check/event-sourcing/declared-shape.d.ts +13 -0
- package/dist/check/event-sourcing/declared-shape.d.ts.map +1 -0
- package/dist/check/event-sourcing/declared-shape.js +100 -0
- package/dist/check/event-sourcing/declared-shape.js.map +1 -0
- package/dist/check/event-sourcing/event-sourced-classes.d.ts +5 -0
- package/dist/check/event-sourcing/event-sourced-classes.d.ts.map +1 -0
- package/dist/check/event-sourcing/event-sourced-classes.js +13 -0
- package/dist/check/event-sourcing/event-sourced-classes.js.map +1 -0
- package/dist/check/event-sourcing/fact-words.d.ts +6 -0
- package/dist/check/event-sourcing/fact-words.d.ts.map +1 -0
- package/dist/check/event-sourcing/fact-words.js +10 -0
- package/dist/check/event-sourcing/fact-words.js.map +1 -0
- package/dist/check/event-sourcing/is-apply-method.d.ts +3 -0
- package/dist/check/event-sourcing/is-apply-method.d.ts.map +1 -0
- package/dist/check/event-sourcing/is-apply-method.js +6 -0
- package/dist/check/event-sourcing/is-apply-method.js.map +1 -0
- package/dist/check/event-sourcing/kernel-names.d.ts +8 -0
- package/dist/check/event-sourcing/kernel-names.d.ts.map +1 -0
- package/dist/check/event-sourcing/kernel-names.js +7 -0
- package/dist/check/event-sourcing/kernel-names.js.map +1 -0
- package/dist/check/event-sourcing/read-stored-events-lock.d.ts +5 -0
- package/dist/check/event-sourcing/read-stored-events-lock.d.ts.map +1 -0
- package/dist/check/event-sourcing/read-stored-events-lock.js +25 -0
- package/dist/check/event-sourcing/read-stored-events-lock.js.map +1 -0
- package/dist/check/event-sourcing/read-stored-shape.d.ts +12 -0
- package/dist/check/event-sourcing/read-stored-shape.d.ts.map +1 -0
- package/dist/check/event-sourcing/read-stored-shape.js +46 -0
- package/dist/check/event-sourcing/read-stored-shape.js.map +1 -0
- package/dist/check/event-sourcing/state-writes.d.ts +3 -0
- package/dist/check/event-sourcing/state-writes.d.ts.map +1 -0
- package/dist/check/event-sourcing/state-writes.js +11 -0
- package/dist/check/event-sourcing/state-writes.js.map +1 -0
- package/dist/check/event-sourcing/stored-event-declaration.d.ts +18 -0
- package/dist/check/event-sourcing/stored-event-declaration.d.ts.map +1 -0
- package/dist/check/event-sourcing/stored-event-declaration.js +2 -0
- package/dist/check/event-sourcing/stored-event-declaration.js.map +1 -0
- package/dist/check/event-sourcing/stored-event-declarations.d.ts +9 -0
- package/dist/check/event-sourcing/stored-event-declarations.d.ts.map +1 -0
- package/dist/check/event-sourcing/stored-event-declarations.js +64 -0
- package/dist/check/event-sourcing/stored-event-declarations.js.map +1 -0
- package/dist/check/event-sourcing/stored-events-lock-file.d.ts +3 -0
- package/dist/check/event-sourcing/stored-events-lock-file.d.ts.map +1 -0
- package/dist/check/event-sourcing/stored-events-lock-file.js +3 -0
- package/dist/check/event-sourcing/stored-events-lock-file.js.map +1 -0
- package/dist/check/event-sourcing/stored-shape.d.ts +7 -0
- package/dist/check/event-sourcing/stored-shape.d.ts.map +1 -0
- package/dist/check/event-sourcing/stored-shape.js +2 -0
- package/dist/check/event-sourcing/stored-shape.js.map +1 -0
- package/dist/check/event-sourcing/words-of.d.ts +3 -0
- package/dist/check/event-sourcing/words-of.d.ts.map +1 -0
- package/dist/check/event-sourcing/words-of.js +9 -0
- package/dist/check/event-sourcing/words-of.js.map +1 -0
- package/dist/check/events/event-name-resolver.d.ts +6 -0
- package/dist/check/events/event-name-resolver.d.ts.map +1 -0
- package/dist/check/events/event-name-resolver.js +21 -0
- package/dist/check/events/event-name-resolver.js.map +1 -0
- package/dist/check/explain/errors-docs.js +1 -1
- package/dist/check/explain/errors-docs.js.map +1 -1
- package/dist/check/explain/event-sourcing-docs.d.ts +3 -0
- package/dist/check/explain/event-sourcing-docs.d.ts.map +1 -0
- package/dist/check/explain/event-sourcing-docs.js +62 -0
- package/dist/check/explain/event-sourcing-docs.js.map +1 -0
- package/dist/check/explain/persistence-docs.d.ts.map +1 -1
- package/dist/check/explain/persistence-docs.js +26 -0
- package/dist/check/explain/persistence-docs.js.map +1 -1
- package/dist/check/explain/rule-docs.d.ts.map +1 -1
- package/dist/check/explain/rule-docs.js +2 -0
- package/dist/check/explain/rule-docs.js.map +1 -1
- package/dist/check/files/file-content-findings.d.ts +4 -0
- package/dist/check/files/file-content-findings.d.ts.map +1 -0
- package/dist/check/files/file-content-findings.js +29 -0
- package/dist/check/files/file-content-findings.js.map +1 -0
- package/dist/check/files/file-content-pattern.d.ts +7 -0
- package/dist/check/files/file-content-pattern.d.ts.map +1 -0
- package/dist/check/files/file-content-pattern.js +17 -0
- package/dist/check/files/file-content-pattern.js.map +1 -0
- package/dist/check/files/schema-sources.d.ts +13 -0
- package/dist/check/files/schema-sources.d.ts.map +1 -0
- package/dist/check/files/schema-sources.js +37 -0
- package/dist/check/files/schema-sources.js.map +1 -0
- package/dist/check/fixtures/event-sourcing-fixtures.d.ts +3 -0
- package/dist/check/fixtures/event-sourcing-fixtures.d.ts.map +1 -0
- package/dist/check/fixtures/event-sourcing-fixtures.js +199 -0
- package/dist/check/fixtures/event-sourcing-fixtures.js.map +1 -0
- package/dist/check/fixtures/event-sourcing-projection-fixtures.d.ts +3 -0
- package/dist/check/fixtures/event-sourcing-projection-fixtures.d.ts.map +1 -0
- package/dist/check/fixtures/event-sourcing-projection-fixtures.js +48 -0
- package/dist/check/fixtures/event-sourcing-projection-fixtures.js.map +1 -0
- package/dist/check/fixtures/files-fixtures.d.ts +3 -0
- package/dist/check/fixtures/files-fixtures.d.ts.map +1 -0
- package/dist/check/fixtures/files-fixtures.js +92 -0
- package/dist/check/fixtures/files-fixtures.js.map +1 -0
- package/dist/check/fixtures/fixtures.d.ts.map +1 -1
- package/dist/check/fixtures/fixtures.js +4 -0
- package/dist/check/fixtures/fixtures.js.map +1 -1
- package/dist/check/layers/outside-concern.d.ts.map +1 -1
- package/dist/check/layers/outside-concern.js +4 -0
- package/dist/check/layers/outside-concern.js.map +1 -1
- package/dist/check/rules/event-sourcing-past-tense.d.ts +3 -0
- package/dist/check/rules/event-sourcing-past-tense.d.ts.map +1 -0
- package/dist/check/rules/event-sourcing-past-tense.js +31 -0
- package/dist/check/rules/event-sourcing-past-tense.js.map +1 -0
- package/dist/check/rules/event-sourcing-projection-never-appends.d.ts +3 -0
- package/dist/check/rules/event-sourcing-projection-never-appends.d.ts.map +1 -0
- package/dist/check/rules/event-sourcing-projection-never-appends.js +54 -0
- package/dist/check/rules/event-sourcing-projection-never-appends.js.map +1 -0
- package/dist/check/rules/event-sourcing-state-in-apply.d.ts +3 -0
- package/dist/check/rules/event-sourcing-state-in-apply.d.ts.map +1 -0
- package/dist/check/rules/event-sourcing-state-in-apply.js +34 -0
- package/dist/check/rules/event-sourcing-state-in-apply.js.map +1 -0
- package/dist/check/rules/event-sourcing-store-behind-repository.d.ts +3 -0
- package/dist/check/rules/event-sourcing-store-behind-repository.d.ts.map +1 -0
- package/dist/check/rules/event-sourcing-store-behind-repository.js +34 -0
- package/dist/check/rules/event-sourcing-store-behind-repository.js.map +1 -0
- package/dist/check/rules/event-sourcing-upcaster-required.d.ts +3 -0
- package/dist/check/rules/event-sourcing-upcaster-required.d.ts.map +1 -0
- package/dist/check/rules/event-sourcing-upcaster-required.js +79 -0
- package/dist/check/rules/event-sourcing-upcaster-required.js.map +1 -0
- package/dist/check/rules/files-port-not-connector.d.ts +3 -0
- package/dist/check/rules/files-port-not-connector.d.ts.map +1 -0
- package/dist/check/rules/files-port-not-connector.js +28 -0
- package/dist/check/rules/files-port-not-connector.js.map +1 -0
- package/dist/check/rules/persistence-no-file-content.d.ts +3 -0
- package/dist/check/rules/persistence-no-file-content.d.ts.map +1 -0
- package/dist/check/rules/persistence-no-file-content.js +14 -0
- package/dist/check/rules/persistence-no-file-content.js.map +1 -0
- package/dist/commands/docs.d.ts.map +1 -1
- package/dist/commands/docs.js +44 -11
- package/dist/commands/docs.js.map +1 -1
- package/dist/commands/format-context.d.ts.map +1 -1
- package/dist/commands/format-context.js +1 -1
- package/dist/commands/format-context.js.map +1 -1
- package/dist/commands/generate.js +2 -0
- package/dist/commands/generate.js.map +1 -1
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +1 -0
- package/dist/commands/init.js.map +1 -1
- package/dist/config/index.d.ts +1 -1
- package/dist/config/index.d.ts.map +1 -1
- package/dist/config/index.js +1 -1
- package/dist/config/index.js.map +1 -1
- package/dist/config/parse-stack.d.ts.map +1 -1
- package/dist/config/parse-stack.js +2 -1
- package/dist/config/parse-stack.js.map +1 -1
- package/dist/config/stack.d.ts +8 -0
- package/dist/config/stack.d.ts.map +1 -1
- package/dist/config/stack.js +1 -0
- package/dist/config/stack.js.map +1 -1
- package/dist/describe/context-description.d.ts +2 -0
- package/dist/describe/context-description.d.ts.map +1 -1
- package/dist/describe/describe-context.js +1 -0
- package/dist/describe/describe-context.js.map +1 -1
- package/dist/describe/format-description.d.ts.map +1 -1
- package/dist/describe/format-description.js +4 -1
- package/dist/describe/format-description.js.map +1 -1
- package/dist/docs/context-facts.d.ts +2 -0
- package/dist/docs/context-facts.d.ts.map +1 -1
- package/dist/docs/declared-stored-events.d.ts +9 -0
- package/dist/docs/declared-stored-events.d.ts.map +1 -0
- package/dist/docs/declared-stored-events.js +22 -0
- package/dist/docs/declared-stored-events.js.map +1 -0
- package/dist/docs/read-context.js +10 -0
- package/dist/docs/read-context.js.map +1 -1
- package/dist/docs/refresh-context-doc.js +1 -1
- package/dist/docs/refresh-context-doc.js.map +1 -1
- package/dist/docs/unrecorded-stored-events.d.ts +9 -0
- package/dist/docs/unrecorded-stored-events.d.ts.map +1 -0
- package/dist/docs/unrecorded-stored-events.js +17 -0
- package/dist/docs/unrecorded-stored-events.js.map +1 -0
- package/dist/docs/write-docs.d.ts +1 -2
- package/dist/docs/write-docs.d.ts.map +1 -1
- package/dist/docs/write-docs.js +1 -2
- package/dist/docs/write-docs.js.map +1 -1
- package/dist/docs/write-stored-events-lock.d.ts +12 -0
- package/dist/docs/write-stored-events-lock.d.ts.map +1 -0
- package/dist/docs/write-stored-events-lock.js +41 -0
- package/dist/docs/write-stored-events-lock.js.map +1 -0
- package/dist/generate/event-names.d.ts +2 -0
- package/dist/generate/event-names.d.ts.map +1 -1
- package/dist/generate/event-names.js +1 -0
- package/dist/generate/event-names.js.map +1 -1
- package/dist/generate/kind.d.ts +2 -0
- package/dist/generate/kind.d.ts.map +1 -1
- package/dist/generate/kinds/aggregate-kind.d.ts.map +1 -1
- package/dist/generate/kinds/aggregate-kind.js +8 -2
- package/dist/generate/kinds/aggregate-kind.js.map +1 -1
- package/dist/generate/kinds/domain-event-kind.d.ts.map +1 -1
- package/dist/generate/kinds/domain-event-kind.js +5 -2
- package/dist/generate/kinds/domain-event-kind.js.map +1 -1
- package/dist/generate/kinds/event-vars.d.ts.map +1 -1
- package/dist/generate/kinds/event-vars.js +6 -1
- package/dist/generate/kinds/event-vars.js.map +1 -1
- package/dist/generate/kinds/extend-event-sourced-aggregate.d.ts +9 -0
- package/dist/generate/kinds/extend-event-sourced-aggregate.d.ts.map +1 -0
- package/dist/generate/kinds/extend-event-sourced-aggregate.js +67 -0
- package/dist/generate/kinds/extend-event-sourced-aggregate.js.map +1 -0
- package/dist/generate/kinds/file-upload/write-upload-controller.d.ts +5 -0
- package/dist/generate/kinds/file-upload/write-upload-controller.d.ts.map +1 -0
- package/dist/generate/kinds/file-upload/write-upload-controller.js +42 -0
- package/dist/generate/kinds/file-upload/write-upload-controller.js.map +1 -0
- package/dist/generate/kinds/file-upload-kind.d.ts +3 -0
- package/dist/generate/kinds/file-upload-kind.d.ts.map +1 -0
- package/dist/generate/kinds/file-upload-kind.js +55 -0
- package/dist/generate/kinds/file-upload-kind.js.map +1 -0
- package/dist/generate/kinds/write-event-sourced-aggregate.d.ts +8 -0
- package/dist/generate/kinds/write-event-sourced-aggregate.d.ts.map +1 -0
- package/dist/generate/kinds/write-event-sourced-aggregate.js +24 -0
- package/dist/generate/kinds/write-event-sourced-aggregate.js.map +1 -0
- package/dist/generate/kinds.d.ts.map +1 -1
- package/dist/generate/kinds.js +2 -0
- package/dist/generate/kinds.js.map +1 -1
- package/dist/help/notes.d.ts +1 -1
- package/dist/help/notes.d.ts.map +1 -1
- package/dist/help/notes.js +3 -0
- package/dist/help/notes.js.map +1 -1
- package/dist/help/usage.d.ts +1 -1
- package/dist/help/usage.d.ts.map +1 -1
- package/dist/help/usage.js +3 -2
- package/dist/help/usage.js.map +1 -1
- package/dist/init/files/platform-files.d.ts.map +1 -1
- package/dist/init/files/platform-files.js +22 -0
- package/dist/init/files/platform-files.js.map +1 -1
- package/dist/init/manifest/dependencies-for.d.ts.map +1 -1
- package/dist/init/manifest/dependencies-for.js +7 -1
- package/dist/init/manifest/dependencies-for.js.map +1 -1
- package/dist/init/manifest/versions.d.ts.map +1 -1
- package/dist/init/manifest/versions.js +28 -23
- package/dist/init/manifest/versions.js.map +1 -1
- package/dist/init/stack/resolve-stack.d.ts.map +1 -1
- package/dist/init/stack/resolve-stack.js +11 -2
- package/dist/init/stack/resolve-stack.js.map +1 -1
- package/dist/init/stack/stack-options.d.ts +1 -0
- package/dist/init/stack/stack-options.d.ts.map +1 -1
- package/dist/init/template-vars.d.ts.map +1 -1
- package/dist/init/template-vars.js +3 -2
- package/dist/init/template-vars.js.map +1 -1
- package/dist/naming/index.d.ts +2 -0
- package/dist/naming/index.d.ts.map +1 -1
- package/dist/naming/index.js +2 -0
- package/dist/naming/index.js.map +1 -1
- package/dist/naming/is-past-fact.d.ts +7 -0
- package/dist/naming/is-past-fact.d.ts.map +1 -0
- package/dist/naming/is-past-fact.js +18 -0
- package/dist/naming/is-past-fact.js.map +1 -0
- package/dist/naming/is-past-word.d.ts +3 -0
- package/dist/naming/is-past-word.d.ts.map +1 -0
- package/dist/naming/is-past-word.js +113 -0
- package/dist/naming/is-past-word.js.map +1 -0
- package/dist/naming/looks-like-past-fact.d.ts +1 -1
- package/dist/naming/looks-like-past-fact.d.ts.map +1 -1
- package/dist/naming/looks-like-past-fact.js +3 -66
- package/dist/naming/looks-like-past-fact.js.map +1 -1
- package/dist/stack/files-vars.d.ts +5 -0
- package/dist/stack/files-vars.d.ts.map +1 -0
- package/dist/stack/files-vars.js +11 -0
- package/dist/stack/files-vars.js.map +1 -0
- package/dist/stack/stack-vars.d.ts.map +1 -1
- package/dist/stack/stack-vars.js +13 -4
- package/dist/stack/stack-vars.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.d.ts.map +1 -1
- package/dist/version.js +1 -1
- package/dist/version.js.map +1 -1
- package/package.json +3 -3
- package/templates/aggregate/event-sourced.aggregate.spec.ts.tpl +26 -0
- package/templates/aggregate/event-sourced.aggregate.ts.tpl +61 -0
- package/templates/aggregate/event-sourced.events.mapper.ts.tpl +15 -0
- package/templates/builder/event-sourced.builder.ts.tpl +36 -0
- package/templates/file-upload/command.ts.tpl +41 -0
- package/templates/file-upload/controller.spec.ts.tpl +46 -0
- package/templates/file-upload/controller.ts.tpl +23 -0
- package/templates/file-upload/handler.declared.ts.tpl +18 -0
- package/templates/file-upload/handler.spec.ts.tpl +79 -0
- package/templates/file-upload/handler.ts.tpl +16 -0
- package/templates/file-upload/wiring.manual.tpl +9 -0
- package/templates/file-upload/wiring.tsyringe.tpl +7 -0
- package/templates/init/CLAUDE.md.tpl +1 -1
- package/templates/init/README.md.tpl +22 -0
- package/templates/init/docker/docker-compose.yml.tpl +25 -0
- package/templates/init/docker/env.example.tpl +21 -0
- package/templates/init/gitignore.tpl +3 -0
- package/templates/init/main.ts.tpl +6 -0
- package/templates/init/platform/auth/http-boundary.ts.tpl +25 -1
- package/templates/init/platform/buses/create-command-bus.ts.tpl +7 -1
- package/templates/init/platform/compose-application.manual.ts.tpl +25 -0
- package/templates/init/platform/compose-application.tsyringe.ts.tpl +31 -0
- package/templates/init/platform/config/app-config.ts.tpl +7 -0
- package/templates/init/platform/config/load-config.ts.tpl +9 -0
- package/templates/init/platform/config/read-files.local.ts.tpl +39 -0
- package/templates/init/platform/config/read-files.s3.ts.tpl +42 -0
- package/templates/init/platform/create-platform.manual.ts.tpl +38 -1
- package/templates/init/platform/files/bind-files.tsyringe.ts.tpl +32 -0
- package/templates/init/platform/files/create-files.ts.tpl +34 -0
- package/templates/init/platform/files/file-routes.ts.tpl +10 -0
- package/templates/init/platform/files/files.module.tsyringe.ts.tpl +18 -0
- package/templates/init/platform/files/open-files.local.ts.tpl +13 -0
- package/templates/init/platform/files/open-files.s3.ts.tpl +10 -0
- package/templates/init/platform/files/staging-uploads.tsyringe.ts.tpl +11 -0
- package/templates/init/platform/http/start-http-server.express.ts.tpl +5 -1
- package/templates/init/platform/http/start-http-server.fastify.ts.tpl +4 -1
- package/templates/init/platform/http/start-http-server.nest.ts.tpl +12 -1
- package/templates/init/platform/platform-test.module.tsyringe.ts.tpl +11 -0
- package/templates/init/platform/platform.manual.ts.tpl +7 -0
- package/templates/init/rules/application.md.tpl +4 -0
- package/templates/init/rules/domain.md.tpl +1 -0
- package/templates/init/rules/infrastructure.md.tpl +2 -0
- package/templates/init/rules/platform.md.tpl +3 -0
- package/templates/pipeline/package-scripts.json +1 -1
- package/templates/service/main.ts.tpl +6 -0
- package/templates/worker/main.ts.tpl +6 -0
|
@@ -0,0 +1,61 @@
|
|
|
1
|
+
import { EventSourcedAggregate, type AggregateHistory, type Appliers } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import { {{event.pascal}} } from '{{alias.context}}/domain/events/{{event.kebab}}.event{{alias.ext}}';
|
|
3
|
+
|
|
4
|
+
export type {{name.pascal}}Event = {{event.pascal}};
|
|
5
|
+
|
|
6
|
+
export interface {{name.pascal}}State {
|
|
7
|
+
readonly registeredAt: Date;
|
|
8
|
+
// TODO(domain): declare the rest of the state of {{name.pascal}} with value objects (a Money, a Period), not primitives: what its commands need to decide, nothing more.
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export class {{name.pascal}} extends EventSourcedAggregate<{{name.pascal}}Event, {{name.pascal}}State> {
|
|
12
|
+
private state: {{name.pascal}}State = { registeredAt: new Date(0) };
|
|
13
|
+
|
|
14
|
+
private constructor(id: string) {
|
|
15
|
+
super(id);
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
static create(id: string, now: Date): {{name.pascal}} {
|
|
19
|
+
{{#if refusals.factories}}
|
|
20
|
+
// TODO(domain): take the facts known at birth as parameters, check each birth invariant and throw the refusal of its error class when one is violated: `throw {{name.pascal}}Error.<refusal>(...)`, added with `ddd-ts-kit generate error {{context.kebab}} {{name.pascal}} <refusal>`.
|
|
21
|
+
{{/if}}
|
|
22
|
+
{{#if refusals.classes}}
|
|
23
|
+
// TODO(domain): take the facts known at birth as parameters, check each birth invariant and throw a DomainError of ../errors when one is violated.
|
|
24
|
+
{{/if}}
|
|
25
|
+
const created = new {{name.pascal}}(id);
|
|
26
|
+
|
|
27
|
+
created.record(new {{event.pascal}}(id, now));
|
|
28
|
+
|
|
29
|
+
return created;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
static rehydrate(id: string, history: AggregateHistory<{{name.pascal}}Event, {{name.pascal}}State>): {{name.pascal}} {
|
|
33
|
+
const loaded = new {{name.pascal}}(id);
|
|
34
|
+
|
|
35
|
+
return loaded.replay(history, (state) => loaded.restore(state));
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
// TODO(domain): add one method per business intention: it checks its invariants on `this.state`, then records what it decided, `this.record(new {{name.pascal}}<Fact>(this.id, now, ...))`, after `ddd-ts-kit generate domain-event {{context.kebab}} {{name.pascal}} <Fact>` added the event and its apply method. It never writes the state itself (event-sourcing/state-in-apply).
|
|
39
|
+
|
|
40
|
+
get registeredAt(): Date {
|
|
41
|
+
return this.state.registeredAt;
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
toState(): {{name.pascal}}State {
|
|
45
|
+
return { ...this.state };
|
|
46
|
+
}
|
|
47
|
+
|
|
48
|
+
protected appliers(): Appliers<{{name.pascal}}Event> {
|
|
49
|
+
return {
|
|
50
|
+
[{{event.pascal}}.eventName]: (event) => this.apply{{event.fact}}(event),
|
|
51
|
+
};
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
private restore(state: {{name.pascal}}State): void {
|
|
55
|
+
this.state = state;
|
|
56
|
+
}
|
|
57
|
+
|
|
58
|
+
private apply{{event.fact}}(event: {{event.pascal}}): void {
|
|
59
|
+
this.state = { ...this.state, registeredAt: event.occurredAt };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
import { defineStoredEvent, type StoredEventTypes } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import type { {{name.pascal}}Event } from '{{alias.context}}/domain/aggregates/{{name.kebab}}.aggregate{{alias.ext}}';
|
|
3
|
+
import { {{event.pascal}} } from '{{alias.context}}/domain/events/{{event.kebab}}.event{{alias.ext}}';
|
|
4
|
+
|
|
5
|
+
// TODO(domain): give each data type the fields its event carries beyond its id and instant, in plain data (texts, numbers, ISO dates); personal data is written `personalData(subjectId, value)`. A type stored once never changes: a change is a new version with its upcaster (event-sourcing/upcaster-required).
|
|
6
|
+
interface {{event.pascal}}Data {}
|
|
7
|
+
|
|
8
|
+
/** How the events of {{name.pascal}} are kept in its stream: one entry per event, read back by `decode`. */
|
|
9
|
+
export const {{name.camel}}Events: StoredEventTypes<{{name.pascal}}Event> = {
|
|
10
|
+
[{{event.pascal}}.eventName]: defineStoredEvent({{event.pascal}}, {
|
|
11
|
+
version: 1,
|
|
12
|
+
encode: (): {{event.pascal}}Data => ({}),
|
|
13
|
+
decode: (_data, { aggregateId, occurredAt }) => new {{event.pascal}}(aggregateId, occurredAt),
|
|
14
|
+
}),
|
|
15
|
+
};
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { Builder, sequence, type Defaults } from '@ddd-ts-kit/testing';
|
|
2
|
+
import { {{aggregate.pascal}} } from '{{alias.context}}/domain/aggregates/{{aggregate.kebab}}.aggregate{{alias.ext}}';
|
|
3
|
+
import { {{event.pascal}} } from '{{alias.context}}/domain/events/{{event.kebab}}.event{{alias.ext}}';
|
|
4
|
+
|
|
5
|
+
interface {{aggregate.pascal}}Ingredients {
|
|
6
|
+
readonly id: string;
|
|
7
|
+
readonly bornAt: Date;
|
|
8
|
+
// TODO(domain): add one ingredient per fact a test may need to choose, each with a valid default below.
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
/** Builds {{aggregate.pascal}} the way a load does: by replaying a history of events. */
|
|
12
|
+
export class {{aggregate.pascal}}Builder extends Builder<{{aggregate.pascal}}Ingredients, {{aggregate.pascal}}> {
|
|
13
|
+
identifiedBy(id: string): this {
|
|
14
|
+
return this.with('id', id);
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
bornAt(instant: Date): this {
|
|
18
|
+
return this.with('bornAt', instant);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
// TODO(domain): add one method per state a test needs, named in the ubiquitous language, and add the events that reach it to the history replayed in `construct`.
|
|
22
|
+
|
|
23
|
+
protected defaults(): Defaults<{{aggregate.pascal}}Ingredients> {
|
|
24
|
+
return { id: sequence('{{aggregate.kebab}}'), bornAt: new Date('2026-01-01T00:00:00.000Z') };
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
protected construct(ingredients: {{aggregate.pascal}}Ingredients): {{aggregate.pascal}} {
|
|
28
|
+
return {{aggregate.pascal}}.rehydrate(ingredients.id, {
|
|
29
|
+
events: [new {{event.pascal}}(ingredients.id, ingredients.bornAt)],
|
|
30
|
+
});
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export function {{aggregate.article}}{{aggregate.pascal}}(): {{aggregate.pascal}}Builder {
|
|
35
|
+
return new {{aggregate.pascal}}Builder();
|
|
36
|
+
}
|
|
@@ -0,0 +1,41 @@
|
|
|
1
|
+
{{#if access.protected}}
|
|
2
|
+
import { AuthorizedCommand, FileUpload, type Actor, type ReceivedFile } from '@ddd-ts-kit/kernel';
|
|
3
|
+
{{#if style.declared}}
|
|
4
|
+
import { Requires } from '@ddd-ts-kit/kernel/declarations';
|
|
5
|
+
{{access.useCasePermissionsImport}}
|
|
6
|
+
{{/if}}
|
|
7
|
+
|
|
8
|
+
{{#if style.declared}}
|
|
9
|
+
@{{access.decoration}}
|
|
10
|
+
{{/if}}
|
|
11
|
+
export class {{name.pascal}}Command extends AuthorizedCommand<string> {
|
|
12
|
+
/** Staged by the bus under a key of its area before the transaction opens: the handler reads `upload.file`. */
|
|
13
|
+
readonly upload: FileUpload;
|
|
14
|
+
|
|
15
|
+
// TODO(domain): add the identifier of what the file is attached to, read from the path by the controller, for example `readonly invoiceId: string`.
|
|
16
|
+
constructor(actor: Actor, file: ReceivedFile) {
|
|
17
|
+
super(actor);
|
|
18
|
+
this.upload = new FileUpload(file, '{{name.kebab}}');
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
{{/if}}
|
|
22
|
+
{{#if access.public}}
|
|
23
|
+
import { Command, FileUpload, type ReceivedFile } from '@ddd-ts-kit/kernel';
|
|
24
|
+
{{#if style.declared}}
|
|
25
|
+
import { Public } from '@ddd-ts-kit/kernel/declarations';
|
|
26
|
+
{{/if}}
|
|
27
|
+
|
|
28
|
+
{{#if style.declared}}
|
|
29
|
+
@Public()
|
|
30
|
+
{{/if}}
|
|
31
|
+
export class {{name.pascal}}Command extends Command<string> {
|
|
32
|
+
/** Staged by the bus under a key of its area before the transaction opens: the handler reads `upload.file`. */
|
|
33
|
+
readonly upload: FileUpload;
|
|
34
|
+
|
|
35
|
+
// TODO(domain): add the identifier of what the file is attached to, read from the path by the controller, for example `readonly invoiceId: string`.
|
|
36
|
+
constructor(file: ReceivedFile) {
|
|
37
|
+
super();
|
|
38
|
+
this.upload = new FileUpload(file, '{{name.kebab}}');
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
{{/if}}
|
|
@@ -0,0 +1,46 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { describe, expect, it } from '{{tests.module}}';
|
|
3
|
+
import { controllerRoute, executeRoute } from '@ddd-ts-kit/http';
|
|
4
|
+
import { InMemoryCommandBus{{#if route.protected}}, type Actor{{/if}} } from '@ddd-ts-kit/kernel';
|
|
5
|
+
{{route.messageImport}}
|
|
6
|
+
{{route.specImports}}
|
|
7
|
+
|
|
8
|
+
{{#if route.protected}}
|
|
9
|
+
const caller: Actor = { id: 'user-1', type: 'user', roles: [], attributes: {} };
|
|
10
|
+
|
|
11
|
+
{{/if}}
|
|
12
|
+
describe('{{route.method}} {{route.path}}', () => {
|
|
13
|
+
it('declares the file it receives, and hands it to the command as it streams', async () => {
|
|
14
|
+
const received: string[] = [];
|
|
15
|
+
|
|
16
|
+
const commands = new InMemoryCommandBus().register({{route.input}}, {
|
|
17
|
+
execute: async ({ upload }) => {
|
|
18
|
+
received.push(upload.fileName);
|
|
19
|
+
|
|
20
|
+
return 'file-1';
|
|
21
|
+
},
|
|
22
|
+
});
|
|
23
|
+
|
|
24
|
+
const route = controllerRoute(new {{name.pascal}}Controller(commands));
|
|
25
|
+
const file = { fileName: 'scan.pdf', mediaType: 'application/pdf', stream: Readable.from([Buffer.from('%PDF-')]) };
|
|
26
|
+
|
|
27
|
+
const response = await executeRoute(
|
|
28
|
+
route,
|
|
29
|
+
{ params: {{route.sampleParams}}, query: {}, headers: {}, body: {}, raw: undefined },
|
|
30
|
+
{
|
|
31
|
+
validateResponses: true,
|
|
32
|
+
uploads: async () => ({ fields: {}, file }),
|
|
33
|
+
{{#if route.protected}}
|
|
34
|
+
resolvePrincipal: () => caller,
|
|
35
|
+
{{/if}}
|
|
36
|
+
},
|
|
37
|
+
);
|
|
38
|
+
|
|
39
|
+
expect(response.status).toBe(201);
|
|
40
|
+
expect(received).toEqual(['scan.pdf']);
|
|
41
|
+
expect(route.upload?.field).toBe('file');
|
|
42
|
+
});
|
|
43
|
+
|
|
44
|
+
// TODO(domain): replace each entry below by a test of the contract a client relies on.
|
|
45
|
+
it.todo('answers the documented status for each error code of the use case');
|
|
46
|
+
});
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import { {{upload.httpImports}} } from '@ddd-ts-kit/http';
|
|
2
|
+
import type { CommandBus } from '@ddd-ts-kit/kernel';
|
|
3
|
+
{{route.imports}}
|
|
4
|
+
|
|
5
|
+
@Controller({
|
|
6
|
+
{{route.access}}
|
|
7
|
+
method: '{{route.method}}',
|
|
8
|
+
path: '{{route.groupPath}}',
|
|
9
|
+
summary: '{{name.title}}',
|
|
10
|
+
// TODO(domain): the largest file and the media types "{{name.words}}" accepts: both are part of the contract, and the content is checked against them as it streams.
|
|
11
|
+
upload: { field: 'file', maxBytes: 10_485_760, mediaTypes: ['application/pdf', 'image/png', 'image/jpeg'] },
|
|
12
|
+
response: { status: 201, description: 'Stored; the file is visible once the command has committed.' },
|
|
13
|
+
// TODO(domain): list the codes of the refusals "{{name.words}}" can raise, as their factories set them, so that they appear in the contract with their status (`errors/known-code`).
|
|
14
|
+
errors: [],
|
|
15
|
+
})
|
|
16
|
+
export class {{name.pascal}}Controller {
|
|
17
|
+
constructor(private readonly commands: CommandBus) {}
|
|
18
|
+
|
|
19
|
+
async handle(request: HttpRequest<{ upload: IncomingFile }>): Promise<void> {
|
|
20
|
+
// TODO(domain): hand the command the identifier of what the file is attached to, from `request.params`.
|
|
21
|
+
await this.commands.execute(new {{route.input}}({{#if route.protected}}request.actor, {{/if}}request.upload));
|
|
22
|
+
}
|
|
23
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import type { FileStaging } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import { CommandHandler } from '@ddd-ts-kit/kernel/declarations';
|
|
3
|
+
import { {{name.pascal}}Command } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.command{{alias.ext}}';
|
|
4
|
+
|
|
5
|
+
@CommandHandler({{name.pascal}}Command)
|
|
6
|
+
export class {{name.pascal}}Handler implements CommandHandler<{{name.pascal}}Command> {
|
|
7
|
+
constructor(private readonly staging: FileStaging) {}
|
|
8
|
+
|
|
9
|
+
/** The file arrives staged and scanned; it reaches its key with the commit, and stays staged, then purged, on a rollback. */
|
|
10
|
+
async execute(command: {{name.pascal}}Command): Promise<string> {
|
|
11
|
+
const { file } = command.upload;
|
|
12
|
+
|
|
13
|
+
// TODO(domain): record `file` (key, size, media type, checksum: never the content) on the aggregate "{{name.words}}" attaches it to, and save that aggregate here: the reference commits with the file.
|
|
14
|
+
await this.staging.promoteOnCommit(file.key);
|
|
15
|
+
|
|
16
|
+
return file.key;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
import { Readable } from 'node:stream';
|
|
2
|
+
import { describe, expect, it } from '{{tests.module}}';
|
|
3
|
+
{{#if access.protected}}
|
|
4
|
+
import { AuthorizationMap, RoleBasedAuthorizer, authorizationMiddleware, requires } from '@ddd-ts-kit/auth';
|
|
5
|
+
import { anActor } from '@ddd-ts-kit/auth/testing';
|
|
6
|
+
{{/if}}
|
|
7
|
+
import {
|
|
8
|
+
AcceptEveryFileScanner,
|
|
9
|
+
FileStaging,
|
|
10
|
+
InMemoryErrorReporter,
|
|
11
|
+
InMemoryFileStorage,
|
|
12
|
+
TenantFileKeys,
|
|
13
|
+
fileUploadsMiddleware,
|
|
14
|
+
} from '@ddd-ts-kit/kernel';
|
|
15
|
+
import { createTestBuses } from '@ddd-ts-kit/testing';
|
|
16
|
+
{{#if access.protected}}
|
|
17
|
+
import { {{context.camel}}Permissions } from '{{alias.context}}/application/authorization/{{context.kebab}}.permissions{{alias.ext}}';
|
|
18
|
+
{{/if}}
|
|
19
|
+
import { {{name.pascal}}Command } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.command{{alias.ext}}';
|
|
20
|
+
import { {{name.pascal}}Handler } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.handler{{alias.ext}}';
|
|
21
|
+
{{#if access.protected}}
|
|
22
|
+
|
|
23
|
+
const holders = authorizationMiddleware({
|
|
24
|
+
authorizer: new RoleBasedAuthorizer({
|
|
25
|
+
roles: { holder: [{{context.camel}}Permissions['{{access.permission}}']] },
|
|
26
|
+
}),
|
|
27
|
+
map: AuthorizationMap.of(requires({{name.pascal}}Command, {{context.camel}}Permissions['{{access.permission}}'])),
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
const holder = anActor().withRole('holder').build();
|
|
31
|
+
{{/if}}
|
|
32
|
+
|
|
33
|
+
const PDF = '%PDF-1.7\n%%EOF\n';
|
|
34
|
+
|
|
35
|
+
describe('{{name.title}}', () => {
|
|
36
|
+
it('keeps the file under a key of its own, visible once the command has committed', async () => {
|
|
37
|
+
const { commandBus, storage } = arrange();
|
|
38
|
+
|
|
39
|
+
const key = await commandBus.execute(new {{name.pascal}}Command({{#if access.protected}}holder, {{/if}}aFile()));
|
|
40
|
+
|
|
41
|
+
expect(storage.keys()).toEqual([key]);
|
|
42
|
+
expect(await storage.stat(key)).toMatchObject({ size: PDF.length, mediaType: 'application/pdf' });
|
|
43
|
+
});
|
|
44
|
+
{{#if access.protected}}
|
|
45
|
+
|
|
46
|
+
it('is refused to an actor who does not hold the permission {{access.permission}}, and keeps nothing', async () => {
|
|
47
|
+
const { commandBus, storage } = arrange();
|
|
48
|
+
const command = new {{name.pascal}}Command(anActor().build(), aFile());
|
|
49
|
+
|
|
50
|
+
await expect(commandBus.execute(command)).rejects.toMatchObject({ code: 'FORBIDDEN' });
|
|
51
|
+
expect(storage.keys()).toEqual([]);
|
|
52
|
+
});
|
|
53
|
+
{{/if}}
|
|
54
|
+
|
|
55
|
+
// TODO(domain): replace each entry below by a test named after the business behaviour of "{{name.words}}": the reference recorded on the aggregate, the refusal that rolls the command back and leaves only a staged file.
|
|
56
|
+
it.todo('records the reference of the file on the aggregate it is attached to');
|
|
57
|
+
it.todo('leaves no visible file when the command is refused');
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
function aFile() {
|
|
61
|
+
return { fileName: 'scan.pdf', mediaType: 'application/pdf', stream: Readable.from([Buffer.from(PDF)]) };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
function arrange() {
|
|
65
|
+
const { commandBus, clock, ids } = createTestBuses({ idPrefix: 'file' });
|
|
66
|
+
const storage = new InMemoryFileStorage({ clock });
|
|
67
|
+
const staging = new FileStaging(storage, new AcceptEveryFileScanner(), new InMemoryErrorReporter());
|
|
68
|
+
|
|
69
|
+
const keys = new TenantFileKeys(ids, clock);
|
|
70
|
+
|
|
71
|
+
commandBus
|
|
72
|
+
{{#if access.protected}}
|
|
73
|
+
.use(holders)
|
|
74
|
+
{{/if}}
|
|
75
|
+
.use(fileUploadsMiddleware(() => ({ keys, staging })))
|
|
76
|
+
.register({{name.pascal}}Command, new {{name.pascal}}Handler(staging));
|
|
77
|
+
|
|
78
|
+
return { commandBus, storage };
|
|
79
|
+
}
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { CommandHandler, FileStaging } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import type { {{name.pascal}}Command } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.command{{alias.ext}}';
|
|
3
|
+
|
|
4
|
+
export class {{name.pascal}}Handler implements CommandHandler<{{name.pascal}}Command> {
|
|
5
|
+
constructor(private readonly staging: FileStaging) {}
|
|
6
|
+
|
|
7
|
+
/** The file arrives staged and scanned; it reaches its key with the commit, and stays staged, then purged, on a rollback. */
|
|
8
|
+
async execute(command: {{name.pascal}}Command): Promise<string> {
|
|
9
|
+
const { file } = command.upload;
|
|
10
|
+
|
|
11
|
+
// TODO(domain): record `file` (key, size, media type, checksum: never the content) on the aggregate "{{name.words}}" attaches it to, and save that aggregate here: the reference commits with the file.
|
|
12
|
+
await this.staging.promoteOnCommit(file.key);
|
|
13
|
+
|
|
14
|
+
return file.key;
|
|
15
|
+
}
|
|
16
|
+
}
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
@@imports
|
|
2
|
+
{{access.manualImport}}
|
|
3
|
+
{{access.permissionsImport}}
|
|
4
|
+
import { {{name.pascal}}Command } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.command{{alias.ext}}';
|
|
5
|
+
import { {{name.pascal}}Handler } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.handler{{alias.ext}}';
|
|
6
|
+
@@registrations
|
|
7
|
+
platform.commands.register({{name.pascal}}Command, new {{name.pascal}}Handler(platform.files.staging), { context: '{{context.kebab}}' });
|
|
8
|
+
@@authorization
|
|
9
|
+
{{access.manualDeclaration}}
|
|
@@ -0,0 +1,7 @@
|
|
|
1
|
+
@@imports
|
|
2
|
+
import { FileStaging } from '@ddd-ts-kit/kernel';
|
|
3
|
+
{{access.permissionsImport}}
|
|
4
|
+
import { {{name.pascal}}Command } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.command{{alias.ext}}';
|
|
5
|
+
import { {{name.pascal}}Handler } from '{{alias.context}}/application/commands/{{name.kebab}}/{{name.kebab}}.handler{{alias.ext}}';
|
|
6
|
+
@@registrations
|
|
7
|
+
builder.command({{name.pascal}}Command){{access.declaration}}.handledBy({{name.pascal}}Handler, [FileStaging]);
|
|
@@ -29,7 +29,7 @@ Never edit `<context>.declarations.ts`: `generate`, `docs` and `npm run dev` rew
|
|
|
29
29
|
{{#if assembly.discovery}}
|
|
30
30
|
Never edit `<context>.dependencies.ts`: `generate`, `docs` and `npm run dev` rewrite it from the constructor signatures (it registers what each constructor is injected). After changing a constructor by hand, `npx ddd-ts-kit docs`.
|
|
31
31
|
{{/if}}
|
|
32
|
-
Never edit the contracts under `openapi/` (`openapi/openapi.yaml`, one file per document when `openapi.documents` splits it), `docs/integration/asyncapi.yaml`, `docs/integration/event-catalog.md`, `{{config.platformDir}}/http/openapi-documents.ts`, `.dependency-cruiser.cjs` or the generated sections of a `CONTEXT.md`: change the code or the config, then `npm run docs:generate`, `npx ddd-ts-kit docs` and `npx ddd-ts-kit depcruise-config --write`.
|
|
32
|
+
Never edit the contracts under `openapi/` (`openapi/openapi.yaml`, one file per document when `openapi.documents` splits it), `docs/integration/asyncapi.yaml`, `docs/integration/event-catalog.md`, `{{config.platformDir}}/http/openapi-documents.ts`, `.dependency-cruiser.cjs` or the generated sections of a `CONTEXT.md`: change the code or the config, then `npm run docs:generate`, `npx ddd-ts-kit docs` and `npx ddd-ts-kit depcruise-config --write`. `stored-events.lock.json` is written by `npx ddd-ts-kit docs --stored-events` once the shape of a version is final (`docs --check` fails while one is unrecorded), never by hand: a stored shape changes only by a new version with its upcaster (`event-sourcing/upcaster-required`).
|
|
33
33
|
{{#if sdk.on}}
|
|
34
34
|
{{sdk.instruction}}
|
|
35
35
|
{{/if}}
|
|
@@ -163,6 +163,28 @@ and `execution.scheduledAt` says which period the work is for.
|
|
|
163
163
|
on the API), or initialise the project with `--cache redis`, which binds `RedisLock`, or bind a
|
|
164
164
|
lock between processes of your own to `Lock` in the platform.
|
|
165
165
|
{{/unless}}
|
|
166
|
+
{{#if files.any}}
|
|
167
|
+
|
|
168
|
+
## Files
|
|
169
|
+
|
|
170
|
+
The content of a file lives in a file storage, never in the database: {{#if files.local}}a directory
|
|
171
|
+
(`FILES_DIRECTORY`, `.data/files` by default, kept out of version control) whose signed links the
|
|
172
|
+
application answers under `/files`; before running several instances, move to an object store
|
|
173
|
+
with `npx ddd-ts-kit init --force --storage s3`{{/if}}{{#if files.s3}}a bucket of the S3 protocol (`S3_*` variables), MinIO
|
|
174
|
+
in `docker-compose.yml` while developing, which creates the bucket at its first start{{/if}}. The tests keep files in memory. An
|
|
175
|
+
aggregate keeps the `StoredFile` reference (key, size, media type, checksum). The command bus
|
|
176
|
+
stages a file a command carries before the transaction opens, `FileStaging` moves it to its key
|
|
177
|
+
with the commit and, every hour, purges what refused or rolled-back commands left in `staging/`. `npx ddd-ts-kit generate file-upload <context> <UseCase>` writes an
|
|
178
|
+
upload; see https://github.com/gbourgeat/ddd-ts-kit/blob/main/docs/concepts/file-storage.md.
|
|
179
|
+
{{#if files.local}}
|
|
180
|
+
|
|
181
|
+
| Variable | Says |
|
|
182
|
+
|---|---|
|
|
183
|
+
| `FILES_DIRECTORY` | the directory of the files, `.data/files` by default |
|
|
184
|
+
| `FILES_PUBLIC_URL` | where clients reach `/files`, which signed links point to: required in production, `http://localhost:<PORT>/files` elsewhere |
|
|
185
|
+
| `FILES_SIGNING_SECRET` | signs the links, 32 characters at least: required in production, even before the first file arrives; drawn at each start elsewhere |
|
|
186
|
+
{{/if}}
|
|
187
|
+
{{/if}}
|
|
166
188
|
{{#if errors.sentry}}
|
|
167
189
|
|
|
168
190
|
## Error reporting
|
|
@@ -118,6 +118,28 @@ services:
|
|
|
118
118
|
timeout: 3s
|
|
119
119
|
retries: 30
|
|
120
120
|
|
|
121
|
+
{{/if}}
|
|
122
|
+
{{#if files.s3}}
|
|
123
|
+
# MinIO, an S3 server for local use (pgsty/minio builds it from the upstream sources, which no longer publish images).
|
|
124
|
+
# The bucket of S3_BUCKET is created at the first start; its root account is for local development ONLY.
|
|
125
|
+
minio:
|
|
126
|
+
image: pgsty/minio:RELEASE.2026-08-04T00-00-00Z@sha256:b6bfe7239bfc83fb90d31612d9704d86039dd714f7904b3f1ad68f211e602372
|
|
127
|
+
entrypoint: ["sh", "-c"]
|
|
128
|
+
command: ["mkdir -p /data/$${S3_BUCKET} && exec minio server /data"]
|
|
129
|
+
environment:
|
|
130
|
+
MINIO_ROOT_USER: ${S3_ACCESS_KEY_ID:-minio-app}
|
|
131
|
+
MINIO_ROOT_PASSWORD: ${S3_SECRET_ACCESS_KEY:-minio-app-secret}
|
|
132
|
+
S3_BUCKET: ${S3_BUCKET:-{{project.name}}-files}
|
|
133
|
+
ports:
|
|
134
|
+
- "127.0.0.1:${MINIO_PORT:-9000}:9000"
|
|
135
|
+
volumes:
|
|
136
|
+
- minio-data:/data
|
|
137
|
+
healthcheck:
|
|
138
|
+
test: ["CMD", "curl", "-fsS", "-o", "/dev/null", "http://127.0.0.1:9000/minio/health/ready"]
|
|
139
|
+
interval: 2s
|
|
140
|
+
timeout: 3s
|
|
141
|
+
retries: 30
|
|
142
|
+
|
|
121
143
|
{{/if}}
|
|
122
144
|
{{#if authn.keycloak}}
|
|
123
145
|
# Keycloak {{keycloak.version}} in development mode, for local use only: it imports docker/keycloak/{{project.name}}-realm.json
|
|
@@ -232,3 +254,6 @@ volumes:
|
|
|
232
254
|
{{#if errors.sentry}}
|
|
233
255
|
glitchtip-postgres:
|
|
234
256
|
{{/if}}
|
|
257
|
+
{{#if files.s3}}
|
|
258
|
+
minio-data:
|
|
259
|
+
{{/if}}
|
|
@@ -126,6 +126,27 @@ KEYCLOAK_AUDIENCE={{project.name}}-api
|
|
|
126
126
|
# SENTRY_SCRUB_MESSAGES=false
|
|
127
127
|
GLITCHTIP_PORT=8000
|
|
128
128
|
{{/if}}
|
|
129
|
+
{{#if files.local}}
|
|
130
|
+
|
|
131
|
+
# Files live in a directory, never in the database; the application answers their signed links under /files.
|
|
132
|
+
# FILES_DIRECTORY=.data/files
|
|
133
|
+
# Where clients reach /files: required in production, this value by default elsewhere.
|
|
134
|
+
# FILES_PUBLIC_URL=http://localhost:3000/files
|
|
135
|
+
# Signs the links, 32 characters at least: required in production (openssl rand -hex 32), drawn at each start elsewhere.
|
|
136
|
+
# FILES_SIGNING_SECRET=
|
|
137
|
+
{{/if}}
|
|
138
|
+
{{#if files.s3}}
|
|
139
|
+
|
|
140
|
+
# Files live in a bucket of the S3 protocol (MinIO in docker-compose.yml), never in the database.
|
|
141
|
+
# For AWS leave S3_ENDPOINT out; OVHcloud, Scaleway and Cloudflare R2 give their own endpoint (S3_REGION=auto for R2).
|
|
142
|
+
S3_ENDPOINT=http://localhost:9000
|
|
143
|
+
S3_REGION=us-east-1
|
|
144
|
+
S3_BUCKET={{project.name}}-files
|
|
145
|
+
# The root account of the local MinIO, for local development ONLY: in a deployment, an access key allowed on this bucket only.
|
|
146
|
+
S3_ACCESS_KEY_ID=minio-app
|
|
147
|
+
S3_SECRET_ACCESS_KEY=minio-app-secret
|
|
148
|
+
MINIO_PORT=9000
|
|
149
|
+
{{/if}}
|
|
129
150
|
|
|
130
151
|
# Traces: start Jaeger with `docker compose --profile observability up -d jaeger`, then open http://127.0.0.1:16686
|
|
131
152
|
# Without an endpoint no span leaves the process.
|
|
@@ -14,6 +14,9 @@ import { {{events.providerName}} } from '{{alias.platform}}/events/{{events.prov
|
|
|
14
14
|
{{#if events.outbox}}
|
|
15
15
|
import { startOutboxRelay } from '{{alias.platform}}/events/start-outbox-relay{{alias.ext}}';
|
|
16
16
|
{{/if}}
|
|
17
|
+
{{#if files.any}}
|
|
18
|
+
import { openFiles } from '{{alias.platform}}/files/open-files{{alias.ext}}';
|
|
19
|
+
{{/if}}
|
|
17
20
|
{{#if http.any}}
|
|
18
21
|
import { startHttpServer } from '{{alias.platform}}/http/start-http-server{{alias.ext}}';
|
|
19
22
|
{{/if}}
|
|
@@ -48,6 +51,9 @@ const application = composeApplication(persistence, {
|
|
|
48
51
|
{{#if cache.redis}}
|
|
49
52
|
redis,
|
|
50
53
|
{{/if}}
|
|
54
|
+
{{#if files.any}}
|
|
55
|
+
files: openFiles(config.files),
|
|
56
|
+
{{/if}}
|
|
51
57
|
});
|
|
52
58
|
|
|
53
59
|
await application.start();
|
|
@@ -1,12 +1,23 @@
|
|
|
1
1
|
import type { Authenticator } from '@ddd-ts-kit/auth';
|
|
2
2
|
{{#if errors.none}}
|
|
3
|
-
import type { ErrorContext, ErrorMapper, {{#if audit.on}}GuardRefusal, {{/if}}RouteAround } from '@ddd-ts-kit/http';
|
|
3
|
+
import type { ErrorContext, ErrorMapper, {{#if audit.on}}GuardRefusal, {{/if}}RouteAround{{#if files.http}}, UploadReader{{/if}} } from '@ddd-ts-kit/http';
|
|
4
4
|
import type { Clock, ExecutionScope, Logger, Session } from '@ddd-ts-kit/kernel';
|
|
5
5
|
{{/if}}
|
|
6
6
|
{{#if errors.sentry}}
|
|
7
7
|
{{#unless audit.on}}
|
|
8
|
+
{{#unless files.http}}
|
|
8
9
|
import { reportHttpErrors, type ErrorContext, type ErrorMapper, type RouteAround } from '@ddd-ts-kit/http';
|
|
9
10
|
{{/unless}}
|
|
11
|
+
{{#if files.http}}
|
|
12
|
+
import {
|
|
13
|
+
reportHttpErrors,
|
|
14
|
+
type ErrorContext,
|
|
15
|
+
type ErrorMapper,
|
|
16
|
+
type RouteAround,
|
|
17
|
+
type UploadReader,
|
|
18
|
+
} from '@ddd-ts-kit/http';
|
|
19
|
+
{{/if}}
|
|
20
|
+
{{/unless}}
|
|
10
21
|
{{#if audit.on}}
|
|
11
22
|
import {
|
|
12
23
|
reportHttpErrors,
|
|
@@ -14,10 +25,16 @@ import {
|
|
|
14
25
|
type ErrorMapper,
|
|
15
26
|
type GuardRefusal,
|
|
16
27
|
type RouteAround,
|
|
28
|
+
{{#if files.http}}
|
|
29
|
+
type UploadReader,
|
|
30
|
+
{{/if}}
|
|
17
31
|
} from '@ddd-ts-kit/http';
|
|
18
32
|
{{/if}}
|
|
19
33
|
import type { Clock, ErrorReporter, ExecutionScope, Logger, Session } from '@ddd-ts-kit/kernel';
|
|
20
34
|
{{/if}}
|
|
35
|
+
{{#if files.http}}
|
|
36
|
+
import { multipartUploads } from '@ddd-ts-kit/http/uploads';
|
|
37
|
+
{{/if}}
|
|
21
38
|
import { tracedRoutes } from '@ddd-ts-kit/tracing';
|
|
22
39
|
import { principalOf } from '{{alias.platform}}/auth/principal-of{{alias.ext}}';
|
|
23
40
|
|
|
@@ -32,6 +49,10 @@ export interface HttpBoundary {
|
|
|
32
49
|
readonly onError: (error: unknown, context: ErrorContext) => void;
|
|
33
50
|
/** The span of a request, named by its route template; probes and documentation routes make none. */
|
|
34
51
|
readonly around: RouteAround;
|
|
52
|
+
{{#if files.http}}
|
|
53
|
+
/** Reads the file of a controller that declares an upload, as it streams: its size and media types are checked on the way. */
|
|
54
|
+
readonly uploads: UploadReader;
|
|
55
|
+
{{/if}}
|
|
35
56
|
{{#if audit.on}}
|
|
36
57
|
/** A request a guard refused at the edge, written to the audit trail as a denied decision. */
|
|
37
58
|
readonly onGuardRefusal: (refusal: GuardRefusal) => Promise<void>;
|
|
@@ -91,6 +112,9 @@ export function httpBoundary({
|
|
|
91
112
|
errorMapper,
|
|
92
113
|
clock,
|
|
93
114
|
around: tracedRoutes(),
|
|
115
|
+
{{#if files.http}}
|
|
116
|
+
uploads: multipartUploads(),
|
|
117
|
+
{{/if}}
|
|
94
118
|
{{#if audit.on}}
|
|
95
119
|
onGuardRefusal: refusals,
|
|
96
120
|
{{/if}}
|
|
@@ -31,6 +31,9 @@ import {
|
|
|
31
31
|
} from '@ddd-ts-kit/kernel';
|
|
32
32
|
{{/unless}}
|
|
33
33
|
{{/if}}
|
|
34
|
+
{{#if files.any}}
|
|
35
|
+
import { stagingUploads } from '{{alias.platform}}/files/staging-uploads{{alias.ext}}';
|
|
36
|
+
{{/if}}
|
|
34
37
|
import type { Foundations } from '{{alias.platform}}/lay-foundations{{alias.ext}}';
|
|
35
38
|
{{#if cache.relaySignal}}
|
|
36
39
|
import type { RedisServices } from '{{alias.platform}}/redis/open-redis{{alias.ext}}';
|
|
@@ -48,7 +51,7 @@ interface Signalled {
|
|
|
48
51
|
* Around every command, outermost first: the report of an unexpected failure (a failed commit
|
|
49
52
|
* included), the log entry and the measure of the use case, the
|
|
50
53
|
{{/if}}
|
|
51
|
-
* permission, the span of the use case (a refused command never started){{#if cache.relaySignal}}, the wake-up of the outbox relay{{/if}}{{#if audit.on}}, the audit entry{{/if}}, then the unit of work. The guard and the unit of
|
|
54
|
+
* permission, the span of the use case (a refused command never started){{#if files.any}}, the staging of the file it carries{{/if}}{{#if cache.relaySignal}}, the wake-up of the outbox relay{{/if}}{{#if audit.on}}, the audit entry{{/if}}, then the unit of work. The guard and the unit of
|
|
52
55
|
* work are resolved at each command: building the bus must not open anything.{{#if audit.on}} The audit entry
|
|
53
56
|
* opens the transaction itself, so that the entry of a success commits with the state change
|
|
54
57
|
* and the entry of a failure survives the rollback.{{/if}}
|
|
@@ -69,6 +72,9 @@ export function createCommandBus(resolve: Resolve, foundations: Foundations): In
|
|
|
69
72
|
.use(metricsMiddleware(foundations.metrics))
|
|
70
73
|
.use((message, next, info) => resolve(UseCaseGuard).check(message, next, info))
|
|
71
74
|
.use(foundations.traced.useCase)
|
|
75
|
+
{{#if files.any}}
|
|
76
|
+
.use(stagingUploads(resolve))
|
|
77
|
+
{{/if}}
|
|
72
78
|
{{#if cache.relaySignal}}
|
|
73
79
|
.use(wakingTheRelay(options.redis))
|
|
74
80
|
{{/if}}
|
|
@@ -11,6 +11,9 @@ import { subscribeAll } from '@ddd-ts-kit/events';
|
|
|
11
11
|
{{#if http.any}}
|
|
12
12
|
import { createErrorMapper, defineRoutes, type Route } from '@ddd-ts-kit/http';
|
|
13
13
|
{{/if}}
|
|
14
|
+
{{#if files.any}}
|
|
15
|
+
import { PurgeStagedFiles } from '@ddd-ts-kit/kernel';
|
|
16
|
+
{{/if}}
|
|
14
17
|
{{#if cache.rateLimit}}
|
|
15
18
|
import type { IntegrationEventPublisher, IntegrationEventSubscriber, Logger, RateLimiter } from '@ddd-ts-kit/kernel';
|
|
16
19
|
{{/if}}
|
|
@@ -28,6 +31,9 @@ import { relateSystemActors } from '{{alias.platform}}/auth/relate-system-actors
|
|
|
28
31
|
{{/if}}
|
|
29
32
|
import { createPlatform, type PlatformOptions } from '{{alias.platform}}/create-platform{{alias.ext}}';
|
|
30
33
|
import type { Persistence } from '{{alias.platform}}/database/persistence{{alias.ext}}';
|
|
34
|
+
{{#if files.http}}
|
|
35
|
+
import { fileRoutes } from '{{alias.platform}}/files/file-routes{{alias.ext}}';
|
|
36
|
+
{{/if}}
|
|
31
37
|
import type { Platform } from '{{alias.platform}}/platform{{alias.ext}}';
|
|
32
38
|
// ddd-ts-kit:imports
|
|
33
39
|
// /ddd-ts-kit:imports
|
|
@@ -65,6 +71,10 @@ export interface Application {
|
|
|
65
71
|
/** Authenticates every request and runs it inside the execution scope. */
|
|
66
72
|
readonly boundary: HttpBoundary;
|
|
67
73
|
{{/if}}
|
|
74
|
+
{{#if files.http}}
|
|
75
|
+
/** The routes that answer the signed links of a storage the application serves itself; mounted before any body parser. */
|
|
76
|
+
readonly fileRoutes: readonly Route[];
|
|
77
|
+
{{/if}}
|
|
68
78
|
{{#unless http.any}}
|
|
69
79
|
readonly routes: readonly never[];
|
|
70
80
|
readonly contexts: readonly ContextWiring[];
|
|
@@ -101,6 +111,9 @@ export function composeApplication(
|
|
|
101
111
|
const { logger, metrics, cache, clock } = platform;
|
|
102
112
|
|
|
103
113
|
installAspectRuntime({ logger, metrics, cache, clock });
|
|
114
|
+
{{#if files.any}}
|
|
115
|
+
purgeStagedFiles(platform);
|
|
116
|
+
{{/if}}
|
|
104
117
|
|
|
105
118
|
{{#if audit.route}}
|
|
106
119
|
const contexts = [...selection, auditModule].map((module) => module(platform));
|
|
@@ -132,6 +145,9 @@ export function composeApplication(
|
|
|
132
145
|
routes: [],
|
|
133
146
|
contexts,
|
|
134
147
|
{{/unless}}
|
|
148
|
+
{{#if files.http}}
|
|
149
|
+
fileRoutes: fileRoutes(platform.files.storage),
|
|
150
|
+
{{/if}}
|
|
135
151
|
eventBus: platform.eventBus,
|
|
136
152
|
{{#if broker.external}}
|
|
137
153
|
// Behind a broker a listener has queues of its own, under its own name.
|
|
@@ -188,3 +204,12 @@ async function stop(platform: Platform, options: ApplicationOptions): Promise<vo
|
|
|
188
204
|
{{/if}}
|
|
189
205
|
await platform.logger.flush();
|
|
190
206
|
}
|
|
207
|
+
{{#if files.any}}
|
|
208
|
+
|
|
209
|
+
/** What rolled-back commands left in `staging/`, removed every hour by the process that runs the jobs. */
|
|
210
|
+
function purgeStagedFiles(platform: Platform): void {
|
|
211
|
+
const purge = new PurgeStagedFiles(platform.files.staging, platform.clock);
|
|
212
|
+
|
|
213
|
+
platform.jobs.register({ name: 'files.purge-staged', schedule: { every: '1h' }, run: () => purge.run() });
|
|
214
|
+
}
|
|
215
|
+
{{/if}}
|