@type-bridge/node 1.5.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 +102 -0
- package/dist/attribute.d.ts +102 -0
- package/dist/attribute.js +176 -0
- package/dist/codec.d.ts +36 -0
- package/dist/codec.js +274 -0
- package/dist/flags.d.ts +67 -0
- package/dist/flags.js +123 -0
- package/dist/generator/cardinality.d.ts +15 -0
- package/dist/generator/cardinality.js +28 -0
- package/dist/generator/index.d.ts +44 -0
- package/dist/generator/index.js +64 -0
- package/dist/generator/naming.d.ts +64 -0
- package/dist/generator/naming.js +69 -0
- package/dist/generator/renderAttributes.d.ts +22 -0
- package/dist/generator/renderAttributes.js +153 -0
- package/dist/generator/renderEntities.d.ts +39 -0
- package/dist/generator/renderEntities.js +273 -0
- package/dist/generator/renderPackage.d.ts +22 -0
- package/dist/generator/renderPackage.js +33 -0
- package/dist/generator/renderRelations.d.ts +36 -0
- package/dist/generator/renderRelations.js +395 -0
- package/dist/iid.d.ts +5 -0
- package/dist/iid.js +20 -0
- package/dist/index.d.ts +469 -0
- package/dist/index.js +451 -0
- package/dist/manager.d.ts +51 -0
- package/dist/manager.js +282 -0
- package/dist/model.d.ts +261 -0
- package/dist/model.js +481 -0
- package/dist/native.d.ts +14 -0
- package/dist/native.js +125 -0
- package/dist/parser.d.ts +106 -0
- package/dist/parser.js +20 -0
- package/dist/query.d.ts +109 -0
- package/dist/query.js +240 -0
- package/dist/type-bridge-node-1.5.0.tgz +0 -0
- package/package.json +53 -0
- package/type_bridge_node.linux-x64-gnu.node +0 -0
package/dist/manager.js
ADDED
|
@@ -0,0 +1,282 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.TypedRelationManager = exports.TypedEntityManager = void 0;
|
|
4
|
+
exports.entityManagerFor = entityManagerFor;
|
|
5
|
+
exports.relationManagerFor = relationManagerFor;
|
|
6
|
+
exports.buildRolePlayers = buildRolePlayers;
|
|
7
|
+
const query_js_1 = require("./query.js");
|
|
8
|
+
const codec_js_1 = require("./codec.js");
|
|
9
|
+
const iid_js_1 = require("./iid.js");
|
|
10
|
+
class TypedEntityManager {
|
|
11
|
+
#modelClass;
|
|
12
|
+
#dynamic;
|
|
13
|
+
constructor(modelClass, connection) {
|
|
14
|
+
this.#modelClass = modelClass;
|
|
15
|
+
this.#dynamic = connection.entityManager(modelClass.descriptor());
|
|
16
|
+
}
|
|
17
|
+
insert(instance) {
|
|
18
|
+
return (0, iid_js_1.setIid)(instance, this.#dynamic.insert((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema)));
|
|
19
|
+
}
|
|
20
|
+
insertMany(instances) {
|
|
21
|
+
const iids = this.#dynamic.insertMany(instances.map((instance) => (0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema)));
|
|
22
|
+
return setIids(instances, iids);
|
|
23
|
+
}
|
|
24
|
+
put(instance) {
|
|
25
|
+
return (0, iid_js_1.setIid)(instance, this.#dynamic.put((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema)));
|
|
26
|
+
}
|
|
27
|
+
putMany(instances) {
|
|
28
|
+
const iids = this.#dynamic.putMany(instances.map((instance) => (0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema)));
|
|
29
|
+
return setIids(instances, iids);
|
|
30
|
+
}
|
|
31
|
+
update(instance) {
|
|
32
|
+
this.#dynamic.update((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema), instance._iid);
|
|
33
|
+
return instance;
|
|
34
|
+
}
|
|
35
|
+
get(filters) {
|
|
36
|
+
return this.#dynamic
|
|
37
|
+
.get(lowerTypedFilters(filters, this.#modelClass.schema))
|
|
38
|
+
.map((row) => hydrateEntity(this.#modelClass, row));
|
|
39
|
+
}
|
|
40
|
+
all() {
|
|
41
|
+
return this.#dynamic.all().map((row) => hydrateEntity(this.#modelClass, row));
|
|
42
|
+
}
|
|
43
|
+
getByIid(iid) {
|
|
44
|
+
const row = this.#dynamic.getByIid(iid);
|
|
45
|
+
return row === null ? null : hydrateEntity(this.#modelClass, row);
|
|
46
|
+
}
|
|
47
|
+
count(filters) {
|
|
48
|
+
return this.#dynamic.count(lowerTypedFilters(filters, this.#modelClass.schema));
|
|
49
|
+
}
|
|
50
|
+
query() {
|
|
51
|
+
const modelClass = this.#modelClass;
|
|
52
|
+
return new query_js_1.TypedQuery(this.#dynamic, (rows) => rows.map((row) => hydrateEntity(modelClass, row)));
|
|
53
|
+
}
|
|
54
|
+
delete(instanceOrIid) {
|
|
55
|
+
this.#dynamic.deleteByIid(resolveIid(instanceOrIid, "entity"));
|
|
56
|
+
}
|
|
57
|
+
}
|
|
58
|
+
exports.TypedEntityManager = TypedEntityManager;
|
|
59
|
+
class TypedRelationManager {
|
|
60
|
+
#modelClass;
|
|
61
|
+
#dynamic;
|
|
62
|
+
constructor(modelClass, connection) {
|
|
63
|
+
this.#modelClass = modelClass;
|
|
64
|
+
this.#dynamic = connection.relationManager(modelClass.descriptor());
|
|
65
|
+
}
|
|
66
|
+
insert(instance) {
|
|
67
|
+
const iid = this.#dynamic.insert((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema), buildRolePlayers(instance, this.#modelClass.schema));
|
|
68
|
+
return (0, iid_js_1.setIid)(instance, iid);
|
|
69
|
+
}
|
|
70
|
+
insertMany(instances) {
|
|
71
|
+
const iids = this.#dynamic.insertMany(instances.map((instance) => ({
|
|
72
|
+
attributes: (0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema),
|
|
73
|
+
role_players: buildRolePlayers(instance, this.#modelClass.schema),
|
|
74
|
+
})));
|
|
75
|
+
return setIids(instances, iids);
|
|
76
|
+
}
|
|
77
|
+
put(instance) {
|
|
78
|
+
const iid = this.#dynamic.put((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema), buildRolePlayers(instance, this.#modelClass.schema));
|
|
79
|
+
return (0, iid_js_1.setIid)(instance, iid);
|
|
80
|
+
}
|
|
81
|
+
putMany(instances) {
|
|
82
|
+
const iids = this.#dynamic.putMany(instances.map((instance) => ({
|
|
83
|
+
attributes: (0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema),
|
|
84
|
+
role_players: buildRolePlayers(instance, this.#modelClass.schema),
|
|
85
|
+
})));
|
|
86
|
+
return setIids(instances, iids);
|
|
87
|
+
}
|
|
88
|
+
update(instance) {
|
|
89
|
+
this.#dynamic.update((0, codec_js_1.lowerAttributes)(instance, this.#modelClass.schema), buildRolePlayers(instance, this.#modelClass.schema), instance._iid);
|
|
90
|
+
return instance;
|
|
91
|
+
}
|
|
92
|
+
get(filters) {
|
|
93
|
+
return hydrateRelationRows(this.#modelClass, this.#dynamic.get(lowerTypedFilters(filters, this.#modelClass.schema)));
|
|
94
|
+
}
|
|
95
|
+
all() {
|
|
96
|
+
return hydrateRelationRows(this.#modelClass, this.#dynamic.all());
|
|
97
|
+
}
|
|
98
|
+
getByIid(iid) {
|
|
99
|
+
const rows = hydrateRelationRows(this.#modelClass, this.#dynamic.getByIid(iid));
|
|
100
|
+
return rows[0] ?? null;
|
|
101
|
+
}
|
|
102
|
+
count(filters) {
|
|
103
|
+
return this.#dynamic.count(lowerTypedFilters(filters, this.#modelClass.schema));
|
|
104
|
+
}
|
|
105
|
+
query() {
|
|
106
|
+
const modelClass = this.#modelClass;
|
|
107
|
+
return new query_js_1.TypedQuery(this.#dynamic, (rows) => hydrateRelationRows(modelClass, rows));
|
|
108
|
+
}
|
|
109
|
+
delete(instanceOrIid) {
|
|
110
|
+
this.#dynamic.deleteByIid(resolveIid(instanceOrIid, "relation"));
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
exports.TypedRelationManager = TypedRelationManager;
|
|
114
|
+
function entityManagerFor(modelClass, connection) {
|
|
115
|
+
return new TypedEntityManager(modelClass, connection);
|
|
116
|
+
}
|
|
117
|
+
function relationManagerFor(modelClass, connection) {
|
|
118
|
+
return new TypedRelationManager(modelClass, connection);
|
|
119
|
+
}
|
|
120
|
+
function buildRolePlayers(instance, schema) {
|
|
121
|
+
const source = instance;
|
|
122
|
+
const inputs = [];
|
|
123
|
+
for (const [fieldName, spec] of Object.entries(schema)) {
|
|
124
|
+
if (!isRoleSpec(spec)) {
|
|
125
|
+
continue;
|
|
126
|
+
}
|
|
127
|
+
const value = source[fieldName];
|
|
128
|
+
if (value === undefined || value === null) {
|
|
129
|
+
continue;
|
|
130
|
+
}
|
|
131
|
+
const players = Array.isArray(value) ? value : [value];
|
|
132
|
+
for (const player of players) {
|
|
133
|
+
inputs.push(rolePlayerInput(fieldName, spec, player));
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
return inputs;
|
|
137
|
+
}
|
|
138
|
+
function hydrateEntity(modelClass, row) {
|
|
139
|
+
return (0, iid_js_1.setIid)(new modelClass((0, codec_js_1.hydrateAttributes)(row, modelClass.schema)), row.iid);
|
|
140
|
+
}
|
|
141
|
+
function hydrateRelationRows(modelClass, rows) {
|
|
142
|
+
return Array.from(groupRelationRows(rows).values()).map((group) => hydrateRelationGroup(modelClass, group));
|
|
143
|
+
}
|
|
144
|
+
function hydrateRelationGroup(modelClass, rows) {
|
|
145
|
+
const values = {
|
|
146
|
+
...(0, codec_js_1.hydrateAttributes)(rows[0], modelClass.schema),
|
|
147
|
+
...hydrateRoleFields(modelClass.schema, rows),
|
|
148
|
+
};
|
|
149
|
+
return (0, iid_js_1.setIid)(new modelClass(values), rows[0]?.iid ?? null);
|
|
150
|
+
}
|
|
151
|
+
function hydrateRoleFields(schema, rows) {
|
|
152
|
+
const values = {};
|
|
153
|
+
const seen = new Set();
|
|
154
|
+
for (const [fieldName, spec] of Object.entries(schema)) {
|
|
155
|
+
if (!isRoleSpec(spec)) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
const players = [];
|
|
159
|
+
for (const row of rows) {
|
|
160
|
+
for (const player of row.role_players) {
|
|
161
|
+
if (player.role_name !== fieldName) {
|
|
162
|
+
continue;
|
|
163
|
+
}
|
|
164
|
+
const hydrated = hydrateRolePlayer(spec, player);
|
|
165
|
+
const key = `${fieldName}:${hydrated._iid ?? JSON.stringify(hydrated)}`;
|
|
166
|
+
if (seen.has(key)) {
|
|
167
|
+
continue;
|
|
168
|
+
}
|
|
169
|
+
seen.add(key);
|
|
170
|
+
players.push(hydrated);
|
|
171
|
+
}
|
|
172
|
+
}
|
|
173
|
+
if (players.length === 0) {
|
|
174
|
+
if (isMultiRole(spec)) {
|
|
175
|
+
values[fieldName] = [];
|
|
176
|
+
}
|
|
177
|
+
continue;
|
|
178
|
+
}
|
|
179
|
+
values[fieldName] = isMultiRole(spec) || players.length > 1 ? players : players[0];
|
|
180
|
+
}
|
|
181
|
+
return values;
|
|
182
|
+
}
|
|
183
|
+
function hydrateRolePlayer(spec, player) {
|
|
184
|
+
const modelClass = modelClassForPlayer(spec, player);
|
|
185
|
+
const entries = player.attributes.map(([name, value]) => [
|
|
186
|
+
name,
|
|
187
|
+
(0, codec_js_1.runtimeAttributeValueFromUnknown)(value, valueTypeForAttributeName(modelClass.schema, name)),
|
|
188
|
+
]);
|
|
189
|
+
return (0, iid_js_1.setIid)(new modelClass((0, codec_js_1.hydrateAttributeEntries)(entries, modelClass.schema)), player.player_iid);
|
|
190
|
+
}
|
|
191
|
+
function modelClassForPlayer(spec, player) {
|
|
192
|
+
const classes = spec.players.filter(isModelClass);
|
|
193
|
+
const matching = classes.find((modelClass) => modelClass.typeName === player.player_type_name);
|
|
194
|
+
if (matching !== undefined) {
|
|
195
|
+
return matching;
|
|
196
|
+
}
|
|
197
|
+
if (classes.length === 1) {
|
|
198
|
+
return classes[0];
|
|
199
|
+
}
|
|
200
|
+
throw new codec_js_1.TypedCodecError(`Cannot hydrate role "${player.role_name}" player type "${player.player_type_name ?? "<unknown>"}"`);
|
|
201
|
+
}
|
|
202
|
+
function rolePlayerInput(fieldName, spec, player) {
|
|
203
|
+
if (!isModelInstance(player)) {
|
|
204
|
+
throw new codec_js_1.TypedCodecError(`Role field "${fieldName}" must contain typed model instances`);
|
|
205
|
+
}
|
|
206
|
+
const modelClass = player.constructor;
|
|
207
|
+
const input = {
|
|
208
|
+
role_name: fieldName,
|
|
209
|
+
player_type_name: modelClass.descriptor().type_name,
|
|
210
|
+
};
|
|
211
|
+
if (player._iid !== null) {
|
|
212
|
+
return { ...input, iid: player._iid };
|
|
213
|
+
}
|
|
214
|
+
const key = (0, codec_js_1.keyAttributeDescriptor)(modelClass.schema);
|
|
215
|
+
if (key === null) {
|
|
216
|
+
throw new codec_js_1.TypedCodecError(`Role player for role "${fieldName}" needs _iid or a key attribute`);
|
|
217
|
+
}
|
|
218
|
+
const keyValue = player[key.field_name];
|
|
219
|
+
if (!isAttributeLike(keyValue)) {
|
|
220
|
+
throw new codec_js_1.TypedCodecError(`Role player key field "${key.field_name}" is missing`);
|
|
221
|
+
}
|
|
222
|
+
return {
|
|
223
|
+
...input,
|
|
224
|
+
key_attr: key.attr_name,
|
|
225
|
+
key_value: (0, codec_js_1.lowerAttributeValue)(key.value_type, keyValue.value),
|
|
226
|
+
};
|
|
227
|
+
}
|
|
228
|
+
function lowerTypedFilters(filters, schema) {
|
|
229
|
+
return (0, codec_js_1.lowerFilters)(filters, schema);
|
|
230
|
+
}
|
|
231
|
+
function groupRelationRows(rows) {
|
|
232
|
+
const grouped = new Map();
|
|
233
|
+
for (const [index, row] of rows.entries()) {
|
|
234
|
+
const key = row.iid ?? `__row_${index}`;
|
|
235
|
+
const group = grouped.get(key);
|
|
236
|
+
if (group === undefined) {
|
|
237
|
+
grouped.set(key, [row]);
|
|
238
|
+
}
|
|
239
|
+
else {
|
|
240
|
+
group.push(row);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
return grouped;
|
|
244
|
+
}
|
|
245
|
+
function setIids(instances, iids) {
|
|
246
|
+
if (instances.length !== iids.length) {
|
|
247
|
+
throw new codec_js_1.TypedCodecError(`Rust runtime returned ${iids.length} IIDs for ${instances.length} instances`);
|
|
248
|
+
}
|
|
249
|
+
return instances.map((instance, index) => (0, iid_js_1.setIid)(instance, iids[index]));
|
|
250
|
+
}
|
|
251
|
+
function resolveIid(instanceOrIid, kind) {
|
|
252
|
+
if (typeof instanceOrIid === "string") {
|
|
253
|
+
return instanceOrIid;
|
|
254
|
+
}
|
|
255
|
+
if (instanceOrIid._iid === null) {
|
|
256
|
+
throw new codec_js_1.TypedCodecError(`Cannot delete ${kind} instance without _iid`);
|
|
257
|
+
}
|
|
258
|
+
return instanceOrIid._iid;
|
|
259
|
+
}
|
|
260
|
+
function isRoleSpec(spec) {
|
|
261
|
+
return spec.kind === "role";
|
|
262
|
+
}
|
|
263
|
+
function valueTypeForAttributeName(schema, attrName) {
|
|
264
|
+
for (const spec of Object.values(schema)) {
|
|
265
|
+
if ((spec.kind === "field" || spec.kind === "list-field") && spec.attrType.attrName === attrName) {
|
|
266
|
+
return spec.attrType.valueType;
|
|
267
|
+
}
|
|
268
|
+
}
|
|
269
|
+
return undefined;
|
|
270
|
+
}
|
|
271
|
+
function isMultiRole(spec) {
|
|
272
|
+
return spec.cardinality !== null && (spec.cardinality[1] === null || spec.cardinality[1] > 1);
|
|
273
|
+
}
|
|
274
|
+
function isModelClass(value) {
|
|
275
|
+
return typeof value === "function";
|
|
276
|
+
}
|
|
277
|
+
function isModelInstance(value) {
|
|
278
|
+
return typeof value === "object" && value !== null && "_iid" in value;
|
|
279
|
+
}
|
|
280
|
+
function isAttributeLike(value) {
|
|
281
|
+
return typeof value === "object" && value !== null && "value" in value;
|
|
282
|
+
}
|
package/dist/model.d.ts
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
1
|
+
import type { Attribute } from "./attribute.js";
|
|
2
|
+
import type { ValueType } from "./index.js";
|
|
3
|
+
import type { AttributeSchemaEntry, EntityDescriptor, RelationDescriptor } from "./index.js";
|
|
4
|
+
import { type CardSpec, type FlagInput, type ResolvedTypeFlags } from "./flags.js";
|
|
5
|
+
import { type IidBearing } from "./iid.js";
|
|
6
|
+
import { type ManagerConnection, type TypedEntityManager, type TypedRelationManager } from "./manager.js";
|
|
7
|
+
export type AttributeClass = (new (value: never) => Attribute<unknown, string>) & {
|
|
8
|
+
readonly attrName: string;
|
|
9
|
+
readonly valueType: ValueType;
|
|
10
|
+
readonly attributeSchema: AttributeSchemaEntry;
|
|
11
|
+
readonly attributeSchemaEntries: readonly AttributeSchemaEntry[];
|
|
12
|
+
};
|
|
13
|
+
type ModelClassLike = (new (values: never) => object) & {
|
|
14
|
+
readonly typeName: string;
|
|
15
|
+
};
|
|
16
|
+
type ModelToken = string | ModelClassLike;
|
|
17
|
+
/**
|
|
18
|
+
* A model class that also exposes its schema, used as a parent reference.
|
|
19
|
+
* The schema constraint lets `Entity()` / `Relation()` merge parent fields into
|
|
20
|
+
* the child at both the type level and the descriptor-emission level.
|
|
21
|
+
*/
|
|
22
|
+
export type ParentModelClass<ParentSchema extends Record<string, SchemaSpec> = Record<string, SchemaSpec>> = ModelClassLike & {
|
|
23
|
+
readonly schema: ParentSchema;
|
|
24
|
+
};
|
|
25
|
+
/**
|
|
26
|
+
* Options accepted as the third argument to `Entity()` / `Relation()` for
|
|
27
|
+
* declaring a parent (supertype) model.
|
|
28
|
+
*/
|
|
29
|
+
export interface ParentOption<ParentSchema extends Record<string, SchemaSpec>> {
|
|
30
|
+
readonly parent: ParentModelClass<ParentSchema>;
|
|
31
|
+
}
|
|
32
|
+
/**
|
|
33
|
+
* Merge two schema records: parent fields followed by child-local fields.
|
|
34
|
+
* Child keys shadow parent keys when both declare the same field name.
|
|
35
|
+
*/
|
|
36
|
+
export type MergedSchema<ParentSchema extends Record<string, SchemaSpec>, ChildSchema extends Record<string, SchemaSpec>> = Omit<ParentSchema, keyof ChildSchema> & ChildSchema;
|
|
37
|
+
/**
|
|
38
|
+
* A declared owned-attribute field: the attribute class, its resolved flags, and
|
|
39
|
+
* whether it is optional. Produced by `field()`; consumed by the model factory to
|
|
40
|
+
* type construction and to emit `owned_attributes`.
|
|
41
|
+
*/
|
|
42
|
+
export declare class FieldSpec<Attr extends AttributeClass, Optional extends boolean = false> {
|
|
43
|
+
readonly attrType: Attr;
|
|
44
|
+
readonly isOptional: Optional;
|
|
45
|
+
readonly kind = "field";
|
|
46
|
+
readonly flags: import("./flags.js").FlagSpec;
|
|
47
|
+
constructor(attrType: Attr, flags?: readonly FlagInput[], isOptional?: Optional);
|
|
48
|
+
optional(): FieldSpec<Attr, true>;
|
|
49
|
+
/**
|
|
50
|
+
* Declare this field as a multi-value (list) attribute. Pass a `Card(min, max)`
|
|
51
|
+
* cardinality bound; an unbounded list uses `Card(0)` (max = null).
|
|
52
|
+
*
|
|
53
|
+
* The returned `ListFieldSpec` emits `{"Card":[min,max|null]}` in `annotations`
|
|
54
|
+
* and sets `is_optional` when `card_min == 0` (mirroring `_is_optional` in
|
|
55
|
+
* `type_bridge/_rust_runtime.py`). The typed value is `Attr[] | undefined`
|
|
56
|
+
* when optional, `Attr[]` when required.
|
|
57
|
+
*/
|
|
58
|
+
list<const Min extends number, const Max extends number | null>(card: CardSpec<Min, Max>): ListFieldSpec<Attr, Min extends 0 ? true : false>;
|
|
59
|
+
}
|
|
60
|
+
/**
|
|
61
|
+
* A declared multi-value (list) owned-attribute field. Produced by
|
|
62
|
+
* `field(Attr).list(Card(min, max))`; emits a `{"Card":[min,max|null]}`
|
|
63
|
+
* annotation. Analogous to the Python ORM's `card_min`/`card_max` flags.
|
|
64
|
+
*
|
|
65
|
+
* `is_optional` is derived from `card_min == 0`, matching `_is_optional` in
|
|
66
|
+
* `type_bridge/_rust_runtime.py`.
|
|
67
|
+
*/
|
|
68
|
+
export declare class ListFieldSpec<Attr extends AttributeClass, Optional extends boolean = false> {
|
|
69
|
+
readonly attrType: Attr;
|
|
70
|
+
readonly kind = "list-field";
|
|
71
|
+
/** Explicit cardinality `[min, max | null]`. Always present for list fields. */
|
|
72
|
+
readonly card: [number, number | null];
|
|
73
|
+
readonly isOptional: Optional;
|
|
74
|
+
/** True when the parent FieldSpec carried the `Ordered` flag. A multi-value
|
|
75
|
+
* `Card` field is a set, not a TypeDB list: only the `Ordered` flag makes
|
|
76
|
+
* the descriptor emit `is_ordered: true` (and `[]` in the define block). */
|
|
77
|
+
readonly isOrdered: boolean;
|
|
78
|
+
/** True when the parent FieldSpec carried the `Distinct` flag. */
|
|
79
|
+
readonly isDistinct: boolean;
|
|
80
|
+
constructor(attrType: Attr, cardSpec: CardSpec, isOrdered?: boolean, isDistinct?: boolean);
|
|
81
|
+
}
|
|
82
|
+
/**
|
|
83
|
+
* A declared relation role: its permitted player model(s), optional
|
|
84
|
+
* relates-side cardinality, and optional plays-side cardinality. Produced by
|
|
85
|
+
* `role()`; consumed by the model factory to emit `roles[*]` plus metadata
|
|
86
|
+
* used by `DescriptorRegistry.schemaInfo()`. Multi-player roles list more than
|
|
87
|
+
* one player token.
|
|
88
|
+
*/
|
|
89
|
+
export declare class RoleSpec<Players extends readonly ModelToken[]> {
|
|
90
|
+
readonly players: Players;
|
|
91
|
+
readonly kind = "role";
|
|
92
|
+
readonly cardinality: [number, number | null] | null;
|
|
93
|
+
readonly playsCardinality: [number, number | null] | null;
|
|
94
|
+
/** Parent role name this role specializes via TypeDB's `relates child as parent` syntax.
|
|
95
|
+
* Used only for descriptor computation (effective-set role exclusion); specialization
|
|
96
|
+
* semantics are resolved at schema-define time. */
|
|
97
|
+
readonly overrides: string | undefined;
|
|
98
|
+
/** When ``true``, this role is abstract at the TypeDB schema level (``@abstract`` on
|
|
99
|
+
* the ``relates`` clause). The engine rejects direct players at the declaring
|
|
100
|
+
* relation's own scope; subtypes that plain-inherit or override the role are
|
|
101
|
+
* unaffected. */
|
|
102
|
+
readonly isAbstract: boolean;
|
|
103
|
+
/** When ``true``, declares this role as a list role (``relates name[]`` in TypeQL).
|
|
104
|
+
* Schema-only; instance-level list writes are not yet supported by the engine. */
|
|
105
|
+
readonly ordered: boolean;
|
|
106
|
+
/** When ``true``, emits ``@distinct`` on the relates clause. Requires ``ordered``. */
|
|
107
|
+
readonly distinct: boolean;
|
|
108
|
+
constructor(players: Players, cardinality?: CardSpec | null, playsCardinality?: CardSpec | null, overrides?: string, isAbstract?: boolean, ordered?: boolean, distinct?: boolean);
|
|
109
|
+
}
|
|
110
|
+
export type SchemaSpec = FieldSpec<AttributeClass, boolean> | ListFieldSpec<AttributeClass, boolean> | RoleSpec<readonly ModelToken[]>;
|
|
111
|
+
export type EntitySchema = Record<string, FieldSpec<AttributeClass, boolean> | ListFieldSpec<AttributeClass, boolean>>;
|
|
112
|
+
export type RelationSchema = Record<string, SchemaSpec>;
|
|
113
|
+
export type FieldValue<Spec> = Spec extends ListFieldSpec<infer Attr, boolean> ? InstanceType<Attr>[] : Spec extends FieldSpec<infer Attr, boolean> ? InstanceType<Attr> : Spec extends RoleSpec<infer Players> ? RoleValue<Players> : never;
|
|
114
|
+
export type InstanceFields<Schema extends Record<string, SchemaSpec>> = {
|
|
115
|
+
readonly [Key in keyof Schema]: Schema[Key] extends FieldSpec<AttributeClass, true> | ListFieldSpec<AttributeClass, true> ? FieldValue<Schema[Key]> | undefined : FieldValue<Schema[Key]>;
|
|
116
|
+
};
|
|
117
|
+
/**
|
|
118
|
+
* The plain primitive value corresponding to a schema field spec, used as the
|
|
119
|
+
* value type in the `toDict()` / `fromDict()` canonical dict shape.
|
|
120
|
+
*
|
|
121
|
+
* - `FieldSpec<Attr, false>` → the raw `.value` type of the attribute instance
|
|
122
|
+
* - `FieldSpec<Attr, true>` → raw `.value` type or `undefined` (optional field)
|
|
123
|
+
* - `ListFieldSpec<Attr, *>` → array of raw `.value` type (optional list is
|
|
124
|
+
* included only when the list is non-empty; absent optional list is omitted)
|
|
125
|
+
*
|
|
126
|
+
* `RoleSpec` fields are deliberately excluded — `toDict`/`fromDict` are for
|
|
127
|
+
* attribute serialization only.
|
|
128
|
+
*/
|
|
129
|
+
export type PlainFieldValue<Spec> = Spec extends ListFieldSpec<infer Attr, boolean> ? InstanceType<Attr>["value"][] : Spec extends FieldSpec<infer Attr, boolean> ? InstanceType<Attr>["value"] : never;
|
|
130
|
+
/**
|
|
131
|
+
* The schema-derived canonical plain dict type produced by `toDict()` and
|
|
132
|
+
* consumed by `fromDict()`. Each field key maps to its plain primitive (or
|
|
133
|
+
* plain-primitive array for list fields). Optional fields are marked `?` and
|
|
134
|
+
* are omitted from `toDict()` output when absent (mirroring Python's
|
|
135
|
+
* `model_dump(exclude_unset=...)` behaviour).
|
|
136
|
+
*
|
|
137
|
+
* This type is intentionally NOT `Record<string, unknown>` — it is fully
|
|
138
|
+
* derived from the schema so that indexing an unknown key is a type error.
|
|
139
|
+
*
|
|
140
|
+
* Required non-role attribute fields → required key with plain primitive type.
|
|
141
|
+
* Optional attribute fields → optional key (`?`) with plain primitive type.
|
|
142
|
+
* Role fields → excluded entirely (key maps to `never` via the `as` clause).
|
|
143
|
+
*/
|
|
144
|
+
export type InstanceDict<Schema extends Record<string, SchemaSpec>> = {
|
|
145
|
+
readonly [Key in keyof Schema as Schema[Key] extends RoleSpec<readonly ModelToken[]> | FieldSpec<AttributeClass, true> | ListFieldSpec<AttributeClass, true> ? never : Key]: PlainFieldValue<Schema[Key]>;
|
|
146
|
+
} & {
|
|
147
|
+
readonly [Key in keyof Schema as Schema[Key] extends FieldSpec<AttributeClass, true> | ListFieldSpec<AttributeClass, true> ? Key : never]?: PlainFieldValue<Schema[Key]>;
|
|
148
|
+
};
|
|
149
|
+
type ConstructorInput<Schema extends Record<string, SchemaSpec>> = {
|
|
150
|
+
readonly [Key in RequiredKeys<Schema>]: FieldValue<Schema[Key]>;
|
|
151
|
+
} & {
|
|
152
|
+
readonly [Key in OptionalKeys<Schema>]?: FieldValue<Schema[Key]>;
|
|
153
|
+
};
|
|
154
|
+
type OptionalKeys<Schema extends Record<string, SchemaSpec>> = {
|
|
155
|
+
[Key in keyof Schema]: Schema[Key] extends FieldSpec<AttributeClass, true> | ListFieldSpec<AttributeClass, true> | RoleSpec<readonly []> ? Key : never;
|
|
156
|
+
}[keyof Schema];
|
|
157
|
+
type RequiredKeys<Schema extends Record<string, SchemaSpec>> = Exclude<keyof Schema, OptionalKeys<Schema>>;
|
|
158
|
+
type RoleValue<Players extends readonly ModelToken[]> = Players extends readonly [] ? undefined : RolePlayerInstance<Players[number]> | readonly RolePlayerInstance<Players[number]>[];
|
|
159
|
+
type RolePlayerInstance<Token> = Token extends string ? object : Token extends new (values: never) => infer Instance ? Instance : never;
|
|
160
|
+
/**
|
|
161
|
+
* The canonical hydrated-instance type for a model schema. This is the single
|
|
162
|
+
* source of truth for what `class X extends Entity(...) {}` produces AND what a
|
|
163
|
+
* manager's `get`/`all`/`first`/`insert`/hydrate paths return. Both surfaces
|
|
164
|
+
* reference this type so a manager-fetched instance is assignable to the user's
|
|
165
|
+
* model class — manager-hydrated instances are real `new modelClass(...)`
|
|
166
|
+
* instances, so they genuinely carry `toDict` at runtime.
|
|
167
|
+
*
|
|
168
|
+
* Shape: schema-derived fields, the `_iid` slot (`IidBearing`), and the
|
|
169
|
+
* precise `toDict(): InstanceDict<Schema>` serializer.
|
|
170
|
+
*/
|
|
171
|
+
export type ModelInstance<Schema extends Record<string, SchemaSpec>> = InstanceFields<Schema> & IidBearing & {
|
|
172
|
+
/**
|
|
173
|
+
* Serialize this typed instance to a canonical plain dict, byte-shape
|
|
174
|
+
* identical to Python `to_dict()` (`type_bridge/models/entity.py:291-335`).
|
|
175
|
+
*
|
|
176
|
+
* Each branded `Attribute` field is unwrapped to its plain primitive; list
|
|
177
|
+
* fields serialize to a plain array of primitives. Absent optional fields
|
|
178
|
+
* are omitted from the result (not included as `undefined`) — this mirrors
|
|
179
|
+
* Python `model_dump(exclude_unset=...)` + `_unwrap_value`.
|
|
180
|
+
*
|
|
181
|
+
* Return type: `InstanceDict<Schema>` — a schema-derived mapped type keyed
|
|
182
|
+
* by field name with plain primitive values. Indexing a non-schema key is a
|
|
183
|
+
* compile-time type error; the return is never `Record<string, any>`.
|
|
184
|
+
*/
|
|
185
|
+
toDict(): InstanceDict<Schema>;
|
|
186
|
+
};
|
|
187
|
+
export type ModelClass<Schema extends Record<string, SchemaSpec>, Descriptor extends EntityDescriptor | RelationDescriptor> = (new (values: ConstructorInput<Schema>) => ModelInstance<Schema>) & {
|
|
188
|
+
readonly typeName: string;
|
|
189
|
+
readonly schema: Schema;
|
|
190
|
+
readonly flags: ResolvedTypeFlags;
|
|
191
|
+
descriptor(): Descriptor;
|
|
192
|
+
/**
|
|
193
|
+
* Construct a typed instance from a canonical plain dict, mirroring Python
|
|
194
|
+
* `from_dict()` (`type_bridge/models/entity.py:337-378`).
|
|
195
|
+
*
|
|
196
|
+
* Each plain primitive (or array) is re-branded into a `Attribute` /
|
|
197
|
+
* `Attribute[]` using the field's attribute class constructor (`new
|
|
198
|
+
* attrType(value)`). The resulting values are passed to the model
|
|
199
|
+
* constructor. Runtime guards:
|
|
200
|
+
* - Unknown keys: throws `TypedCodecError` (strict mode).
|
|
201
|
+
* - Missing required fields: the constructor throws `TypeError`.
|
|
202
|
+
*/
|
|
203
|
+
fromDict(data: InstanceDict<Schema>): ModelInstance<Schema>;
|
|
204
|
+
manager(db: ManagerConnection): Descriptor extends EntityDescriptor ? TypedEntityManager<ModelInstance<Schema>> : TypedRelationManager<ModelInstance<Schema>>;
|
|
205
|
+
};
|
|
206
|
+
/** Declare an owned-attribute field on a model schema, with optional flags. */
|
|
207
|
+
export declare function field<Attr extends AttributeClass>(attrType: Attr, ...flags: FlagInput[]): FieldSpec<Attr, false>;
|
|
208
|
+
/**
|
|
209
|
+
* Declare a relation role. Pass zero or more player model tokens, optionally
|
|
210
|
+
* followed by `{ cardinality, playsCardinality }`. Player tokens may be model
|
|
211
|
+
* classes or raw type name strings (the latter for players whose typed class is
|
|
212
|
+
* not yet declared). `cardinality` is relates-side; `playsCardinality` is
|
|
213
|
+
* player-side and therefore requires at least one player token.
|
|
214
|
+
*/
|
|
215
|
+
export declare function role(): RoleSpec<readonly []>;
|
|
216
|
+
export declare function role(options: RelatesOnlyRoleOptions): RoleSpec<readonly []>;
|
|
217
|
+
export declare function role<const Players extends readonly [ModelToken, ...ModelToken[]]>(...players: Players): RoleSpec<Players>;
|
|
218
|
+
export declare function role<const Players extends readonly [ModelToken, ...ModelToken[]]>(...playersAndOptions: [...Players, RoleOptions]): RoleSpec<Players>;
|
|
219
|
+
/**
|
|
220
|
+
* Build a hard-typed entity base class from a name (or `TypeFlags`) and a field
|
|
221
|
+
* schema. Extend the result to declare the model:
|
|
222
|
+
* `class Person extends Entity("person", { name: field(Name, Key) }) {}`.
|
|
223
|
+
* Construction, field reads, and `descriptor()` are all typed from the schema.
|
|
224
|
+
*
|
|
225
|
+
* Pass a third `{ parent: ParentClass }` argument to declare an inheritance
|
|
226
|
+
* relationship. The child's descriptor emits `parent_type` and the full flattened
|
|
227
|
+
* `owned_attributes` (parent attrs re-listed, then child-local attrs). Inherited
|
|
228
|
+
* fields are accessible on the child instance with the parent's attribute brand.
|
|
229
|
+
*/
|
|
230
|
+
export declare function Entity<const Schema extends EntitySchema>(typeNameOrFlags: string | ResolvedTypeFlags, schema: Schema): ModelClass<Schema, EntityDescriptor>;
|
|
231
|
+
export declare function Entity<const ParentSchema extends EntitySchema, const Schema extends EntitySchema>(typeNameOrFlags: string | ResolvedTypeFlags, schema: Schema, options: ParentOption<ParentSchema>): ModelClass<MergedSchema<ParentSchema, Schema>, EntityDescriptor>;
|
|
232
|
+
/**
|
|
233
|
+
* Build a hard-typed relation base class. Like `Entity`, but the schema may also
|
|
234
|
+
* contain `role(...)` specs, which are emitted as `roles[*]` in the descriptor.
|
|
235
|
+
*
|
|
236
|
+
* Pass a third `{ parent: ParentClass }` argument to declare a parent relation
|
|
237
|
+
* type. The child's descriptor emits `parent_type`, the full flattened
|
|
238
|
+
* `owned_attributes`, and the **effective role set**: plain-inherited parent roles
|
|
239
|
+
* first (in the parent's own effective order), then child-local roles — excluding
|
|
240
|
+
* any parent role whose name appears as the `overrides` target of a child role.
|
|
241
|
+
* This mirrors the Python descriptor contract (see `internals.md`, "Descriptor Contract").
|
|
242
|
+
*/
|
|
243
|
+
export declare function Relation<const Schema extends RelationSchema>(typeNameOrFlags: string | ResolvedTypeFlags, schema: Schema): ModelClass<Schema, RelationDescriptor>;
|
|
244
|
+
export declare function Relation<const ParentSchema extends RelationSchema, const Schema extends RelationSchema>(typeNameOrFlags: string | ResolvedTypeFlags, schema: Schema, options: ParentOption<ParentSchema>): ModelClass<MergedSchema<ParentSchema, Schema>, RelationDescriptor>;
|
|
245
|
+
type RelatesOnlyRoleOptions = {
|
|
246
|
+
readonly cardinality?: CardSpec | null;
|
|
247
|
+
readonly playsCardinality?: never;
|
|
248
|
+
readonly overrides?: string;
|
|
249
|
+
readonly abstract?: boolean;
|
|
250
|
+
readonly ordered?: boolean;
|
|
251
|
+
readonly distinct?: boolean;
|
|
252
|
+
};
|
|
253
|
+
type RoleOptions = {
|
|
254
|
+
readonly cardinality?: CardSpec | null;
|
|
255
|
+
readonly playsCardinality?: CardSpec | null;
|
|
256
|
+
readonly overrides?: string;
|
|
257
|
+
readonly abstract?: boolean;
|
|
258
|
+
readonly ordered?: boolean;
|
|
259
|
+
readonly distinct?: boolean;
|
|
260
|
+
};
|
|
261
|
+
export type { IidBearing };
|