@orkestrel/relation 0.0.27 → 0.0.28
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 +6 -6
- package/dist/src/core/index.cjs +88 -86
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +271 -269
- package/dist/src/core/index.d.ts +271 -269
- package/dist/src/core/index.js +88 -86
- package/dist/src/core/index.js.map +1 -1
- package/package.json +8 -8
package/dist/src/core/index.js
CHANGED
|
@@ -11,7 +11,7 @@ import { Emitter } from "@orkestrel/emitter";
|
|
|
11
11
|
* relation targeting a table or junction the database does not declare, raised when
|
|
12
12
|
* the manager is created (`INVALID`), a junction keyed by its relation's `source` or
|
|
13
13
|
* `target` column (`INVALID`), a row a storage returns that is not a plain record
|
|
14
|
-
* (`INVALID`), a reference to a relation a
|
|
14
|
+
* (`INVALID`), a reference to a relation a repository does not
|
|
15
15
|
* define (`UNKNOWN_RELATION`), and `link` / `unlink` / `links` on a relation that is
|
|
16
16
|
* not a `through` (`NOT_THROUGH`).
|
|
17
17
|
*/
|
|
@@ -65,7 +65,7 @@ function isRelationError(value) {
|
|
|
65
65
|
*
|
|
66
66
|
* @remarks
|
|
67
67
|
* Every member is optional: `relationship` is one of the `Relationship` values, and
|
|
68
|
-
* `column`, `key`, `through`, `source`, `target`, `tag`, `label`, and `
|
|
68
|
+
* `column`, `key`, `through`, `source`, `target`, `tag`, `label`, and `table` are strings.
|
|
69
69
|
* The object is closed, so an enumerable, string-keyed member the descriptor does not declare
|
|
70
70
|
* refuses it.
|
|
71
71
|
*
|
|
@@ -92,7 +92,7 @@ var relationDescriptorShape = objectShape({
|
|
|
92
92
|
target: optionalShape(stringShape()),
|
|
93
93
|
tag: optionalShape(stringShape()),
|
|
94
94
|
label: optionalShape(stringShape()),
|
|
95
|
-
|
|
95
|
+
table: optionalShape(stringShape())
|
|
96
96
|
});
|
|
97
97
|
// src/core/contracts.ts
|
|
98
98
|
/**
|
|
@@ -113,7 +113,7 @@ var relationDescriptorContract = createContract(relationDescriptorShape);
|
|
|
113
113
|
* @remarks
|
|
114
114
|
* The guard of the compiled `relationDescriptorContract`: a plain record whose every
|
|
115
115
|
* member is optional, with `relationship` one of the `Relationship` values and `column`,
|
|
116
|
-
* `key`, `through`, `source`, `target`, `tag`, `label`, and `
|
|
116
|
+
* `key`, `through`, `source`, `target`, `tag`, `label`, and `table` strings. A member
|
|
117
117
|
* present with an `undefined` value, a member holding another type, and an enumerable,
|
|
118
118
|
* string-keyed member the descriptor does not declare each refuse it. That is what lets
|
|
119
119
|
* `resolveRelation` read each member after one narrow and report a malformed definition
|
|
@@ -132,7 +132,7 @@ var isRelationDescriptor = relationDescriptorContract.is;
|
|
|
132
132
|
* Validates a relation table through its bound storage, translating only an undeclared table.
|
|
133
133
|
*
|
|
134
134
|
* @param storage - The storage that resolves the table
|
|
135
|
-
* @param
|
|
135
|
+
* @param repository - The owning repository's name
|
|
136
136
|
* @param relation - The relation's name
|
|
137
137
|
* @param table - The target or junction table's name
|
|
138
138
|
* @param junction - If `true`, reports an undeclared junction; if `false`, reports an undeclared target. Default: `false`.
|
|
@@ -143,13 +143,13 @@ var isRelationDescriptor = relationDescriptorContract.is;
|
|
|
143
143
|
* validateRelationTable(storage, 'accounts', 'contacts', 'contacts')
|
|
144
144
|
* ```
|
|
145
145
|
*/
|
|
146
|
-
function validateRelationTable(storage,
|
|
146
|
+
function validateRelationTable(storage, repository, relation, table, junction = false) {
|
|
147
147
|
try {
|
|
148
148
|
storage.table(table);
|
|
149
149
|
} catch (error) {
|
|
150
150
|
if (!isDatabaseError(error) || error.code !== "NOT_FOUND") throw error;
|
|
151
|
-
throw new RelationError("INVALID", junction ? `Relation ${renderPath([
|
|
152
|
-
|
|
151
|
+
throw new RelationError("INVALID", junction ? `Relation ${renderPath([repository, relation])} uses undeclared junction ${renderPath([table])}; declare it in the database's tables or correct the relation's through` : `Relation ${renderPath([repository, relation])} targets undeclared table ${renderPath([table])}; declare it in the database's tables or correct the relation's table`, {
|
|
152
|
+
repository,
|
|
153
153
|
relation,
|
|
154
154
|
table
|
|
155
155
|
});
|
|
@@ -162,7 +162,7 @@ function validateRelationTable(storage, model, relation, table, junction = false
|
|
|
162
162
|
* `link` writes the pair's `computeLinkKey` into the junction's primary column, so a junction
|
|
163
163
|
* whose primary column is the relation's `source` or `target` column cannot hold that key.
|
|
164
164
|
*
|
|
165
|
-
* @param
|
|
165
|
+
* @param repository - The owning repository's name
|
|
166
166
|
* @param resolved - The resolved `through` relation
|
|
167
167
|
* @param primary - The junction table's primary-key column
|
|
168
168
|
* @throws An `INVALID` {@link RelationError} when `primary` is the relation's `source` or
|
|
@@ -173,11 +173,11 @@ function validateRelationTable(storage, model, relation, table, junction = false
|
|
|
173
173
|
* validateRelationJunction('accounts', resolved, 'id') // a junction keyed by `id` passes
|
|
174
174
|
* ```
|
|
175
175
|
*/
|
|
176
|
-
function validateRelationJunction(
|
|
176
|
+
function validateRelationJunction(repository, resolved, primary) {
|
|
177
177
|
const role = primary === resolved.source ? "source" : primary === resolved.target ? "target" : void 0;
|
|
178
178
|
if (role === void 0) return;
|
|
179
|
-
throw new RelationError("INVALID", `Relation ${renderPath([
|
|
180
|
-
|
|
179
|
+
throw new RelationError("INVALID", `Relation ${renderPath([repository, resolved.name])} keys its junction ${renderPath([resolved.through])} by its ${role} column ${renderPath([primary])}; give the junction a separate text primary column, which link fills with computeLinkKey`, {
|
|
180
|
+
repository,
|
|
181
181
|
relation: resolved.name,
|
|
182
182
|
table: resolved.through,
|
|
183
183
|
column: primary
|
|
@@ -211,7 +211,7 @@ function readRow(table, row) {
|
|
|
211
211
|
* A `string` is a `belongs` (foreign-key column on this table); a `readonly string[]`
|
|
212
212
|
* is a `many` (foreign-key column on the related table); a {@link RelationDescriptor}
|
|
213
213
|
* uses its explicit `relationship`, or infers it from the fields present. The target
|
|
214
|
-
*
|
|
214
|
+
* table defaults to the relation name. A descriptor must pass `relationDescriptorContract`'s
|
|
215
215
|
* strict guard: an undeclared member, enumerable or not, a member holding another type, and
|
|
216
216
|
* a member present as `undefined` each refuse it, and no lossless repair admits it. The
|
|
217
217
|
* refusal carries the contract's audit faults, described against its schema. Every column the resolved arm declares is validated here, which is
|
|
@@ -233,7 +233,7 @@ function resolveRelation(name, value) {
|
|
|
233
233
|
if (isString(value)) return {
|
|
234
234
|
relationship: "belongs",
|
|
235
235
|
name,
|
|
236
|
-
|
|
236
|
+
table: name,
|
|
237
237
|
column: value
|
|
238
238
|
};
|
|
239
239
|
const path = renderPath([name]);
|
|
@@ -243,7 +243,7 @@ function resolveRelation(name, value) {
|
|
|
243
243
|
return {
|
|
244
244
|
relationship: "many",
|
|
245
245
|
name,
|
|
246
|
-
|
|
246
|
+
table: name,
|
|
247
247
|
key
|
|
248
248
|
};
|
|
249
249
|
}
|
|
@@ -256,7 +256,7 @@ function resolveRelation(name, value) {
|
|
|
256
256
|
});
|
|
257
257
|
}
|
|
258
258
|
const descriptor = admitted.value;
|
|
259
|
-
const
|
|
259
|
+
const table = descriptor.table ?? name;
|
|
260
260
|
const relationship = descriptor.relationship !== void 0 ? descriptor.relationship : isDefined(descriptor.through) ? "through" : isDefined(descriptor.tag) ? "morph" : isDefined(descriptor.column) ? "belongs" : isDefined(descriptor.key) ? "one" : void 0;
|
|
261
261
|
if (relationship === void 0) throw new RelationError("INVALID", `Relation ${path}: cannot infer the relationship; set relationship, or give column (belongs), key (one), through (through), or tag (morph)`, { relation: name });
|
|
262
262
|
switch (relationship) {
|
|
@@ -266,7 +266,7 @@ function resolveRelation(name, value) {
|
|
|
266
266
|
return {
|
|
267
267
|
relationship,
|
|
268
268
|
name,
|
|
269
|
-
|
|
269
|
+
table,
|
|
270
270
|
column
|
|
271
271
|
};
|
|
272
272
|
}
|
|
@@ -276,7 +276,7 @@ function resolveRelation(name, value) {
|
|
|
276
276
|
return {
|
|
277
277
|
relationship,
|
|
278
278
|
name,
|
|
279
|
-
|
|
279
|
+
table,
|
|
280
280
|
key
|
|
281
281
|
};
|
|
282
282
|
}
|
|
@@ -286,7 +286,7 @@ function resolveRelation(name, value) {
|
|
|
286
286
|
return {
|
|
287
287
|
relationship,
|
|
288
288
|
name,
|
|
289
|
-
|
|
289
|
+
table,
|
|
290
290
|
key
|
|
291
291
|
};
|
|
292
292
|
}
|
|
@@ -296,7 +296,7 @@ function resolveRelation(name, value) {
|
|
|
296
296
|
return {
|
|
297
297
|
relationship,
|
|
298
298
|
name,
|
|
299
|
-
|
|
299
|
+
table,
|
|
300
300
|
through,
|
|
301
301
|
source,
|
|
302
302
|
target
|
|
@@ -308,7 +308,7 @@ function resolveRelation(name, value) {
|
|
|
308
308
|
return {
|
|
309
309
|
relationship,
|
|
310
310
|
name,
|
|
311
|
-
|
|
311
|
+
table,
|
|
312
312
|
key,
|
|
313
313
|
tag,
|
|
314
314
|
label
|
|
@@ -351,7 +351,7 @@ function computeLinkKey(key, target) {
|
|
|
351
351
|
* @remarks
|
|
352
352
|
* An array-valued relation (`many` / `through` / `morph`) sums its lengths; a
|
|
353
353
|
* single-valued one (`belongs` / `one`) counts each present row. This is the count a
|
|
354
|
-
* `
|
|
354
|
+
* `Repository` carries on its `load` event.
|
|
355
355
|
*
|
|
356
356
|
* @param values - One relation's loaded value per record
|
|
357
357
|
* @returns The total related rows attached across the set
|
|
@@ -424,7 +424,7 @@ function groupRows(rows, column, values = rows) {
|
|
|
424
424
|
* (single).
|
|
425
425
|
*
|
|
426
426
|
* @param column - The foreign-key column on this table
|
|
427
|
-
* @param
|
|
427
|
+
* @param table - Target table name (defaults to the relation name)
|
|
428
428
|
* @returns A {@link RelationDescriptor}
|
|
429
429
|
*
|
|
430
430
|
* @example
|
|
@@ -432,11 +432,11 @@ function groupRows(rows, column, values = rows) {
|
|
|
432
432
|
* { classification: belongsTo('classificationId', 'classifications') }
|
|
433
433
|
* ```
|
|
434
434
|
*/
|
|
435
|
-
function belongsTo(column,
|
|
435
|
+
function belongsTo(column, table) {
|
|
436
436
|
return {
|
|
437
437
|
relationship: "belongs",
|
|
438
438
|
column,
|
|
439
|
-
...
|
|
439
|
+
...table !== void 0 ? { table } : {}
|
|
440
440
|
};
|
|
441
441
|
}
|
|
442
442
|
/**
|
|
@@ -444,37 +444,37 @@ function belongsTo(column, model) {
|
|
|
444
444
|
* (array).
|
|
445
445
|
*
|
|
446
446
|
* @param key - The foreign-key column on the related table
|
|
447
|
-
* @param
|
|
447
|
+
* @param table - Target table name (defaults to the relation name)
|
|
448
448
|
* @returns A {@link RelationDescriptor}
|
|
449
449
|
*/
|
|
450
|
-
function hasMany(key,
|
|
450
|
+
function hasMany(key, table) {
|
|
451
451
|
return {
|
|
452
452
|
relationship: "many",
|
|
453
453
|
key,
|
|
454
|
-
...
|
|
454
|
+
...table !== void 0 ? { table } : {}
|
|
455
455
|
};
|
|
456
456
|
}
|
|
457
457
|
/**
|
|
458
458
|
* Builds a `one` relation — like {@link hasMany}, but a single related row.
|
|
459
459
|
*
|
|
460
460
|
* @param key - The foreign-key column on the related table
|
|
461
|
-
* @param
|
|
461
|
+
* @param table - Target table name (defaults to the relation name)
|
|
462
462
|
* @returns A {@link RelationDescriptor}
|
|
463
463
|
*/
|
|
464
|
-
function hasOne(key,
|
|
464
|
+
function hasOne(key, table) {
|
|
465
465
|
return {
|
|
466
466
|
relationship: "one",
|
|
467
467
|
key,
|
|
468
|
-
...
|
|
468
|
+
...table !== void 0 ? { table } : {}
|
|
469
469
|
};
|
|
470
470
|
}
|
|
471
471
|
/**
|
|
472
472
|
* Builds a `through` relation — a junction table links the two sides (many-to-many).
|
|
473
473
|
*
|
|
474
474
|
* @param through - The junction table name
|
|
475
|
-
* @param source - The junction foreign-key column pointing at the owning
|
|
476
|
-
* @param target - The junction foreign-key column pointing at the related
|
|
477
|
-
* @param
|
|
475
|
+
* @param source - The junction foreign-key column pointing at the owning table
|
|
476
|
+
* @param target - The junction foreign-key column pointing at the related table
|
|
477
|
+
* @param table - Target table name (defaults to the relation name)
|
|
478
478
|
* @returns A {@link RelationDescriptor}
|
|
479
479
|
*
|
|
480
480
|
* @example
|
|
@@ -482,13 +482,13 @@ function hasOne(key, model) {
|
|
|
482
482
|
* { reps: hasThrough('accountReps', 'accountId', 'repId', 'reps') }
|
|
483
483
|
* ```
|
|
484
484
|
*/
|
|
485
|
-
function hasThrough(through, source, target,
|
|
485
|
+
function hasThrough(through, source, target, table) {
|
|
486
486
|
return {
|
|
487
487
|
relationship: "through",
|
|
488
488
|
through,
|
|
489
489
|
source,
|
|
490
490
|
target,
|
|
491
|
-
...
|
|
491
|
+
...table !== void 0 ? { table } : {}
|
|
492
492
|
};
|
|
493
493
|
}
|
|
494
494
|
/**
|
|
@@ -497,8 +497,8 @@ function hasThrough(through, source, target, model) {
|
|
|
497
497
|
*
|
|
498
498
|
* @param key - The foreign-key column on the related table
|
|
499
499
|
* @param tag - The discriminator column on the related table
|
|
500
|
-
* @param label - The discriminator value identifying the owning
|
|
501
|
-
* @param
|
|
500
|
+
* @param label - The discriminator value identifying the owning repository
|
|
501
|
+
* @param table - Target table name (defaults to the relation name)
|
|
502
502
|
* @returns A {@link RelationDescriptor}
|
|
503
503
|
*
|
|
504
504
|
* @example
|
|
@@ -506,21 +506,21 @@ function hasThrough(through, source, target, model) {
|
|
|
506
506
|
* { notes: hasMorph('entityId', 'entityType', 'account', 'notes') }
|
|
507
507
|
* ```
|
|
508
508
|
*/
|
|
509
|
-
function hasMorph(key, tag, label,
|
|
509
|
+
function hasMorph(key, tag, label, table) {
|
|
510
510
|
return {
|
|
511
511
|
relationship: "morph",
|
|
512
512
|
key,
|
|
513
513
|
tag,
|
|
514
514
|
label,
|
|
515
|
-
...
|
|
515
|
+
...table !== void 0 ? { table } : {}
|
|
516
516
|
};
|
|
517
517
|
}
|
|
518
|
-
// src/core/
|
|
518
|
+
// src/core/Repository.ts
|
|
519
519
|
/**
|
|
520
520
|
* Pairs a typed table with relation-aware `load` / `find` and junction management.
|
|
521
521
|
*
|
|
522
522
|
* @remarks
|
|
523
|
-
* The
|
|
523
|
+
* The repository's own table is fully typed (`table`); related tables are reached by
|
|
524
524
|
* runtime name through the bound storage at the broad `Row` type. Loading is batched:
|
|
525
525
|
* a direct relation uses one query for the whole record set, while a `through`
|
|
526
526
|
* relation uses two (junction then target); both counts remain constant regardless
|
|
@@ -531,7 +531,7 @@ function hasMorph(key, tag, label, model) {
|
|
|
531
531
|
* `Object.assign` — no `as`.
|
|
532
532
|
*
|
|
533
533
|
* @remarks
|
|
534
|
-
* - **Observable.** The owned {@link emitter} ({@link
|
|
534
|
+
* - **Observable.** The owned {@link emitter} ({@link RepositoryEventMap}) carries the
|
|
535
535
|
* eager-load + junction moments — `load` (a relation resolved: its name + the count of
|
|
536
536
|
* related rows attached across the whole record set), `link` / `unlink` (a junction row
|
|
537
537
|
* written) — for fire-and-forget observers. Every event is emitted directly, strictly
|
|
@@ -542,10 +542,10 @@ function hasMorph(key, tag, label, model) {
|
|
|
542
542
|
*
|
|
543
543
|
* @example
|
|
544
544
|
* ```ts
|
|
545
|
-
* import {
|
|
545
|
+
* import { Repository, resolveRelationMap, hasMany } from '@orkestrel/relation'
|
|
546
546
|
*
|
|
547
547
|
* const relations = { posts: hasMany('author') }
|
|
548
|
-
* const users = new
|
|
548
|
+
* const users = new Repository(
|
|
549
549
|
* 'users',
|
|
550
550
|
* storage.table('users'),
|
|
551
551
|
* resolveRelationMap(relations),
|
|
@@ -557,7 +557,7 @@ function hasMorph(key, tag, label, model) {
|
|
|
557
557
|
* const ada = await users.load('u1', { posts: true })
|
|
558
558
|
* ```
|
|
559
559
|
*/
|
|
560
|
-
var
|
|
560
|
+
var Repository = class {
|
|
561
561
|
#name;
|
|
562
562
|
#table;
|
|
563
563
|
#resolved;
|
|
@@ -566,19 +566,19 @@ var Model = class {
|
|
|
566
566
|
#storage;
|
|
567
567
|
#emitter;
|
|
568
568
|
/**
|
|
569
|
-
* Constructs a
|
|
569
|
+
* Constructs a repository over one table and its resolved relations.
|
|
570
570
|
*
|
|
571
|
-
* @param name - The
|
|
572
|
-
* @param table - The typed table this
|
|
571
|
+
* @param name - The repository's table name
|
|
572
|
+
* @param table - The typed table this repository's own reads and writes go through; take it
|
|
573
573
|
* from `storage` so both belong to one scope, as `RelationManager` does
|
|
574
574
|
* (a root table read inside a transaction refuses with `CONFLICT`)
|
|
575
|
-
* @param resolved - The
|
|
575
|
+
* @param resolved - The repository's relations, resolved by `resolveRelationMap`
|
|
576
576
|
* @param relations - The raw relation map the resolved relations came from
|
|
577
|
-
* @param lookup - Reads a related
|
|
578
|
-
* and returns `undefined` for a
|
|
577
|
+
* @param lookup - Reads a related table's {@link RelationContext} for a nested include,
|
|
578
|
+
* and returns `undefined` for a table the registry does not hold; its contexts must
|
|
579
579
|
* resolve through the same `storage`
|
|
580
580
|
* @param storage - The broad storage used to read related and junction tables by name
|
|
581
|
-
* @param on - Initial {@link
|
|
581
|
+
* @param on - Initial {@link RepositoryEventMap} listeners
|
|
582
582
|
* @param error - Receives a listener throw as `(error, event)`. Default: the throw is
|
|
583
583
|
* swallowed.
|
|
584
584
|
*/
|
|
@@ -668,12 +668,12 @@ var Model = class {
|
|
|
668
668
|
}
|
|
669
669
|
#through(relation) {
|
|
670
670
|
const resolved = this.#resolved.get(relation);
|
|
671
|
-
if (resolved === void 0) throw new RelationError("UNKNOWN_RELATION", `
|
|
672
|
-
|
|
671
|
+
if (resolved === void 0) throw new RelationError("UNKNOWN_RELATION", `Repository ${renderPath([this.#name])} has no relation ${renderPath([relation])}; declare it in the repository's relation map`, {
|
|
672
|
+
repository: this.#name,
|
|
673
673
|
relation
|
|
674
674
|
});
|
|
675
675
|
if (resolved.relationship !== "through") throw new RelationError("NOT_THROUGH", `Relation ${renderPath([this.#name, relation])} is a ${resolved.relationship} relation; link, unlink, and links need a through relation built with hasThrough`, {
|
|
676
|
-
|
|
676
|
+
repository: this.#name,
|
|
677
677
|
relation
|
|
678
678
|
});
|
|
679
679
|
return resolved;
|
|
@@ -708,9 +708,9 @@ var Model = class {
|
|
|
708
708
|
const column = resolved.column;
|
|
709
709
|
const keys = [...new Set(records.map((record) => resolveField(record, column)).filter(isKey))];
|
|
710
710
|
if (keys.length === 0) return records.map(() => void 0);
|
|
711
|
-
const related = this.#storage.table(resolved.
|
|
711
|
+
const related = this.#storage.table(resolved.table);
|
|
712
712
|
const rows = (await related.get(keys)).filter((row) => row !== void 0).map((row) => readRow(related.name, row));
|
|
713
|
-
const index = indexRows(rows, related.primary, await this.#nest(resolved.
|
|
713
|
+
const index = indexRows(rows, related.primary, await this.#nest(resolved.table, rows, sub, options));
|
|
714
714
|
return records.map((record) => {
|
|
715
715
|
const fk = resolveField(record, column);
|
|
716
716
|
return isKey(fk) ? index.get(String(fk)) : void 0;
|
|
@@ -720,13 +720,13 @@ var Model = class {
|
|
|
720
720
|
const foreign = resolved.key;
|
|
721
721
|
const keys = [...new Set(records.map((record) => resolveField(record, primary)).filter(isDefined))];
|
|
722
722
|
if (keys.length === 0) return records.map(() => []);
|
|
723
|
-
const rows = (await this.#storage.table(resolved.
|
|
723
|
+
const rows = (await this.#storage.table(resolved.table).records({ conditions: [{
|
|
724
724
|
column: foreign,
|
|
725
725
|
operator: "any",
|
|
726
726
|
values: keys,
|
|
727
727
|
connector: "and"
|
|
728
|
-
}] }, options)).map((row) => readRow(resolved.
|
|
729
|
-
const groups = groupRows(rows, foreign, await this.#nest(resolved.
|
|
728
|
+
}] }, options)).map((row) => readRow(resolved.table, row));
|
|
729
|
+
const groups = groupRows(rows, foreign, await this.#nest(resolved.table, rows, sub, options));
|
|
730
730
|
return records.map((record) => groups.get(String(resolveField(record, primary))) ?? []);
|
|
731
731
|
}
|
|
732
732
|
async #loadOne(records, resolved, sub, primary, options) {
|
|
@@ -746,9 +746,9 @@ var Model = class {
|
|
|
746
746
|
const targets = [...new Set(junctions.map((junction) => resolveField(junction, target)).filter(isKey))];
|
|
747
747
|
if (targets.length === 0) return records.map(() => []);
|
|
748
748
|
checkAbort(options?.signal);
|
|
749
|
-
const related = this.#storage.table(resolved.
|
|
749
|
+
const related = this.#storage.table(resolved.table);
|
|
750
750
|
const rows = (await related.get(targets)).filter((row) => row !== void 0).map((row) => readRow(related.name, row));
|
|
751
|
-
const index = indexRows(rows, related.primary, await this.#nest(resolved.
|
|
751
|
+
const index = indexRows(rows, related.primary, await this.#nest(resolved.table, rows, sub, options));
|
|
752
752
|
const groups = groupRows(junctions, source);
|
|
753
753
|
return records.map((record) => (groups.get(String(resolveField(record, primary))) ?? []).map((junction) => resolveField(junction, target)).filter(isKey).map((value) => index.get(String(value))).filter(isDefined));
|
|
754
754
|
}
|
|
@@ -756,7 +756,7 @@ var Model = class {
|
|
|
756
756
|
const foreign = resolved.key;
|
|
757
757
|
const keys = [...new Set(records.map((record) => resolveField(record, primary)).filter(isDefined))];
|
|
758
758
|
if (keys.length === 0) return records.map(() => []);
|
|
759
|
-
const rows = (await this.#storage.table(resolved.
|
|
759
|
+
const rows = (await this.#storage.table(resolved.table).records({ conditions: [{
|
|
760
760
|
column: foreign,
|
|
761
761
|
operator: "any",
|
|
762
762
|
values: keys,
|
|
@@ -766,13 +766,13 @@ var Model = class {
|
|
|
766
766
|
operator: "equals",
|
|
767
767
|
values: [resolved.label],
|
|
768
768
|
connector: "and"
|
|
769
|
-
}] }, options)).map((row) => readRow(resolved.
|
|
770
|
-
const groups = groupRows(rows, foreign, await this.#nest(resolved.
|
|
769
|
+
}] }, options)).map((row) => readRow(resolved.table, row));
|
|
770
|
+
const groups = groupRows(rows, foreign, await this.#nest(resolved.table, rows, sub, options));
|
|
771
771
|
return records.map((record) => groups.get(String(resolveField(record, primary))) ?? []);
|
|
772
772
|
}
|
|
773
|
-
async #nest(
|
|
773
|
+
async #nest(table, rows, sub, options) {
|
|
774
774
|
if (isBoolean(sub) || rows.length === 0) return rows;
|
|
775
|
-
const context = this.#lookup(
|
|
775
|
+
const context = this.#lookup(table);
|
|
776
776
|
if (context === void 0) return rows;
|
|
777
777
|
const props = await this.#populate(rows, sub, context.resolved, context.primary, options);
|
|
778
778
|
return rows.map((row, index) => Object.assign({}, row, props[index]));
|
|
@@ -781,15 +781,15 @@ var Model = class {
|
|
|
781
781
|
// src/core/RelationManager.ts
|
|
782
782
|
/**
|
|
783
783
|
* Resolves a {@link RelationSchema} once at construction and vends a typed
|
|
784
|
-
* {@link
|
|
784
|
+
* {@link RepositoryInterface} per declared table.
|
|
785
785
|
*
|
|
786
786
|
* @remarks
|
|
787
|
-
* Holds storage both at its precise type (to type each
|
|
787
|
+
* Holds storage both at its precise type (to type each repository's own table)
|
|
788
788
|
* and at the broad `DatabaseStorageInterface` (to fetch related tables by runtime name
|
|
789
789
|
* while loading). Construction validates every target and junction against the
|
|
790
790
|
* database's declared tables, so invalid definitions fail before an operation runs.
|
|
791
|
-
* The `
|
|
792
|
-
* each `
|
|
791
|
+
* The `repository` option's `on` and `error` are threaded into every repository vended, and
|
|
792
|
+
* each `repository(name)` call constructs a handle owning its own emitter, so retain the
|
|
793
793
|
* handle a subscription is taken on.
|
|
794
794
|
*
|
|
795
795
|
* @example
|
|
@@ -799,9 +799,9 @@ var Model = class {
|
|
|
799
799
|
* const manager = new RelationManager<typeof TABLES>({
|
|
800
800
|
* storage: database,
|
|
801
801
|
* relations: { users: { posts: hasMany('author') } },
|
|
802
|
-
*
|
|
802
|
+
* repository: { on: { load: (name, count) => metrics.record(`relation.${name}`, count) } },
|
|
803
803
|
* })
|
|
804
|
-
* const users = manager.
|
|
804
|
+
* const users = manager.repository('users')
|
|
805
805
|
* const ada = await users.load('u1', { posts: true })
|
|
806
806
|
* ```
|
|
807
807
|
*/
|
|
@@ -809,13 +809,13 @@ var RelationManager = class {
|
|
|
809
809
|
#storage;
|
|
810
810
|
#broad;
|
|
811
811
|
#relations;
|
|
812
|
-
#
|
|
812
|
+
#repository;
|
|
813
813
|
#resolved = /* @__PURE__ */ new Map();
|
|
814
814
|
/**
|
|
815
815
|
* Constructs a registry over storage and its relation definitions.
|
|
816
816
|
*
|
|
817
817
|
* @param options - The `storage`, an optional `relations` map, and the optional
|
|
818
|
-
* `
|
|
818
|
+
* `repository` emitter settings threaded into every repository vended
|
|
819
819
|
* @throws An `INVALID` {@link RelationError} when a relation targets a table or a
|
|
820
820
|
* junction the database does not declare
|
|
821
821
|
*/
|
|
@@ -823,12 +823,12 @@ var RelationManager = class {
|
|
|
823
823
|
this.#storage = options.storage;
|
|
824
824
|
this.#broad = options.storage;
|
|
825
825
|
this.#relations = options.relations ?? {};
|
|
826
|
-
this.#
|
|
826
|
+
this.#repository = options.repository;
|
|
827
827
|
for (const [name, map] of Object.entries(this.#relations)) {
|
|
828
828
|
if (map === void 0) continue;
|
|
829
829
|
const resolved = resolveRelationMap(map);
|
|
830
830
|
for (const [relation, entry] of resolved) {
|
|
831
|
-
validateRelationTable(this.#broad, name, relation, entry.
|
|
831
|
+
validateRelationTable(this.#broad, name, relation, entry.table);
|
|
832
832
|
if (entry.relationship === "through") {
|
|
833
833
|
validateRelationTable(this.#broad, name, relation, entry.through, true);
|
|
834
834
|
validateRelationJunction(name, entry, this.#broad.table(entry.through).primary);
|
|
@@ -840,7 +840,7 @@ var RelationManager = class {
|
|
|
840
840
|
get count() {
|
|
841
841
|
return this.#resolved.size;
|
|
842
842
|
}
|
|
843
|
-
|
|
843
|
+
repository(name) {
|
|
844
844
|
const resolved = this.#resolved.get(name) ?? /* @__PURE__ */ new Map();
|
|
845
845
|
const relations = this.#relations[name] ?? {};
|
|
846
846
|
return this.#vend(name, this.#storage.table(name), resolved, relations);
|
|
@@ -852,14 +852,14 @@ var RelationManager = class {
|
|
|
852
852
|
return this.#resolved.has(name);
|
|
853
853
|
}
|
|
854
854
|
#vend(name, table, resolved, relations) {
|
|
855
|
-
return new
|
|
855
|
+
return new Repository(name, table, resolved, relations, (related) => this.#context(related), this.#broad, this.#repository?.on, this.#repository?.error);
|
|
856
856
|
}
|
|
857
|
-
#context(
|
|
858
|
-
const resolved = this.#resolved.get(
|
|
857
|
+
#context(table) {
|
|
858
|
+
const resolved = this.#resolved.get(table);
|
|
859
859
|
if (resolved === void 0) return void 0;
|
|
860
860
|
return {
|
|
861
861
|
resolved,
|
|
862
|
-
primary: this.#broad.table(
|
|
862
|
+
primary: this.#broad.table(table).primary
|
|
863
863
|
};
|
|
864
864
|
}
|
|
865
865
|
};
|
|
@@ -870,7 +870,7 @@ var RelationManager = class {
|
|
|
870
870
|
* @remarks
|
|
871
871
|
* `relations` maps table names (constrained to the database's tables) to their
|
|
872
872
|
* relation maps; build entries with `belongsTo` / `hasMany` / `hasOne` /
|
|
873
|
-
* `hasThrough` / `hasMorph`. `manager.
|
|
873
|
+
* `hasThrough` / `hasMorph`. `manager.repository(name)` returns the typed repository for a
|
|
874
874
|
* table — its own table is fully typed, related rows load at the `Row` type.
|
|
875
875
|
*
|
|
876
876
|
* At a typed root, pass the table map as the type argument. Transaction storage infers it.
|
|
@@ -903,12 +903,14 @@ var RelationManager = class {
|
|
|
903
903
|
* },
|
|
904
904
|
* })
|
|
905
905
|
*
|
|
906
|
-
* const acme = await manager
|
|
906
|
+
* const acme = await manager
|
|
907
|
+
* .repository('accounts')
|
|
908
|
+
* .load('acc1', { contacts: true, classification: true })
|
|
907
909
|
* ```
|
|
908
910
|
*/
|
|
909
911
|
function createRelationManager(options) {
|
|
910
912
|
return new RelationManager(options);
|
|
911
913
|
}
|
|
912
|
-
export {
|
|
914
|
+
export { RelationError, RelationManager, Repository, belongsTo, computeLinkKey, countAttached, createRelationManager, groupRows, hasMany, hasMorph, hasOne, hasThrough, indexRows, isRelationDescriptor, isRelationError, readRow, relationDescriptorContract, relationDescriptorShape, resolveRelation, resolveRelationMap, validateRelationJunction, validateRelationTable };
|
|
913
915
|
|
|
914
916
|
//# sourceMappingURL=index.js.map
|