ddd-express 0.0.0-stage → 1.0.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +34 -0
- package/LICENSE +21 -0
- package/README.ar.md +117 -0
- package/README.md +314 -2
- package/dist/analyze/check.d.ts +74 -0
- package/dist/analyze/check.js +503 -0
- package/dist/analyze/document.d.ts +26 -0
- package/dist/analyze/document.js +126 -0
- package/dist/analyze/map.d.ts +19 -0
- package/dist/analyze/map.js +283 -0
- package/dist/analyze/openapi.d.ts +7 -0
- package/dist/analyze/openapi.js +236 -0
- package/dist/cli.d.ts +2 -0
- package/dist/cli.js +12 -0
- package/dist/core/errors.d.ts +5 -0
- package/dist/core/errors.js +13 -0
- package/dist/core/fields.d.ts +38 -0
- package/dist/core/fields.js +233 -0
- package/dist/core/markers.d.ts +25 -0
- package/dist/core/markers.js +60 -0
- package/dist/core/names.d.ts +30 -0
- package/dist/core/names.js +168 -0
- package/dist/core/output.d.ts +21 -0
- package/dist/core/output.js +29 -0
- package/dist/core/project.d.ts +135 -0
- package/dist/core/project.js +287 -0
- package/dist/core/writer.d.ts +39 -0
- package/dist/core/writer.js +138 -0
- package/dist/generators/analyze.d.ts +16 -0
- package/dist/generators/analyze.js +174 -0
- package/dist/generators/apply.d.ts +28 -0
- package/dist/generators/apply.js +234 -0
- package/dist/generators/common.d.ts +49 -0
- package/dist/generators/common.js +173 -0
- package/dist/generators/domain.d.ts +30 -0
- package/dist/generators/domain.js +198 -0
- package/dist/generators/engine.d.ts +71 -0
- package/dist/generators/engine.js +552 -0
- package/dist/generators/project.d.ts +10 -0
- package/dist/generators/project.js +214 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.js +14 -0
- package/dist/model/commands.d.ts +28 -0
- package/dist/model/commands.js +228 -0
- package/dist/model/examples.d.ts +6 -0
- package/dist/model/examples.js +152 -0
- package/dist/model/parser.d.ts +28 -0
- package/dist/model/parser.js +222 -0
- package/dist/model/types.d.ts +98 -0
- package/dist/model/types.js +2 -0
- package/dist/model/validate.d.ts +30 -0
- package/dist/model/validate.js +215 -0
- package/dist/run.d.ts +4 -0
- package/dist/run.js +506 -0
- package/dist/scan/project-scan.d.ts +77 -0
- package/dist/scan/project-scan.js +226 -0
- package/dist/scan/source.d.ts +60 -0
- package/dist/scan/source.js +274 -0
- package/dist/templates/aggregate.d.ts +51 -0
- package/dist/templates/aggregate.js +407 -0
- package/dist/templates/building-blocks.d.ts +30 -0
- package/dist/templates/building-blocks.js +155 -0
- package/dist/templates/code.d.ts +14 -0
- package/dist/templates/code.js +51 -0
- package/dist/templates/command.d.ts +47 -0
- package/dist/templates/command.js +376 -0
- package/dist/templates/context.d.ts +16 -0
- package/dist/templates/context.js +43 -0
- package/dist/templates/field-code.d.ts +47 -0
- package/dist/templates/field-code.js +227 -0
- package/dist/templates/postgres.d.ts +25 -0
- package/dist/templates/postgres.js +113 -0
- package/dist/templates/project.d.ts +20 -0
- package/dist/templates/project.js +153 -0
- package/dist/templates/shared-kernel.d.ts +6 -0
- package/dist/templates/shared-kernel.js +44 -0
- package/package.json +58 -4
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
import { CliError } from '../core/errors';
|
|
2
|
+
import type { DomainModel } from './types';
|
|
3
|
+
/**
|
|
4
|
+
* The domain model language (domain.ddd). One statement per line; indentation shows what belongs
|
|
5
|
+
* to what. Example:
|
|
6
|
+
*
|
|
7
|
+
* context Scheduling
|
|
8
|
+
* aggregate Visit
|
|
9
|
+
* clientId: string
|
|
10
|
+
* status: scheduled | completed | cancelled = scheduled
|
|
11
|
+
* command complete(minutes: integer): scheduled -> completed emits VisitCompleted
|
|
12
|
+
* value VisitTime
|
|
13
|
+
* start: date
|
|
14
|
+
* end: date
|
|
15
|
+
*
|
|
16
|
+
* context Billing
|
|
17
|
+
* on Scheduling.VisitCompleted -> CreateInvoice
|
|
18
|
+
*/
|
|
19
|
+
export interface ModelProblem {
|
|
20
|
+
line: number;
|
|
21
|
+
message: string;
|
|
22
|
+
}
|
|
23
|
+
/** Thrown when the model has mistakes; lists all of them with their line numbers. */
|
|
24
|
+
export declare class ModelError extends CliError {
|
|
25
|
+
readonly problems: ModelProblem[];
|
|
26
|
+
constructor(file: string, problems: ModelProblem[]);
|
|
27
|
+
}
|
|
28
|
+
export declare function parseModel(source: string, file?: string): DomainModel;
|
|
@@ -0,0 +1,222 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.ModelError = void 0;
|
|
4
|
+
exports.parseModel = parseModel;
|
|
5
|
+
const errors_1 = require("../core/errors");
|
|
6
|
+
const fields_1 = require("../core/fields");
|
|
7
|
+
const names_1 = require("../core/names");
|
|
8
|
+
const commands_1 = require("./commands");
|
|
9
|
+
/** Thrown when the model has mistakes; lists all of them with their line numbers. */
|
|
10
|
+
class ModelError extends errors_1.CliError {
|
|
11
|
+
problems;
|
|
12
|
+
constructor(file, problems) {
|
|
13
|
+
const list = problems
|
|
14
|
+
.slice(0, 30)
|
|
15
|
+
.map((problem) => ` ${file}:${problem.line} ${problem.message}`)
|
|
16
|
+
.join('\n');
|
|
17
|
+
super(`${file} has ${problems.length} problem${problems.length === 1 ? '' : 's'}:\n${list}${problems.length > 30 ? '\n ...' : ''}`, 'The model language is described in https://github.com/Moustafa1997/ddd-express/blob/main/docs/model-language.md');
|
|
18
|
+
this.problems = problems;
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
exports.ModelError = ModelError;
|
|
22
|
+
function stripComment(line) {
|
|
23
|
+
let quote = null;
|
|
24
|
+
for (let i = 0; i < line.length; i += 1) {
|
|
25
|
+
const char = line[i];
|
|
26
|
+
if (quote) {
|
|
27
|
+
if (char === quote)
|
|
28
|
+
quote = null;
|
|
29
|
+
continue;
|
|
30
|
+
}
|
|
31
|
+
if (char === '"' || char === "'")
|
|
32
|
+
quote = char;
|
|
33
|
+
else if (char === '#')
|
|
34
|
+
return line.slice(0, i);
|
|
35
|
+
}
|
|
36
|
+
return line;
|
|
37
|
+
}
|
|
38
|
+
const NAME = '[A-Za-z][A-Za-z0-9_-]*';
|
|
39
|
+
function parseModel(source, file = 'domain.ddd') {
|
|
40
|
+
const problems = [];
|
|
41
|
+
const model = { file, contexts: [] };
|
|
42
|
+
const stack = [{ kind: 'root', indent: -1 }];
|
|
43
|
+
const lines = source.replace(/\r\n?/g, '\n').split('\n');
|
|
44
|
+
const names = (raw, what, lineNo, suffix) => {
|
|
45
|
+
try {
|
|
46
|
+
return (0, names_1.toNames)(raw, what, suffix);
|
|
47
|
+
}
|
|
48
|
+
catch (error) {
|
|
49
|
+
problems.push({ line: lineNo, message: error.message });
|
|
50
|
+
return null;
|
|
51
|
+
}
|
|
52
|
+
};
|
|
53
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
54
|
+
const lineNo = index + 1;
|
|
55
|
+
const raw = stripComment(lines[index]);
|
|
56
|
+
if (raw.trim() === '')
|
|
57
|
+
continue;
|
|
58
|
+
if (/^\s*\t/.test(raw) || /^ *\t/.test(raw)) {
|
|
59
|
+
problems.push({ line: lineNo, message: 'Use spaces, not tabs, to indent.' });
|
|
60
|
+
continue;
|
|
61
|
+
}
|
|
62
|
+
const indent = raw.length - raw.trimStart().length;
|
|
63
|
+
const text = raw.trim();
|
|
64
|
+
while (stack.length > 1 && stack[stack.length - 1].indent >= indent)
|
|
65
|
+
stack.pop();
|
|
66
|
+
const parent = stack[stack.length - 1];
|
|
67
|
+
const keyword = /^(\w+)/.exec(text)?.[1] ?? '';
|
|
68
|
+
const push = (node) => stack.push(node);
|
|
69
|
+
const misplaced = (what, where) => problems.push({ line: lineNo, message: `"${what}" belongs inside ${where}.` });
|
|
70
|
+
try {
|
|
71
|
+
if (keyword === 'context') {
|
|
72
|
+
const match = new RegExp(`^context\\s+(${NAME})$`).exec(text);
|
|
73
|
+
if (!match)
|
|
74
|
+
throw new errors_1.CliError('Write "context <Name>", e.g. "context Scheduling".');
|
|
75
|
+
if (parent.kind !== 'root') {
|
|
76
|
+
problems.push({ line: lineNo, message: '"context" starts a new block: do not indent it.' });
|
|
77
|
+
continue;
|
|
78
|
+
}
|
|
79
|
+
const n = names(match[1], 'context name', lineNo);
|
|
80
|
+
if (!n)
|
|
81
|
+
continue;
|
|
82
|
+
const context = { line: lineNo, names: n, aggregates: [], valueObjects: [], events: [], services: [], policies: [] };
|
|
83
|
+
model.contexts.push(context);
|
|
84
|
+
push({ kind: 'context', indent, value: context });
|
|
85
|
+
}
|
|
86
|
+
else if (keyword === 'aggregate') {
|
|
87
|
+
const match = new RegExp(`^aggregate\\s+(${NAME})(?:\\s+at\\s+(\\S+))?$`).exec(text);
|
|
88
|
+
if (!match)
|
|
89
|
+
throw new errors_1.CliError('Write "aggregate <Name>" or "aggregate <Name> at /route".');
|
|
90
|
+
if (parent.kind !== 'context') {
|
|
91
|
+
misplaced('aggregate', 'a context');
|
|
92
|
+
continue;
|
|
93
|
+
}
|
|
94
|
+
const n = names(match[1], 'aggregate name', lineNo);
|
|
95
|
+
if (!n)
|
|
96
|
+
continue;
|
|
97
|
+
const aggregate = { line: lineNo, names: n, route: match[2] ?? null, fields: [], entities: [], commands: [] };
|
|
98
|
+
parent.value.aggregates.push(aggregate);
|
|
99
|
+
push({ kind: 'aggregate', indent, value: aggregate });
|
|
100
|
+
}
|
|
101
|
+
else if (keyword === 'entity') {
|
|
102
|
+
const match = new RegExp(`^entity\\s+(${NAME})$`).exec(text);
|
|
103
|
+
if (!match)
|
|
104
|
+
throw new errors_1.CliError('Write "entity <Name>".');
|
|
105
|
+
if (parent.kind !== 'aggregate') {
|
|
106
|
+
misplaced('entity', 'an aggregate');
|
|
107
|
+
continue;
|
|
108
|
+
}
|
|
109
|
+
const n = names(match[1], 'entity name', lineNo);
|
|
110
|
+
if (!n)
|
|
111
|
+
continue;
|
|
112
|
+
const entity = { line: lineNo, names: n, fields: [] };
|
|
113
|
+
parent.value.entities.push(entity);
|
|
114
|
+
push({ kind: 'entity', indent, value: entity });
|
|
115
|
+
}
|
|
116
|
+
else if (keyword === 'command') {
|
|
117
|
+
if (parent.kind !== 'aggregate') {
|
|
118
|
+
misplaced('command', 'an aggregate');
|
|
119
|
+
continue;
|
|
120
|
+
}
|
|
121
|
+
parent.value.commands.push((0, commands_1.parseCommand)(text.slice('command'.length), lineNo));
|
|
122
|
+
push({ kind: 'leaf', indent });
|
|
123
|
+
}
|
|
124
|
+
else if (keyword === 'value') {
|
|
125
|
+
const match = new RegExp(`^value\\s+(${NAME})(?:\\s*:\\s*(.+))?$`).exec(text);
|
|
126
|
+
if (!match)
|
|
127
|
+
throw new errors_1.CliError('Write "value <Name>: <type>" or "value <Name>" followed by its fields.');
|
|
128
|
+
if (parent.kind !== 'context') {
|
|
129
|
+
misplaced('value', 'a context');
|
|
130
|
+
continue;
|
|
131
|
+
}
|
|
132
|
+
const n = names(match[1], 'value object name', lineNo);
|
|
133
|
+
if (!n)
|
|
134
|
+
continue;
|
|
135
|
+
const single = match[2] ? (0, fields_1.parseField)(`${n.camel}: ${match[2]}`) : null;
|
|
136
|
+
if (single?.optional || single?.defaultValue !== undefined) {
|
|
137
|
+
throw new errors_1.CliError('A single-value value object cannot be optional or have a default.');
|
|
138
|
+
}
|
|
139
|
+
const value = { line: lineNo, names: n, single, fields: [] };
|
|
140
|
+
parent.value.valueObjects.push(value);
|
|
141
|
+
push(single ? { kind: 'leaf', indent } : { kind: 'value', indent, value });
|
|
142
|
+
}
|
|
143
|
+
else if (keyword === 'event') {
|
|
144
|
+
const match = new RegExp(`^event\\s+(${NAME})(?:\\s*\\((.*)\\))?$`).exec(text);
|
|
145
|
+
if (!match)
|
|
146
|
+
throw new errors_1.CliError('Write "event <Name>" or "event <Name>(field: type, ...)".');
|
|
147
|
+
if (parent.kind !== 'context') {
|
|
148
|
+
misplaced('event', 'a context');
|
|
149
|
+
continue;
|
|
150
|
+
}
|
|
151
|
+
const n = names(match[1], 'event name', lineNo, ['event']);
|
|
152
|
+
if (!n)
|
|
153
|
+
continue;
|
|
154
|
+
const fields = match[2] ? (0, fields_1.splitTopLevel)(match[2]).map((token) => (0, fields_1.parseField)(token)) : [];
|
|
155
|
+
const event = { line: lineNo, names: n, fields };
|
|
156
|
+
parent.value.events.push(event);
|
|
157
|
+
push({ kind: 'leaf', indent });
|
|
158
|
+
}
|
|
159
|
+
else if (keyword === 'service') {
|
|
160
|
+
const match = new RegExp(`^service\\s+(${NAME})$`).exec(text);
|
|
161
|
+
if (!match)
|
|
162
|
+
throw new errors_1.CliError('Write "service <Name>".');
|
|
163
|
+
if (parent.kind !== 'context') {
|
|
164
|
+
misplaced('service', 'a context');
|
|
165
|
+
continue;
|
|
166
|
+
}
|
|
167
|
+
const n = names(match[1], 'service name', lineNo, ['service']);
|
|
168
|
+
if (!n)
|
|
169
|
+
continue;
|
|
170
|
+
const service = { line: lineNo, names: n };
|
|
171
|
+
parent.value.services.push(service);
|
|
172
|
+
push({ kind: 'leaf', indent });
|
|
173
|
+
}
|
|
174
|
+
else if (keyword === 'on') {
|
|
175
|
+
const match = new RegExp(`^on\\s+(?:(${NAME})\\.)?(${NAME})(?:\\s*->\\s*(${NAME}))?$`).exec(text);
|
|
176
|
+
if (!match)
|
|
177
|
+
throw new errors_1.CliError('Write "on <Event> -> <Handler>" or "on <Context>.<Event> -> <Handler>".');
|
|
178
|
+
if (parent.kind !== 'context') {
|
|
179
|
+
misplaced('on', 'the context that reacts');
|
|
180
|
+
continue;
|
|
181
|
+
}
|
|
182
|
+
const fromContext = match[1] ? names(match[1], 'context name', lineNo) : null;
|
|
183
|
+
const event = names(match[2], 'event name', lineNo, ['event']);
|
|
184
|
+
const handler = match[3] ? names(match[3], 'handler name', lineNo) : null;
|
|
185
|
+
if (!event)
|
|
186
|
+
continue;
|
|
187
|
+
const policy = { line: lineNo, fromContext, event, handler };
|
|
188
|
+
parent.value.policies.push(policy);
|
|
189
|
+
push({ kind: 'leaf', indent });
|
|
190
|
+
}
|
|
191
|
+
else if (/^[A-Za-z][\w-]*\??\s*:/.test(text)) {
|
|
192
|
+
let target = null;
|
|
193
|
+
if (parent.kind === 'aggregate' || parent.kind === 'entity' || parent.kind === 'value')
|
|
194
|
+
target = parent.value.fields;
|
|
195
|
+
if (!target) {
|
|
196
|
+
misplaced('field', 'an aggregate, an entity or a value object');
|
|
197
|
+
continue;
|
|
198
|
+
}
|
|
199
|
+
const field = (0, fields_1.parseField)(text);
|
|
200
|
+
if (target.some((existing) => existing.names.camel === field.names.camel)) {
|
|
201
|
+
throw new errors_1.CliError(`Field "${field.names.camel}" is declared twice.`);
|
|
202
|
+
}
|
|
203
|
+
target.push(field);
|
|
204
|
+
push({ kind: 'leaf', indent });
|
|
205
|
+
}
|
|
206
|
+
else {
|
|
207
|
+
throw new errors_1.CliError(`Unknown statement "${text}".`, 'Lines start with context, aggregate, entity, command, value, event, service, on — or are fields ("name: type").');
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
catch (error) {
|
|
211
|
+
if (!(error instanceof errors_1.CliError))
|
|
212
|
+
throw error;
|
|
213
|
+
problems.push({ line: lineNo, message: error.message + (error.hint ? ` ${error.hint}` : '') });
|
|
214
|
+
}
|
|
215
|
+
}
|
|
216
|
+
if (problems.length === 0 && model.contexts.length === 0) {
|
|
217
|
+
problems.push({ line: 1, message: 'The model is empty: start with "context <Name>".' });
|
|
218
|
+
}
|
|
219
|
+
if (problems.length > 0)
|
|
220
|
+
throw new ModelError(file, problems);
|
|
221
|
+
return model;
|
|
222
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type { DefaultValue, Field } from '../core/fields';
|
|
2
|
+
import type { Names } from '../core/names';
|
|
3
|
+
/** One value carried by an event a command records. */
|
|
4
|
+
export interface PayloadItem {
|
|
5
|
+
name: string;
|
|
6
|
+
/** Where the value comes from: a command parameter, or a field of the aggregate (after the change). */
|
|
7
|
+
source: 'param' | 'field';
|
|
8
|
+
field: Field;
|
|
9
|
+
}
|
|
10
|
+
/** A behaviour of an aggregate, resolved against its fields. */
|
|
11
|
+
export interface CommandSpec {
|
|
12
|
+
names: Names;
|
|
13
|
+
params: Field[];
|
|
14
|
+
/** State change on the aggregate's state field (the field marked `managed`). */
|
|
15
|
+
transition: {
|
|
16
|
+
from: string[];
|
|
17
|
+
to: string;
|
|
18
|
+
} | null;
|
|
19
|
+
/** The event recorded by the command. */
|
|
20
|
+
emits: {
|
|
21
|
+
names: Names;
|
|
22
|
+
payload: PayloadItem[];
|
|
23
|
+
} | null;
|
|
24
|
+
/** Parameters that replace the aggregate field with the same name. */
|
|
25
|
+
assigns: string[];
|
|
26
|
+
/** Fields set to a fixed value ("sets paid = true"); null clears an optional field; { now } is the current time. */
|
|
27
|
+
sets: {
|
|
28
|
+
name: string;
|
|
29
|
+
field: Field;
|
|
30
|
+
value: DefaultValue | null;
|
|
31
|
+
}[];
|
|
32
|
+
}
|
|
33
|
+
/** A command as written by the user, before it is resolved against an aggregate. */
|
|
34
|
+
export interface RawCommand {
|
|
35
|
+
line: number;
|
|
36
|
+
text: string;
|
|
37
|
+
names: Names;
|
|
38
|
+
params: Field[];
|
|
39
|
+
from: string[] | null;
|
|
40
|
+
to: string | null;
|
|
41
|
+
emits: {
|
|
42
|
+
name: string;
|
|
43
|
+
fields: string[] | null;
|
|
44
|
+
} | null;
|
|
45
|
+
sets: {
|
|
46
|
+
name: string;
|
|
47
|
+
raw: string;
|
|
48
|
+
}[];
|
|
49
|
+
}
|
|
50
|
+
export interface ModelEntity {
|
|
51
|
+
line: number;
|
|
52
|
+
names: Names;
|
|
53
|
+
fields: Field[];
|
|
54
|
+
}
|
|
55
|
+
export interface ModelAggregate {
|
|
56
|
+
line: number;
|
|
57
|
+
names: Names;
|
|
58
|
+
route: string | null;
|
|
59
|
+
fields: Field[];
|
|
60
|
+
entities: ModelEntity[];
|
|
61
|
+
commands: RawCommand[];
|
|
62
|
+
}
|
|
63
|
+
export interface ModelValueObject {
|
|
64
|
+
line: number;
|
|
65
|
+
names: Names;
|
|
66
|
+
/** One value (`value Email: string`) or several (`value VisitTime` + fields). */
|
|
67
|
+
single: Field | null;
|
|
68
|
+
fields: Field[];
|
|
69
|
+
}
|
|
70
|
+
export interface ModelEvent {
|
|
71
|
+
line: number;
|
|
72
|
+
names: Names;
|
|
73
|
+
fields: Field[];
|
|
74
|
+
}
|
|
75
|
+
export interface ModelService {
|
|
76
|
+
line: number;
|
|
77
|
+
names: Names;
|
|
78
|
+
}
|
|
79
|
+
export interface ModelPolicy {
|
|
80
|
+
line: number;
|
|
81
|
+
/** Context that publishes the event, when written as Context.Event. */
|
|
82
|
+
fromContext: Names | null;
|
|
83
|
+
event: Names;
|
|
84
|
+
handler: Names | null;
|
|
85
|
+
}
|
|
86
|
+
export interface ModelContext {
|
|
87
|
+
line: number;
|
|
88
|
+
names: Names;
|
|
89
|
+
aggregates: ModelAggregate[];
|
|
90
|
+
valueObjects: ModelValueObject[];
|
|
91
|
+
events: ModelEvent[];
|
|
92
|
+
services: ModelService[];
|
|
93
|
+
policies: ModelPolicy[];
|
|
94
|
+
}
|
|
95
|
+
export interface DomainModel {
|
|
96
|
+
file: string;
|
|
97
|
+
contexts: ModelContext[];
|
|
98
|
+
}
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
import { type Field } from '../core/fields';
|
|
2
|
+
import { type Names } from '../core/names';
|
|
3
|
+
import type { CommandSpec, DomainModel, ModelAggregate, ModelContext, ModelPolicy } from './types';
|
|
4
|
+
export interface ResolvedAggregate {
|
|
5
|
+
model: ModelAggregate;
|
|
6
|
+
fields: Field[];
|
|
7
|
+
commands: CommandSpec[];
|
|
8
|
+
route: string;
|
|
9
|
+
}
|
|
10
|
+
export interface KnownEvent {
|
|
11
|
+
names: Names;
|
|
12
|
+
/** Payload fields (without aggregateId), when known. */
|
|
13
|
+
fields: Field[];
|
|
14
|
+
origin: string;
|
|
15
|
+
}
|
|
16
|
+
export interface ResolvedPolicy {
|
|
17
|
+
context: ModelContext;
|
|
18
|
+
policy: ModelPolicy;
|
|
19
|
+
/** The context that publishes the event, when the model knows it (otherwise found in the code). */
|
|
20
|
+
publisher: Names | null;
|
|
21
|
+
}
|
|
22
|
+
export interface ResolvedModel {
|
|
23
|
+
model: DomainModel;
|
|
24
|
+
aggregates: Map<ModelAggregate, ResolvedAggregate>;
|
|
25
|
+
/** Events each context publishes: context kebab → event Pascal → event. */
|
|
26
|
+
events: Map<string, Map<string, KnownEvent>>;
|
|
27
|
+
policies: ResolvedPolicy[];
|
|
28
|
+
}
|
|
29
|
+
/** Checks the whole model and resolves commands, state fields and policies. Throws ModelError. */
|
|
30
|
+
export declare function validateModel(model: DomainModel): ResolvedModel;
|
|
@@ -0,0 +1,215 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.validateModel = validateModel;
|
|
4
|
+
const errors_1 = require("../core/errors");
|
|
5
|
+
const fields_1 = require("../core/fields");
|
|
6
|
+
const names_1 = require("../core/names");
|
|
7
|
+
const aggregate_1 = require("../templates/aggregate");
|
|
8
|
+
const building_blocks_1 = require("../templates/building-blocks");
|
|
9
|
+
const commands_1 = require("./commands");
|
|
10
|
+
const parser_1 = require("./parser");
|
|
11
|
+
const sameFieldType = (a, b) => a.type === b.type && a.optional === b.optional && a.values.join('|') === b.values.join('|');
|
|
12
|
+
/** Checks the whole model and resolves commands, state fields and policies. Throws ModelError. */
|
|
13
|
+
function validateModel(model) {
|
|
14
|
+
const problems = [];
|
|
15
|
+
const problem = (line, message) => problems.push({ line, message });
|
|
16
|
+
const guard = (line, fn) => {
|
|
17
|
+
try {
|
|
18
|
+
fn();
|
|
19
|
+
}
|
|
20
|
+
catch (error) {
|
|
21
|
+
if (!(error instanceof errors_1.CliError))
|
|
22
|
+
throw error;
|
|
23
|
+
problem(line, error.message + (error.hint ? ` ${error.hint}` : ''));
|
|
24
|
+
}
|
|
25
|
+
};
|
|
26
|
+
const resolved = { model, aggregates: new Map(), events: new Map(), policies: [] };
|
|
27
|
+
const contexts = new Map();
|
|
28
|
+
const routes = new Map();
|
|
29
|
+
for (const context of model.contexts) {
|
|
30
|
+
if (contexts.has(context.names.kebab))
|
|
31
|
+
problem(context.line, `The context ${context.names.pascal} is declared twice.`);
|
|
32
|
+
if (context.names.kebab === 'shared')
|
|
33
|
+
problem(context.line, '"shared" is reserved for the shared kernel.');
|
|
34
|
+
guard(context.line, () => (0, names_1.assertNotReserved)(context.names, 'context name'));
|
|
35
|
+
contexts.set(context.names.kebab, context);
|
|
36
|
+
const events = new Map();
|
|
37
|
+
resolved.events.set(context.names.kebab, events);
|
|
38
|
+
// one name, one meaning inside a context
|
|
39
|
+
const declared = new Map();
|
|
40
|
+
const claim = (names, kind, line) => {
|
|
41
|
+
const previous = declared.get(names.pascal);
|
|
42
|
+
if (previous)
|
|
43
|
+
problem(line, `"${names.pascal}" is already declared in ${context.names.pascal} (as ${previous}).`);
|
|
44
|
+
else
|
|
45
|
+
declared.set(names.pascal, kind);
|
|
46
|
+
};
|
|
47
|
+
for (const event of context.events) {
|
|
48
|
+
claim(event.names, 'an event', event.line);
|
|
49
|
+
guard(event.line, () => (0, names_1.assertNotReserved)(event.names, 'event name'));
|
|
50
|
+
events.set(event.names.pascal, { names: event.names, fields: event.fields, origin: `event ${event.names.pascal}` });
|
|
51
|
+
}
|
|
52
|
+
for (const value of context.valueObjects) {
|
|
53
|
+
claim(value.names, 'a value object', value.line);
|
|
54
|
+
guard(value.line, () => (0, names_1.assertNotReserved)(value.names, 'value object name'));
|
|
55
|
+
if (!value.single && value.fields.length === 0)
|
|
56
|
+
problem(value.line, `Value object ${value.names.pascal} has no fields.`);
|
|
57
|
+
if (value.fields.some((field) => typeof field.defaultValue === 'object')) {
|
|
58
|
+
problem(value.line, `${value.names.pascal}: "= now" is only supported on aggregate fields.`);
|
|
59
|
+
}
|
|
60
|
+
}
|
|
61
|
+
for (const service of context.services) {
|
|
62
|
+
claim({ ...service.names, pascal: `${service.names.pascal}Service` }, 'a service', service.line);
|
|
63
|
+
}
|
|
64
|
+
for (const aggregate of context.aggregates) {
|
|
65
|
+
claim(aggregate.names, 'an aggregate', aggregate.line);
|
|
66
|
+
for (const suffix of ['Created', 'Updated', 'Deleted']) {
|
|
67
|
+
const crud = `${aggregate.names.pascal}${suffix}`;
|
|
68
|
+
if (events.has(crud) && events.get(crud)?.origin.startsWith('event')) {
|
|
69
|
+
problem(aggregate.line, `${crud} is generated for ${aggregate.names.pascal}; do not declare it.`);
|
|
70
|
+
}
|
|
71
|
+
events.set(crud, {
|
|
72
|
+
names: { ...aggregate.names, pascal: crud },
|
|
73
|
+
fields: [],
|
|
74
|
+
origin: `${aggregate.names.pascal} (CRUD)`,
|
|
75
|
+
});
|
|
76
|
+
}
|
|
77
|
+
guard(aggregate.line, () => (0, names_1.assertNotReserved)(aggregate.names, 'aggregate name'));
|
|
78
|
+
if (aggregate.fields.length === 0) {
|
|
79
|
+
problem(aggregate.line, `Aggregate ${aggregate.names.pascal} has no fields.`);
|
|
80
|
+
continue;
|
|
81
|
+
}
|
|
82
|
+
const reserved = aggregate.fields.find((field) => aggregate_1.AGGREGATE_RESERVED_FIELDS.includes(field.names.camel));
|
|
83
|
+
if (reserved)
|
|
84
|
+
problem(aggregate.line, `${aggregate.names.pascal}: "${reserved.names.camel}" is managed by the generated code.`);
|
|
85
|
+
const fields = aggregate.fields.map((field) => ({ ...field, managed: false }));
|
|
86
|
+
let stateOk = true;
|
|
87
|
+
guard(aggregate.line, () => {
|
|
88
|
+
(0, commands_1.markStateField)(fields, aggregate.commands, aggregate.names.pascal);
|
|
89
|
+
});
|
|
90
|
+
if (problems.some((item) => item.line === aggregate.line))
|
|
91
|
+
stateOk = false;
|
|
92
|
+
if ((0, fields_1.inputFields)(fields).length === 0)
|
|
93
|
+
problem(aggregate.line, `${aggregate.names.pascal} needs at least one field besides its state.`);
|
|
94
|
+
const commands = [];
|
|
95
|
+
for (const raw of aggregate.commands) {
|
|
96
|
+
if (!stateOk)
|
|
97
|
+
break;
|
|
98
|
+
guard(raw.line, () => {
|
|
99
|
+
if (commands.some((command) => command.names.camel === raw.names.camel)) {
|
|
100
|
+
throw new errors_1.CliError(`The command "${raw.names.camel}" is declared twice in ${aggregate.names.pascal}.`);
|
|
101
|
+
}
|
|
102
|
+
const emitted = raw.emits ? (0, names_1.toNames)(raw.emits.name, 'event name', ['event']).pascal : null;
|
|
103
|
+
const declaredEvent = emitted ? context.events.find((event) => event.names.pascal === emitted) : undefined;
|
|
104
|
+
const command = (0, commands_1.resolveCommand)(raw, { names: aggregate.names, fields }, declaredEvent ? { fields: declaredEvent.fields } : null);
|
|
105
|
+
if (command.emits) {
|
|
106
|
+
const name = command.emits.names.pascal;
|
|
107
|
+
if (['Created', 'Updated', 'Deleted'].some((suffix) => name === `${aggregate.names.pascal}${suffix}`)) {
|
|
108
|
+
throw new errors_1.CliError(`${name} is already recorded by the generated create/update/delete.`);
|
|
109
|
+
}
|
|
110
|
+
if (declaredEvent) {
|
|
111
|
+
for (const item of command.emits.payload) {
|
|
112
|
+
const declaredField = declaredEvent.fields.find((field) => field.names.camel === item.name);
|
|
113
|
+
if (declaredField && !sameFieldType(declaredField, item.field)) {
|
|
114
|
+
throw new errors_1.CliError(`${name}.${item.name} is declared with another type in "event ${name}".`);
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
const existing = events.get(name);
|
|
119
|
+
if (existing && existing.origin.startsWith('command')) {
|
|
120
|
+
const same = existing.fields.length === command.emits.payload.length &&
|
|
121
|
+
existing.fields.every((field, index) => field.names.camel === command.emits?.payload[index].name && sameFieldType(field, command.emits.payload[index].field));
|
|
122
|
+
if (!same)
|
|
123
|
+
throw new errors_1.CliError(`${name} is also recorded by ${existing.origin.slice('command '.length)} with a different payload.`);
|
|
124
|
+
}
|
|
125
|
+
else if (existing && !existing.origin.startsWith('event')) {
|
|
126
|
+
throw new errors_1.CliError(`${name} clashes with an event generated for ${existing.origin}.`);
|
|
127
|
+
}
|
|
128
|
+
if (declared.has(name) && declared.get(name) !== 'an event') {
|
|
129
|
+
throw new errors_1.CliError(`"${name}" is already declared in ${context.names.pascal} (as ${declared.get(name)}).`);
|
|
130
|
+
}
|
|
131
|
+
events.set(name, {
|
|
132
|
+
names: command.emits.names,
|
|
133
|
+
fields: command.emits.payload.map((item) => item.field),
|
|
134
|
+
origin: `command ${aggregate.names.pascal}.${command.names.camel}`,
|
|
135
|
+
});
|
|
136
|
+
}
|
|
137
|
+
commands.push(command);
|
|
138
|
+
});
|
|
139
|
+
}
|
|
140
|
+
// entities
|
|
141
|
+
const entityNames = new Set();
|
|
142
|
+
for (const entity of aggregate.entities) {
|
|
143
|
+
if (entity.names.kebab === aggregate.names.kebab)
|
|
144
|
+
problem(entity.line, 'An entity cannot have the same name as its aggregate.');
|
|
145
|
+
if (entityNames.has(entity.names.pascal))
|
|
146
|
+
problem(entity.line, `The entity ${entity.names.pascal} is declared twice.`);
|
|
147
|
+
entityNames.add(entity.names.pascal);
|
|
148
|
+
claim(entity.names, 'an entity', entity.line);
|
|
149
|
+
guard(entity.line, () => (0, names_1.assertNotReserved)(entity.names, 'entity name'));
|
|
150
|
+
if (entity.fields.length === 0)
|
|
151
|
+
problem(entity.line, `Entity ${entity.names.pascal} has no fields.`);
|
|
152
|
+
const bad = entity.fields.find((field) => building_blocks_1.ENTITY_RESERVED_FIELDS.includes(field.names.camel));
|
|
153
|
+
if (bad)
|
|
154
|
+
problem(entity.line, `${entity.names.pascal}: "${bad.names.camel}" is managed by the generated code.`);
|
|
155
|
+
if (entity.fields.some((field) => typeof field.defaultValue === 'object')) {
|
|
156
|
+
problem(entity.line, `${entity.names.pascal}: "= now" is only supported on aggregate fields.`);
|
|
157
|
+
}
|
|
158
|
+
}
|
|
159
|
+
// route
|
|
160
|
+
let route = '';
|
|
161
|
+
guard(aggregate.line, () => {
|
|
162
|
+
route = normalize(aggregate.route ?? `/${pluralKebab(aggregate.names)}`);
|
|
163
|
+
const other = routes.get(route);
|
|
164
|
+
if (other)
|
|
165
|
+
throw new errors_1.CliError(`The route ${route} is already used by ${other}; add "at /another-route".`);
|
|
166
|
+
routes.set(route, `${context.names.pascal}.${aggregate.names.pascal}`);
|
|
167
|
+
});
|
|
168
|
+
resolved.aggregates.set(aggregate, { model: aggregate, fields, commands, route });
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
// policies (after every context is known)
|
|
172
|
+
for (const context of model.contexts) {
|
|
173
|
+
const seen = new Set();
|
|
174
|
+
for (const policy of context.policies) {
|
|
175
|
+
const key = `${policy.handler?.pascal ?? ''}@${policy.event.pascal}`;
|
|
176
|
+
if (seen.has(key))
|
|
177
|
+
problem(policy.line, `This reaction to ${policy.event.pascal} is declared twice.`);
|
|
178
|
+
seen.add(key);
|
|
179
|
+
let publisher = null;
|
|
180
|
+
if (policy.fromContext) {
|
|
181
|
+
const from = contexts.get(policy.fromContext.kebab);
|
|
182
|
+
if (from) {
|
|
183
|
+
if (!resolved.events.get(from.names.kebab)?.has(policy.event.pascal)) {
|
|
184
|
+
problem(policy.line, `${from.names.pascal} does not publish ${policy.event.pascal}.`);
|
|
185
|
+
}
|
|
186
|
+
publisher = from.names;
|
|
187
|
+
}
|
|
188
|
+
else {
|
|
189
|
+
publisher = policy.fromContext; // a context that exists only in the code
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
else {
|
|
193
|
+
const publishers = model.contexts.filter((candidate) => resolved.events.get(candidate.names.kebab)?.has(policy.event.pascal));
|
|
194
|
+
if (publishers.length > 1) {
|
|
195
|
+
problem(policy.line, `${policy.event.pascal} is published by ${publishers.map((item) => item.names.pascal).join(' and ')}: write "on <Context>.${policy.event.pascal}".`);
|
|
196
|
+
}
|
|
197
|
+
publisher = publishers[0]?.names ?? null;
|
|
198
|
+
}
|
|
199
|
+
resolved.policies.push({ context, policy, publisher });
|
|
200
|
+
}
|
|
201
|
+
}
|
|
202
|
+
if (problems.length > 0)
|
|
203
|
+
throw new parser_1.ModelError(model.file, problems.sort((a, b) => a.line - b.line));
|
|
204
|
+
return resolved;
|
|
205
|
+
}
|
|
206
|
+
function normalize(route) {
|
|
207
|
+
const cleaned = `/${route.trim().replace(/^\/+|\/+$/g, '')}`;
|
|
208
|
+
if (!/^\/[A-Za-z0-9/_-]*$/.test(cleaned) || cleaned.includes('//') || cleaned === '/') {
|
|
209
|
+
throw new errors_1.CliError(`Invalid route "${route}".`, 'Example: at /product-categories');
|
|
210
|
+
}
|
|
211
|
+
return cleaned;
|
|
212
|
+
}
|
|
213
|
+
function pluralKebab(names) {
|
|
214
|
+
return (0, names_1.pluralize)(names).kebab;
|
|
215
|
+
}
|
package/dist/run.d.ts
ADDED