@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.
@@ -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 THIS table points at the related row.
15
- *
16
- * @param column - The FK column on this table
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 relation manager over a database and its relation definitions.
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 { createRelationManager, belongsTo, hasMany } from '@src/core'
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: { classification: belongsTo('classificationId', 'classifications'), contacts: hasMany('accountId') },
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 RELATED table points back here.
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 FK column on the related table
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 FK plus a discriminator on the RELATED table.
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 FK column on the related table
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 THIS model
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 FK column on the related table
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 FK column pointing at THIS model
142
- * @param target - The junction FK column pointing at the related model
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 loading.
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
- * AFTER the load resolves / the junction op completes; the emitter isolates a listener
264
- * throw and routes it to its `error` handler (the `error` option), so a buggy observer can
265
- * never corrupt the batched eager-load (no N+1 in the events either — one `load` per
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 NAME +
321
- * the COUNT of related rows attached for the whole record set (it is the batched load
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 AFTER the load resolves / the junction op completes
327
- * — so a throwing observer can never corrupt the eager-load batching or a junction write.
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 FK column on this table); a `readonly string[]`
405
- * is a `many` (the first element is the FK column on the related table); a
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 THIS table points at the related row (single).
561
- * `many` — a foreign key on the RELATED table points back here (array).
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 RELATED table (array, polymorphic).
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 THIS table.
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 RELATED
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 THIS model.
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 THIS
658
- * model, and `target` its foreign-key column pointing at the related model. These are
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` (FK column on this table); a `readonly string[]` is a
675
- * `many` (FK column on the related table); a {@link RelationDescriptor} uses its
676
- * explicit `relationship`, or infers it from the fields present. The target model
677
- * defaults to the relation name. Every column the resolved arm declares is validated
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
@@ -3,7 +3,8 @@ import { checkAbort, extractKey } from "@orkestrel/database";
3
3
  import { Emitter } from "@orkestrel/emitter";
4
4
  //#region src/core/errors.ts
5
5
  /**
6
- * Represents an error thrown by the relations layer.
6
+ * Represents an error thrown by the relations layer, carrying a machine-readable
7
+ * {@link RelationErrorCode}.
7
8
  *
8
9
  * @remarks
9
10
  * Thrown for: a relation value whose relationship cannot be inferred (`INVALID`), a
@@ -77,11 +78,11 @@ function isRelationDescriptor(value) {
77
78
  * Resolves one raw {@link Relation} value into a flat {@link ResolvedRelation}.
78
79
  *
79
80
  * @remarks
80
- * A `string` is a `belongs` (FK column on this table); a `readonly string[]` is a
81
- * `many` (FK column on the related table); a {@link RelationDescriptor} uses its
82
- * explicit `relationship`, or infers it from the fields present. The target model
83
- * defaults to the relation name. Every column the resolved arm declares is validated
84
- * here, which is what lets each arm declare its columns required.
81
+ * A `string` is a `belongs` (foreign-key column on this table); a `readonly string[]`
82
+ * is a `many` (foreign-key column on the related table); a {@link RelationDescriptor}
83
+ * uses its explicit `relationship`, or infers it from the fields present. The target
84
+ * model defaults to the relation name. Every column the resolved arm declares is
85
+ * validated here, which is what lets each arm declare its columns required.
85
86
  *
86
87
  * @param name - The relation name (its key in the {@link RelationMap})
87
88
  * @param value - The raw relation value
@@ -184,7 +185,7 @@ function resolveRelationMap(relations) {
184
185
  return resolved;
185
186
  }
186
187
  /**
187
- * Reads one column off any record.
188
+ * Reads one column off any record, whatever its declared type.
188
189
  *
189
190
  * @remarks
190
191
  * A base row's type is closed, so the column is read with `Reflect.get` rather than
@@ -279,9 +280,10 @@ function groupRows(rows, column) {
279
280
  return groups;
280
281
  }
281
282
  /**
282
- * Builds a `belongs` relation — a foreign key on THIS table points at the related row.
283
+ * Builds a `belongs` relation — a foreign key on the owning table points at the related row
284
+ * (single).
283
285
  *
284
- * @param column - The FK column on this table
286
+ * @param column - The foreign-key column on this table
285
287
  * @param model - Target table name (defaults to the relation name)
286
288
  * @returns A {@link RelationDescriptor}
287
289
  *
@@ -298,9 +300,10 @@ function belongsTo(column, model) {
298
300
  };
299
301
  }
300
302
  /**
301
- * Builds a `many` relation — a foreign key on the RELATED table points back here.
303
+ * Builds a `many` relation — a foreign key on the related table points back at the owning row
304
+ * (array).
302
305
  *
303
- * @param key - The FK column on the related table
306
+ * @param key - The foreign-key column on the related table
304
307
  * @param model - Target table name (defaults to the relation name)
305
308
  * @returns A {@link RelationDescriptor}
306
309
  */
@@ -314,7 +317,7 @@ function hasMany(key, model) {
314
317
  /**
315
318
  * Builds a `one` relation — like {@link hasMany}, but a single related row.
316
319
  *
317
- * @param key - The FK column on the related table
320
+ * @param key - The foreign-key column on the related table
318
321
  * @param model - Target table name (defaults to the relation name)
319
322
  * @returns A {@link RelationDescriptor}
320
323
  */
@@ -329,8 +332,8 @@ function hasOne(key, model) {
329
332
  * Builds a `through` relation — a junction table links the two sides (many-to-many).
330
333
  *
331
334
  * @param through - The junction table name
332
- * @param source - The junction FK column pointing at THIS model
333
- * @param target - The junction FK column pointing at the related model
335
+ * @param source - The junction foreign-key column pointing at the owning model
336
+ * @param target - The junction foreign-key column pointing at the related model
334
337
  * @param model - Target table name (defaults to the relation name)
335
338
  * @returns A {@link RelationDescriptor}
336
339
  *
@@ -349,11 +352,12 @@ function hasThrough(through, source, target, model) {
349
352
  };
350
353
  }
351
354
  /**
352
- * Builds a `morph` relation — a polymorphic FK plus a discriminator on the RELATED table.
355
+ * Builds a `morph` relation — a polymorphic foreign key plus a discriminator column on the
356
+ * related table (array).
353
357
  *
354
- * @param key - The FK column on the related table
358
+ * @param key - The foreign-key column on the related table
355
359
  * @param tag - The discriminator column on the related table
356
- * @param label - The discriminator value identifying THIS model
360
+ * @param label - The discriminator value identifying the owning model
357
361
  * @param model - Target table name (defaults to the relation name)
358
362
  * @returns A {@link RelationDescriptor}
359
363
  *
@@ -374,7 +378,7 @@ function hasMorph(key, tag, label, model) {
374
378
  //#endregion
375
379
  //#region src/core/Model.ts
376
380
  /**
377
- * Pairs a typed table with relation-aware loading.
381
+ * Pairs a typed table with relation-aware `load` / `find` and junction management.
378
382
  *
379
383
  * @remarks
380
384
  * The model's own table is fully typed (`table`); related tables are reached by
@@ -390,10 +394,10 @@ function hasMorph(key, tag, label, model) {
390
394
  * eager-load + junction moments — `load` (a relation resolved: its name + the count of
391
395
  * related rows attached across the whole record set), `link` / `unlink` (a junction row
392
396
  * written) — for fire-and-forget observers. Every event is emitted directly, strictly
393
- * AFTER the load resolves / the junction op completes; the emitter isolates a listener
394
- * throw and routes it to its `error` handler (the `error` option), so a buggy observer can
395
- * never corrupt the batched eager-load (no N+1 in the events either — one `load` per
396
- * relation, not per record).
397
+ * after the load resolves and after the junction operation completes; the emitter
398
+ * isolates a listener throw and routes it to its `error` handler (the `error` option),
399
+ * so a buggy observer can never corrupt the batched eager-load (no N+1 in the events
400
+ * either — one `load` per relation, not per record).
397
401
  *
398
402
  * @example
399
403
  * ```ts
@@ -778,7 +782,7 @@ var RelationManager = class {
778
782
  //#endregion
779
783
  //#region src/core/factories.ts
780
784
  /**
781
- * Creates a relation manager over a database and its relation definitions.
785
+ * Creates a {@link RelationManagerInterface} over a database and its relation map.
782
786
  *
783
787
  * @remarks
784
788
  * `relations` maps table names (constrained to the database's tables) to their
@@ -789,17 +793,31 @@ var RelationManager = class {
789
793
  * @param options - The `database` and an optional `relations` map
790
794
  * @returns A typed {@link RelationManagerInterface}
791
795
  *
792
- * @example
796
+ * @example Defining relations
793
797
  * ```ts
794
- * import { createRelationManager, belongsTo, hasMany } from '@src/core'
798
+ * import {
799
+ * createRelationManager,
800
+ * belongsTo,
801
+ * hasMany,
802
+ * hasOne,
803
+ * hasThrough,
804
+ * hasMorph,
805
+ * } from '@orkestrel/relation'
795
806
  *
796
807
  * const manager = createRelationManager({
797
808
  * database: db,
798
809
  * relations: {
799
- * accounts: { classification: belongsTo('classificationId', 'classifications'), contacts: hasMany('accountId') },
810
+ * accounts: {
811
+ * classification: belongsTo('classificationId', 'classifications'), // foreign key on accounts
812
+ * contacts: hasMany('accountId'), // foreign key on contacts → accounts
813
+ * profile: hasOne('accountId', 'profiles'), // single, foreign key on profiles
814
+ * representatives: hasThrough('accountReps', 'accountId', 'repId', 'representatives'), // through a junction
815
+ * notes: hasMorph('entityId', 'entityType', 'account', 'notes'), // polymorphic
816
+ * },
800
817
  * contacts: { account: belongsTo('accountId', 'accounts') },
801
818
  * },
802
819
  * })
820
+ *
803
821
  * const acme = await manager.model('accounts').load('acc1', { contacts: true, classification: true })
804
822
  * ```
805
823
  */