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