ddd-ts-kit 0.7.0 → 0.9.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 +9 -4
- package/dist/check/catalogue/architecture-rules.d.ts.map +1 -1
- package/dist/check/catalogue/architecture-rules.js +6 -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/explain/errors-docs.js +1 -1
- package/dist/check/explain/errors-docs.js.map +1 -1
- 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/explain/tenancy-docs.d.ts +3 -0
- package/dist/check/explain/tenancy-docs.d.ts.map +1 -0
- package/dist/check/explain/tenancy-docs.js +15 -0
- package/dist/check/explain/tenancy-docs.js.map +1 -0
- 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/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/fixtures/tenancy-fixtures.d.ts +3 -0
- package/dist/check/fixtures/tenancy-fixtures.d.ts.map +1 -0
- package/dist/check/fixtures/tenancy-fixtures.js +136 -0
- package/dist/check/fixtures/tenancy-fixtures.js.map +1 -0
- 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/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/check/rules/tenancy-scoped-statement.d.ts +3 -0
- package/dist/check/rules/tenancy-scoped-statement.d.ts.map +1 -0
- package/dist/check/rules/tenancy-scoped-statement.js +37 -0
- package/dist/check/rules/tenancy-scoped-statement.js.map +1 -0
- package/dist/check/tenancy/balanced-arguments.d.ts +3 -0
- package/dist/check/tenancy/balanced-arguments.d.ts.map +1 -0
- package/dist/check/tenancy/balanced-arguments.js +13 -0
- package/dist/check/tenancy/balanced-arguments.js.map +1 -0
- package/dist/check/tenancy/declared-with.d.ts +11 -0
- package/dist/check/tenancy/declared-with.d.ts.map +1 -0
- package/dist/check/tenancy/declared-with.js +13 -0
- package/dist/check/tenancy/declared-with.js.map +1 -0
- package/dist/check/tenancy/drizzle-unscoped.d.ts +7 -0
- package/dist/check/tenancy/drizzle-unscoped.d.ts.map +1 -0
- package/dist/check/tenancy/drizzle-unscoped.js +35 -0
- package/dist/check/tenancy/drizzle-unscoped.js.map +1 -0
- package/dist/check/tenancy/executor-uses.d.ts +11 -0
- package/dist/check/tenancy/executor-uses.d.ts.map +1 -0
- package/dist/check/tenancy/executor-uses.js +14 -0
- package/dist/check/tenancy/executor-uses.js.map +1 -0
- package/dist/check/tenancy/knex-unscoped.d.ts +7 -0
- package/dist/check/tenancy/knex-unscoped.d.ts.map +1 -0
- package/dist/check/tenancy/knex-unscoped.js +19 -0
- package/dist/check/tenancy/knex-unscoped.js.map +1 -0
- package/dist/check/tenancy/unscoped-read-models.d.ts +4 -0
- package/dist/check/tenancy/unscoped-read-models.d.ts.map +1 -0
- package/dist/check/tenancy/unscoped-read-models.js +15 -0
- package/dist/check/tenancy/unscoped-read-models.js.map +1 -0
- package/dist/commands/init.d.ts.map +1 -1
- package/dist/commands/init.js +2 -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 +3 -1
- package/dist/config/parse-stack.js.map +1 -1
- package/dist/config/stack.d.ts +18 -0
- package/dist/config/stack.d.ts.map +1 -1
- package/dist/config/stack.js +8 -0
- package/dist/config/stack.js.map +1 -1
- package/dist/config/written-stack.d.ts.map +1 -1
- package/dist/config/written-stack.js +6 -2
- package/dist/config/written-stack.js.map +1 -1
- 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/generate-repository.js +5 -4
- package/dist/generate/kinds/generate-repository.js.map +1 -1
- 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/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 +13 -2
- package/dist/init/stack/resolve-stack.js.map +1 -1
- package/dist/init/stack/resolve-tenancy.d.ts +8 -0
- package/dist/init/stack/resolve-tenancy.d.ts.map +1 -0
- package/dist/init/stack/resolve-tenancy.js +31 -0
- package/dist/init/stack/resolve-tenancy.js.map +1 -0
- package/dist/init/stack/stack-options.d.ts +2 -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/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 +14 -4
- package/dist/stack/stack-vars.js.map +1 -1
- package/dist/version.d.ts +1 -1
- package/dist/version.js +1 -1
- package/package.json +3 -3
- 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/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/audit/audit.module.manual.ts.tpl +5 -0
- package/templates/init/platform/audit/audit.module.tsyringe.ts.tpl +5 -0
- package/templates/init/platform/audit/create-audit.ts.tpl +24 -0
- package/templates/init/platform/audit/search-audit-entries.handler.ts.tpl +13 -0
- package/templates/init/platform/audit/search-audit-entries.query.ts.tpl +5 -0
- package/templates/init/platform/auth/http-boundary.ts.tpl +62 -1
- package/templates/init/platform/buses/create-command-bus.ts.tpl +7 -1
- package/templates/init/platform/compose-application.manual.ts.tpl +28 -0
- package/templates/init/platform/compose-application.tsyringe.ts.tpl +45 -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/database/persistence-over.knex.ts.tpl +5 -0
- package/templates/init/platform/database/schema.drizzle.ts.tpl +8 -0
- package/templates/init/platform/database/schema.knex.ts.tpl +6 -0
- 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 +3 -0
- package/templates/init/rules/infrastructure.md.tpl +4 -0
- package/templates/init/rules/platform.md.tpl +3 -0
- package/templates/pipeline/package-scripts.json +1 -1
- package/templates/repository/drizzle/repository.ts.tpl +7 -8
- package/templates/repository/knex/repository.ts.tpl +32 -12
- package/templates/service/main.ts.tpl +6 -0
- package/templates/worker/main.ts.tpl +6 -0
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
type Variables = Readonly<Record<string, string | undefined>>;
|
|
2
|
+
|
|
3
|
+
/** The bucket that holds the content of files, on any provider of the S3 protocol. */
|
|
4
|
+
export interface FilesConfig {
|
|
5
|
+
/** `S3_ENDPOINT`: empty for AWS; MinIO of the compose stack `http://localhost:9000`; OVHcloud, Scaleway or R2 their own. */
|
|
6
|
+
readonly endpoint?: string;
|
|
7
|
+
/** `S3_REGION`: `us-east-1` by default, `auto` for Cloudflare R2. */
|
|
8
|
+
readonly region: string;
|
|
9
|
+
/** `S3_BUCKET`: created by the infrastructure code of the deployment, by the compose stack in development. */
|
|
10
|
+
readonly bucket: string;
|
|
11
|
+
/** `S3_ACCESS_KEY_ID` and `S3_SECRET_ACCESS_KEY`: an access key allowed on this bucket only. */
|
|
12
|
+
readonly accessKeyId: string;
|
|
13
|
+
readonly secretAccessKey: string;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
/** The settings of the S3 file storage; a problem is reported by variable name only, never by value. */
|
|
17
|
+
export function readFiles(env: Variables, problems: string[]): FilesConfig {
|
|
18
|
+
const endpoint = (env['S3_ENDPOINT'] ?? '').trim();
|
|
19
|
+
const region = (env['S3_REGION'] ?? '').trim();
|
|
20
|
+
|
|
21
|
+
if (endpoint !== '' && !URL.canParse(endpoint)) {
|
|
22
|
+
problems.push('S3_ENDPOINT must be a URL, or empty for AWS');
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
return {
|
|
26
|
+
...(endpoint === '' ? {} : { endpoint }),
|
|
27
|
+
region: region === '' ? 'us-east-1' : region,
|
|
28
|
+
bucket: requiredText(env, 'S3_BUCKET', problems),
|
|
29
|
+
accessKeyId: requiredText(env, 'S3_ACCESS_KEY_ID', problems),
|
|
30
|
+
secretAccessKey: requiredText(env, 'S3_SECRET_ACCESS_KEY', problems),
|
|
31
|
+
};
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
function requiredText(env: Variables, name: string, problems: string[]): string {
|
|
35
|
+
const value = (env[name] ?? '').trim();
|
|
36
|
+
|
|
37
|
+
if (value === '') {
|
|
38
|
+
problems.push(name + ' must be set');
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
return value;
|
|
42
|
+
}
|
|
@@ -8,6 +8,9 @@ import {
|
|
|
8
8
|
InMemoryCache,
|
|
9
9
|
InMemoryCommandBus,
|
|
10
10
|
InMemoryDomainEventDispatcher,
|
|
11
|
+
{{#if files.any}}
|
|
12
|
+
InMemoryFileStorage,
|
|
13
|
+
{{/if}}
|
|
11
14
|
InMemoryLock,
|
|
12
15
|
InMemoryQueryBus,
|
|
13
16
|
{{#if cache.rateLimit}}
|
|
@@ -19,6 +22,9 @@ import {
|
|
|
19
22
|
UuidGenerator,
|
|
20
23
|
{{#if errors.sentry}}
|
|
21
24
|
errorReportingMiddleware,
|
|
25
|
+
{{/if}}
|
|
26
|
+
{{#if files.any}}
|
|
27
|
+
fileUploadsMiddleware,
|
|
22
28
|
{{/if}}
|
|
23
29
|
scopedPublisher,
|
|
24
30
|
dispatchingOnSave,
|
|
@@ -28,6 +34,9 @@ import {
|
|
|
28
34
|
type DomainEventDispatcher,
|
|
29
35
|
{{#if errors.sentry}}
|
|
30
36
|
type ErrorReporter,
|
|
37
|
+
{{/if}}
|
|
38
|
+
{{#if files.any}}
|
|
39
|
+
type FileStorage,
|
|
31
40
|
{{/if}}
|
|
32
41
|
type JobSettings,
|
|
33
42
|
} from '@ddd-ts-kit/kernel';
|
|
@@ -58,6 +67,9 @@ import { logDecisions } from '{{alias.platform}}/auth/log-decisions{{alias.ext}}
|
|
|
58
67
|
import { UseCaseProtection } from '{{alias.platform}}/auth/use-case-protection{{alias.ext}}';
|
|
59
68
|
import type { Persistence } from '{{alias.platform}}/database/persistence{{alias.ext}}';
|
|
60
69
|
import type { EventProvider } from '{{alias.platform}}/events/event-provider{{alias.ext}}';
|
|
70
|
+
{{#if files.any}}
|
|
71
|
+
import { createFiles, type Files } from '{{alias.platform}}/files/create-files{{alias.ext}}';
|
|
72
|
+
{{/if}}
|
|
61
73
|
import { ConfiguredJobScheduler } from '{{alias.platform}}/jobs/configured-job-scheduler{{alias.ext}}';
|
|
62
74
|
import { createLogger, type LogSettings } from '{{alias.platform}}/observability/create-logger{{alias.ext}}';
|
|
63
75
|
import { createMetrics } from '{{alias.platform}}/observability/create-metrics{{alias.ext}}';
|
|
@@ -96,6 +108,10 @@ export interface PlatformOptions {
|
|
|
96
108
|
/** The Redis protocol server; left out, the cache, the lock and the limiter stay in the memory of the process. */
|
|
97
109
|
readonly redis?: RedisServices;
|
|
98
110
|
{{/if}}
|
|
111
|
+
{{#if files.any}}
|
|
112
|
+
/** Where the content of files lives: the storage of `main.ts`; left out (the tools, the tests), the memory of the process. */
|
|
113
|
+
readonly files?: FileStorage;
|
|
114
|
+
{{/if}}
|
|
99
115
|
}
|
|
100
116
|
|
|
101
117
|
/** Replacing a provider (another broker, another clock) happens here and nowhere else. */
|
|
@@ -121,13 +137,19 @@ export function createPlatform(persistence: Persistence, options: PlatformOption
|
|
|
121
137
|
const protection = new UseCaseProtection(authorizer, onDecision);
|
|
122
138
|
const adapter = adapterOf(options.resilience ?? defaultResilience, { traced, domainEvents });
|
|
123
139
|
const observed = observing(foundations, protection.guard, traced.useCase);
|
|
140
|
+
{{#if files.any}}
|
|
141
|
+
const files = filesOf(options, foundations);
|
|
142
|
+
{{/if}}
|
|
124
143
|
|
|
125
144
|
return {
|
|
126
145
|
...foundations,
|
|
127
146
|
...eventsOn(persistence, options, { scope, traced }),
|
|
128
|
-
// Around every use case, outermost first: {{#if errors.sentry}}the report of an unexpected failure, {{/if}}its log entry, its measure, the permission, its span; a command then runs in the unit of work.
|
|
147
|
+
// Around every use case, outermost first: {{#if errors.sentry}}the report of an unexpected failure, {{/if}}its log entry, its measure, the permission, its span; a command then {{#if files.any}}has the file it carries staged, out of any transaction, and {{/if}}runs in the unit of work.
|
|
129
148
|
commands: chain(new InMemoryCommandBus(), [
|
|
130
149
|
...observed,
|
|
150
|
+
{{#if files.any}}
|
|
151
|
+
fileUploadsMiddleware(() => files),
|
|
152
|
+
{{/if}}
|
|
131
153
|
...beforeTheTransaction(foundations, options),
|
|
132
154
|
unitOfWorkMiddleware(persistence.unitOfWork),
|
|
133
155
|
]),
|
|
@@ -139,9 +161,24 @@ export function createPlatform(persistence: Persistence, options: PlatformOption
|
|
|
139
161
|
adapter,
|
|
140
162
|
selfGoverned: (instance, info) => adapter(ownPolicy()(instance, info), info),
|
|
141
163
|
protection,
|
|
164
|
+
{{#if files.any}}
|
|
165
|
+
files,
|
|
166
|
+
{{/if}}
|
|
142
167
|
};
|
|
143
168
|
}
|
|
144
169
|
|
|
170
|
+
{{#if files.any}}
|
|
171
|
+
/** Where the content of files lives: the storage of `main.ts`; left out (the tools, the tests), the memory of the process. */
|
|
172
|
+
function filesOf(options: PlatformOptions, foundations: Foundations): Files {
|
|
173
|
+
{{#if errors.none}}
|
|
174
|
+
return createFiles(options.files ?? new InMemoryFileStorage(), { ...foundations, errors: new NoopErrorReporter() });
|
|
175
|
+
{{/if}}
|
|
176
|
+
{{#if errors.sentry}}
|
|
177
|
+
return createFiles(options.files ?? new InMemoryFileStorage(), foundations);
|
|
178
|
+
{{/if}}
|
|
179
|
+
}
|
|
180
|
+
|
|
181
|
+
{{/if}}
|
|
145
182
|
/** What surrounds every use case on both buses, outermost first, before what each bus adds. */
|
|
146
183
|
function observing(foundations: Foundations, guard: BusMiddleware, span: BusMiddleware): readonly BusMiddleware[] {
|
|
147
184
|
{{#if errors.none}}
|
|
@@ -10,7 +10,12 @@ export function persistenceOver(db: Knex, close: () => Promise<void>): Persisten
|
|
|
10
10
|
return {
|
|
11
11
|
unitOfWork,
|
|
12
12
|
database: unitOfWork,
|
|
13
|
+
{{#if tenancy.any}}
|
|
14
|
+
outbox: new KnexOutboxStore(unitOfWork, { schema: MESSAGING, tenant: true }),
|
|
15
|
+
{{/if}}
|
|
16
|
+
{{#unless tenancy.any}}
|
|
13
17
|
outbox: new KnexOutboxStore(unitOfWork, { schema: MESSAGING }),
|
|
18
|
+
{{/unless}}
|
|
14
19
|
processedEvents: new KnexProcessedEventStore(unitOfWork, { schema: MESSAGING }),
|
|
15
20
|
close,
|
|
16
21
|
};
|
|
@@ -1,9 +1,17 @@
|
|
|
1
1
|
{{#if audit.on}}
|
|
2
2
|
import { auditChainsTable, auditEntriesTable } from '@ddd-ts-kit/audit/drizzle';
|
|
3
3
|
{{/if}}
|
|
4
|
+
{{#if tenancy.any}}
|
|
5
|
+
import { processedEventsTable, tenantOutboxTable } from '@ddd-ts-kit/drizzle/postgres';
|
|
6
|
+
|
|
7
|
+
/** It keeps the tenant of each event, so that its consumers run in that tenant. */
|
|
8
|
+
export const outbox = tenantOutboxTable('messaging');
|
|
9
|
+
{{/if}}
|
|
10
|
+
{{#unless tenancy.any}}
|
|
4
11
|
import { outboxTable, processedEventsTable } from '@ddd-ts-kit/drizzle/postgres';
|
|
5
12
|
|
|
6
13
|
export const outbox = outboxTable('messaging');
|
|
14
|
+
{{/unless}}
|
|
7
15
|
export const processedEvents = processedEventsTable('messaging');
|
|
8
16
|
{{#if audit.on}}
|
|
9
17
|
|
|
@@ -18,7 +18,13 @@ export const AUDIT_SCHEMA = 'audit';
|
|
|
18
18
|
* existing table goes into a migration of its own (`npm run db:generate <what-it-does>`).
|
|
19
19
|
*/
|
|
20
20
|
export const createTables = async (knex: Knex): Promise<void> => {
|
|
21
|
+
{{#if tenancy.any}}
|
|
22
|
+
// The tenant of each event, so that its consumers run in that tenant.
|
|
23
|
+
await createOutboxTables(knex, { schema: MESSAGING, tenant: true });
|
|
24
|
+
{{/if}}
|
|
25
|
+
{{#unless tenancy.any}}
|
|
21
26
|
await createOutboxTables(knex, { schema: MESSAGING });
|
|
27
|
+
{{/unless}}
|
|
22
28
|
{{#if audit.on}}
|
|
23
29
|
await createAuditTables(knex, { schema: AUDIT_SCHEMA });
|
|
24
30
|
{{/if}}
|
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { ModuleBuilder } from '@ddd-ts-kit/di';
|
|
2
|
+
import {
|
|
3
|
+
AcceptEveryFileScanner,
|
|
4
|
+
Clock,
|
|
5
|
+
ErrorReporter,
|
|
6
|
+
ExecutionScope,
|
|
7
|
+
FileKeys,
|
|
8
|
+
FileScanner,
|
|
9
|
+
FileStaging,
|
|
10
|
+
FileStorage,
|
|
11
|
+
IdGenerator,
|
|
12
|
+
TenantFileKeys,
|
|
13
|
+
} from '@ddd-ts-kit/kernel';
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The ports of files on a storage: the keys generated on the server, under the tenant when there
|
|
17
|
+
* is one, the scanner a file passes before it is accepted, and the staging that keeps the storage
|
|
18
|
+
* consistent with the database. The application and the handler specs bind them alike.
|
|
19
|
+
*/
|
|
20
|
+
export function bindFiles(builder: ModuleBuilder, storage: FileStorage): void {
|
|
21
|
+
builder.bind(FileStorage).toValue(storage);
|
|
22
|
+
// TODO(platform): bind a scanner of your own (an antivirus, a content check) before accepting files from people you do not know.
|
|
23
|
+
builder.bind(FileScanner).toValue(new AcceptEveryFileScanner());
|
|
24
|
+
|
|
25
|
+
builder
|
|
26
|
+
.bind(FileKeys)
|
|
27
|
+
.toFactory((resolve) => new TenantFileKeys(resolve(IdGenerator), resolve(Clock), resolve(ExecutionScope)));
|
|
28
|
+
|
|
29
|
+
builder
|
|
30
|
+
.bind(FileStaging)
|
|
31
|
+
.toFactory((resolve) => new FileStaging(resolve(FileStorage), resolve(FileScanner), resolve(ErrorReporter)));
|
|
32
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
import {
|
|
2
|
+
AcceptEveryFileScanner,
|
|
3
|
+
FileStaging,
|
|
4
|
+
TenantFileKeys,
|
|
5
|
+
type Clock,
|
|
6
|
+
type ErrorReporter,
|
|
7
|
+
type ExecutionScope,
|
|
8
|
+
type FileKeys,
|
|
9
|
+
type FileStorage,
|
|
10
|
+
type IdGenerator,
|
|
11
|
+
} from '@ddd-ts-kit/kernel';
|
|
12
|
+
|
|
13
|
+
/** Where files live, the keys they are stored under and the staging that keeps them consistent with the database. */
|
|
14
|
+
export interface Files {
|
|
15
|
+
readonly storage: FileStorage;
|
|
16
|
+
readonly keys: FileKeys;
|
|
17
|
+
readonly staging: FileStaging;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
/** What files are built on: the foundations of the platform. */
|
|
21
|
+
interface Grounds {
|
|
22
|
+
readonly clock: Clock;
|
|
23
|
+
readonly ids: IdGenerator;
|
|
24
|
+
readonly scope: ExecutionScope;
|
|
25
|
+
readonly errors: ErrorReporter;
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
/** Files as a capability of the platform: the keys generated on the server, under the tenant when there is one, and the staging. */
|
|
29
|
+
export function createFiles(storage: FileStorage, { clock, ids, scope, errors }: Grounds): Files {
|
|
30
|
+
// TODO(platform): give a scanner of your own (an antivirus, a content check) before accepting files from people you do not know.
|
|
31
|
+
const staging = new FileStaging(storage, new AcceptEveryFileScanner(), errors);
|
|
32
|
+
|
|
33
|
+
return { storage, keys: new TenantFileKeys(ids, clock, scope), staging };
|
|
34
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import { signedFileRoutes, type Route } from '@ddd-ts-kit/http';
|
|
2
|
+
import { ServedFileStorage, type FileStorage } from '@ddd-ts-kit/kernel';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* The routes that answer the signed links of a storage the application serves itself: the local
|
|
6
|
+
* directory, or the memory of the tests. None for an object store, which answers its own links.
|
|
7
|
+
*/
|
|
8
|
+
export function fileRoutes(storage: FileStorage): readonly Route[] {
|
|
9
|
+
return storage instanceof ServedFileStorage ? signedFileRoutes(storage) : [];
|
|
10
|
+
}
|
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
import { defineContextModule, type ContextModule } from '@ddd-ts-kit/di';
|
|
2
|
+
import { Clock, FileStaging, PurgeStagedFiles, type FileStorage } from '@ddd-ts-kit/kernel';
|
|
3
|
+
import { bindFiles } from '{{alias.platform}}/files/bind-files{{alias.ext}}';
|
|
4
|
+
|
|
5
|
+
/**
|
|
6
|
+
* Files as a capability of the platform: the storage `main.ts` opened (the memory of the process
|
|
7
|
+
* for the tools and the tests), its ports, and the hourly purge of what rolled-back commands left
|
|
8
|
+
* in `staging/`.
|
|
9
|
+
*/
|
|
10
|
+
export function filesModule(storage: FileStorage): ContextModule {
|
|
11
|
+
return defineContextModule({
|
|
12
|
+
name: 'files',
|
|
13
|
+
register(builder) {
|
|
14
|
+
bindFiles(builder, storage);
|
|
15
|
+
builder.job('files.purge-staged', { every: '1h' }).handledBy(PurgeStagedFiles, [FileStaging, Clock]);
|
|
16
|
+
},
|
|
17
|
+
});
|
|
18
|
+
}
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { LocalFileStorage, type FileStorage } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import type { FilesConfig } from '{{alias.platform}}/config/read-files{{alias.ext}}';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Where the content of files lives: a directory of this machine, never the database. The
|
|
6
|
+
* application answers its signed links itself, under `/files`. One machine only: before running
|
|
7
|
+
* several instances, move to an object store (`ddd-ts-kit init --force --storage s3`).
|
|
8
|
+
*/
|
|
9
|
+
export function openFiles(config: FilesConfig): FileStorage {
|
|
10
|
+
const { directory, publicUrl, signingSecret } = config;
|
|
11
|
+
|
|
12
|
+
return new LocalFileStorage({ directory, baseUrl: publicUrl, ...(signingSecret === undefined ? {} : { secret: signingSecret }) });
|
|
13
|
+
}
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { FileStorage } from '@ddd-ts-kit/kernel';
|
|
2
|
+
import { S3FileStorage, s3ClientOf } from '@ddd-ts-kit/s3';
|
|
3
|
+
import type { FilesConfig } from '{{alias.platform}}/config/read-files{{alias.ext}}';
|
|
4
|
+
|
|
5
|
+
/** Where the content of files lives: a bucket of the S3 protocol, MinIO in the compose stack, never the database. */
|
|
6
|
+
export function openFiles(config: FilesConfig): FileStorage {
|
|
7
|
+
const { bucket, ...settings } = config;
|
|
8
|
+
|
|
9
|
+
return new S3FileStorage({ client: s3ClientOf(settings), bucket });
|
|
10
|
+
}
|
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
import type { Resolve } from '@ddd-ts-kit/di';
|
|
2
|
+
import { FileKeys, FileStaging, fileUploadsMiddleware, type BusMiddleware } from '@ddd-ts-kit/kernel';
|
|
3
|
+
|
|
4
|
+
/**
|
|
5
|
+
* Stages the file a command carries (a `FileUpload`), after the permission and before the
|
|
6
|
+
* transaction, so that no transaction waits while a client uploads. Resolved at each command
|
|
7
|
+
* that carries a file: building the bus opens nothing.
|
|
8
|
+
*/
|
|
9
|
+
export function stagingUploads(resolve: Resolve): BusMiddleware {
|
|
10
|
+
return fileUploadsMiddleware(() => ({ keys: resolve(FileKeys), staging: resolve(FileStaging) }));
|
|
11
|
+
}
|
|
@@ -19,7 +19,7 @@ export interface HttpServer {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/** What the server needs of the application: its routes and what stands at its edge. */
|
|
22
|
-
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}>;
|
|
22
|
+
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}{{#if files.http}} | 'fileRoutes'{{/if}}>;
|
|
23
23
|
|
|
24
24
|
export async function startHttpServer(served: Served, config: AppConfig): Promise<HttpServer> {
|
|
25
25
|
const app = express();
|
|
@@ -27,6 +27,10 @@ export async function startHttpServer(served: Served, config: AppConfig): Promis
|
|
|
27
27
|
app.disable('x-powered-by');
|
|
28
28
|
{{#if cache.rateLimit}}
|
|
29
29
|
guardTheDoor(app, served, config);
|
|
30
|
+
{{/if}}
|
|
31
|
+
{{#if files.http}}
|
|
32
|
+
// Before the JSON parser, which would read first a JSON file sent through a signed link.
|
|
33
|
+
mountExpress(app, served.fileRoutes, served.boundary);
|
|
30
34
|
{{/if}}
|
|
31
35
|
app.use(express.json());
|
|
32
36
|
{{#if docs.any}}
|
|
@@ -17,7 +17,7 @@ export interface HttpServer {
|
|
|
17
17
|
}
|
|
18
18
|
|
|
19
19
|
/** What the server needs of the application: its routes and what stands at its edge. */
|
|
20
|
-
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}>;
|
|
20
|
+
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}{{#if files.http}} | 'fileRoutes'{{/if}}>;
|
|
21
21
|
|
|
22
22
|
export async function startHttpServer(served: Served, config: AppConfig): Promise<HttpServer> {
|
|
23
23
|
const app = Fastify();
|
|
@@ -25,6 +25,9 @@ export async function startHttpServer(served: Served, config: AppConfig): Promis
|
|
|
25
25
|
{{#if cache.rateLimit}}
|
|
26
26
|
guardTheDoor(app, served, config);
|
|
27
27
|
{{/if}}
|
|
28
|
+
{{#if files.http}}
|
|
29
|
+
mountFastify(app, served.fileRoutes, served.boundary);
|
|
30
|
+
{{/if}}
|
|
28
31
|
{{#if docs.any}}
|
|
29
32
|
mountFastify(app, withDocumentation(served.routes, config), served.boundary);
|
|
30
33
|
{{/if}}
|
|
@@ -19,7 +19,7 @@ export interface HttpServer {
|
|
|
19
19
|
}
|
|
20
20
|
|
|
21
21
|
/** What the server needs of the application: its routes and what stands at its edge. */
|
|
22
|
-
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}>;
|
|
22
|
+
type Served = Pick<Application, 'routes' | 'boundary'{{#if cache.rateLimit}} | 'rateLimiter' | 'ready'{{/if}}{{#if files.http}} | 'fileRoutes'{{/if}}>;
|
|
23
23
|
{{#if cache.rateLimit}}
|
|
24
24
|
|
|
25
25
|
/** The part of the incoming request and of its response the limiter and the probe need, whatever Nest runs on. */
|
|
@@ -36,6 +36,16 @@ interface Outgoing {
|
|
|
36
36
|
{{/if}}
|
|
37
37
|
|
|
38
38
|
export async function startHttpServer(served: Served, config: AppConfig): Promise<HttpServer> {
|
|
39
|
+
{{#if files.http}}
|
|
40
|
+
{{#if docs.any}}
|
|
41
|
+
const routes = [...withDocumentation(served.routes, config), ...served.fileRoutes];
|
|
42
|
+
{{/if}}
|
|
43
|
+
{{#unless docs.any}}
|
|
44
|
+
const routes = [...served.routes, ...served.fileRoutes];
|
|
45
|
+
{{/unless}}
|
|
46
|
+
const app = await NestFactory.create(createAppModule(routes, served.boundary), { logger: ['error', 'warn'] });
|
|
47
|
+
{{/if}}
|
|
48
|
+
{{#unless files.http}}
|
|
39
49
|
{{#if docs.any}}
|
|
40
50
|
const app = await NestFactory.create(createAppModule(withDocumentation(served.routes, config), served.boundary), {
|
|
41
51
|
logger: ['error', 'warn'],
|
|
@@ -44,6 +54,7 @@ export async function startHttpServer(served: Served, config: AppConfig): Promis
|
|
|
44
54
|
{{#unless docs.any}}
|
|
45
55
|
const app = await NestFactory.create(createAppModule(served.routes, served.boundary), { logger: ['error', 'warn'] });
|
|
46
56
|
{{/unless}}
|
|
57
|
+
{{/unless}}
|
|
47
58
|
|
|
48
59
|
{{#if cache.rateLimit}}
|
|
49
60
|
app.use(
|
|
@@ -1,10 +1,16 @@
|
|
|
1
1
|
import { defineContextModule } from '@ddd-ts-kit/di';
|
|
2
|
+
{{#if files.any}}
|
|
3
|
+
import { InMemoryFileStorage } from '@ddd-ts-kit/kernel';
|
|
4
|
+
{{/if}}
|
|
2
5
|
{{#if persistence.packagedToken}}
|
|
3
6
|
import { {{persistence.token}} } from '{{persistence.tokenPackage}}';
|
|
4
7
|
{{/if}}
|
|
5
8
|
{{#if persistence.ownDatabase}}
|
|
6
9
|
import { Database } from '{{alias.platform}}/database/database{{alias.ext}}';
|
|
7
10
|
{{/if}}
|
|
11
|
+
{{#if files.any}}
|
|
12
|
+
import { bindFiles } from '{{alias.platform}}/files/bind-files{{alias.ext}}';
|
|
13
|
+
{{/if}}
|
|
8
14
|
// ddd-ts-kit:imports
|
|
9
15
|
// /ddd-ts-kit:imports
|
|
10
16
|
|
|
@@ -17,6 +23,11 @@ export const platformTestModule = defineContextModule({
|
|
|
17
23
|
throw new Error('A handler spec has no database: give a fake for the repository it uses.');
|
|
18
24
|
});
|
|
19
25
|
|
|
26
|
+
{{/if}}
|
|
27
|
+
{{#if files.any}}
|
|
28
|
+
// The files a handler spec stores stay in the memory of the test.
|
|
29
|
+
bindFiles(builder, new InMemoryFileStorage());
|
|
30
|
+
|
|
20
31
|
{{/if}}
|
|
21
32
|
// ddd-ts-kit:test-bindings
|
|
22
33
|
// /ddd-ts-kit:test-bindings
|
|
@@ -26,6 +26,9 @@ import type { Audit } from '{{alias.platform}}/audit/create-audit{{alias.ext}}';
|
|
|
26
26
|
{{/if}}
|
|
27
27
|
import type { UseCaseProtection } from '{{alias.platform}}/auth/use-case-protection{{alias.ext}}';
|
|
28
28
|
import type { Persistence } from '{{alias.platform}}/database/persistence{{alias.ext}}';
|
|
29
|
+
{{#if files.any}}
|
|
30
|
+
import type { Files } from '{{alias.platform}}/files/create-files{{alias.ext}}';
|
|
31
|
+
{{/if}}
|
|
29
32
|
|
|
30
33
|
/** Wraps an adapter from outside: a span for its kind and, for what leaves the process, the default policy unless it states its own. */
|
|
31
34
|
type Adapt = <T extends object>(instance: T, info: AspectInfo) => T;
|
|
@@ -78,4 +81,8 @@ export interface Platform {
|
|
|
78
81
|
{{#if audit.on}}
|
|
79
82
|
readonly audit: Audit;
|
|
80
83
|
{{/if}}
|
|
84
|
+
{{#if files.any}}
|
|
85
|
+
/** Where files live, the keys they are stored under and the staging that keeps them consistent with the database: never their content in a table. */
|
|
86
|
+
readonly files: Files;
|
|
87
|
+
{{/if}}
|
|
81
88
|
}
|
|
@@ -25,6 +25,9 @@ paths:
|
|
|
25
25
|
- A dependency list names port classes, imported as values, in the order of the constructor: never an adapter or a fake (`ports/value-import`, `ports/no-concrete-dependency`).
|
|
26
26
|
{{/if}}
|
|
27
27
|
- A job (`scheduling/*.job.ts`) and an integration event handler (`integration/handlers/`) only build one command and send it as a system actor; both are safe to repeat (`scheduling/job-triggers-use-case`, `events/consumer-idempotent`).
|
|
28
|
+
{{#if files.any}}
|
|
29
|
+
- A file arrives in the command as a `FileUpload` of its area, which the bus stages before the transaction under a key of `FileKeys` (never a name or a path from the user) and scans; the handler records `upload.file`, its `StoredFile`, on the aggregate and calls `FileStaging.promoteOnCommit`, which moves it to its key with the commit. A direct upload is confirmed from the `PendingUpload` the aggregate kept, never from a key the client sends. The ports are named here, never a connector (`files/port-not-connector`); `npx ddd-ts-kit generate file-upload` writes the slice.
|
|
30
|
+
{{/if}}
|
|
28
31
|
- A query never saves, deletes or publishes (`application/command-query-separation`); it hands the access scope of the actor to its read port and never filters after loading.
|
|
29
32
|
- A read that takes criteria, a scope or a page is a search, never a list (`naming/search-not-list`): `Search<Things>Query` in `queries/search-<things>/` (`generate query ... --search`), `search...` on the port, its adapters and its fakes; one element or none stays `findById` or `findBy...`.
|
|
30
33
|
{{#if literals.named}}
|
|
@@ -8,8 +8,12 @@ paths:
|
|
|
8
8
|
- An adapter `implements` a port of `application/ports/` or a repository port of `domain/repositories/`, and never `extends` it (`ports/implements-never-extends`). It never imports `presentation/` (`layer/infrastructure-no-presentation`).
|
|
9
9
|
- `persistence/` holds repository adapters, mappers and rows, `external/` the adapters of outside systems, `anti-corruption/` the translators of another model.
|
|
10
10
|
- Rows never leave `persistence/`: a mapper translates to and from the aggregate, and is the only place for a type assertion (`clean/no-escape-hatch`).
|
|
11
|
+
- The content of a file never goes in a table: no binary or base64 column (`persistence/no-file-content`). The file lives in the `FileStorage` port; a row keeps the key, size, media type and checksum of its `StoredFile`.
|
|
11
12
|
- Own tables, in the schema of the context: no join, foreign key or transaction across contexts (`context/extractable`).
|
|
12
13
|
- Audit columns and the tenant column are stamped from the execution scope with the kernel helpers (`stampCreation`, `stampUpdate`, `tenantScopeOf`), never handed over by a use case.
|
|
14
|
+
{{#if tenancy.any}}
|
|
15
|
+
- Tenant data is reached through the helpers of the ORM package, never straight from the executor: {{#if orm.knex}}`tenantTable` to read, `ownTenantTable` to update or delete{{/if}}{{#if orm.drizzle}}`tenantCondition` in the condition of a read, `ownTenantCondition` in that of an update or a delete{{/if}}, the tenant named on every insert (`tenancy/scoped-statement`). A table of no tenant is a recorded deviation. See `docs/concepts/multitenancy.md` of the kit.
|
|
16
|
+
{{/if}}
|
|
13
17
|
{{#if style.declared}}
|
|
14
18
|
- An adapter declares itself on its class, one per file (`declarations/role-declared`): `@Repository(InvoiceRepository)`, `@Gateway(Port)` or `@Adapter(Port, { kind })`. What it is given is what its constructor takes, each parameter typed by one class (`{{persistence.tokenOrNone}}`, `ExecutionScope`, `Clock`, a port, an abstract class the platform binds to a value) and nothing listed in the decorator (`declarations/constructor-injection`); an adapter that extends a class of a package writes its own constructor.
|
|
15
19
|
- Retry, timeout and cache go on the declaration (`aspects: [ownPolicy(retry(...), timeout(...))]`) or, when the policy is part of what the adapter is, as decorators of `@ddd-ts-kit/aspects/decorators` (`aspects/outside-in`). Kinds `external` and `gateway` get the default policy of the platform otherwise. `ownPolicy` is the helper of `{{alias.platform}}/resilience/own-policy{{alias.ext}}`, which `generate adapter` writes when the platform does not have it yet.
|
|
@@ -20,6 +20,9 @@ paths:
|
|
|
20
20
|
- One `Platform` is built in `create-platform.ts` and handed to each context; a port or a value is typed by an abstract class. No token helper, no injection constant; one exported class, interface or function per file.
|
|
21
21
|
- Cross-cutting behaviour lives here: spans through the members of `traceApplication()` (`create-platform.ts`), `around: tracedRoutes()` at the HTTP boundary and `tracedPublisher` for the outbox relay; logs, measures, permissions and audit as bus middlewares; the policy of outbound adapters in `resilience/`.
|
|
22
22
|
{{/unless}}
|
|
23
|
+
{{#if files.any}}
|
|
24
|
+
- Files: `files/open-files.ts` opens the connector of `stack.storage` from `config/read-files.ts`{{#if di.tsyringe}}, `files/files.module.ts` binds the ports and the hourly purge of staged files, `files/staging-uploads.ts` stages the file a command carries on the command bus, before the transaction{{/if}}{{#unless di.tsyringe}}, `files/create-files.ts` builds them, `compose-application.ts` registers the hourly purge of staged files and `create-platform.ts` stages the file a command carries on the command bus, before the transaction{{/unless}}; the tools and the tests keep files in memory. A scanner of your own (an antivirus) is bound here, never called from a use case.
|
|
25
|
+
{{/if}}
|
|
23
26
|
- `@ddd-ts-kit/tracing` and `@opentelemetry/*` are imported here and nowhere else (`tracing/platform-only`).
|
|
24
27
|
- Logs, spans and error reports carry identifiers and codes only: never arguments, payloads, error messages, nor anything personal beyond the id of the actor.
|
|
25
28
|
- Configuration is read here only, by `config/load-config.ts`: validated, fail fast, every problem named by its variable. A new variable joins `AppConfig` and `.env.example`. `RUN_JOBS` says whether a process runs the jobs, `job-settings.ts` what configuration changes of each.
|
|
@@ -23,7 +23,7 @@
|
|
|
23
23
|
"@biomejs/biome": "2.5.15",
|
|
24
24
|
"@commitlint/cli": "^21.2.3",
|
|
25
25
|
"@commitlint/config-conventional": "^21.2.3",
|
|
26
|
-
"@ddd-ts-kit/eslint-plugin": "^0.
|
|
26
|
+
"@ddd-ts-kit/eslint-plugin": "^0.9.0",
|
|
27
27
|
"@eslint/js": "^10.0.1",
|
|
28
28
|
"@typescript/native": "npm:typescript@~7.0.2",
|
|
29
29
|
"@vitest/coverage-v8": "5.0.3",
|
|
@@ -1,5 +1,10 @@
|
|
|
1
1
|
import { and, eq } from 'drizzle-orm';
|
|
2
|
+
{{#if tenant.scoped}}
|
|
3
|
+
import { optimisticUpdate, ownTenantCondition, tenantCondition } from '@ddd-ts-kit/drizzle/postgres';
|
|
4
|
+
{{/if}}
|
|
5
|
+
{{#unless tenant.scoped}}
|
|
2
6
|
import { optimisticUpdate } from '@ddd-ts-kit/drizzle/postgres';
|
|
7
|
+
{{/unless}}
|
|
3
8
|
{{#if tenant.scoped}}
|
|
4
9
|
import { stampCreation, stampUpdate, tenantScopeOf, type Clock, type ExecutionScope } from '@ddd-ts-kit/kernel';
|
|
5
10
|
{{/if}}
|
|
@@ -34,7 +39,7 @@ export class Drizzle{{aggregate.pascal}}Repository implements {{aggregate.pascal
|
|
|
34
39
|
|
|
35
40
|
async findById(id: string): Promise<{{aggregate.pascal}} | undefined> {
|
|
36
41
|
{{#if tenant.scoped}}
|
|
37
|
-
const mine = and(eq({{aggregate.tableCamel}}.id, id), this.
|
|
42
|
+
const mine = and(eq({{aggregate.tableCamel}}.id, id), tenantCondition({{aggregate.tableCamel}}.tenantId, this.scope));
|
|
38
43
|
const [row] = await this.database.executor().select().from({{aggregate.tableCamel}}).where(mine);
|
|
39
44
|
{{/if}}
|
|
40
45
|
{{#unless tenant.scoped}}
|
|
@@ -70,13 +75,7 @@ export class Drizzle{{aggregate.pascal}}Repository implements {{aggregate.pascal
|
|
|
70
75
|
.executor()
|
|
71
76
|
.update({{aggregate.tableCamel}})
|
|
72
77
|
.set({ ...state, ...stampUpdate(this.scope, this.clock) })
|
|
73
|
-
.where(and(sameId, sameVersion{{#if tenant.scoped}}, this.
|
|
78
|
+
.where(and(sameId, sameVersion{{#if tenant.scoped}}, ownTenantCondition({{aggregate.tableCamel}}.tenantId, this.scope){{/if}}))
|
|
74
79
|
.returning({ id: {{aggregate.tableCamel}}.id });
|
|
75
80
|
}
|
|
76
|
-
{{#if tenant.scoped}}
|
|
77
|
-
|
|
78
|
-
private ownTenant() {
|
|
79
|
-
return eq({{aggregate.tableCamel}}.tenantId, tenantScopeOf(this.scope).tenantId);
|
|
80
|
-
}
|
|
81
|
-
{{/if}}
|
|
82
81
|
}
|
|
@@ -1,4 +1,6 @@
|
|
|
1
|
+
{{#unless tenant.scoped}}
|
|
1
2
|
import type { Knex } from 'knex';
|
|
3
|
+
{{/unless}}
|
|
2
4
|
{{#if tenant.scoped}}
|
|
3
5
|
import { stampCreation, stampUpdate, tenantScopeOf, type Clock, type ExecutionScope } from '@ddd-ts-kit/kernel';
|
|
4
6
|
{{/if}}
|
|
@@ -14,8 +16,9 @@ import {
|
|
|
14
16
|
auditRow,
|
|
15
17
|
insertIgnoringConflict,
|
|
16
18
|
optimisticUpdate,
|
|
19
|
+
ownTenantTable,
|
|
17
20
|
tenantRow,
|
|
18
|
-
|
|
21
|
+
tenantTable,
|
|
19
22
|
} from '@ddd-ts-kit/knex';
|
|
20
23
|
{{/if}}
|
|
21
24
|
{{#unless tenant.scoped}}
|
|
@@ -33,6 +36,11 @@ import { {{aggregate.pascal}}Mapper } from '{{alias.context}}/infrastructure/per
|
|
|
33
36
|
import type { {{aggregate.pascal}}Columns, {{aggregate.pascal}}Row } from '{{alias.context}}/infrastructure/persistence/{{aggregate.kebab}}-row.schema{{alias.ext}}';
|
|
34
37
|
|
|
35
38
|
type Versioned{{aggregate.pascal}} = {{aggregate.pascal}}Columns & { readonly version: number };
|
|
39
|
+
{{#if tenant.scoped}}
|
|
40
|
+
|
|
41
|
+
/** Tenant data: every statement reaches it through `tenantTable` or `ownTenantTable`, confined to the tenant of the work. */
|
|
42
|
+
const TABLE = { schema: {{context.constant}}_SCHEMA, table: '{{aggregate.table}}' };
|
|
43
|
+
{{/if}}
|
|
36
44
|
|
|
37
45
|
{{#if style.declared}}
|
|
38
46
|
@Repository({{aggregate.pascal}}Repository)
|
|
@@ -46,9 +54,7 @@ export class Knex{{aggregate.pascal}}Repository implements {{aggregate.pascal}}R
|
|
|
46
54
|
|
|
47
55
|
async findById(id: string): Promise<{{aggregate.pascal}} | undefined> {
|
|
48
56
|
{{#if tenant.scoped}}
|
|
49
|
-
const row = await
|
|
50
|
-
.where({ id, ...this.ownTenant() })
|
|
51
|
-
.first();
|
|
57
|
+
const row = await tenantTable<{{aggregate.pascal}}Row>(this.database, this.scope, TABLE).where({ id }).first();
|
|
52
58
|
{{/if}}
|
|
53
59
|
{{#unless tenant.scoped}}
|
|
54
60
|
const row = await tableOf(this.database).where({ id }).first();
|
|
@@ -65,8 +71,27 @@ export class Knex{{aggregate.pascal}}Repository implements {{aggregate.pascal}}R
|
|
|
65
71
|
});
|
|
66
72
|
}
|
|
67
73
|
|
|
74
|
+
{{#if tenant.scoped}}
|
|
68
75
|
private async insert(state: Versioned{{aggregate.pascal}}): Promise<number> {
|
|
69
|
-
const
|
|
76
|
+
const tenant = tenantRow(tenantScopeOf(this.scope));
|
|
77
|
+
const row = { ...state, ...tenant, ...auditRow(stampCreation(this.scope, this.clock)) };
|
|
78
|
+
const table = ownTenantTable<{{aggregate.pascal}}Row>(this.database, this.scope, TABLE);
|
|
79
|
+
|
|
80
|
+
const inserted = await insertIgnoringConflict(this.database.executor(), table, { row, conflict: 'id' });
|
|
81
|
+
|
|
82
|
+
return inserted ? 1 : 0;
|
|
83
|
+
}
|
|
84
|
+
|
|
85
|
+
private async update(state: Versioned{{aggregate.pascal}}, expected: number): Promise<number> {
|
|
86
|
+
return ownTenantTable<{{aggregate.pascal}}Row>(this.database, this.scope, TABLE)
|
|
87
|
+
.where({ id: state.id, version: expected })
|
|
88
|
+
.update({ ...state, ...auditRow(stampUpdate(this.scope, this.clock)) });
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
{{/if}}
|
|
92
|
+
{{#unless tenant.scoped}}
|
|
93
|
+
private async insert(state: Versioned{{aggregate.pascal}}): Promise<number> {
|
|
94
|
+
const row = { ...state, ...auditRow(stampCreation(this.scope, this.clock)) };
|
|
70
95
|
|
|
71
96
|
const inserted = await insertIgnoringConflict(this.database.executor(), tableOf(this.database), {
|
|
72
97
|
row,
|
|
@@ -78,15 +103,9 @@ export class Knex{{aggregate.pascal}}Repository implements {{aggregate.pascal}}R
|
|
|
78
103
|
|
|
79
104
|
private async update(state: Versioned{{aggregate.pascal}}, expected: number): Promise<number> {
|
|
80
105
|
return tableOf(this.database)
|
|
81
|
-
.where({ id: state.id, version: expected
|
|
106
|
+
.where({ id: state.id, version: expected })
|
|
82
107
|
.update({ ...state, ...auditRow(stampUpdate(this.scope, this.clock)) });
|
|
83
108
|
}
|
|
84
|
-
{{#if tenant.scoped}}
|
|
85
|
-
|
|
86
|
-
private ownTenant(): TenantRow {
|
|
87
|
-
return tenantRow(tenantScopeOf(this.scope));
|
|
88
|
-
}
|
|
89
|
-
{{/if}}
|
|
90
109
|
}
|
|
91
110
|
|
|
92
111
|
/** A function and not a method: a query builder is thenable, and a method that answers one is read as asynchronous by whatever wraps the adapter (tracing), which then answers a promise. */
|
|
@@ -95,3 +114,4 @@ function tableOf(database: KnexContext): Knex.QueryBuilder<{{aggregate.pascal}}R
|
|
|
95
114
|
|
|
96
115
|
return table.withSchema({{context.constant}}_SCHEMA);
|
|
97
116
|
}
|
|
117
|
+
{{/unless}}
|
|
@@ -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}}
|
|
@@ -52,6 +55,9 @@ const application = composeApplication(
|
|
|
52
55
|
{{#if cache.redis}}
|
|
53
56
|
redis,
|
|
54
57
|
{{/if}}
|
|
58
|
+
{{#if files.any}}
|
|
59
|
+
files: openFiles(config.files),
|
|
60
|
+
{{/if}}
|
|
55
61
|
},
|
|
56
62
|
{{service.selection}}
|
|
57
63
|
);
|