ddd-ts-kit 0.92.0 → 0.94.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.
Files changed (176) hide show
  1. package/CHANGELOG.md +90 -0
  2. package/README.fr.md +13 -8
  3. package/README.md +10 -6
  4. package/dist/acceptance/check-locked-files.d.ts +25 -0
  5. package/dist/acceptance/check-locked-files.d.ts.map +1 -0
  6. package/dist/acceptance/check-locked-files.js +84 -0
  7. package/dist/acceptance/check-locked-files.js.map +1 -0
  8. package/dist/acceptance/scenarios-in.d.ts +4 -0
  9. package/dist/acceptance/scenarios-in.d.ts.map +1 -1
  10. package/dist/acceptance/scenarios-in.js +2 -2
  11. package/dist/acceptance/scenarios-in.js.map +1 -1
  12. package/dist/check/authz/modelled-permissions.d.ts +32 -0
  13. package/dist/check/authz/modelled-permissions.d.ts.map +1 -0
  14. package/dist/check/authz/modelled-permissions.js +169 -0
  15. package/dist/check/authz/modelled-permissions.js.map +1 -0
  16. package/dist/check/catalogue/architecture-rules.d.ts.map +1 -1
  17. package/dist/check/catalogue/architecture-rules.js +4 -0
  18. package/dist/check/catalogue/architecture-rules.js.map +1 -1
  19. package/dist/check/explain/authz-docs.d.ts.map +1 -1
  20. package/dist/check/explain/authz-docs.js +17 -0
  21. package/dist/check/explain/authz-docs.js.map +1 -1
  22. package/dist/check/explain/scheduling-docs.d.ts.map +1 -1
  23. package/dist/check/explain/scheduling-docs.js +7 -0
  24. package/dist/check/explain/scheduling-docs.js.map +1 -1
  25. package/dist/check/fixtures/authz-resource-modelled-fixtures.d.ts +3 -0
  26. package/dist/check/fixtures/authz-resource-modelled-fixtures.d.ts.map +1 -0
  27. package/dist/check/fixtures/authz-resource-modelled-fixtures.js +194 -0
  28. package/dist/check/fixtures/authz-resource-modelled-fixtures.js.map +1 -0
  29. package/dist/check/fixtures/fixtures.d.ts.map +1 -1
  30. package/dist/check/fixtures/fixtures.js +2 -0
  31. package/dist/check/fixtures/fixtures.js.map +1 -1
  32. package/dist/check/fixtures/scheduling-fixtures.d.ts.map +1 -1
  33. package/dist/check/fixtures/scheduling-fixtures.js +69 -1
  34. package/dist/check/fixtures/scheduling-fixtures.js.map +1 -1
  35. package/dist/check/rules/authz-resource-modelled.d.ts +13 -0
  36. package/dist/check/rules/authz-resource-modelled.d.ts.map +1 -0
  37. package/dist/check/rules/authz-resource-modelled.js +128 -0
  38. package/dist/check/rules/authz-resource-modelled.js.map +1 -0
  39. package/dist/check/rules/scheduling-tenancy-declared.d.ts +3 -0
  40. package/dist/check/rules/scheduling-tenancy-declared.d.ts.map +1 -0
  41. package/dist/check/rules/scheduling-tenancy-declared.js +45 -0
  42. package/dist/check/rules/scheduling-tenancy-declared.js.map +1 -0
  43. package/dist/check/scheduling/job-names.d.ts +1 -1
  44. package/dist/check/scheduling/job-names.d.ts.map +1 -1
  45. package/dist/check/scheduling/job-names.js +3 -21
  46. package/dist/check/scheduling/job-names.js.map +1 -1
  47. package/dist/check/scheduling/jobs-declared.d.ts +11 -0
  48. package/dist/check/scheduling/jobs-declared.d.ts.map +1 -0
  49. package/dist/check/scheduling/jobs-declared.js +24 -0
  50. package/dist/check/scheduling/jobs-declared.js.map +1 -0
  51. package/dist/commands/acceptance.d.ts.map +1 -1
  52. package/dist/commands/acceptance.js +14 -0
  53. package/dist/commands/acceptance.js.map +1 -1
  54. package/dist/commands/generate.js +2 -0
  55. package/dist/commands/generate.js.map +1 -1
  56. package/dist/commit/run-git.d.ts +5 -1
  57. package/dist/commit/run-git.d.ts.map +1 -1
  58. package/dist/commit/run-git.js +19 -2
  59. package/dist/commit/run-git.js.map +1 -1
  60. package/dist/controllers/finish-migration.js +4 -2
  61. package/dist/controllers/finish-migration.js.map +1 -1
  62. package/dist/docs/behaviour-page.d.ts.map +1 -1
  63. package/dist/docs/behaviour-page.js +9 -1
  64. package/dist/docs/behaviour-page.js.map +1 -1
  65. package/dist/generate/access-vars.js +18 -0
  66. package/dist/generate/access-vars.js.map +1 -1
  67. package/dist/generate/kind.d.ts +2 -0
  68. package/dist/generate/kind.d.ts.map +1 -1
  69. package/dist/generate/kinds/job/grant-system-actor.js +2 -2
  70. package/dist/generate/kinds/job/grant-system-actor.js.map +1 -1
  71. package/dist/generate/kinds/job/job-vars.d.ts +4 -1
  72. package/dist/generate/kinds/job/job-vars.d.ts.map +1 -1
  73. package/dist/generate/kinds/job/job-vars.js +11 -1
  74. package/dist/generate/kinds/job/job-vars.js.map +1 -1
  75. package/dist/generate/kinds/job-kind.js +20 -3
  76. package/dist/generate/kinds/job-kind.js.map +1 -1
  77. package/dist/generate/kinds/service-kind.d.ts.map +1 -1
  78. package/dist/generate/kinds/service-kind.js +30 -3
  79. package/dist/generate/kinds/service-kind.js.map +1 -1
  80. package/dist/init/files/inert-persistence-file.d.ts +5 -0
  81. package/dist/init/files/inert-persistence-file.d.ts.map +1 -0
  82. package/dist/init/files/inert-persistence-file.js +16 -0
  83. package/dist/init/files/inert-persistence-file.js.map +1 -0
  84. package/dist/init/files/platform-files.js +7 -1
  85. package/dist/init/files/platform-files.js.map +1 -1
  86. package/dist/init/manifest/versions.js +24 -24
  87. package/dist/measures/branch-commits.d.ts +4 -2
  88. package/dist/measures/branch-commits.d.ts.map +1 -1
  89. package/dist/measures/branch-commits.js +32 -8
  90. package/dist/measures/branch-commits.js.map +1 -1
  91. package/dist/measures/timeline-of.d.ts +3 -2
  92. package/dist/measures/timeline-of.d.ts.map +1 -1
  93. package/dist/measures/timeline-of.js +37 -8
  94. package/dist/measures/timeline-of.js.map +1 -1
  95. package/dist/platform-layout/platform-layout.d.ts +3 -0
  96. package/dist/platform-layout/platform-layout.d.ts.map +1 -1
  97. package/dist/platform-layout/platform-layout.js +3 -0
  98. package/dist/platform-layout/platform-layout.js.map +1 -1
  99. package/dist/presets/apply-preset.d.ts.map +1 -1
  100. package/dist/presets/apply-preset.js +4 -0
  101. package/dist/presets/apply-preset.js.map +1 -1
  102. package/dist/presets/no-flags.d.ts.map +1 -1
  103. package/dist/presets/no-flags.js +1 -0
  104. package/dist/presets/no-flags.js.map +1 -1
  105. package/dist/presets/organisations-as-tenant-directory.d.ts +10 -0
  106. package/dist/presets/organisations-as-tenant-directory.d.ts.map +1 -0
  107. package/dist/presets/organisations-as-tenant-directory.js +16 -0
  108. package/dist/presets/organisations-as-tenant-directory.js.map +1 -0
  109. package/dist/progress/build-next-of.d.ts +9 -0
  110. package/dist/progress/build-next-of.d.ts.map +1 -0
  111. package/dist/progress/build-next-of.js +22 -0
  112. package/dist/progress/build-next-of.js.map +1 -0
  113. package/dist/progress/format-progress.d.ts.map +1 -1
  114. package/dist/progress/format-progress.js +3 -0
  115. package/dist/progress/format-progress.js.map +1 -1
  116. package/dist/progress/progress.d.ts +3 -1
  117. package/dist/progress/progress.d.ts.map +1 -1
  118. package/dist/progress/read-progress.d.ts.map +1 -1
  119. package/dist/progress/read-progress.js +7 -2
  120. package/dist/progress/read-progress.js.map +1 -1
  121. package/dist/version.d.ts +1 -1
  122. package/dist/version.js +1 -1
  123. package/package.json +4 -4
  124. package/templates/architecture/extractable.declared.spec.ts.tpl +2 -2
  125. package/templates/architecture/extractable.manual.spec.ts.tpl +2 -2
  126. package/templates/architecture/extractable.tsyringe.spec.ts.tpl +2 -2
  127. package/templates/architecture/hosting.declared.spec.ts.tpl +69 -0
  128. package/templates/architecture/hosting.manual.spec.ts.tpl +57 -0
  129. package/templates/architecture/hosting.tsyringe.spec.ts.tpl +69 -0
  130. package/templates/command/command.ts.tpl +3 -0
  131. package/templates/file-upload/command.ts.tpl +3 -0
  132. package/templates/init/docker/env.example.tpl +9 -0
  133. package/templates/init/gitignore.tpl +3 -0
  134. package/templates/init/main.ts.tpl +27 -20
  135. package/templates/init/platform/audit/audit.module.tsyringe.ts.tpl +11 -3
  136. package/templates/init/platform/auth/relationships/relationship-schema.ts.tpl +24 -9
  137. package/templates/init/platform/composition/compose-application.manual.ts.tpl +25 -4
  138. package/templates/init/platform/composition/compose-application.tsyringe.ts.tpl +47 -65
  139. package/templates/init/platform/composition/context-selection.tsyringe.ts.tpl +17 -0
  140. package/templates/init/platform/composition/lay-foundations.manual.ts.tpl +13 -2
  141. package/templates/init/platform/composition/platform-options.manual.ts.tpl +9 -0
  142. package/templates/init/platform/composition/routes-of.tsyringe.ts.tpl +51 -0
  143. package/templates/init/platform/composition/tenant-directory.ts.tpl +14 -0
  144. package/templates/init/platform/config/app-config.ts.tpl +7 -0
  145. package/templates/init/platform/config/load-config.ts.tpl +25 -0
  146. package/templates/init/platform/files/files.module.tsyringe.ts.tpl +6 -2
  147. package/templates/init/platform/jobs/job-settings.ts.tpl +13 -3
  148. package/templates/init/platform/privacy/privacy.module.tsyringe.ts.tpl +9 -6
  149. package/templates/init/platform/redis/open-redis.ts.tpl +13 -8
  150. package/templates/init/scripts/privacy-erase.manual.ts.tpl +18 -8
  151. package/templates/init/scripts/privacy-erase.tsyringe.ts.tpl +18 -8
  152. package/templates/init/scripts/privacy-export.manual.ts.tpl +17 -7
  153. package/templates/init/scripts/privacy-export.tsyringe.ts.tpl +17 -7
  154. package/templates/job/job.declared.ts.tpl +1 -1
  155. package/templates/job/job.spec.buses.ts.tpl +3 -0
  156. package/templates/job/job.spec.tsyringe.ts.tpl +29 -1
  157. package/templates/job/wiring.manual.tpl +3 -0
  158. package/templates/job/wiring.tsyringe.tpl +1 -1
  159. package/templates/pipeline/biome.json.tpl +7 -0
  160. package/templates/pipeline/github/workflows/ci.yml.tpl +2 -0
  161. package/templates/pipeline/gitlab/gitlab-ci.yml.tpl +2 -0
  162. package/templates/pipeline/knip.json.tpl +1 -0
  163. package/templates/pipeline/package-scripts.json +1 -1
  164. package/templates/presets/identity/context/infrastructure/persistence/drizzle-organisation-tenant-directory.reader.drizzle.ts.tpl +26 -0
  165. package/templates/presets/identity/context/infrastructure/persistence/knex-organisation-tenant-directory.reader.knex.ts.tpl +30 -0
  166. package/templates/presets/identity/context/infrastructure/persistence/prisma-organisation-tenant-directory.reader.prisma.ts.tpl +25 -0
  167. package/templates/presets/identity/context/test/integration/organisation-tenant-directory.spec.ts.tpl +107 -0
  168. package/templates/presets/identity/platform/tenant-directory.ts.tpl +13 -0
  169. package/templates/query/query.ts.tpl +3 -0
  170. package/templates/service/compose.tpl +1 -1
  171. package/templates/service/extracted-name.tpl +2 -0
  172. package/templates/service/extracted.declared.tpl +6 -0
  173. package/templates/service/extracted.manual.tpl +2 -0
  174. package/templates/service/extracted.tsyringe.tpl +6 -0
  175. package/templates/service/main.ts.tpl +5 -1
  176. package/templates/worker/main.ts.tpl +20 -13
@@ -0,0 +1,69 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { describe, expect, it } from '{{tests.module}}';
3
+ import type { WiringDescription } from '@ddd-ts-kit/di';
4
+ {{#if http.any}}
5
+ import { offlineAuthenticator } from '{{alias.platform}}/auth/authentication/offline-authenticator{{alias.ext}}';
6
+ {{/if}}
7
+ import { composeApplication, everyContext, hostedContexts } from '{{alias.platform}}/composition/compose-application{{alias.ext}}';
8
+ import { inertPersistence } from '{{alias.platform}}/database/inert-persistence{{alias.ext}}';
9
+ import { inProcessEvents } from '{{alias.platform}}/events/in-process-events{{alias.ext}}';
10
+
11
+ /** The contexts run as services of their own: `ddd-ts-kit generate service` adds one. */
12
+ const extracted: readonly string[] = [
13
+ // ddd-ts-kit:extracted
14
+ // /ddd-ts-kit:extracted
15
+ ];
16
+
17
+ const options = {
18
+ events: inProcessEvents(),
19
+ {{#if http.any}}
20
+ authenticator: offlineAuthenticator,
21
+ {{/if}}
22
+ };
23
+
24
+ describe('A context run as a service leaves the whole application', () => {
25
+ // Jest refuses a table without rows: until a context becomes a service, its cases wait.
26
+ if (extracted.length === 0) {
27
+ it.todo('hosts no consumer, process nor job of a context run as a service, once the project has one');
28
+ } else {
29
+ it.each(extracted)('%s: the whole application hosts none of its modules, consumers, processes nor jobs', (name) => {
30
+ const whole = composeApplication(inertPersistence(), options, hostedContexts).container.describe();
31
+
32
+ expect(whole.modules).not.toContain(name);
33
+ expect(registrationsOf(whole, name)).toEqual([]);
34
+ });
35
+
36
+ it.each(extracted)('%s: its service hosts it, each consumer once, and no job of the platform', (name) => {
37
+ const service = composeApplication(
38
+ inertPersistence(),
39
+ { ...options, platformJobs: false },
40
+ { modules: everyContext.modules.filter((module) => module.name === name) },
41
+ ).container.describe();
42
+
43
+ const consumers = service.integrationEventHandlers.map((handler) => `${handler.consumer}:${handler.event}`);
44
+
45
+ expect(service.modules).toContain(name);
46
+ expect(new Set(consumers).size).toBe(consumers.length);
47
+ expect(service.jobs.filter((job) => !job.name.startsWith(name + '.'))).toEqual([]);
48
+ });
49
+
50
+ it.each(extracted)('%s: its entry point leaves the jobs of the platform to the whole application', (name) => {
51
+ expect(readFileSync('{{config.sourceRoot}}/apps/' + name + '/main.ts', 'utf8')).toContain('platformJobs: false');
52
+ });
53
+ }
54
+ });
55
+
56
+ /** What a module registers to be driven by something other than a request: the reactions, the jobs, the projections. */
57
+ function registrationsOf(description: WiringDescription, name: string): readonly string[] {
58
+ return [
59
+ ...description.integrationEventHandlers,
60
+ ...description.domainEventHandlers,
61
+ ...description.projections,
62
+ ...description.jobs,
63
+ ...description.backgroundJobs,
64
+ ...description.commands,
65
+ ...description.queries,
66
+ ]
67
+ .filter(({ module }) => module === name)
68
+ .map((registration) => JSON.stringify(registration));
69
+ }
@@ -0,0 +1,57 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { describe, expect, it } from '{{tests.module}}';
3
+ {{#if http.any}}
4
+ import { offlineAuthenticator } from '{{alias.platform}}/auth/authentication/offline-authenticator{{alias.ext}}';
5
+ {{/if}}
6
+ import { composeApplication, everyContext, hostedContexts } from '{{alias.platform}}/composition/compose-application{{alias.ext}}';
7
+ import { inertPersistence } from '{{alias.platform}}/database/inert-persistence{{alias.ext}}';
8
+ import { inProcessEvents } from '{{alias.platform}}/events/in-process-events{{alias.ext}}';
9
+
10
+ /** The contexts run as services of their own: `ddd-ts-kit generate service` adds one. */
11
+ const extracted: readonly string[] = [
12
+ // ddd-ts-kit:extracted
13
+ // /ddd-ts-kit:extracted
14
+ ];
15
+
16
+ type ContextModule = (typeof everyContext)[number];
17
+
18
+ /** The modules `hostedContexts` leaves out. */
19
+ const services = everyContext.filter((module) => !hostedContexts.includes(module));
20
+
21
+ /** The jobs of the platform are named after its capabilities, and run in the whole application only. */
22
+ const PLATFORM = ['audit', 'privacy', 'files'];
23
+
24
+ const options = {
25
+ events: inProcessEvents(),
26
+ {{#if http.any}}
27
+ authenticator: offlineAuthenticator,
28
+ {{/if}}
29
+ };
30
+
31
+ describe('A context run as a service leaves the whole application', () => {
32
+ // Jest refuses a table without rows: until a context becomes a service, its cases wait.
33
+ if (extracted.length === 0) {
34
+ it.todo('hosts no job of a context run as a service, once the project has one');
35
+ } else {
36
+ it.each(extracted)('%s: the whole application schedules none of its jobs', (name) => {
37
+ const whole = composeApplication(inertPersistence(), options, hostedContexts).platform.jobs.describe();
38
+
39
+ expect(whole.filter((job) => job.name.startsWith(name + '.'))).toEqual([]);
40
+ });
41
+
42
+ it.each(services.map((module, index): [number, ContextModule] => [index + 1, module]))(
43
+ 'extracted context %s: its service schedules the jobs of one context, and none of the platform',
44
+ (_position, module) => {
45
+ const application = composeApplication(inertPersistence(), { ...options, platformJobs: false }, [module]);
46
+ const owners = application.platform.jobs.describe().map((job) => job.name.split('.')[0]);
47
+
48
+ expect(new Set(owners).size).toBeLessThanOrEqual(1);
49
+ expect(owners.filter((owner) => PLATFORM.includes(owner ?? ''))).toEqual([]);
50
+ },
51
+ );
52
+
53
+ it.each(extracted)('%s: its entry point leaves the jobs of the platform to the whole application', (name) => {
54
+ expect(readFileSync('{{config.sourceRoot}}/apps/' + name + '/main.ts', 'utf8')).toContain('platformJobs: false');
55
+ });
56
+ }
57
+ });
@@ -0,0 +1,69 @@
1
+ import { readFileSync } from 'node:fs';
2
+ import { describe, expect, it } from '{{tests.module}}';
3
+ import type { WiringDescription } from '@ddd-ts-kit/di';
4
+ {{#if http.any}}
5
+ import { offlineAuthenticator } from '{{alias.platform}}/auth/authentication/offline-authenticator{{alias.ext}}';
6
+ {{/if}}
7
+ import { composeApplication, everyContext, hostedContexts } from '{{alias.platform}}/composition/compose-application{{alias.ext}}';
8
+ import { inertPersistence } from '{{alias.platform}}/database/inert-persistence{{alias.ext}}';
9
+ import { inProcessEvents } from '{{alias.platform}}/events/in-process-events{{alias.ext}}';
10
+
11
+ /** The contexts run as services of their own: `ddd-ts-kit generate service` adds one. */
12
+ const extracted: readonly string[] = [
13
+ // ddd-ts-kit:extracted
14
+ // /ddd-ts-kit:extracted
15
+ ];
16
+
17
+ const options = {
18
+ events: inProcessEvents(),
19
+ {{#if http.any}}
20
+ authenticator: offlineAuthenticator,
21
+ {{/if}}
22
+ };
23
+
24
+ describe('A context run as a service leaves the whole application', () => {
25
+ // Jest refuses a table without rows: until a context becomes a service, its cases wait.
26
+ if (extracted.length === 0) {
27
+ it.todo('hosts no consumer, process nor job of a context run as a service, once the project has one');
28
+ } else {
29
+ it.each(extracted)('%s: the whole application hosts none of its modules, consumers, processes nor jobs', (name) => {
30
+ const whole = composeApplication(inertPersistence(), options, hostedContexts).container.describe();
31
+
32
+ expect(whole.modules).not.toContain(name);
33
+ expect(registrationsOf(whole, name)).toEqual([]);
34
+ });
35
+
36
+ it.each(extracted)('%s: its service hosts it, each consumer once, and no job of the platform', (name) => {
37
+ const service = composeApplication(
38
+ inertPersistence(),
39
+ { ...options, platformJobs: false },
40
+ { modules: everyContext.modules.filter((module) => module.name === name) },
41
+ ).container.describe();
42
+
43
+ const consumers = service.integrationEventHandlers.map((handler) => `${handler.consumer}:${handler.event}`);
44
+
45
+ expect(service.modules).toContain(name);
46
+ expect(new Set(consumers).size).toBe(consumers.length);
47
+ expect(service.jobs.filter((job) => !job.name.startsWith(name + '.'))).toEqual([]);
48
+ });
49
+
50
+ it.each(extracted)('%s: its entry point leaves the jobs of the platform to the whole application', (name) => {
51
+ expect(readFileSync('{{config.sourceRoot}}/apps/' + name + '/main.ts', 'utf8')).toContain('platformJobs: false');
52
+ });
53
+ }
54
+ });
55
+
56
+ /** What a module registers to be driven by something other than a request: the reactions, the jobs, the projections. */
57
+ function registrationsOf(description: WiringDescription, name: string): readonly string[] {
58
+ return [
59
+ ...description.integrationEventHandlers,
60
+ ...description.domainEventHandlers,
61
+ ...description.projections,
62
+ ...description.jobs,
63
+ ...description.backgroundJobs,
64
+ ...description.commands,
65
+ ...description.queries,
66
+ ]
67
+ .filter(({ module }) => module === name)
68
+ .map((registration) => JSON.stringify(registration));
69
+ }
@@ -9,6 +9,9 @@ import { Requires } from '@ddd-ts-kit/kernel/declarations';
9
9
  @{{access.decoration}}
10
10
  {{/if}}
11
11
  export class {{name.pascal}}Command extends AuthorizedCommand<{{command.result}}> {
12
+ {{#if access.resourceUnnamed}}
13
+ // {{access.resourceTodo}}
14
+ {{/if}}
12
15
  // TODO(domain): declare the input of "{{name.words}}" as a constructor with the actor first, then readonly primitives and ids, for example `constructor(actor: Actor, readonly customerId: string) { super(actor); }`; the handler turns them into value objects.
13
16
  }
14
17
  {{/if}}
@@ -12,6 +12,9 @@ export class {{name.pascal}}Command extends AuthorizedCommand<string> {
12
12
  /** Staged by the bus under a key of its area before the transaction opens: the handler reads `upload.file`. */
13
13
  readonly upload: FileUpload;
14
14
 
15
+ {{#if access.resourceUnnamed}}
16
+ // {{access.resourceTodo}}
17
+ {{/if}}
15
18
  // 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
19
  constructor(actor: Actor, file: ReceivedFile) {
17
20
  super(actor);
@@ -165,6 +165,15 @@ MINIO_PORT=9000
165
165
  # TRACE_SAMPLE_RATIO=1
166
166
  # continue: a consumed event stays in the trace of the request that published it. link: each delivery starts its own trace, linked to it.
167
167
  # TRACE_CONSUMERS=continue
168
+ # What this process is called in traces and logs: one name per container (the worker, a service) tells the processes apart.
168
169
  OTEL_SERVICE_NAME={{project.name}}
170
+ # What every process of one deployment shares: the namespace of the keys they exchange through the server (the lock of a job, the cache,
171
+ # the rate limiter, the job store). The same for the API, the worker and the services; never the name of a container.
172
+ DEPLOYMENT_NAME={{project.name}}
173
+ {{#if tenancy.any}}
174
+ # The tenants that exist, comma separated, read once when the process starts: a scheduled job declared per-tenant runs once for each, and the privacy scripts reach each.
175
+ # A tenant created later is missed until the process restarts. With the identity preset the scheduler reads the organisations of the database at each occurrence instead.
176
+ TENANTS=
177
+ {{/if}}
169
178
  JAEGER_UI_PORT=16686
170
179
  OTLP_HTTP_PORT=4318
@@ -6,6 +6,9 @@ dist
6
6
  .ddd-ts-kit/
7
7
  .eslintcache/
8
8
  .worktrees/
9
+ {{#if pm.pnpm}}
10
+ .pnpm-store/
11
+ {{/if}}
9
12
  .stryker-tmp/
10
13
  /reports/mutation/
11
14
  {{#if orm.prisma}}
@@ -11,7 +11,7 @@ import { PurgingPublisher } from '@ddd-ts-kit/kernel';
11
11
  {{#if http.any}}
12
12
  import { createAuthenticator } from '{{alias.platform}}/auth/authentication/create-authenticator{{alias.ext}}';
13
13
  {{/if}}
14
- import { composeApplication } from '{{alias.platform}}/composition/compose-application{{alias.ext}}';
14
+ import { composeApplication, hostedContexts } from '{{alias.platform}}/composition/compose-application{{alias.ext}}';
15
15
  import { loadConfig } from '{{alias.platform}}/config/load-config{{alias.ext}}';
16
16
  import { openPersistence } from '{{alias.platform}}/database/open-persistence{{alias.ext}}';
17
17
  import { {{events.providerName}} } from '{{alias.platform}}/events/{{events.providerFile}}{{alias.ext}}';
@@ -40,25 +40,32 @@ const persistence = openPersistence();
40
40
  const redis = openRedis(config);
41
41
  {{/if}}
42
42
 
43
- const application = composeApplication(persistence, {
44
- events: {{events.provider}},
45
- {{#if errors.sentry}}
46
- errors,
47
- {{/if}}
48
- {{#if http.any}}
49
- authenticator: createAuthenticator(config),
50
- {{/if}}
51
- log: { ...config.log, service: config.serviceName, environment: config.environment },
52
- traceConsumers: config.tracing.consumers,
53
- resilience: config.resilience,
54
- jobs: { start: config.runJobs, settings: jobSettings(process.env) },
55
- {{#if cache.redis}}
56
- redis,
57
- {{/if}}
58
- {{#if files.any}}
59
- files: openFiles(config.files),
60
- {{/if}}
61
- });
43
+ const application = composeApplication(
44
+ persistence,
45
+ {
46
+ events: {{events.provider}},
47
+ {{#if errors.sentry}}
48
+ errors,
49
+ {{/if}}
50
+ {{#if http.any}}
51
+ authenticator: createAuthenticator(config),
52
+ {{/if}}
53
+ log: { ...config.log, service: config.serviceName, environment: config.environment },
54
+ traceConsumers: config.tracing.consumers,
55
+ resilience: config.resilience,
56
+ jobs: { start: config.runJobs, settings: jobSettings(process.env) },
57
+ {{#if tenancy.any}}
58
+ tenants: config.tenants,
59
+ {{/if}}
60
+ {{#if cache.redis}}
61
+ redis,
62
+ {{/if}}
63
+ {{#if files.any}}
64
+ files: openFiles(config.files),
65
+ {{/if}}
66
+ },
67
+ hostedContexts,
68
+ );
62
69
 
63
70
  await application.start();
64
71
 
@@ -29,7 +29,12 @@ import { SearchAuditEntriesQuery } from '{{alias.platform}}/audit/search-audit-e
29
29
  * the entries older than the twelve whole months kept online moved to the storage of files,
30
30
  * verified and anchored (`audit.archive`){{/if}}.
31
31
  */
32
+ {{#if audit.archive}}
33
+ export function auditModule(audit: Audit, { jobs = true }: { readonly jobs?: boolean } = {}): ContextModule {
34
+ {{/if}}
35
+ {{#unless audit.archive}}
32
36
  export function auditModule(audit: Audit): ContextModule {
37
+ {{/unless}}
33
38
  return defineContextModule({
34
39
  name: 'audit',
35
40
  register(builder) {
@@ -58,9 +63,12 @@ export function auditModule(audit: Audit): ContextModule {
58
63
  {{/if}}
59
64
  {{#if audit.archive}}
60
65
 
61
- builder
62
- .job('audit.archive', { cron: '0 4 1 * *' })
63
- .handledByFactory((resolve) => new ArchiveAuditEntries(audit.trail, resolve(FileStorage), resolve(Clock)));
66
+ // Registered by the process that runs the jobs of the platform; a service of the deployment passes `jobs: false`.
67
+ if (jobs) {
68
+ builder
69
+ .job('audit.archive', { cron: '0 4 1 * *', tenancy: 'global' })
70
+ .handledByFactory((resolve) => new ArchiveAuditEntries(audit.trail, resolve(FileStorage), resolve(Clock)));
71
+ }
64
72
  {{/if}}
65
73
  },
66
74
  onStart({ resolve }) {
@@ -1,21 +1,36 @@
1
- import { defineSchema } from '@ddd-ts-kit/auth/rebac';
1
+ import { defineSchema, type SchemaDefinition } from '@ddd-ts-kit/auth/rebac';
2
2
  import { permissions } from '{{alias.platform}}/auth/authorization/permissions{{alias.ext}}';
3
3
 
4
4
  /**
5
- * Who is what to which element (the relations, stored as tuples), who counts as what (the
6
- * derived relations), and which permission each one grants: a derived relation named after a
7
- * permission id grants it on that type. One example type: a bounded context and who operates it.
8
- * Use cases start by naming their context as the resource (`{ type: 'context', id: '<context>' }`),
9
- * so every permission of the project is held by the operators of its context until the elements
10
- * are modelled. A system actor (a scheduled job, an integration event handler) is a subject of
11
- * type `system`.
5
+ * The elements rights are shared on (documents, folders, teams): a type each, with who is what to
6
+ * it (the relations, stored as tuples) and which permission it grants (a derived relation named
7
+ * after a permission id grants it on that type). A permission derived on an element leaves the
8
+ * context below: the operators of the context no longer hold it, so name that element in the
9
+ * `resource` of the requirement, never the context.
12
10
  */
13
11
  // TODO(platform): model the elements rights are shared on (documents, folders, teams), name them in the `resource` of each requirement and derive each permission on its type.
12
+ const elements: SchemaDefinition = {};
13
+
14
+ const modelledOnAnElement = new Set(Object.values(elements).flatMap((element) => Object.keys(element.derived ?? {})));
15
+
16
+ /**
17
+ * Who is what to which element, who counts as what (the derived relations), and which permission
18
+ * each one grants. A bounded context and who operates it is the example type: a use case on no
19
+ * element names its context as the resource (`{ type: 'context', id: '<context>' }`), and the
20
+ * operators of the context hold every permission no element derives. A system actor (a scheduled
21
+ * job, an integration event handler) is a subject of type `system`.
22
+ */
14
23
  export const relationshipSchema = defineSchema({
15
24
  user: {},
16
25
  system: {},
17
26
  context: {
18
27
  relations: { operator: ['user', 'system'] },
19
- derived: Object.fromEntries(permissions.all().map((permission) => [permission.id, 'operator'])),
28
+ derived: Object.fromEntries(
29
+ permissions
30
+ .all()
31
+ .filter((permission) => !modelledOnAnElement.has(permission.id))
32
+ .map((permission) => [permission.id, 'operator']),
33
+ ),
20
34
  },
35
+ ...elements,
21
36
  });
@@ -86,11 +86,28 @@ export type ApplicationOptions = PlatformOptions;
86
86
  /** Wires one context on the platform. An application hosts all of them here, one alone in `apps/<context>/main.ts`. */
87
87
  type ContextModule = (platform: Platform) => ContextWiring;
88
88
 
89
- const everyContext: readonly ContextModule[] = [
89
+ export const everyContext: readonly ContextModule[] = [
90
90
  // ddd-ts-kit:contexts
91
91
  // /ddd-ts-kit:contexts
92
92
  ];
93
93
 
94
+ /**
95
+ * The contexts run as services of their own (`ddd-ts-kit generate service`): the application
96
+ * `main.ts` and the worker run leaves them out, since their consumers, their processes and their
97
+ * jobs belong to the service, not to both.
98
+ */
99
+ const extracted: readonly ContextModule[] = [
100
+ // ddd-ts-kit:extracted
101
+ // /ddd-ts-kit:extracted
102
+ ];
103
+
104
+ /**
105
+ * What the application of `main.ts` and the worker host: every context but the extracted ones.
106
+ * `everyContext` stays the full list, the default of the tools, the contracts and the end-to-end
107
+ * tests, which see the whole system in one process.
108
+ */
109
+ export const hostedContexts: readonly ContextModule[] = everyContext.filter((module) => !extracted.includes(module));
110
+
94
111
  export interface Application {
95
112
  {{#if http.any}}
96
113
  readonly routes: readonly Route[];
@@ -142,7 +159,7 @@ export function composeApplication(
142
159
 
143
160
  installAspectRuntime({ logger, metrics, cache, clock });
144
161
  {{#if platformJobs.any}}
145
- registerPlatformJobs(platform);
162
+ registerPlatformJobs(platform, options);
146
163
  {{/if}}
147
164
 
148
165
  {{#if audit.route}}
@@ -276,8 +293,12 @@ async function stop(platform: Platform, options: ApplicationOptions): Promise<vo
276
293
  }
277
294
  {{#if platformJobs.any}}
278
295
 
279
- /** The jobs of the platform itself, run by the process that runs the jobs. */
280
- function registerPlatformJobs(platform: Platform): void {
296
+ /** The jobs of the platform itself, run by the whole application: a service passes `platformJobs: false`. */
297
+ function registerPlatformJobs(platform: Platform, options: ApplicationOptions): void {
298
+ if (options.platformJobs === false) {
299
+ return;
300
+ }
301
+
281
302
  {{#if files.any}}
282
303
  purgeStagedFiles(platform);
283
304
  {{/if}}
@@ -8,9 +8,6 @@ import { Authenticator } from '@ddd-ts-kit/auth';
8
8
  {{/unless}}
9
9
  {{/if}}
10
10
  import {
11
- {{#if routes.declaredControllers}}
12
- DeclaredEdges,
13
- {{/if}}
14
11
  createApplication,
15
12
  kernelModule,
16
13
  type Application as Container,
@@ -21,19 +18,11 @@ import {
21
18
  import { OutboxPublisher } from '@ddd-ts-kit/events';
22
19
  {{/if}}
23
20
  {{#if http.any}}
24
- {{#if routes.mountsControllers}}
25
- import { ErrorMapper, controllerRoutes, defineRoutes, type Route } from '@ddd-ts-kit/http';
26
- {{/if}}
27
- {{#unless routes.mountsControllers}}
28
- import { ErrorMapper, defineRoutes, type Route } from '@ddd-ts-kit/http';
29
- {{/unless}}
21
+ import { ErrorMapper, type Route } from '@ddd-ts-kit/http';
30
22
  {{/if}}
31
23
  import {
32
24
  Cache,
33
25
  Clock,
34
- {{#if http.any}}
35
- CommandBus,
36
- {{/if}}
37
26
  {{#if errors.sentry}}
38
27
  {{#if http.any}}
39
28
  ErrorReporter,
@@ -57,9 +46,6 @@ import {
57
46
  {{#if httpCache.perTenant}}
58
47
  PublishedCacheInvalidation,
59
48
  {{/if}}
60
- {{#if http.any}}
61
- QueryBus,
62
- {{/if}}
63
49
  {{#if cache.rateLimit}}
64
50
  RateLimiter,
65
51
  {{/if}}
@@ -78,16 +64,7 @@ import { tracedPublisher } from '@ddd-ts-kit/tracing';
78
64
  {{#if audit.on}}
79
65
  import { auditModule } from '{{alias.platform}}/audit/audit.module{{alias.ext}}';
80
66
  {{/if}}
81
- {{#if audit.functionRoute}}
82
- import { searchAuditEntriesRoute } from '{{alias.platform}}/audit/search-audit-entries/search-audit-entries.route{{alias.ext}}';
83
- {{/if}}
84
- {{#if audit.controller}}
85
- import { SearchAuditEntriesController } from '{{alias.platform}}/audit/search-audit-entries/search-audit-entries.controller{{alias.ext}}';
86
- {{/if}}
87
67
  import { authorization } from '{{alias.platform}}/auth/authorization/authorization{{alias.ext}}';
88
- {{#if routes.mountsControllers}}
89
- import { securitySchemes } from '{{alias.platform}}/auth/authentication/security-schemes{{alias.ext}}';
90
- {{/if}}
91
68
  {{#if http.any}}
92
69
  import { httpBoundary, type HttpBoundary } from '{{alias.platform}}/auth/authentication/http-boundary{{alias.ext}}';
93
70
  {{/if}}
@@ -115,7 +92,14 @@ import { filesModule } from '{{alias.platform}}/files/files.module{{alias.ext}}'
115
92
  {{#if privacy.on}}
116
93
  import { privacyModule } from '{{alias.platform}}/privacy/privacy.module{{alias.ext}}';
117
94
  {{/if}}
95
+ import type { ContextSelection } from '{{alias.platform}}/composition/context-selection{{alias.ext}}';
96
+ {{#if http.any}}
97
+ import { routesOf } from '{{alias.platform}}/composition/routes-of{{alias.ext}}';
98
+ {{/if}}
118
99
  import { layFoundations, type Foundations } from '{{alias.platform}}/composition/lay-foundations{{alias.ext}}';
100
+ {{#if tenancy.any}}
101
+ import { tenantDirectory } from '{{alias.platform}}/composition/tenant-directory{{alias.ext}}';
102
+ {{/if}}
119
103
  import { platformModule, type PlatformOptions } from '{{alias.platform}}/composition/platform.module{{alias.ext}}';
120
104
  // ddd-ts-kit:imports
121
105
  // /ddd-ts-kit:imports
@@ -123,6 +107,12 @@ import { platformModule, type PlatformOptions } from '{{alias.platform}}/composi
123
107
  export interface ApplicationOptions extends PlatformOptions {
124
108
  /** Whether this process runs the scheduled jobs, and their settings; left out (the tools, the tests), no job runs by time. */
125
109
  readonly jobs?: JobsOptions;
110
+ /** Whether the jobs of the platform itself are registered: a service passes `false`, they run in the whole application. Default `true`. */
111
+ readonly platformJobs?: boolean;
112
+ {{#if tenancy.any}}
113
+ /** The ids of the tenants that exist (`TENANTS`): a job declared `per-tenant` runs once for each. Left out (the tools, the tests), none, and such a job runs for nobody. */
114
+ readonly tenants?: readonly string[];
115
+ {{/if}}
126
116
  {{#if files.any}}
127
117
  /** Where the content of files lives: the storage of `main.ts`; left out (the tools, the tests), the memory of the process. */
128
118
  readonly files?: FileStorage;
@@ -164,16 +154,7 @@ export interface Application {
164
154
  stop(): Promise<void>;
165
155
  }
166
156
 
167
- /** The contexts an application hosts: all of them here, one alone in `apps/<context>/main.ts`. */
168
- export interface ContextSelection {
169
- readonly modules: readonly ContextModule[];
170
- {{#if http.any}}
171
- /** The function routes of each context, given the two buses they call; controllers need no list. */
172
- readonly routes?: readonly ((commands: CommandBus, queries: QueryBus) => readonly Route[])[];
173
- {{/if}}
174
- }
175
-
176
- const everyContext: ContextSelection = {
157
+ export const everyContext: ContextSelection = {
177
158
  modules: [
178
159
  // ddd-ts-kit:contexts
179
160
  // /ddd-ts-kit:contexts
@@ -186,6 +167,27 @@ const everyContext: ContextSelection = {
186
167
  {{/if}}
187
168
  };
188
169
 
170
+ /** The contexts run as services of their own (`generate service`): `main.ts` and the worker leave them out, routes included. */
171
+ const extractedModules: readonly string[] = [
172
+ // ddd-ts-kit:extracted
173
+ // /ddd-ts-kit:extracted
174
+ ];
175
+ {{#if http.any}}
176
+
177
+ const extractedRoutes: NonNullable<ContextSelection['routes']> = [
178
+ // ddd-ts-kit:extracted-routes
179
+ // /ddd-ts-kit:extracted-routes
180
+ ];
181
+ {{/if}}
182
+
183
+ /** What `main.ts` and the worker host; `everyContext` stays the full list for the tools, the contracts and the end-to-end tests. */
184
+ export const hostedContexts: ContextSelection = {
185
+ modules: everyContext.modules.filter((module) => !extractedModules.includes(module.name)),
186
+ {{#if http.any}}
187
+ routes: (everyContext.routes ?? []).filter((routes) => !extractedRoutes.includes(routes)),
188
+ {{/if}}
189
+ };
190
+
189
191
  /** Wires the bounded contexts on the kernel and the platform. Also used to generate the contracts and by the end-to-end tests. */
190
192
  export function composeApplication(
191
193
  persistence: Persistence,
@@ -209,13 +211,18 @@ export function composeApplication(
209
211
  {{/if}}
210
212
  guard,
211
213
  {{#if audit.on}}
214
+ {{#if audit.archive}}
215
+ auditModule(foundations.audit, { jobs: options.platformJobs !== false }),
216
+ {{/if}}
217
+ {{#unless audit.archive}}
212
218
  auditModule(foundations.audit),
219
+ {{/unless}}
213
220
  {{/if}}
214
221
  {{#if files.any}}
215
- filesModule(options.files ?? new InMemoryFileStorage()),
222
+ filesModule(options.files ?? new InMemoryFileStorage(), { jobs: options.platformJobs !== false }),
216
223
  {{/if}}
217
224
  {{#if privacy.on}}
218
- privacyModule(),
225
+ privacyModule({ jobs: options.platformJobs !== false }),
219
226
  {{/if}}
220
227
  ...contexts.modules,
221
228
  ],
@@ -257,6 +264,10 @@ function kernel(foundations: Foundations, persistence: Persistence, options: App
257
264
  logger,
258
265
  metrics,
259
266
  eventBus: bus,
267
+ {{#if tenancy.any}}
268
+ // The tenants a job declared `per-tenant` runs for, asked at each occurrence.
269
+ tenants: tenantDirectory(persistence, options.tenants),
270
+ {{/if}}
260
271
  {{#if events.outbox}}
261
272
  // Use cases publish to the outbox, in the transaction of their command; the relay of `main.ts` delivers to the provider.
262
273
  eventPublisher: scopedPublisher(new OutboxPublisher(persistence.outbox), scope),
@@ -373,32 +384,3 @@ function running({ container, options, contexts }: Composed): Application {
373
384
  stop: () => container.stop(),
374
385
  };
375
386
  }
376
- {{#if http.any}}
377
-
378
- function routesOf(container: Container, contexts: ContextSelection): readonly Route[] {
379
- const commands = container.resolve(CommandBus);
380
- const queries = container.resolve(QueryBus);
381
-
382
- {{#unless routes.mountsControllers}}
383
- return defineRoutes(...(contexts.routes ?? []).map((routes) => routes(commands, queries)){{#if audit.route}}, [searchAuditEntriesRoute(queries)]{{/if}});
384
- {{/unless}}
385
- {{#if routes.mountsControllers}}
386
- // Every route that is not public names these schemes in the contract.
387
- const security = Object.keys(securitySchemes);
388
-
389
- return defineRoutes(
390
- ...(contexts.routes ?? []).map((routes) => routes(commands, queries)),
391
- {{#if routes.declaredControllers}}
392
- // The controllers the hosted contexts declare, each built from its constructor.
393
- controllerRoutes(container.resolve(DeclaredEdges).of('controller'), { resolve: container.resolve, security }),
394
- {{/if}}
395
- {{#if audit.functionRoute}}
396
- [searchAuditEntriesRoute(queries)],
397
- {{/if}}
398
- {{#if audit.controller}}
399
- controllerRoutes([new SearchAuditEntriesController(queries)], { security }),
400
- {{/if}}
401
- );
402
- {{/if}}
403
- }
404
- {{/if}}
@@ -0,0 +1,17 @@
1
+ {{#if http.any}}
2
+ import type { ContextModule } from '@ddd-ts-kit/di';
3
+ import type { Route } from '@ddd-ts-kit/http';
4
+ import type { CommandBus, QueryBus } from '@ddd-ts-kit/kernel';
5
+ {{/if}}
6
+ {{#unless http.any}}
7
+ import type { ContextModule } from '@ddd-ts-kit/di';
8
+ {{/unless}}
9
+
10
+ /** The contexts an application hosts: all of them here, one alone in `apps/<context>/main.ts`. */
11
+ export interface ContextSelection {
12
+ readonly modules: readonly ContextModule[];
13
+ {{#if http.any}}
14
+ /** The function routes of each context, given the two buses they call; controllers need no list. */
15
+ readonly routes?: readonly ((commands: CommandBus, queries: QueryBus) => readonly Route[])[];
16
+ {{/if}}
17
+ }