@orkestrel/relation 0.0.11 → 0.0.12
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 +12 -12
- package/dist/src/core/index.cjs +44 -26
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +154 -56
- package/dist/src/core/index.d.ts +154 -56
- package/dist/src/core/index.js +44 -26
- package/dist/src/core/index.js.map +1 -1
- package/package.json +15 -16
|
@@ -1,19 +1,20 @@
|
|
|
1
|
-
import { DatabaseInterface } from '@orkestrel/database';
|
|
2
|
-
import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
3
|
-
import { EmitterHooks } from '@orkestrel/emitter';
|
|
4
|
-
import { EmitterInterface } from '@orkestrel/emitter';
|
|
5
|
-
import { Key } from '@orkestrel/database';
|
|
6
|
-
import { OperationOptions } from '@orkestrel/database';
|
|
7
|
-
import { OrderDirection } from '@orkestrel/database';
|
|
8
|
-
import { Row } from '@orkestrel/database';
|
|
9
|
-
import { RowOf } from '@orkestrel/database';
|
|
10
|
-
import { TableInterface } from '@orkestrel/database';
|
|
11
|
-
import { TableMap } from '@orkestrel/database';
|
|
12
|
-
|
|
13
|
-
/**
|
|
14
|
-
* Builds a `belongs` relation — a foreign key on
|
|
15
|
-
*
|
|
16
|
-
*
|
|
1
|
+
import type { DatabaseInterface } from '@orkestrel/database';
|
|
2
|
+
import type { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
3
|
+
import type { EmitterHooks } from '@orkestrel/emitter';
|
|
4
|
+
import type { EmitterInterface } from '@orkestrel/emitter';
|
|
5
|
+
import type { Key } from '@orkestrel/database';
|
|
6
|
+
import type { OperationOptions } from '@orkestrel/database';
|
|
7
|
+
import type { OrderDirection } from '@orkestrel/database';
|
|
8
|
+
import type { Row } from '@orkestrel/database';
|
|
9
|
+
import type { RowOf } from '@orkestrel/database';
|
|
10
|
+
import type { TableInterface } from '@orkestrel/database';
|
|
11
|
+
import type { TableMap } from '@orkestrel/database';
|
|
12
|
+
|
|
13
|
+
/**
|
|
14
|
+
* Builds a `belongs` relation — a foreign key on the owning table points at the related row
|
|
15
|
+
* (single).
|
|
16
|
+
*
|
|
17
|
+
* @param column - The foreign-key column on this table
|
|
17
18
|
* @param model - Target table name (defaults to the relation name)
|
|
18
19
|
* @returns A {@link RelationDescriptor}
|
|
19
20
|
*
|
|
@@ -43,7 +44,7 @@ export declare function belongsTo(column: string, model?: string): RelationDescr
|
|
|
43
44
|
export declare function countAttached(values: ReadonlyArray<Row | readonly Row[] | undefined>): number;
|
|
44
45
|
|
|
45
46
|
/**
|
|
46
|
-
* Creates a
|
|
47
|
+
* Creates a {@link RelationManagerInterface} over a database and its relation map.
|
|
47
48
|
*
|
|
48
49
|
* @remarks
|
|
49
50
|
* `relations` maps table names (constrained to the database's tables) to their
|
|
@@ -54,17 +55,31 @@ export declare function countAttached(values: ReadonlyArray<Row | readonly Row[]
|
|
|
54
55
|
* @param options - The `database` and an optional `relations` map
|
|
55
56
|
* @returns A typed {@link RelationManagerInterface}
|
|
56
57
|
*
|
|
57
|
-
* @example
|
|
58
|
+
* @example Defining relations
|
|
58
59
|
* ```ts
|
|
59
|
-
* import {
|
|
60
|
+
* import {
|
|
61
|
+
* createRelationManager,
|
|
62
|
+
* belongsTo,
|
|
63
|
+
* hasMany,
|
|
64
|
+
* hasOne,
|
|
65
|
+
* hasThrough,
|
|
66
|
+
* hasMorph,
|
|
67
|
+
* } from '@orkestrel/relation'
|
|
60
68
|
*
|
|
61
69
|
* const manager = createRelationManager({
|
|
62
70
|
* database: db,
|
|
63
71
|
* relations: {
|
|
64
|
-
* accounts: {
|
|
72
|
+
* accounts: {
|
|
73
|
+
* classification: belongsTo('classificationId', 'classifications'), // foreign key on accounts
|
|
74
|
+
* contacts: hasMany('accountId'), // foreign key on contacts → accounts
|
|
75
|
+
* profile: hasOne('accountId', 'profiles'), // single, foreign key on profiles
|
|
76
|
+
* representatives: hasThrough('accountReps', 'accountId', 'repId', 'representatives'), // through a junction
|
|
77
|
+
* notes: hasMorph('entityId', 'entityType', 'account', 'notes'), // polymorphic
|
|
78
|
+
* },
|
|
65
79
|
* contacts: { account: belongsTo('accountId', 'accounts') },
|
|
66
80
|
* },
|
|
67
81
|
* })
|
|
82
|
+
*
|
|
68
83
|
* const acme = await manager.model('accounts').load('acc1', { contacts: true, classification: true })
|
|
69
84
|
* ```
|
|
70
85
|
*/
|
|
@@ -101,20 +116,22 @@ export declare interface FindOptions extends OperationOptions {
|
|
|
101
116
|
export declare function groupRows(rows: readonly Row[], column: string): ReadonlyMap<string, readonly Row[]>;
|
|
102
117
|
|
|
103
118
|
/**
|
|
104
|
-
* Builds a `many` relation — a foreign key on the
|
|
119
|
+
* Builds a `many` relation — a foreign key on the related table points back at the owning row
|
|
120
|
+
* (array).
|
|
105
121
|
*
|
|
106
|
-
* @param key - The
|
|
122
|
+
* @param key - The foreign-key column on the related table
|
|
107
123
|
* @param model - Target table name (defaults to the relation name)
|
|
108
124
|
* @returns A {@link RelationDescriptor}
|
|
109
125
|
*/
|
|
110
126
|
export declare function hasMany(key: string, model?: string): RelationDescriptor;
|
|
111
127
|
|
|
112
128
|
/**
|
|
113
|
-
* Builds a `morph` relation — a polymorphic
|
|
129
|
+
* Builds a `morph` relation — a polymorphic foreign key plus a discriminator column on the
|
|
130
|
+
* related table (array).
|
|
114
131
|
*
|
|
115
|
-
* @param key - The
|
|
132
|
+
* @param key - The foreign-key column on the related table
|
|
116
133
|
* @param tag - The discriminator column on the related table
|
|
117
|
-
* @param label - The discriminator value identifying
|
|
134
|
+
* @param label - The discriminator value identifying the owning model
|
|
118
135
|
* @param model - Target table name (defaults to the relation name)
|
|
119
136
|
* @returns A {@link RelationDescriptor}
|
|
120
137
|
*
|
|
@@ -128,7 +145,7 @@ export declare function hasMorph(key: string, tag: string, label: string, model?
|
|
|
128
145
|
/**
|
|
129
146
|
* Builds a `one` relation — like {@link hasMany}, but a single related row.
|
|
130
147
|
*
|
|
131
|
-
* @param key - The
|
|
148
|
+
* @param key - The foreign-key column on the related table
|
|
132
149
|
* @param model - Target table name (defaults to the relation name)
|
|
133
150
|
* @returns A {@link RelationDescriptor}
|
|
134
151
|
*/
|
|
@@ -138,8 +155,8 @@ export declare function hasOne(key: string, model?: string): RelationDescriptor;
|
|
|
138
155
|
* Builds a `through` relation — a junction table links the two sides (many-to-many).
|
|
139
156
|
*
|
|
140
157
|
* @param through - The junction table name
|
|
141
|
-
* @param source - The junction
|
|
142
|
-
* @param target - The junction
|
|
158
|
+
* @param source - The junction foreign-key column pointing at the owning model
|
|
159
|
+
* @param target - The junction foreign-key column pointing at the related model
|
|
143
160
|
* @param model - Target table name (defaults to the relation name)
|
|
144
161
|
* @returns A {@link RelationDescriptor}
|
|
145
162
|
*
|
|
@@ -244,7 +261,7 @@ export declare type Loaded<T> = T & Readonly<LoadedMap>;
|
|
|
244
261
|
export declare type LoadedMap = Record<string, Row | readonly Row[] | undefined>;
|
|
245
262
|
|
|
246
263
|
/**
|
|
247
|
-
* Pairs a typed table with relation-aware
|
|
264
|
+
* Pairs a typed table with relation-aware `load` / `find` and junction management.
|
|
248
265
|
*
|
|
249
266
|
* @remarks
|
|
250
267
|
* The model's own table is fully typed (`table`); related tables are reached by
|
|
@@ -260,10 +277,10 @@ export declare type LoadedMap = Record<string, Row | readonly Row[] | undefined>
|
|
|
260
277
|
* eager-load + junction moments — `load` (a relation resolved: its name + the count of
|
|
261
278
|
* related rows attached across the whole record set), `link` / `unlink` (a junction row
|
|
262
279
|
* written) — for fire-and-forget observers. Every event is emitted directly, strictly
|
|
263
|
-
*
|
|
264
|
-
* throw and routes it to its `error` handler (the `error` option),
|
|
265
|
-
* never corrupt the batched eager-load (no N+1 in the events
|
|
266
|
-
* relation, not per record).
|
|
280
|
+
* after the load resolves and after the junction operation completes; the emitter
|
|
281
|
+
* isolates a listener throw and routes it to its `error` handler (the `error` option),
|
|
282
|
+
* so a buggy observer can never corrupt the batched eager-load (no N+1 in the events
|
|
283
|
+
* either — one `load` per relation, not per record).
|
|
267
284
|
*
|
|
268
285
|
* @example
|
|
269
286
|
* ```ts
|
|
@@ -317,14 +334,15 @@ export declare class Model<T = Row> implements ModelInterface<T> {
|
|
|
317
334
|
* a sync layer) subscribes to.
|
|
318
335
|
*
|
|
319
336
|
* @remarks
|
|
320
|
-
* `load` fires once per relation that an eager-load resolves, carrying the relation
|
|
321
|
-
* the
|
|
337
|
+
* `load` fires once per relation that an eager-load resolves, carrying the relation name +
|
|
338
|
+
* the count of related rows attached for the whole record set (it is the batched load
|
|
322
339
|
* moment, not one event per record — there is no N+1 in the events either). `link` /
|
|
323
340
|
* `unlink` fire after a junction row is inserted / removed, carrying the owning key + the
|
|
324
341
|
* relation name. Listener isolation is the emitter's: every event is emitted
|
|
325
342
|
* directly and a listener throw is routed to the emitter's `error` handler (the `error`
|
|
326
|
-
* option), never onto this map, and sits
|
|
327
|
-
* — so a throwing observer can never corrupt the eager-load batching or a
|
|
343
|
+
* option), never onto this map, and sits strictly after the load resolves / the junction op
|
|
344
|
+
* completes — so a throwing observer can never corrupt the eager-load batching or a
|
|
345
|
+
* junction write.
|
|
328
346
|
* `RelationManager` is event-free by design (a stateless
|
|
329
347
|
* registry that merely vends models — it has no observable lifecycle of its own); the
|
|
330
348
|
* per-entity {@link ModelInterface} is where loading and linking happen, so the emitter
|
|
@@ -371,16 +389,72 @@ export declare interface ModelInterface<T = Row> {
|
|
|
371
389
|
readonly name: string;
|
|
372
390
|
readonly table: TableInterface<T>;
|
|
373
391
|
readonly relations: RelationMap;
|
|
392
|
+
/**
|
|
393
|
+
* Loads one record by key with the chosen relations populated, or a positional array of
|
|
394
|
+
* records for an array of keys.
|
|
395
|
+
*
|
|
396
|
+
* @remarks
|
|
397
|
+
* A key the table does not hold reads as `undefined`, and an array of keys keeps one slot
|
|
398
|
+
* per key in the order given. Either form batches the relation queries over the whole
|
|
399
|
+
* record set.
|
|
400
|
+
*
|
|
401
|
+
* @param key - The primary key to read
|
|
402
|
+
* @param include - The relations to populate, and recursively their own
|
|
403
|
+
* @param options - The database package's abort option
|
|
404
|
+
* @returns The loaded record, or `undefined` when the key holds none
|
|
405
|
+
*/
|
|
374
406
|
load(key: Key, include: Include, options?: OperationOptions): Promise<Loaded<T> | undefined>;
|
|
375
407
|
load(keys: readonly Key[], include: Include, options?: OperationOptions): Promise<ReadonlyArray<Loaded<T> | undefined>>;
|
|
408
|
+
/**
|
|
409
|
+
* Finds many records, paged and sorted through {@link FindOptions}, with the chosen
|
|
410
|
+
* relations populated.
|
|
411
|
+
*
|
|
412
|
+
* @param include - The relations to populate, and recursively their own
|
|
413
|
+
* @param options - Pagination, ordering, and the abort signal
|
|
414
|
+
* @returns The page's records, each carrying its populated relations
|
|
415
|
+
*/
|
|
376
416
|
find(include: Include, options?: FindOptions): Promise<ReadonlyArray<Loaded<T>>>;
|
|
417
|
+
/**
|
|
418
|
+
* Inserts a missing junction row for a `through` relation.
|
|
419
|
+
*
|
|
420
|
+
* @remarks
|
|
421
|
+
* Idempotent for sequential calls: a pair the junction already holds writes nothing and
|
|
422
|
+
* emits nothing. Concurrent calls for one pair may each insert.
|
|
423
|
+
*
|
|
424
|
+
* @param key - The owning record's primary key
|
|
425
|
+
* @param relation - The `through` relation's name
|
|
426
|
+
* @param target - The related record's primary key
|
|
427
|
+
* @param options - The database package's abort option
|
|
428
|
+
* @throws A `NOT_THROUGH` {@link RelationError} for any other relationship, and an
|
|
429
|
+
* `UNKNOWN_RELATION` one for a relation the model never declared
|
|
430
|
+
*/
|
|
377
431
|
link(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
432
|
+
/**
|
|
433
|
+
* Removes every matching junction row for a `through` relation inside one transaction.
|
|
434
|
+
*
|
|
435
|
+
* @param key - The owning record's primary key
|
|
436
|
+
* @param relation - The `through` relation's name
|
|
437
|
+
* @param target - The related record's primary key
|
|
438
|
+
* @param options - The database package's abort option
|
|
439
|
+
* @throws A `NOT_THROUGH` {@link RelationError} for any other relationship, and an
|
|
440
|
+
* `UNKNOWN_RELATION` one for a relation the model never declared
|
|
441
|
+
*/
|
|
378
442
|
unlink(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
443
|
+
/**
|
|
444
|
+
* Lists the related keys reachable through a `through` relation.
|
|
445
|
+
*
|
|
446
|
+
* @param key - The owning record's primary key
|
|
447
|
+
* @param relation - The `through` relation's name
|
|
448
|
+
* @param options - The database package's abort option
|
|
449
|
+
* @returns The related keys the junction holds for that record
|
|
450
|
+
* @throws A `NOT_THROUGH` {@link RelationError} for any other relationship, and an
|
|
451
|
+
* `UNKNOWN_RELATION` one for a relation the model never declared
|
|
452
|
+
*/
|
|
379
453
|
links(key: Key, relation: string, options?: OperationOptions): Promise<readonly Key[]>;
|
|
380
454
|
}
|
|
381
455
|
|
|
382
456
|
/**
|
|
383
|
-
* Reads one column off any record.
|
|
457
|
+
* Reads one column off any record, whatever its declared type.
|
|
384
458
|
*
|
|
385
459
|
* @remarks
|
|
386
460
|
* A base row's type is closed, so the column is read with `Reflect.get` rather than
|
|
@@ -401,9 +475,9 @@ export declare function readColumn(record: unknown, column: string): unknown;
|
|
|
401
475
|
* Represents a single relation definition.
|
|
402
476
|
*
|
|
403
477
|
* @remarks
|
|
404
|
-
* A `string` is a `belongs` (the
|
|
405
|
-
* is a `many` (the first element is the
|
|
406
|
-
* {@link RelationDescriptor} is the object form for everything else.
|
|
478
|
+
* A `string` is a `belongs` (the foreign-key column on this table); a `readonly
|
|
479
|
+
* string[]` is a `many` (the first element is the foreign-key column on the related
|
|
480
|
+
* table); a {@link RelationDescriptor} is the object form for everything else.
|
|
407
481
|
*/
|
|
408
482
|
export declare type Relation = string | readonly string[] | RelationDescriptor;
|
|
409
483
|
|
|
@@ -446,7 +520,8 @@ export declare interface RelationDescriptor {
|
|
|
446
520
|
}
|
|
447
521
|
|
|
448
522
|
/**
|
|
449
|
-
* Represents an error thrown by the relations layer
|
|
523
|
+
* Represents an error thrown by the relations layer, carrying a machine-readable
|
|
524
|
+
* {@link RelationErrorCode}.
|
|
450
525
|
*
|
|
451
526
|
* @remarks
|
|
452
527
|
* Thrown for: a relation value whose relationship cannot be inferred (`INVALID`), a
|
|
@@ -518,8 +593,30 @@ export declare class RelationManager<T extends TableMap = TableMap> implements R
|
|
|
518
593
|
*/
|
|
519
594
|
export declare interface RelationManagerInterface<T extends TableMap = TableMap> {
|
|
520
595
|
readonly count: number;
|
|
596
|
+
/**
|
|
597
|
+
* Returns the typed model for a declared table.
|
|
598
|
+
*
|
|
599
|
+
* @remarks
|
|
600
|
+
* `name` is checked against the database's declared tables, so a typo is a compile error.
|
|
601
|
+
* A declared table with no relation entry yields a relation-less model, still fully usable
|
|
602
|
+
* for typed reads and writes through `table`.
|
|
603
|
+
*
|
|
604
|
+
* @param name - A declared table's name
|
|
605
|
+
* @returns A model typed by that table's row
|
|
606
|
+
*/
|
|
521
607
|
model<K extends keyof T & string>(name: K): ModelInterface<RowOf<T[K]>>;
|
|
608
|
+
/**
|
|
609
|
+
* Lists the names of every model carrying resolved relations.
|
|
610
|
+
*
|
|
611
|
+
* @returns The table names the registry resolved a relation map for
|
|
612
|
+
*/
|
|
522
613
|
names(): readonly string[];
|
|
614
|
+
/**
|
|
615
|
+
* Reports whether a model carries resolved relations.
|
|
616
|
+
*
|
|
617
|
+
* @param name - The table name to test
|
|
618
|
+
* @returns True when the registry resolved a relation map for that table; false otherwise
|
|
619
|
+
*/
|
|
523
620
|
has(name: string): boolean;
|
|
524
621
|
}
|
|
525
622
|
|
|
@@ -557,11 +654,11 @@ export declare type RelationMap = Readonly<Record<string, Relation>>;
|
|
|
557
654
|
* Enumerates the relationships a relation can declare.
|
|
558
655
|
*
|
|
559
656
|
* @remarks
|
|
560
|
-
* `belongs` — a foreign key on
|
|
561
|
-
* `many` — a foreign key on the
|
|
657
|
+
* `belongs` — a foreign key on the owning table points at the related row (single).
|
|
658
|
+
* `many` — a foreign key on the related table points back here (array).
|
|
562
659
|
* `one` — like `many`, but a single related row.
|
|
563
660
|
* `through` — a junction table links the two sides (array, many-to-many).
|
|
564
|
-
* `morph` — a foreign key plus a discriminator column on the
|
|
661
|
+
* `morph` — a foreign key plus a discriminator column on the related table (array, polymorphic).
|
|
565
662
|
*/
|
|
566
663
|
export declare type Relationship = 'belongs' | 'many' | 'one' | 'through' | 'morph';
|
|
567
664
|
|
|
@@ -577,7 +674,8 @@ export declare type RelationsShape<T extends TableMap = TableMap> = {
|
|
|
577
674
|
};
|
|
578
675
|
|
|
579
676
|
/**
|
|
580
|
-
* Represents a `belongs` relation resolved at define-time — the foreign key sits on
|
|
677
|
+
* Represents a `belongs` relation resolved at define-time — the foreign key sits on the owning
|
|
678
|
+
* table.
|
|
581
679
|
*
|
|
582
680
|
* @remarks
|
|
583
681
|
* `column` is that foreign key; the related row is the one whose primary key it holds.
|
|
@@ -591,7 +689,7 @@ export declare interface ResolvedBelongs {
|
|
|
591
689
|
}
|
|
592
690
|
|
|
593
691
|
/**
|
|
594
|
-
* Represents a `many` relation resolved at define-time — the foreign key sits on the
|
|
692
|
+
* Represents a `many` relation resolved at define-time — the foreign key sits on the related
|
|
595
693
|
* table.
|
|
596
694
|
*
|
|
597
695
|
* @remarks
|
|
@@ -611,7 +709,7 @@ export declare interface ResolvedMany {
|
|
|
611
709
|
*
|
|
612
710
|
* @remarks
|
|
613
711
|
* `key` is the foreign key on the related table, `tag` the discriminator column beside
|
|
614
|
-
* it, and `label` the discriminator value identifying
|
|
712
|
+
* it, and `label` the discriminator value identifying the owning model.
|
|
615
713
|
*/
|
|
616
714
|
export declare interface ResolvedMorph {
|
|
617
715
|
readonly relationship: 'morph';
|
|
@@ -654,9 +752,9 @@ export declare type ResolvedRelation = ResolvedBelongs | ResolvedMany | Resolved
|
|
|
654
752
|
* sides.
|
|
655
753
|
*
|
|
656
754
|
* @remarks
|
|
657
|
-
* `through` is the junction table, `source` its foreign-key column pointing at
|
|
658
|
-
* model, and `target` its foreign-key column pointing at the related model.
|
|
659
|
-
* the columns `link` / `unlink` / `links` write and read.
|
|
755
|
+
* `through` is the junction table, `source` its foreign-key column pointing at the
|
|
756
|
+
* owning model, and `target` its foreign-key column pointing at the related model.
|
|
757
|
+
* These are the columns `link` / `unlink` / `links` write and read.
|
|
660
758
|
*/
|
|
661
759
|
export declare interface ResolvedThrough {
|
|
662
760
|
readonly relationship: 'through';
|
|
@@ -671,11 +769,11 @@ export declare interface ResolvedThrough {
|
|
|
671
769
|
* Resolves one raw {@link Relation} value into a flat {@link ResolvedRelation}.
|
|
672
770
|
*
|
|
673
771
|
* @remarks
|
|
674
|
-
* A `string` is a `belongs` (
|
|
675
|
-
* `many` (
|
|
676
|
-
* explicit `relationship`, or infers it from the fields present. The target
|
|
677
|
-
* defaults to the relation name. Every column the resolved arm declares is
|
|
678
|
-
* here, which is what lets each arm declare its columns required.
|
|
772
|
+
* A `string` is a `belongs` (foreign-key column on this table); a `readonly string[]`
|
|
773
|
+
* is a `many` (foreign-key column on the related table); a {@link RelationDescriptor}
|
|
774
|
+
* uses its explicit `relationship`, or infers it from the fields present. The target
|
|
775
|
+
* model defaults to the relation name. Every column the resolved arm declares is
|
|
776
|
+
* validated here, which is what lets each arm declare its columns required.
|
|
679
777
|
*
|
|
680
778
|
* @param name - The relation name (its key in the {@link RelationMap})
|
|
681
779
|
* @param value - The raw relation value
|