@burnsred/entity-sync 0.0.0-stage → 3.0.0-rc.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/.claude-plugin/marketplace.json +14 -0
- package/.claude-plugin/plugin.json +11 -0
- package/CHANGELOG.md +15 -0
- package/LICENSE +21 -0
- package/README.md +240 -2
- package/agents/entity-editor.md +16 -0
- package/bin/entity-sync.mjs +36 -0
- package/dist/cli.d.mts +20 -0
- package/dist/commands/asserts.d.mts +4 -0
- package/dist/commands/check.d.mts +6 -0
- package/dist/commands/inspect.d.mts +30 -0
- package/dist/commands/plan.d.mts +28 -0
- package/dist/commands/schema.d.mts +39 -0
- package/dist/config.d.mts +175 -0
- package/dist/index.d.mts +5 -0
- package/dist/lib/cache.d.mts +120 -0
- package/dist/lib/diff.d.mts +228 -0
- package/dist/lib/field-map.d.mts +93 -0
- package/dist/lib/gen-asserts.d.mts +70 -0
- package/dist/lib/graph.d.mts +90 -0
- package/dist/lib/parse-entities.d.mts +257 -0
- package/dist/lib/parse-schema.d.mts +98 -0
- package/dist/lib/resolve.d.mts +135 -0
- package/dist/project.d.mts +60 -0
- package/hooks/editor-guard.mjs +86 -0
- package/hooks/hooks.json +15 -0
- package/package.json +54 -4
- package/skills/entity/SKILL.md +105 -0
- package/skills/entity/reference/create.md +74 -0
- package/skills/entity/reference/editor.md +80 -0
- package/skills/entity/reference/enums.md +89 -0
- package/skills/entity/reference/fields.md +101 -0
- package/skills/entity/reference/sync.md +191 -0
- package/src/cli.mjs +110 -0
- package/src/commands/asserts.mjs +52 -0
- package/src/commands/check.mjs +88 -0
- package/src/commands/inspect.mjs +290 -0
- package/src/commands/plan.mjs +542 -0
- package/src/commands/schema.mjs +206 -0
- package/src/config.mjs +205 -0
- package/src/index.mjs +19 -0
- package/src/lib/cache.mjs +323 -0
- package/src/lib/diff.mjs +706 -0
- package/src/lib/field-map.mjs +438 -0
- package/src/lib/gen-asserts.mjs +478 -0
- package/src/lib/graph.mjs +284 -0
- package/src/lib/parse-entities.mjs +935 -0
- package/src/lib/parse-schema.mjs +667 -0
- package/src/lib/resolve.mjs +455 -0
- package/src/project.mjs +115 -0
|
@@ -0,0 +1,438 @@
|
|
|
1
|
+
// @ts-check
|
|
2
|
+
/**
|
|
3
|
+
* Map a schema field to the Entity field declaration and TypeScript type it
|
|
4
|
+
* should have.
|
|
5
|
+
*
|
|
6
|
+
* This module is the machine-readable copy of the mapping table in the `/entity`
|
|
7
|
+
* skill; the two must stay in step, because the skill is what a human reads and
|
|
8
|
+
* this is what the diff acts on.
|
|
9
|
+
*
|
|
10
|
+
* ## The rules, and why
|
|
11
|
+
*
|
|
12
|
+
* | schema signal | field |
|
|
13
|
+
* | ------------------------------- | ------------------------------------------ |
|
|
14
|
+
* | `Format: uuid` | `IdField` - but `CharField({ many: true })`|
|
|
15
|
+
* | | for a list, since IdField rejects `many` |
|
|
16
|
+
* | `Format: date` / `date-time` | `DateField` / `DateTimeField` |
|
|
17
|
+
* | `Format: decimal` | `CharField` - DRF serialises decimals as |
|
|
18
|
+
* | | strings (COERCE_DECIMAL_TO_STRING) |
|
|
19
|
+
* | `$ref` to a `*Enum` component | `CharField` plus a hand-maintained union |
|
|
20
|
+
* | | in the Fields type - *not* `EnumField`, |
|
|
21
|
+
* | | which is backed by `EntityEnum` and unused |
|
|
22
|
+
* | `$ref` to an object component | `EntityField({ entity: X })` |
|
|
23
|
+
* | `number` | `IntegerField`, or `NumberField` for a |
|
|
24
|
+
* | | float/double format |
|
|
25
|
+
* | `boolean` | `BooleanField` |
|
|
26
|
+
* | `string` | `CharField` |
|
|
27
|
+
* | `additionalProperties` | `AnyField`, typed `Record<string, V>` - |
|
|
28
|
+
* | | a DRF `DictField`; the library has no |
|
|
29
|
+
* | | keyed-collection field |
|
|
30
|
+
* | `unknown` | `AnyField` |
|
|
31
|
+
* | `readonly` | `readOnly: true` |
|
|
32
|
+
* | `?` or `\| null` | `blank: true` |
|
|
33
|
+
*
|
|
34
|
+
* ## Expected TypeScript type
|
|
35
|
+
*
|
|
36
|
+
* Per the documented convention, `EntityNameFields` describes the **raw API
|
|
37
|
+
* response shape**; the Record/`List` substitution belongs in
|
|
38
|
+
* `EntityNameRecord`. So a nested field's expected type here is the child's
|
|
39
|
+
* *Fields* type, never its Record type.
|
|
40
|
+
*/
|
|
41
|
+
|
|
42
|
+
/**
|
|
43
|
+
* @typedef {import('./parse-schema.mjs').SchemaField} SchemaField
|
|
44
|
+
* @typedef {import('./parse-schema.mjs').SchemaComponent} SchemaComponent
|
|
45
|
+
*/
|
|
46
|
+
|
|
47
|
+
/**
|
|
48
|
+
* @typedef {object} ExpectedField
|
|
49
|
+
* @property {string} fieldClass e.g. 'CharField'
|
|
50
|
+
* @property {Record<string, true|string>} opts expected constructor options
|
|
51
|
+
* @property {string} typeText expected `Fields` member type
|
|
52
|
+
* @property {string | null} recordTypeText expected `Record` member type, when
|
|
53
|
+
* it differs from `typeText`: any `many: true` field (a `List`), a nested
|
|
54
|
+
* object, or a date (a `Moment`)
|
|
55
|
+
* @property {string | null} entityRef expected `entity:` option value
|
|
56
|
+
* @property {string[]} enumValues when the field is an enum union
|
|
57
|
+
* @property {string[]} [enumRefs] the `*Enum` components the field refs
|
|
58
|
+
* @property {string[]} [enumAliases] type aliases of the dicts modelling them
|
|
59
|
+
* @property {string | null} note anything the diff should pass to a human
|
|
60
|
+
*/
|
|
61
|
+
|
|
62
|
+
/** Formats that mean "a number that is not an integer". */
|
|
63
|
+
const FLOAT_FORMATS = new Set(['float', 'double']);
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* Components drf-spectacular emits to widen a field, not to enumerate it.
|
|
67
|
+
*
|
|
68
|
+
* A blank-able choices field is rendered as `StateEnum | BlankEnum`, where
|
|
69
|
+
* `BlankEnum` is the single value `''`. They are not members of the set the
|
|
70
|
+
* broker defines - `''` means "unset" - so a choices dict must not absorb them,
|
|
71
|
+
* or every dict would carry an empty-string member and `Status.` would offer it
|
|
72
|
+
* for selection. They stay as literals in the field's type instead.
|
|
73
|
+
*/
|
|
74
|
+
export const WIDENING_ENUM_COMPONENTS = new Set(['BlankEnum', 'NullEnum']);
|
|
75
|
+
|
|
76
|
+
/**
|
|
77
|
+
* Convention: `Member` -> `MemberEntity`, and the entity's Fields/Record types
|
|
78
|
+
* are `<Component>EntityFields` / `<Component>EntityRecord`.
|
|
79
|
+
*
|
|
80
|
+
* Only used as a fallback. When the component is already mapped to a real
|
|
81
|
+
* entity, that entity's actual names are passed in instead, because several
|
|
82
|
+
* entities deliberately break this convention (`MinimalEvent` models
|
|
83
|
+
* `EventSerializerLite`).
|
|
84
|
+
*
|
|
85
|
+
* @param {string} component
|
|
86
|
+
*/
|
|
87
|
+
function conventionalEntityName(component) {
|
|
88
|
+
return `${component}Entity`;
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
/**
|
|
92
|
+
* Decide the field class and options a schema field should map to.
|
|
93
|
+
*
|
|
94
|
+
* @param {SchemaField} field
|
|
95
|
+
* @param {object} context
|
|
96
|
+
* @param {Record<string, SchemaComponent>} context.components
|
|
97
|
+
* used to tell an enum ref from an object ref
|
|
98
|
+
* @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} [context.entityByComponent]
|
|
99
|
+
* component name -> the entity that models it, when one does
|
|
100
|
+
* @param {Record<string, { name: string, typeName: string, file: string }>} [context.enumByComponent]
|
|
101
|
+
* component name -> the choices dict that models it, when one does
|
|
102
|
+
* @returns {ExpectedField}
|
|
103
|
+
*/
|
|
104
|
+
export function expectedField(
|
|
105
|
+
field,
|
|
106
|
+
{ components, entityByComponent = {}, enumByComponent = {} },
|
|
107
|
+
) {
|
|
108
|
+
/** @type {Record<string, true|string>} */
|
|
109
|
+
const opts = {};
|
|
110
|
+
if (field.readonly) opts.readOnly = true;
|
|
111
|
+
// DRF `required: false` and `null` both mean the client may omit a value.
|
|
112
|
+
if (field.optional || field.nullable) opts.blank = true;
|
|
113
|
+
if (field.isArray) opts.many = true;
|
|
114
|
+
|
|
115
|
+
const refKinds = field.refs.map((ref) => components[ref]?.kind);
|
|
116
|
+
const objectRefs = field.refs.filter(
|
|
117
|
+
(ref) => components[ref]?.kind === 'object',
|
|
118
|
+
);
|
|
119
|
+
const enumRefs = field.refs.filter((ref) => components[ref]?.kind === 'enum');
|
|
120
|
+
|
|
121
|
+
// ── Nested object ──────────────────────────────────────────────────────────
|
|
122
|
+
if (objectRefs.length === 1 && enumRefs.length === 0) {
|
|
123
|
+
const component = objectRefs[0];
|
|
124
|
+
const mapped = entityByComponent[component];
|
|
125
|
+
|
|
126
|
+
const className = mapped?.className ?? conventionalEntityName(component);
|
|
127
|
+
const fieldsTypeName = mapped?.fieldsTypeName ?? `${className}Fields`;
|
|
128
|
+
const recordTypeName = mapped?.recordTypeName ?? `${className}Record`;
|
|
129
|
+
|
|
130
|
+
return {
|
|
131
|
+
fieldClass: 'EntityField',
|
|
132
|
+
opts: { ...opts, entity: className },
|
|
133
|
+
// Fields carries the raw wire shape, so the child's Fields type.
|
|
134
|
+
typeText: withModifiers(fieldsTypeName, field),
|
|
135
|
+
// Record substitutes the Immutable form.
|
|
136
|
+
recordTypeText: recordOfElement(recordTypeName, field, {
|
|
137
|
+
substitutes: true,
|
|
138
|
+
}),
|
|
139
|
+
entityRef: className,
|
|
140
|
+
enumValues: [],
|
|
141
|
+
note: mapped ? null : `no entity models component ${component}`,
|
|
142
|
+
};
|
|
143
|
+
}
|
|
144
|
+
|
|
145
|
+
// ── Enum union ─────────────────────────────────────────────────────────────
|
|
146
|
+
// `EnumField` is backed by `EntityEnum` and is unused in this project: it
|
|
147
|
+
// turns the runtime value into an Immutable Map, which would break every
|
|
148
|
+
// `status === Status.draft` comparison. The pattern is a CharField plus a
|
|
149
|
+
// choices dict, referenced by its type alias.
|
|
150
|
+
if (enumRefs.length && objectRefs.length === 0) {
|
|
151
|
+
const values = enumRefs.flatMap((ref) => components[ref]?.enumValues ?? []);
|
|
152
|
+
|
|
153
|
+
// A dict models the *named* enum; the widening components stay literal. So
|
|
154
|
+
// the alias stands in for the refs a dict covers and the rest stay literal,
|
|
155
|
+
// which is what produces `SupplierStatus | ''` rather than a five-member
|
|
156
|
+
// union with an empty string in it.
|
|
157
|
+
const modellable = enumRefs.filter(
|
|
158
|
+
(ref) => !WIDENING_ENUM_COMPONENTS.has(ref),
|
|
159
|
+
);
|
|
160
|
+
const aliases = [
|
|
161
|
+
...new Set(
|
|
162
|
+
modellable
|
|
163
|
+
.filter((ref) => enumByComponent[ref])
|
|
164
|
+
.map((ref) => enumByComponent[ref].typeName),
|
|
165
|
+
),
|
|
166
|
+
];
|
|
167
|
+
const literals = enumRefs
|
|
168
|
+
.filter((ref) => !aliases.length || !enumByComponent[ref])
|
|
169
|
+
.flatMap((ref) => components[ref]?.enumValues ?? [])
|
|
170
|
+
.map((value) => `'${value}'`);
|
|
171
|
+
|
|
172
|
+
const union = [...aliases, ...literals].join(' | ');
|
|
173
|
+
const base =
|
|
174
|
+
union ||
|
|
175
|
+
(values.length ? values.map((v) => `'${v}'`).join(' | ') : 'string');
|
|
176
|
+
|
|
177
|
+
return {
|
|
178
|
+
fieldClass: 'CharField',
|
|
179
|
+
opts,
|
|
180
|
+
typeText: withModifiers(base, field),
|
|
181
|
+
recordTypeText: recordOfElement(base, field, { substitutes: false }),
|
|
182
|
+
entityRef: null,
|
|
183
|
+
enumValues: values,
|
|
184
|
+
// Widening components are excluded: they are never modelled by a dict, so
|
|
185
|
+
// `diff.mjs` must not ask for one.
|
|
186
|
+
enumRefs: modellable,
|
|
187
|
+
enumAliases: aliases,
|
|
188
|
+
note: null,
|
|
189
|
+
};
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
// More than one object ref, or a mix of object and enum, is not something the
|
|
193
|
+
// entity layer models - flag it rather than guessing.
|
|
194
|
+
if (objectRefs.length > 1 || (objectRefs.length && enumRefs.length)) {
|
|
195
|
+
return {
|
|
196
|
+
fieldClass: 'AnyField',
|
|
197
|
+
opts,
|
|
198
|
+
typeText: withModifiers('unknown', field),
|
|
199
|
+
recordTypeText: recordOfElement('unknown', field, { substitutes: false }),
|
|
200
|
+
entityRef: null,
|
|
201
|
+
enumValues: [],
|
|
202
|
+
note: `unsupported union of ${field.refs.length} refs (${refKinds.join(', ')})`,
|
|
203
|
+
};
|
|
204
|
+
}
|
|
205
|
+
|
|
206
|
+
// ── Scalars, keyed on Format first ─────────────────────────────────────────
|
|
207
|
+
if (field.format === 'uuid') {
|
|
208
|
+
// IdField throws on `many`, so a list of ids has to be a CharField.
|
|
209
|
+
return scalar(
|
|
210
|
+
field.isArray ? 'CharField' : 'IdField',
|
|
211
|
+
'string',
|
|
212
|
+
field,
|
|
213
|
+
opts,
|
|
214
|
+
);
|
|
215
|
+
}
|
|
216
|
+
// `DateField.dataToValue` returns a Moment, so by the convention the Fields
|
|
217
|
+
// type carries the wire `string` while the Record type carries `Moment` - the
|
|
218
|
+
// same split as a nested entity, just with a scalar on the wire.
|
|
219
|
+
if (field.format === 'date') {
|
|
220
|
+
return scalar('DateField', 'string', field, opts, 'Moment');
|
|
221
|
+
}
|
|
222
|
+
if (field.format === 'date-time') {
|
|
223
|
+
return scalar('DateTimeField', 'string', field, opts, 'Moment');
|
|
224
|
+
}
|
|
225
|
+
if (field.format === 'decimal') {
|
|
226
|
+
// DRF serialises Decimal as a string by default.
|
|
227
|
+
return scalar('CharField', 'string', field, opts);
|
|
228
|
+
}
|
|
229
|
+
|
|
230
|
+
switch (field.tsType) {
|
|
231
|
+
case 'number':
|
|
232
|
+
return scalar(
|
|
233
|
+
field.format && FLOAT_FORMATS.has(field.format)
|
|
234
|
+
? 'NumberField'
|
|
235
|
+
: 'IntegerField',
|
|
236
|
+
'number',
|
|
237
|
+
field,
|
|
238
|
+
opts,
|
|
239
|
+
);
|
|
240
|
+
case 'boolean':
|
|
241
|
+
return scalar('BooleanField', 'boolean', field, opts);
|
|
242
|
+
case 'string':
|
|
243
|
+
return scalar('CharField', 'string', field, opts);
|
|
244
|
+
case 'record':
|
|
245
|
+
// An `additionalProperties` map - a DRF `DictField`. The entity library
|
|
246
|
+
// has no keyed-collection field, so it stays an `AnyField`; the value
|
|
247
|
+
// type is still worth naming, because the schema does know it and a
|
|
248
|
+
// `Record<string, T>` on the entity is more honest than `unknown`.
|
|
249
|
+
return scalar(
|
|
250
|
+
'AnyField',
|
|
251
|
+
`Record<string, ${mapValueType(field.mapValue, { components, entityByComponent })}>`,
|
|
252
|
+
field,
|
|
253
|
+
opts,
|
|
254
|
+
);
|
|
255
|
+
default:
|
|
256
|
+
return scalar('AnyField', 'unknown', field, opts);
|
|
257
|
+
}
|
|
258
|
+
}
|
|
259
|
+
|
|
260
|
+
/**
|
|
261
|
+
* Render the value side of an `additionalProperties` map.
|
|
262
|
+
*
|
|
263
|
+
* A map *of* `$ref`s is still named by its component's Fields type, matching
|
|
264
|
+
* the nested-object branch - `EntityField` describes one nested object, not a
|
|
265
|
+
* keyed collection of them, so only the type text carries the information.
|
|
266
|
+
*
|
|
267
|
+
* @param {import('./parse-schema.mjs').MapValue | null} mapValue
|
|
268
|
+
* @param {object} context
|
|
269
|
+
* @param {Record<string, SchemaComponent>} context.components
|
|
270
|
+
* @param {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} context.entityByComponent
|
|
271
|
+
* @returns {string}
|
|
272
|
+
*/
|
|
273
|
+
function mapValueType(mapValue, { components, entityByComponent }) {
|
|
274
|
+
if (!mapValue) return 'unknown';
|
|
275
|
+
|
|
276
|
+
const base = (() => {
|
|
277
|
+
// A single object ref names the child's Fields type; anything else - an
|
|
278
|
+
// enum union, a multi-ref union - is not worth guessing at inside a map.
|
|
279
|
+
const objectRefs = mapValue.refs.filter(
|
|
280
|
+
(ref) => components[ref]?.kind === 'object',
|
|
281
|
+
);
|
|
282
|
+
if (objectRefs.length === 1 && mapValue.refs.length === 1) {
|
|
283
|
+
const mapped = entityByComponent[objectRefs[0]];
|
|
284
|
+
return (
|
|
285
|
+
mapped?.fieldsTypeName ??
|
|
286
|
+
`${conventionalEntityName(objectRefs[0])}Fields`
|
|
287
|
+
);
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
switch (mapValue.tsType) {
|
|
291
|
+
case 'string':
|
|
292
|
+
case 'number':
|
|
293
|
+
case 'boolean':
|
|
294
|
+
return mapValue.tsType;
|
|
295
|
+
default:
|
|
296
|
+
return 'unknown';
|
|
297
|
+
}
|
|
298
|
+
})();
|
|
299
|
+
|
|
300
|
+
// `MapValue` carries the same `isArray` / `nullable` pair a field does, so
|
|
301
|
+
// the `T[] | null` ordering rule is shared rather than restated.
|
|
302
|
+
return withModifiers(base, mapValue);
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
/**
|
|
306
|
+
* The `Record` member type for a non-nested field, or `null` when the Record
|
|
307
|
+
* carries the same type as `Fields` and needs no substitution.
|
|
308
|
+
*
|
|
309
|
+
* `Entity.dataToRecord` keys the Immutable conversion on the *value* being an
|
|
310
|
+
* array, not on the field class:
|
|
311
|
+
*
|
|
312
|
+
* ```js
|
|
313
|
+
* List.isList(value) || Array.isArray(value) ? List(value).map(...) : ...
|
|
314
|
+
* ```
|
|
315
|
+
*
|
|
316
|
+
* So every `many: true` field is a `List` at runtime, a `CharField` exactly as
|
|
317
|
+
* much as an `EntityField`. Only the nested branch used to say so, which left
|
|
318
|
+
* a scalar array expected as a bare `T[]` - wrong about runtime, and wrong in
|
|
319
|
+
* the direction that passes silently, because both `List` and `Array` have
|
|
320
|
+
* `.map`.
|
|
321
|
+
*
|
|
322
|
+
* @param {string} elementType the element type the Record carries
|
|
323
|
+
* @param {SchemaField} field
|
|
324
|
+
* @param {{ substitutes: boolean }} options whether the element type differs
|
|
325
|
+
* from the wire type (a date arriving as a Moment). A non-array field that
|
|
326
|
+
* substitutes nothing needs no Record member at all.
|
|
327
|
+
* @returns {string | null}
|
|
328
|
+
*/
|
|
329
|
+
function recordOfElement(elementType, field, { substitutes }) {
|
|
330
|
+
if (field.isArray) return nullableSuffix(`List<${elementType}>`, field);
|
|
331
|
+
return substitutes ? withModifiers(elementType, field) : null;
|
|
332
|
+
}
|
|
333
|
+
|
|
334
|
+
/**
|
|
335
|
+
* @param {string} fieldClass
|
|
336
|
+
* @param {string} baseType the wire type, for the `Fields` member
|
|
337
|
+
* @param {SchemaField} field
|
|
338
|
+
* @param {Record<string, true|string>} opts
|
|
339
|
+
* @param {string} [recordBaseType] the Record member type, when the field
|
|
340
|
+
* converts on the way in (dates become Moments)
|
|
341
|
+
* @returns {ExpectedField}
|
|
342
|
+
*/
|
|
343
|
+
function scalar(fieldClass, baseType, field, opts, recordBaseType) {
|
|
344
|
+
return {
|
|
345
|
+
fieldClass,
|
|
346
|
+
opts,
|
|
347
|
+
typeText: withModifiers(baseType, field),
|
|
348
|
+
recordTypeText: recordOfElement(recordBaseType ?? baseType, field, {
|
|
349
|
+
substitutes: Boolean(recordBaseType),
|
|
350
|
+
}),
|
|
351
|
+
entityRef: null,
|
|
352
|
+
enumValues: [],
|
|
353
|
+
note: null,
|
|
354
|
+
};
|
|
355
|
+
}
|
|
356
|
+
|
|
357
|
+
/**
|
|
358
|
+
* Apply `[]` then `| null` to a base type, in that order.
|
|
359
|
+
*
|
|
360
|
+
* Order matters: `X[] | null` (a nullable list) is what DRF sends, not
|
|
361
|
+
* `(X | null)[]`.
|
|
362
|
+
*
|
|
363
|
+
* Typed on the two modifiers it reads rather than on `SchemaField`, so a
|
|
364
|
+
* `MapValue` - which carries the same pair for the value side of an
|
|
365
|
+
* `additionalProperties` map - can reuse the same ordering rule.
|
|
366
|
+
*
|
|
367
|
+
* @param {string} baseType
|
|
368
|
+
* @param {Pick<SchemaField, 'isArray' | 'nullable'>} field
|
|
369
|
+
* @param {{ array?: boolean }} [options]
|
|
370
|
+
*/
|
|
371
|
+
function withModifiers(baseType, field, options = {}) {
|
|
372
|
+
const array = options.array ?? field.isArray;
|
|
373
|
+
const withArray = array ? `${wrapForArray(baseType)}[]` : baseType;
|
|
374
|
+
return nullableSuffix(withArray, field);
|
|
375
|
+
}
|
|
376
|
+
|
|
377
|
+
/**
|
|
378
|
+
* Parenthesise a union before appending `[]`, so `'a' | 'b'` becomes
|
|
379
|
+
* `('a' | 'b')[]` rather than the wrong `'a' | 'b'[]`.
|
|
380
|
+
*
|
|
381
|
+
* @param {string} baseType
|
|
382
|
+
*/
|
|
383
|
+
function wrapForArray(baseType) {
|
|
384
|
+
return baseType.includes('|') ? `(${baseType})` : baseType;
|
|
385
|
+
}
|
|
386
|
+
|
|
387
|
+
/**
|
|
388
|
+
* @param {string} baseType
|
|
389
|
+
* @param {Pick<SchemaField, 'nullable'>} field
|
|
390
|
+
*/
|
|
391
|
+
function nullableSuffix(baseType, field) {
|
|
392
|
+
return field.nullable ? `${baseType} | null` : baseType;
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
/**
|
|
396
|
+
* Build `component -> entity` from resolutions, for `entityByComponent`.
|
|
397
|
+
*
|
|
398
|
+
* When two entities model the same component - as `Application` is modelled by
|
|
399
|
+
* both `ApplicationRowEntity` and (via the detail serializer) others - the first
|
|
400
|
+
* wins, and the ambiguity is already reported by `resolve.mjs`.
|
|
401
|
+
*
|
|
402
|
+
* @param {import('./resolve.mjs').Resolution[]} resolutions
|
|
403
|
+
* @param {import('./parse-entities.mjs').ParsedEntity[]} entities
|
|
404
|
+
*/
|
|
405
|
+
export function buildEntityByComponent(resolutions, entities) {
|
|
406
|
+
const byClassName = new Map(entities.map((e) => [e.className, e]));
|
|
407
|
+
|
|
408
|
+
/** @type {Record<string, { className: string, fieldsTypeName: string, recordTypeName: string }>} */
|
|
409
|
+
const index = {};
|
|
410
|
+
|
|
411
|
+
for (const resolution of resolutions) {
|
|
412
|
+
if (!resolution.component || index[resolution.component]) continue;
|
|
413
|
+
|
|
414
|
+
const entity = byClassName.get(resolution.className);
|
|
415
|
+
if (!entity?.fieldsTypeName || !entity.recordTypeName) continue;
|
|
416
|
+
|
|
417
|
+
index[resolution.component] = {
|
|
418
|
+
className: entity.className,
|
|
419
|
+
fieldsTypeName: entity.fieldsTypeName,
|
|
420
|
+
recordTypeName: entity.recordTypeName,
|
|
421
|
+
};
|
|
422
|
+
}
|
|
423
|
+
|
|
424
|
+
// Components an entity also models (`@schemaAlso`), where nothing resolves to
|
|
425
|
+
// them itself.
|
|
426
|
+
for (const entity of entities) {
|
|
427
|
+
if (!entity.fieldsTypeName || !entity.recordTypeName) continue;
|
|
428
|
+
for (const component of entity.schemaAlso ?? []) {
|
|
429
|
+
index[component] ??= {
|
|
430
|
+
className: entity.className,
|
|
431
|
+
fieldsTypeName: entity.fieldsTypeName,
|
|
432
|
+
recordTypeName: entity.recordTypeName,
|
|
433
|
+
};
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
return index;
|
|
438
|
+
}
|