@type-bridge/node 1.5.0 → 1.5.2
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 +5 -1
- package/dist/generator/index.d.ts +46 -30
- package/dist/generator/index.js +35 -43
- package/dist/index.d.ts +16 -3
- package/dist/index.js +16 -3
- package/dist/type-bridge-node-1.5.2.tgz +0 -0
- package/package.json +1 -1
- package/type_bridge_node.linux-x64-gnu.node +0 -0
- package/dist/generator/cardinality.d.ts +0 -15
- package/dist/generator/cardinality.js +0 -28
- package/dist/generator/renderAttributes.d.ts +0 -22
- package/dist/generator/renderAttributes.js +0 -153
- package/dist/generator/renderEntities.d.ts +0 -39
- package/dist/generator/renderEntities.js +0 -273
- package/dist/generator/renderPackage.d.ts +0 -22
- package/dist/generator/renderPackage.js +0 -33
- package/dist/generator/renderRelations.d.ts +0 -36
- package/dist/generator/renderRelations.js +0 -395
- package/dist/type-bridge-node-1.5.0.tgz +0 -0
|
@@ -1,273 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Render the `entities.ts` module text from a parsed TypeSchema.
|
|
4
|
-
*
|
|
5
|
-
* Mirrors `type_bridge/generator/render/entities.py` → `render_entities()`.
|
|
6
|
-
* Field KEY decision logic is ported directly from `_render_attr_field`:
|
|
7
|
-
*
|
|
8
|
-
* 1. is_key (schema @key OR implicit key) → field(Attr, Key)
|
|
9
|
-
* 2. is_unique (schema @unique) → field(Attr, Unique)
|
|
10
|
-
* 3. cardinality === null || is_optional_single (min=0, max=1)
|
|
11
|
-
* → field(Attr).optional()
|
|
12
|
-
* 4. is_multi (max === null || max > 1) → field(Attr).list(Card(min, max))
|
|
13
|
-
* 5. is_required && is_single (min>=1, max=1) → field(Attr) (plain required)
|
|
14
|
-
* 6. fallback → field(Attr).optional()
|
|
15
|
-
*
|
|
16
|
-
* Field keys = toFieldName(attr_name) — prefix kept, no pluralization.
|
|
17
|
-
*
|
|
18
|
-
* Build-time code generation; no runtime ORM logic.
|
|
19
|
-
*/
|
|
20
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
21
|
-
exports.renderEntities = renderEntities;
|
|
22
|
-
const naming_js_1 = require("./naming.js");
|
|
23
|
-
const cardinality_js_1 = require("./cardinality.js");
|
|
24
|
-
// ---------------------------------------------------------------------------
|
|
25
|
-
// Single-field renderer
|
|
26
|
-
// ---------------------------------------------------------------------------
|
|
27
|
-
/**
|
|
28
|
-
* Render the `field(...)` expression for one owned attribute.
|
|
29
|
-
*
|
|
30
|
-
* Decision logic mirrors Python `_render_attr_field` (entities.py:38-65):
|
|
31
|
-
* Key > Unique > ordered (list) > optional_single > multi (list) > required_single > fallback optional
|
|
32
|
-
*
|
|
33
|
-
* Ordered list attributes use `.ordered()` / `.ordered().distinct()` instead of
|
|
34
|
-
* `.list(Card(...))`. These correspond to `owns attr[] @distinct` in TypeQL.
|
|
35
|
-
*/
|
|
36
|
-
function renderFieldExpr(owned, attrClassName, isKey) {
|
|
37
|
-
const card = owned.cardinality;
|
|
38
|
-
if (isKey) {
|
|
39
|
-
return `field(${attrClassName}, Key)`;
|
|
40
|
-
}
|
|
41
|
-
if (owned.is_unique) {
|
|
42
|
-
return `field(${attrClassName}, Unique)`;
|
|
43
|
-
}
|
|
44
|
-
// Ordered list attribute: field(Attr).ordered() or field(Attr).ordered().distinct()
|
|
45
|
-
if (owned.ordered) {
|
|
46
|
-
const base = `field(${attrClassName}).ordered()`;
|
|
47
|
-
return owned.distinct ? `${base}.distinct()` : base;
|
|
48
|
-
}
|
|
49
|
-
// cardinality === null means default (optional single, same as @card(0..1))
|
|
50
|
-
if (card === null || (0, cardinality_js_1.isCardOptionalSingle)(card)) {
|
|
51
|
-
return `field(${attrClassName}).optional()`;
|
|
52
|
-
}
|
|
53
|
-
if ((0, cardinality_js_1.isCardMulti)(card)) {
|
|
54
|
-
const maxStr = card.max === null ? "null" : String(card.max);
|
|
55
|
-
return `field(${attrClassName}).list(Card(${card.min}, ${maxStr}))`;
|
|
56
|
-
}
|
|
57
|
-
if ((0, cardinality_js_1.isCardRequired)(card) && (0, cardinality_js_1.isCardSingle)(card)) {
|
|
58
|
-
// required single: plain field, no modifier
|
|
59
|
-
return `field(${attrClassName})`;
|
|
60
|
-
}
|
|
61
|
-
// Fallback: optional
|
|
62
|
-
return `field(${attrClassName}).optional()`;
|
|
63
|
-
}
|
|
64
|
-
// ---------------------------------------------------------------------------
|
|
65
|
-
// Topological sort — parents before children (mirror `_topological_sort_entities`)
|
|
66
|
-
// ---------------------------------------------------------------------------
|
|
67
|
-
function topologicalSortEntities(schema) {
|
|
68
|
-
const entities = schema.entities;
|
|
69
|
-
const names = Object.keys(entities);
|
|
70
|
-
const sorted = [];
|
|
71
|
-
const visited = new Set();
|
|
72
|
-
function visit(name) {
|
|
73
|
-
if (visited.has(name))
|
|
74
|
-
return;
|
|
75
|
-
visited.add(name);
|
|
76
|
-
const parent = entities[name]?.parent;
|
|
77
|
-
if (parent && entities[parent]) {
|
|
78
|
-
visit(parent);
|
|
79
|
-
}
|
|
80
|
-
sorted.push(name);
|
|
81
|
-
}
|
|
82
|
-
for (const name of names) {
|
|
83
|
-
visit(name);
|
|
84
|
-
}
|
|
85
|
-
return sorted;
|
|
86
|
-
}
|
|
87
|
-
// ---------------------------------------------------------------------------
|
|
88
|
-
// Main renderer
|
|
89
|
-
// ---------------------------------------------------------------------------
|
|
90
|
-
/**
|
|
91
|
-
* Render the complete `entities.ts` source text from the parsed schema.
|
|
92
|
-
*
|
|
93
|
-
* Each entity emits:
|
|
94
|
-
* export class <ClassName> extends Entity("<schema-name>", { ... }) {}
|
|
95
|
-
* or for abstract entities:
|
|
96
|
-
* export class <ClassName> extends Entity(TypeFlags({ name: "<name>", abstract: true }), { ... }) {}
|
|
97
|
-
* and with parent:
|
|
98
|
-
* ... Entity("<name>", { ... }, { parent: <ParentClass> }) {}
|
|
99
|
-
*
|
|
100
|
-
* Field keys = toFieldName(attr_name); values = the field() expression above.
|
|
101
|
-
* Attribute imports come from "./attributes"; entity parent imports also from
|
|
102
|
-
* the same file.
|
|
103
|
-
*
|
|
104
|
-
* The import header always uses the PACKAGE ENTRYPOINT `@type-bridge/node`,
|
|
105
|
-
* never a hardcoded relative path, so generated files stay valid across
|
|
106
|
-
* the packaging layout change.
|
|
107
|
-
*/
|
|
108
|
-
function renderEntities(schema, options) {
|
|
109
|
-
const sortedNames = topologicalSortEntities(schema);
|
|
110
|
-
// Collect which attribute class names are actually referenced (for the import)
|
|
111
|
-
const referencedAttrClasses = new Set();
|
|
112
|
-
// Pre-compute entity class name map
|
|
113
|
-
const entityClassMap = new Map();
|
|
114
|
-
for (const name of Object.keys(schema.entities)) {
|
|
115
|
-
entityClassMap.set(name, (0, naming_js_1.toClassName)(name));
|
|
116
|
-
}
|
|
117
|
-
// Pre-compute attribute class name map
|
|
118
|
-
const attrClassMap = new Map();
|
|
119
|
-
for (const name of Object.keys(schema.attributes)) {
|
|
120
|
-
attrClassMap.set(name, (0, naming_js_1.toClassName)(name));
|
|
121
|
-
}
|
|
122
|
-
// Collect imports by scanning all entities
|
|
123
|
-
for (const entityName of sortedNames) {
|
|
124
|
-
const entity = schema.entities[entityName];
|
|
125
|
-
if (!entity)
|
|
126
|
-
continue;
|
|
127
|
-
// Determine which attrs this entity owns LOCALLY (not inherited from parent)
|
|
128
|
-
const parentOwns = new Set();
|
|
129
|
-
if (entity.parent) {
|
|
130
|
-
const parentEntity = schema.entities[entity.parent];
|
|
131
|
-
if (parentEntity) {
|
|
132
|
-
for (const o of parentEntity.owns) {
|
|
133
|
-
parentOwns.add(o.name);
|
|
134
|
-
}
|
|
135
|
-
}
|
|
136
|
-
}
|
|
137
|
-
for (const owned of entity.owns) {
|
|
138
|
-
if (parentOwns.has(owned.name))
|
|
139
|
-
continue;
|
|
140
|
-
const attrClass = attrClassMap.get(owned.name);
|
|
141
|
-
if (attrClass)
|
|
142
|
-
referencedAttrClasses.add(attrClass);
|
|
143
|
-
}
|
|
144
|
-
}
|
|
145
|
-
// Determine which factory symbols are needed
|
|
146
|
-
const needsKey = sortedNames.some((entityName) => {
|
|
147
|
-
const entity = schema.entities[entityName];
|
|
148
|
-
if (!entity)
|
|
149
|
-
return false;
|
|
150
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
151
|
-
return entity.owns.some((owned) => {
|
|
152
|
-
if (parentOwns.has(owned.name))
|
|
153
|
-
return false;
|
|
154
|
-
return (0, naming_js_1.isKeyAttribute)(owned.name, owned.is_key, options);
|
|
155
|
-
});
|
|
156
|
-
});
|
|
157
|
-
const needsUnique = sortedNames.some((entityName) => {
|
|
158
|
-
const entity = schema.entities[entityName];
|
|
159
|
-
if (!entity)
|
|
160
|
-
return false;
|
|
161
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
162
|
-
return entity.owns.some((owned) => !parentOwns.has(owned.name) && owned.is_unique);
|
|
163
|
-
});
|
|
164
|
-
const needsCard = sortedNames.some((entityName) => {
|
|
165
|
-
const entity = schema.entities[entityName];
|
|
166
|
-
if (!entity)
|
|
167
|
-
return false;
|
|
168
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
169
|
-
return entity.owns.some((owned) => {
|
|
170
|
-
if (parentOwns.has(owned.name))
|
|
171
|
-
return false;
|
|
172
|
-
const card = owned.cardinality;
|
|
173
|
-
return card !== null && (0, cardinality_js_1.isCardMulti)(card);
|
|
174
|
-
});
|
|
175
|
-
});
|
|
176
|
-
const needsTypeFlags = sortedNames.some((name) => schema.entities[name]?.is_abstract);
|
|
177
|
-
const needsOrdered = sortedNames.some((entityName) => {
|
|
178
|
-
const entity = schema.entities[entityName];
|
|
179
|
-
if (!entity)
|
|
180
|
-
return false;
|
|
181
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
182
|
-
return entity.owns.some((owned) => !parentOwns.has(owned.name) && owned.ordered);
|
|
183
|
-
});
|
|
184
|
-
const needsDistinct = sortedNames.some((entityName) => {
|
|
185
|
-
const entity = schema.entities[entityName];
|
|
186
|
-
if (!entity)
|
|
187
|
-
return false;
|
|
188
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
189
|
-
return entity.owns.some((owned) => !parentOwns.has(owned.name) && owned.distinct);
|
|
190
|
-
});
|
|
191
|
-
// Build the factory import list
|
|
192
|
-
const factoryImports = ["Entity", "field"];
|
|
193
|
-
if (needsKey)
|
|
194
|
-
factoryImports.push("Key");
|
|
195
|
-
if (needsUnique)
|
|
196
|
-
factoryImports.push("Unique");
|
|
197
|
-
if (needsCard)
|
|
198
|
-
factoryImports.push("Card");
|
|
199
|
-
if (needsTypeFlags)
|
|
200
|
-
factoryImports.push("TypeFlags");
|
|
201
|
-
if (needsOrdered)
|
|
202
|
-
factoryImports.push("Ordered");
|
|
203
|
-
if (needsDistinct)
|
|
204
|
-
factoryImports.push("Distinct");
|
|
205
|
-
factoryImports.sort();
|
|
206
|
-
const sortedAttrImports = [...referencedAttrClasses].sort();
|
|
207
|
-
const lines = [`import { ${factoryImports.join(", ")} } from "@type-bridge/node";`];
|
|
208
|
-
if (sortedAttrImports.length > 0) {
|
|
209
|
-
lines.push(`import { ${sortedAttrImports.join(", ")} } from "./attributes.js";`);
|
|
210
|
-
}
|
|
211
|
-
lines.push(``);
|
|
212
|
-
// Emit each entity class
|
|
213
|
-
for (const entityName of sortedNames) {
|
|
214
|
-
const entity = schema.entities[entityName];
|
|
215
|
-
if (!entity)
|
|
216
|
-
continue;
|
|
217
|
-
const className = entityClassMap.get(entityName) ?? (0, naming_js_1.toClassName)(entityName);
|
|
218
|
-
// Determine own attrs (not inherited)
|
|
219
|
-
const parentOwns = buildParentOwns(entityName, schema);
|
|
220
|
-
// Use owns_order for deterministic field ordering (mirrors Python `entity.owns_order`)
|
|
221
|
-
const ownLocalAttrs = entity.owns_order.filter((n) => !parentOwns.has(n));
|
|
222
|
-
// Build the fields object entries
|
|
223
|
-
const fieldEntries = [];
|
|
224
|
-
for (const attrName of ownLocalAttrs) {
|
|
225
|
-
const owned = entity.owns.find((o) => o.name === attrName);
|
|
226
|
-
if (!owned)
|
|
227
|
-
continue;
|
|
228
|
-
const attrClass = attrClassMap.get(attrName);
|
|
229
|
-
if (!attrClass)
|
|
230
|
-
continue;
|
|
231
|
-
const fieldKey = (0, naming_js_1.toFieldName)(attrName);
|
|
232
|
-
const isKey = (0, naming_js_1.isKeyAttribute)(attrName, owned.is_key, options);
|
|
233
|
-
const fieldExpr = renderFieldExpr(owned, attrClass, isKey);
|
|
234
|
-
fieldEntries.push(` ${fieldKey}: ${fieldExpr},`);
|
|
235
|
-
}
|
|
236
|
-
// First argument: TypeFlags({...}) for abstract, or plain string
|
|
237
|
-
let firstArg;
|
|
238
|
-
if (entity.is_abstract) {
|
|
239
|
-
firstArg = `TypeFlags({ name: "${entityName}", abstract: true })`;
|
|
240
|
-
}
|
|
241
|
-
else {
|
|
242
|
-
firstArg = `"${entityName}"`;
|
|
243
|
-
}
|
|
244
|
-
// Third argument: { parent: ParentClass } if parent present
|
|
245
|
-
const parentClassName = entity.parent ? entityClassMap.get(entity.parent) : null;
|
|
246
|
-
const thirdArg = parentClassName ? `, { parent: ${parentClassName} }` : "";
|
|
247
|
-
if (fieldEntries.length === 0) {
|
|
248
|
-
lines.push(`export class ${className} extends Entity(${firstArg}, {}${thirdArg}) {}`);
|
|
249
|
-
}
|
|
250
|
-
else {
|
|
251
|
-
lines.push(`export class ${className} extends Entity(${firstArg}, {`);
|
|
252
|
-
for (const entry of fieldEntries) {
|
|
253
|
-
lines.push(entry);
|
|
254
|
-
}
|
|
255
|
-
lines.push(`}${thirdArg}) {}`);
|
|
256
|
-
}
|
|
257
|
-
lines.push(``);
|
|
258
|
-
}
|
|
259
|
-
return lines.join("\n");
|
|
260
|
-
}
|
|
261
|
-
// ---------------------------------------------------------------------------
|
|
262
|
-
// Helpers
|
|
263
|
-
// ---------------------------------------------------------------------------
|
|
264
|
-
/** Build the set of attribute names owned by the direct parent entity. */
|
|
265
|
-
function buildParentOwns(entityName, schema) {
|
|
266
|
-
const entity = schema.entities[entityName];
|
|
267
|
-
if (!entity?.parent)
|
|
268
|
-
return new Set();
|
|
269
|
-
const parent = schema.entities[entity.parent];
|
|
270
|
-
if (!parent)
|
|
271
|
-
return new Set();
|
|
272
|
-
return new Set(parent.owns.map((o) => o.name));
|
|
273
|
-
}
|
|
@@ -1,22 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Render the output package barrel (`index.ts`) text.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors Python `render_package_init()` in `type_bridge/generator/__init__.py`.
|
|
5
|
-
* The barrel re-exports everything from the three generated modules so that
|
|
6
|
-
* callers can import from a single entry point.
|
|
7
|
-
*
|
|
8
|
-
* This file produces the content written to `<outputDir>/index.ts`.
|
|
9
|
-
* It is intentionally named `renderPackage.ts` (not `index.ts`) to avoid
|
|
10
|
-
* confusion with `generator/index.ts` (the generator entry point).
|
|
11
|
-
*
|
|
12
|
-
* Build-time code generation; no runtime ORM logic.
|
|
13
|
-
*/
|
|
14
|
-
import type { TypeSchema } from "../parser.js";
|
|
15
|
-
/**
|
|
16
|
-
* Render the package barrel `index.ts` that re-exports all generated modules.
|
|
17
|
-
*
|
|
18
|
-
* The `schema` parameter is accepted for API symmetry with the other renderers
|
|
19
|
-
* (and to allow future conditional re-exports), but the barrel is unconditional:
|
|
20
|
-
* all three modules are always re-exported.
|
|
21
|
-
*/
|
|
22
|
-
export declare function renderPackage(_schema: TypeSchema): string;
|
|
@@ -1,33 +0,0 @@
|
|
|
1
|
-
"use strict";
|
|
2
|
-
/**
|
|
3
|
-
* Render the output package barrel (`index.ts`) text.
|
|
4
|
-
*
|
|
5
|
-
* Mirrors Python `render_package_init()` in `type_bridge/generator/__init__.py`.
|
|
6
|
-
* The barrel re-exports everything from the three generated modules so that
|
|
7
|
-
* callers can import from a single entry point.
|
|
8
|
-
*
|
|
9
|
-
* This file produces the content written to `<outputDir>/index.ts`.
|
|
10
|
-
* It is intentionally named `renderPackage.ts` (not `index.ts`) to avoid
|
|
11
|
-
* confusion with `generator/index.ts` (the generator entry point).
|
|
12
|
-
*
|
|
13
|
-
* Build-time code generation; no runtime ORM logic.
|
|
14
|
-
*/
|
|
15
|
-
Object.defineProperty(exports, "__esModule", { value: true });
|
|
16
|
-
exports.renderPackage = renderPackage;
|
|
17
|
-
/**
|
|
18
|
-
* Render the package barrel `index.ts` that re-exports all generated modules.
|
|
19
|
-
*
|
|
20
|
-
* The `schema` parameter is accepted for API symmetry with the other renderers
|
|
21
|
-
* (and to allow future conditional re-exports), but the barrel is unconditional:
|
|
22
|
-
* all three modules are always re-exported.
|
|
23
|
-
*/
|
|
24
|
-
// eslint-disable-next-line @typescript-eslint/no-unused-vars
|
|
25
|
-
function renderPackage(_schema) {
|
|
26
|
-
const lines = [
|
|
27
|
-
`export * from "./attributes.js";`,
|
|
28
|
-
`export * from "./entities.js";`,
|
|
29
|
-
`export * from "./relations.js";`,
|
|
30
|
-
``,
|
|
31
|
-
];
|
|
32
|
-
return lines.join("\n");
|
|
33
|
-
}
|
|
@@ -1,36 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Render the `relations.ts` module text from a parsed TypeSchema.
|
|
3
|
-
*
|
|
4
|
-
* Mirrors `type_bridge/generator/render/relations.py` → `render_relations()`.
|
|
5
|
-
*
|
|
6
|
-
* Role-player derivation:
|
|
7
|
-
* Scan ALL entity `plays[]` entries for `role_ref === "<relation>:<role>"`.
|
|
8
|
-
* Remove ancestor types when a descendant is also present (minimal set).
|
|
9
|
-
* This is the exact algorithm of `minimal_role_players()` in models.py.
|
|
10
|
-
*
|
|
11
|
-
* Cardinality sources:
|
|
12
|
-
* Relates-side cardinality comes from the relation role definition
|
|
13
|
-
* (`RoleSpec.cardinality`). Plays-side cardinality comes from the player's
|
|
14
|
-
* `PlayedRole.cardinality` and renders as `playsCardinality` on `role(...)`.
|
|
15
|
-
*
|
|
16
|
-
* Field key = toFieldName(role.name) / toFieldName(attr.name).
|
|
17
|
-
* Class names = toClassName(typeName) for all players / attr types.
|
|
18
|
-
*
|
|
19
|
-
* Build-time code generation; no runtime ORM logic.
|
|
20
|
-
*/
|
|
21
|
-
import type { TypeSchema } from "../parser.js";
|
|
22
|
-
import { type NamingOptions } from "./naming.js";
|
|
23
|
-
/**
|
|
24
|
-
* Render the complete `relations.ts` source text from the parsed schema.
|
|
25
|
-
*
|
|
26
|
-
* Each relation emits:
|
|
27
|
-
* export class <ClassName> extends Relation("<name>", { <fields> }) {}
|
|
28
|
-
*
|
|
29
|
-
* Role keys = toFieldName(role.name).
|
|
30
|
-
* Attr keys = toFieldName(attr.name).
|
|
31
|
-
* Player names = toClassName(entityName).
|
|
32
|
-
*
|
|
33
|
-
* Imports: `@type-bridge/node` for factory symbols; `./attributes.js` for
|
|
34
|
-
* attribute classes; `./entities.js` for player entity classes.
|
|
35
|
-
*/
|
|
36
|
-
export declare function renderRelations(schema: TypeSchema, options?: NamingOptions): string;
|