@orkestrel/relation 0.0.8 → 0.0.10
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 +1 -1
- package/dist/src/core/index.cjs +107 -64
- package/dist/src/core/index.cjs.map +1 -1
- package/dist/src/core/index.d.cts +24 -18
- package/dist/src/core/index.d.ts +24 -18
- package/dist/src/core/index.js +108 -65
- package/dist/src/core/index.js.map +1 -1
- package/package.json +17 -14
|
@@ -3,6 +3,7 @@ import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
|
3
3
|
import { EmitterHooks } from '@orkestrel/emitter';
|
|
4
4
|
import { EmitterInterface } from '@orkestrel/emitter';
|
|
5
5
|
import { Key } from '@orkestrel/database';
|
|
6
|
+
import { OperationOptions } from '@orkestrel/database';
|
|
6
7
|
import { OrderDirection } from '@orkestrel/database';
|
|
7
8
|
import { Row } from '@orkestrel/database';
|
|
8
9
|
import { RowOf } from '@orkestrel/database';
|
|
@@ -51,8 +52,8 @@ export declare function belongsTo(column: string, model?: string): RelationDescr
|
|
|
51
52
|
*/
|
|
52
53
|
export declare function createRelationManager<T extends TableMap>(options: RelationManagerOptions<T>): RelationManagerInterface<T>;
|
|
53
54
|
|
|
54
|
-
/** Pagination and
|
|
55
|
-
export declare interface FindOptions {
|
|
55
|
+
/** Pagination, ordering, and cancellation for `find`. */
|
|
56
|
+
export declare interface FindOptions extends OperationOptions {
|
|
56
57
|
readonly limit?: number;
|
|
57
58
|
readonly offset?: number;
|
|
58
59
|
readonly sort?: string;
|
|
@@ -182,12 +183,12 @@ export declare class Model<T = Row> implements ModelInterface<T> {
|
|
|
182
183
|
get name(): string;
|
|
183
184
|
get table(): TableInterface<T>;
|
|
184
185
|
get relations(): RelationMap;
|
|
185
|
-
load(key: Key, include: Include): Promise<Loaded<T> | undefined>;
|
|
186
|
-
load(keys: readonly Key[], include: Include): Promise<
|
|
187
|
-
find(include: Include, options?: FindOptions): Promise<
|
|
188
|
-
link(key: Key, relation: string, target: Key): Promise<void>;
|
|
189
|
-
unlink(key: Key, relation: string, target: Key): Promise<void>;
|
|
190
|
-
links(key: Key, relation: string): Promise<readonly Key[]>;
|
|
186
|
+
load(key: Key, include: Include, options?: OperationOptions): Promise<Loaded<T> | undefined>;
|
|
187
|
+
load(keys: readonly Key[], include: Include, options?: OperationOptions): Promise<ReadonlyArray<Loaded<T> | undefined>>;
|
|
188
|
+
find(include: Include, options?: FindOptions): Promise<ReadonlyArray<Loaded<T>>>;
|
|
189
|
+
link(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
190
|
+
unlink(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
191
|
+
links(key: Key, relation: string, options?: OperationOptions): Promise<readonly Key[]>;
|
|
191
192
|
}
|
|
192
193
|
|
|
193
194
|
/**
|
|
@@ -236,18 +237,21 @@ export declare type ModelEventMap<TKey extends Key = Key> = {
|
|
|
236
237
|
* Exposes a typed {@link emitter} (AGENTS §13) carrying the eager-load + junction
|
|
237
238
|
* moments ({@link ModelEventMap}) for fire-and-forget observers — emitting is
|
|
238
239
|
* observation-only (a swallowed listener throw can never corrupt the batched load).
|
|
240
|
+
* `link` is idempotent for sequential calls: an existing pair writes nothing and
|
|
241
|
+
* emits nothing. `unlink` removes matching junction rows inside one database
|
|
242
|
+
* transaction, so a fault rolls the removal back.
|
|
239
243
|
*/
|
|
240
244
|
export declare interface ModelInterface<T = Row> {
|
|
241
245
|
readonly emitter: EmitterInterface<ModelEventMap>;
|
|
242
246
|
readonly name: string;
|
|
243
247
|
readonly table: TableInterface<T>;
|
|
244
248
|
readonly relations: RelationMap;
|
|
245
|
-
load(key: Key, include: Include): Promise<Loaded<T> | undefined>;
|
|
246
|
-
load(keys: readonly Key[], include: Include): Promise<
|
|
247
|
-
find(include: Include, options?: FindOptions): Promise<
|
|
248
|
-
link(key: Key, relation: string, target: Key): Promise<void>;
|
|
249
|
-
unlink(key: Key, relation: string, target: Key): Promise<void>;
|
|
250
|
-
links(key: Key, relation: string): Promise<readonly Key[]>;
|
|
249
|
+
load(key: Key, include: Include, options?: OperationOptions): Promise<Loaded<T> | undefined>;
|
|
250
|
+
load(keys: readonly Key[], include: Include, options?: OperationOptions): Promise<ReadonlyArray<Loaded<T> | undefined>>;
|
|
251
|
+
find(include: Include, options?: FindOptions): Promise<ReadonlyArray<Loaded<T>>>;
|
|
252
|
+
link(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
253
|
+
unlink(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
254
|
+
links(key: Key, relation: string, options?: OperationOptions): Promise<readonly Key[]>;
|
|
251
255
|
}
|
|
252
256
|
|
|
253
257
|
/**
|
|
@@ -303,8 +307,10 @@ export declare interface RelationDescriptor {
|
|
|
303
307
|
*
|
|
304
308
|
* @remarks
|
|
305
309
|
* Thrown for: a relation value whose relationship cannot be inferred (`INVALID`), a
|
|
306
|
-
*
|
|
307
|
-
*
|
|
310
|
+
* relation targeting a table or junction the database does not declare, raised when
|
|
311
|
+
* the manager is created (`INVALID`), a reference to a relation a model does not
|
|
312
|
+
* define (`UNKNOWN_RELATION`), and `link` / `unlink` / `links` on a relation that is
|
|
313
|
+
* not a `through` (`NOT_THROUGH`).
|
|
308
314
|
*/
|
|
309
315
|
export declare class RelationError extends Error {
|
|
310
316
|
readonly code: RelationErrorCode;
|
|
@@ -322,8 +328,8 @@ export declare type RelationErrorCode = 'INVALID' | 'UNKNOWN_RELATION' | 'NOT_TH
|
|
|
322
328
|
* @remarks
|
|
323
329
|
* Holds the database both at its precise type (to type each model's own table)
|
|
324
330
|
* and at the broad `DatabaseInterface` (to fetch related tables by runtime name
|
|
325
|
-
* while loading).
|
|
326
|
-
*
|
|
331
|
+
* while loading). Construction validates every target and junction against the
|
|
332
|
+
* database's declared tables, so invalid definitions fail before an operation runs.
|
|
327
333
|
*/
|
|
328
334
|
export declare class RelationManager<T extends TableMap = TableMap> implements RelationManagerInterface<T> {
|
|
329
335
|
#private;
|
package/dist/src/core/index.d.ts
CHANGED
|
@@ -3,6 +3,7 @@ import { EmitterErrorHandler } from '@orkestrel/emitter';
|
|
|
3
3
|
import { EmitterHooks } from '@orkestrel/emitter';
|
|
4
4
|
import { EmitterInterface } from '@orkestrel/emitter';
|
|
5
5
|
import { Key } from '@orkestrel/database';
|
|
6
|
+
import { OperationOptions } from '@orkestrel/database';
|
|
6
7
|
import { OrderDirection } from '@orkestrel/database';
|
|
7
8
|
import { Row } from '@orkestrel/database';
|
|
8
9
|
import { RowOf } from '@orkestrel/database';
|
|
@@ -51,8 +52,8 @@ export declare function belongsTo(column: string, model?: string): RelationDescr
|
|
|
51
52
|
*/
|
|
52
53
|
export declare function createRelationManager<T extends TableMap>(options: RelationManagerOptions<T>): RelationManagerInterface<T>;
|
|
53
54
|
|
|
54
|
-
/** Pagination and
|
|
55
|
-
export declare interface FindOptions {
|
|
55
|
+
/** Pagination, ordering, and cancellation for `find`. */
|
|
56
|
+
export declare interface FindOptions extends OperationOptions {
|
|
56
57
|
readonly limit?: number;
|
|
57
58
|
readonly offset?: number;
|
|
58
59
|
readonly sort?: string;
|
|
@@ -182,12 +183,12 @@ export declare class Model<T = Row> implements ModelInterface<T> {
|
|
|
182
183
|
get name(): string;
|
|
183
184
|
get table(): TableInterface<T>;
|
|
184
185
|
get relations(): RelationMap;
|
|
185
|
-
load(key: Key, include: Include): Promise<Loaded<T> | undefined>;
|
|
186
|
-
load(keys: readonly Key[], include: Include): Promise<
|
|
187
|
-
find(include: Include, options?: FindOptions): Promise<
|
|
188
|
-
link(key: Key, relation: string, target: Key): Promise<void>;
|
|
189
|
-
unlink(key: Key, relation: string, target: Key): Promise<void>;
|
|
190
|
-
links(key: Key, relation: string): Promise<readonly Key[]>;
|
|
186
|
+
load(key: Key, include: Include, options?: OperationOptions): Promise<Loaded<T> | undefined>;
|
|
187
|
+
load(keys: readonly Key[], include: Include, options?: OperationOptions): Promise<ReadonlyArray<Loaded<T> | undefined>>;
|
|
188
|
+
find(include: Include, options?: FindOptions): Promise<ReadonlyArray<Loaded<T>>>;
|
|
189
|
+
link(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
190
|
+
unlink(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
191
|
+
links(key: Key, relation: string, options?: OperationOptions): Promise<readonly Key[]>;
|
|
191
192
|
}
|
|
192
193
|
|
|
193
194
|
/**
|
|
@@ -236,18 +237,21 @@ export declare type ModelEventMap<TKey extends Key = Key> = {
|
|
|
236
237
|
* Exposes a typed {@link emitter} (AGENTS §13) carrying the eager-load + junction
|
|
237
238
|
* moments ({@link ModelEventMap}) for fire-and-forget observers — emitting is
|
|
238
239
|
* observation-only (a swallowed listener throw can never corrupt the batched load).
|
|
240
|
+
* `link` is idempotent for sequential calls: an existing pair writes nothing and
|
|
241
|
+
* emits nothing. `unlink` removes matching junction rows inside one database
|
|
242
|
+
* transaction, so a fault rolls the removal back.
|
|
239
243
|
*/
|
|
240
244
|
export declare interface ModelInterface<T = Row> {
|
|
241
245
|
readonly emitter: EmitterInterface<ModelEventMap>;
|
|
242
246
|
readonly name: string;
|
|
243
247
|
readonly table: TableInterface<T>;
|
|
244
248
|
readonly relations: RelationMap;
|
|
245
|
-
load(key: Key, include: Include): Promise<Loaded<T> | undefined>;
|
|
246
|
-
load(keys: readonly Key[], include: Include): Promise<
|
|
247
|
-
find(include: Include, options?: FindOptions): Promise<
|
|
248
|
-
link(key: Key, relation: string, target: Key): Promise<void>;
|
|
249
|
-
unlink(key: Key, relation: string, target: Key): Promise<void>;
|
|
250
|
-
links(key: Key, relation: string): Promise<readonly Key[]>;
|
|
249
|
+
load(key: Key, include: Include, options?: OperationOptions): Promise<Loaded<T> | undefined>;
|
|
250
|
+
load(keys: readonly Key[], include: Include, options?: OperationOptions): Promise<ReadonlyArray<Loaded<T> | undefined>>;
|
|
251
|
+
find(include: Include, options?: FindOptions): Promise<ReadonlyArray<Loaded<T>>>;
|
|
252
|
+
link(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
253
|
+
unlink(key: Key, relation: string, target: Key, options?: OperationOptions): Promise<void>;
|
|
254
|
+
links(key: Key, relation: string, options?: OperationOptions): Promise<readonly Key[]>;
|
|
251
255
|
}
|
|
252
256
|
|
|
253
257
|
/**
|
|
@@ -303,8 +307,10 @@ export declare interface RelationDescriptor {
|
|
|
303
307
|
*
|
|
304
308
|
* @remarks
|
|
305
309
|
* Thrown for: a relation value whose relationship cannot be inferred (`INVALID`), a
|
|
306
|
-
*
|
|
307
|
-
*
|
|
310
|
+
* relation targeting a table or junction the database does not declare, raised when
|
|
311
|
+
* the manager is created (`INVALID`), a reference to a relation a model does not
|
|
312
|
+
* define (`UNKNOWN_RELATION`), and `link` / `unlink` / `links` on a relation that is
|
|
313
|
+
* not a `through` (`NOT_THROUGH`).
|
|
308
314
|
*/
|
|
309
315
|
export declare class RelationError extends Error {
|
|
310
316
|
readonly code: RelationErrorCode;
|
|
@@ -322,8 +328,8 @@ export declare type RelationErrorCode = 'INVALID' | 'UNKNOWN_RELATION' | 'NOT_TH
|
|
|
322
328
|
* @remarks
|
|
323
329
|
* Holds the database both at its precise type (to type each model's own table)
|
|
324
330
|
* and at the broad `DatabaseInterface` (to fetch related tables by runtime name
|
|
325
|
-
* while loading).
|
|
326
|
-
*
|
|
331
|
+
* while loading). Construction validates every target and junction against the
|
|
332
|
+
* database's declared tables, so invalid definitions fail before an operation runs.
|
|
327
333
|
*/
|
|
328
334
|
export declare class RelationManager<T extends TableMap = TableMap> implements RelationManagerInterface<T> {
|
|
329
335
|
#private;
|
package/dist/src/core/index.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { isArray, isDefined, isRecord, isString } from "@orkestrel/contract";
|
|
2
|
-
import { extractKey } from "@orkestrel/database";
|
|
2
|
+
import { checkAbort, extractKey } from "@orkestrel/database";
|
|
3
3
|
import { Emitter } from "@orkestrel/emitter";
|
|
4
4
|
//#region src/core/errors.ts
|
|
5
5
|
/**
|
|
@@ -7,8 +7,10 @@ import { Emitter } from "@orkestrel/emitter";
|
|
|
7
7
|
*
|
|
8
8
|
* @remarks
|
|
9
9
|
* Thrown for: a relation value whose relationship cannot be inferred (`INVALID`), a
|
|
10
|
-
*
|
|
11
|
-
*
|
|
10
|
+
* relation targeting a table or junction the database does not declare, raised when
|
|
11
|
+
* the manager is created (`INVALID`), a reference to a relation a model does not
|
|
12
|
+
* define (`UNKNOWN_RELATION`), and `link` / `unlink` / `links` on a relation that is
|
|
13
|
+
* not a `through` (`NOT_THROUGH`).
|
|
12
14
|
*/
|
|
13
15
|
var RelationError = class extends Error {
|
|
14
16
|
code;
|
|
@@ -297,67 +299,89 @@ var Model = class {
|
|
|
297
299
|
get relations() {
|
|
298
300
|
return this.#relations;
|
|
299
301
|
}
|
|
300
|
-
async load(keys, include) {
|
|
302
|
+
async load(keys, include, options) {
|
|
303
|
+
checkAbort(options?.signal);
|
|
301
304
|
if (isArray(keys)) {
|
|
302
305
|
const bases = await this.#table.get(keys);
|
|
303
306
|
const present = bases.filter(isDefined);
|
|
304
|
-
const props = await this.#populate(present, include, this.#resolved, this.#table.primary);
|
|
307
|
+
const props = await this.#populate(present, include, this.#resolved, this.#table.primary, options);
|
|
305
308
|
let next = 0;
|
|
306
309
|
return bases.map((base) => base === void 0 ? void 0 : Object.assign({}, base, props[next++]));
|
|
307
310
|
}
|
|
308
311
|
const base = await this.#table.get(keys);
|
|
309
312
|
if (base === void 0) return void 0;
|
|
310
|
-
const [props] = await this.#populate([base], include, this.#resolved, this.#table.primary);
|
|
313
|
+
const [props] = await this.#populate([base], include, this.#resolved, this.#table.primary, options);
|
|
311
314
|
return Object.assign({}, base, props);
|
|
312
315
|
}
|
|
313
316
|
async find(include, options) {
|
|
314
|
-
|
|
315
|
-
|
|
316
|
-
|
|
317
|
-
|
|
318
|
-
|
|
319
|
-
|
|
320
|
-
|
|
321
|
-
|
|
322
|
-
|
|
317
|
+
checkAbort(options?.signal);
|
|
318
|
+
const records = await this.#table.records({
|
|
319
|
+
...options?.sort !== void 0 ? { order: [{
|
|
320
|
+
column: options.sort,
|
|
321
|
+
direction: options.direction ?? "ascending"
|
|
322
|
+
}] } : {},
|
|
323
|
+
...options?.offset !== void 0 ? { offset: options.offset } : {},
|
|
324
|
+
...options?.limit !== void 0 ? { limit: options.limit } : {}
|
|
325
|
+
}, options);
|
|
326
|
+
const props = await this.#populate(records, include, this.#resolved, this.#table.primary, options);
|
|
323
327
|
return records.map((record, index) => Object.assign({}, record, props[index]));
|
|
324
328
|
}
|
|
325
|
-
async link(key, relation, target) {
|
|
329
|
+
async link(key, relation, target, options) {
|
|
330
|
+
checkAbort(options?.signal);
|
|
326
331
|
const resolved = this.#through(relation);
|
|
327
|
-
|
|
328
|
-
|
|
329
|
-
|
|
330
|
-
|
|
332
|
+
const source = resolved.source ?? "";
|
|
333
|
+
const column = resolved.target ?? "";
|
|
334
|
+
const junction = this.#database.table(resolved.through ?? "");
|
|
335
|
+
if (await junction.count({ conditions: [{
|
|
336
|
+
column: source,
|
|
337
|
+
operator: "equals",
|
|
338
|
+
values: [key],
|
|
339
|
+
connector: "and"
|
|
340
|
+
}, {
|
|
341
|
+
column,
|
|
342
|
+
operator: "equals",
|
|
343
|
+
values: [target],
|
|
344
|
+
connector: "and"
|
|
345
|
+
}] }, options) > 0) return;
|
|
346
|
+
await junction.set({
|
|
347
|
+
[source]: key,
|
|
348
|
+
[column]: target
|
|
349
|
+
}, options);
|
|
331
350
|
this.#emitter.emit("link", key, relation);
|
|
332
351
|
}
|
|
333
|
-
async unlink(key, relation, target) {
|
|
352
|
+
async unlink(key, relation, target, options) {
|
|
353
|
+
checkAbort(options?.signal);
|
|
334
354
|
const resolved = this.#through(relation);
|
|
335
355
|
const junction = this.#database.table(resolved.through ?? "");
|
|
336
|
-
const rows = await junction.
|
|
356
|
+
const rows = await junction.records({ conditions: [{
|
|
337
357
|
column: resolved.source ?? "",
|
|
338
358
|
operator: "equals",
|
|
339
359
|
values: [key],
|
|
340
360
|
connector: "and"
|
|
341
|
-
}
|
|
361
|
+
}, {
|
|
342
362
|
column: resolved.target ?? "",
|
|
343
363
|
operator: "equals",
|
|
344
364
|
values: [target],
|
|
345
365
|
connector: "and"
|
|
346
|
-
})
|
|
347
|
-
|
|
348
|
-
const
|
|
349
|
-
|
|
350
|
-
|
|
366
|
+
}] }, options);
|
|
367
|
+
await this.#database.transaction(async (transaction) => {
|
|
368
|
+
const scoped = transaction.table(resolved.through ?? "");
|
|
369
|
+
for (const row of rows) {
|
|
370
|
+
const id = extractKey(row, junction.primary);
|
|
371
|
+
if (id !== void 0) await scoped.remove(id, options);
|
|
372
|
+
}
|
|
373
|
+
}, options);
|
|
351
374
|
this.#emitter.emit("unlink", key, relation);
|
|
352
375
|
}
|
|
353
|
-
async links(key, relation) {
|
|
376
|
+
async links(key, relation, options) {
|
|
377
|
+
checkAbort(options?.signal);
|
|
354
378
|
const resolved = this.#through(relation);
|
|
355
|
-
const rows = await this.#database.table(resolved.through ?? "").
|
|
379
|
+
const rows = await this.#database.table(resolved.through ?? "").records({ conditions: [{
|
|
356
380
|
column: resolved.source ?? "",
|
|
357
381
|
operator: "equals",
|
|
358
382
|
values: [key],
|
|
359
383
|
connector: "and"
|
|
360
|
-
})
|
|
384
|
+
}] }, options);
|
|
361
385
|
const target = resolved.target ?? "";
|
|
362
386
|
const keys = [];
|
|
363
387
|
for (const row of rows) {
|
|
@@ -382,13 +406,15 @@ var Model = class {
|
|
|
382
406
|
});
|
|
383
407
|
return resolved;
|
|
384
408
|
}
|
|
385
|
-
async #populate(records, include, resolvedMap, primary) {
|
|
409
|
+
async #populate(records, include, resolvedMap, primary, options) {
|
|
410
|
+
checkAbort(options?.signal);
|
|
386
411
|
const props = records.map(() => ({}));
|
|
387
412
|
for (const [name, sub] of Object.entries(include)) {
|
|
388
413
|
if (sub === false) continue;
|
|
414
|
+
checkAbort(options?.signal);
|
|
389
415
|
const resolved = resolvedMap.get(name);
|
|
390
416
|
if (resolved === void 0) continue;
|
|
391
|
-
const values = await this.#load(records, resolved, sub, primary);
|
|
417
|
+
const values = await this.#load(records, resolved, sub, primary, options);
|
|
392
418
|
values.forEach((value, index) => {
|
|
393
419
|
const target = props[index];
|
|
394
420
|
if (target !== void 0) target[resolved.name] = value;
|
|
@@ -403,59 +429,59 @@ var Model = class {
|
|
|
403
429
|
else if (value !== void 0) total += 1;
|
|
404
430
|
return total;
|
|
405
431
|
}
|
|
406
|
-
async #load(records, resolved, sub, primary) {
|
|
432
|
+
async #load(records, resolved, sub, primary, options) {
|
|
407
433
|
switch (resolved.relationship) {
|
|
408
|
-
case "belongs": return this.#loadBelongs(records, resolved, sub);
|
|
409
|
-
case "many": return this.#loadMany(records, resolved, sub, primary);
|
|
410
|
-
case "one": return this.#loadOne(records, resolved, sub, primary);
|
|
411
|
-
case "through": return this.#loadThrough(records, resolved, sub, primary);
|
|
412
|
-
case "morph": return this.#loadMorph(records, resolved, sub, primary);
|
|
434
|
+
case "belongs": return this.#loadBelongs(records, resolved, sub, options);
|
|
435
|
+
case "many": return this.#loadMany(records, resolved, sub, primary, options);
|
|
436
|
+
case "one": return this.#loadOne(records, resolved, sub, primary, options);
|
|
437
|
+
case "through": return this.#loadThrough(records, resolved, sub, primary, options);
|
|
438
|
+
case "morph": return this.#loadMorph(records, resolved, sub, primary, options);
|
|
413
439
|
}
|
|
414
440
|
}
|
|
415
|
-
async #loadBelongs(records, resolved, sub) {
|
|
441
|
+
async #loadBelongs(records, resolved, sub, options) {
|
|
416
442
|
const column = resolved.column ?? "";
|
|
417
443
|
const keys = [...new Set(records.map((record) => this.#field(record, column)).filter(isDefined))];
|
|
418
444
|
if (keys.length === 0) return records.map(() => void 0);
|
|
419
445
|
const related = this.#database.table(resolved.model);
|
|
420
|
-
const rows = await related.
|
|
446
|
+
const rows = await related.records({ conditions: [{
|
|
421
447
|
column: related.primary,
|
|
422
448
|
operator: "any",
|
|
423
449
|
values: keys,
|
|
424
450
|
connector: "and"
|
|
425
|
-
})
|
|
426
|
-
const index = this.#index(await this.#nest(resolved.model, rows, sub), related.primary);
|
|
451
|
+
}] }, options);
|
|
452
|
+
const index = this.#index(await this.#nest(resolved.model, rows, sub, options), related.primary);
|
|
427
453
|
return records.map((record) => {
|
|
428
454
|
const fk = this.#field(record, column);
|
|
429
455
|
return isDefined(fk) ? index.get(String(fk)) : void 0;
|
|
430
456
|
});
|
|
431
457
|
}
|
|
432
|
-
async #loadMany(records, resolved, sub, primary) {
|
|
458
|
+
async #loadMany(records, resolved, sub, primary, options) {
|
|
433
459
|
const foreign = resolved.key ?? "";
|
|
434
460
|
const keys = [...new Set(records.map((record) => this.#field(record, primary)).filter(isDefined))];
|
|
435
461
|
if (keys.length === 0) return records.map(() => []);
|
|
436
|
-
const rows = await this.#database.table(resolved.model).
|
|
462
|
+
const rows = await this.#database.table(resolved.model).records({ conditions: [{
|
|
437
463
|
column: foreign,
|
|
438
464
|
operator: "any",
|
|
439
465
|
values: keys,
|
|
440
466
|
connector: "and"
|
|
441
|
-
})
|
|
442
|
-
const groups = this.#group(await this.#nest(resolved.model, rows, sub), foreign);
|
|
467
|
+
}] }, options);
|
|
468
|
+
const groups = this.#group(await this.#nest(resolved.model, rows, sub, options), foreign);
|
|
443
469
|
return records.map((record) => groups.get(String(this.#field(record, primary))) ?? []);
|
|
444
470
|
}
|
|
445
|
-
async #loadOne(records, resolved, sub, primary) {
|
|
446
|
-
return (await this.#loadMany(records, resolved, sub, primary)).map((group) => group.length > 0 ? group[0] : void 0);
|
|
471
|
+
async #loadOne(records, resolved, sub, primary, options) {
|
|
472
|
+
return (await this.#loadMany(records, resolved, sub, primary, options)).map((group) => group.length > 0 ? group[0] : void 0);
|
|
447
473
|
}
|
|
448
|
-
async #loadThrough(records, resolved, sub, primary) {
|
|
474
|
+
async #loadThrough(records, resolved, sub, primary, options) {
|
|
449
475
|
const source = resolved.source ?? "";
|
|
450
476
|
const target = resolved.target ?? "";
|
|
451
477
|
const parents = [...new Set(records.map((record) => this.#field(record, primary)).filter(isDefined))];
|
|
452
478
|
if (parents.length === 0) return records.map(() => []);
|
|
453
|
-
const junctions = await this.#database.table(resolved.through ?? "").
|
|
479
|
+
const junctions = await this.#database.table(resolved.through ?? "").records({ conditions: [{
|
|
454
480
|
column: source,
|
|
455
481
|
operator: "any",
|
|
456
482
|
values: parents,
|
|
457
483
|
connector: "and"
|
|
458
|
-
})
|
|
484
|
+
}] }, options);
|
|
459
485
|
const targetsBySource = /* @__PURE__ */ new Map();
|
|
460
486
|
for (const junction of junctions) {
|
|
461
487
|
const value = this.#field(junction, target);
|
|
@@ -468,13 +494,13 @@ var Model = class {
|
|
|
468
494
|
const targets = [...new Set([...targetsBySource.values()].flat())];
|
|
469
495
|
if (targets.length === 0) return records.map(() => []);
|
|
470
496
|
const related = this.#database.table(resolved.model);
|
|
471
|
-
const rows = await related.
|
|
497
|
+
const rows = await related.records({ conditions: [{
|
|
472
498
|
column: related.primary,
|
|
473
499
|
operator: "any",
|
|
474
500
|
values: targets,
|
|
475
501
|
connector: "and"
|
|
476
|
-
})
|
|
477
|
-
const index = this.#index(await this.#nest(resolved.model, rows, sub), related.primary);
|
|
502
|
+
}] }, options);
|
|
503
|
+
const index = this.#index(await this.#nest(resolved.model, rows, sub, options), related.primary);
|
|
478
504
|
return records.map((record) => {
|
|
479
505
|
const out = [];
|
|
480
506
|
for (const value of targetsBySource.get(String(this.#field(record, primary))) ?? []) {
|
|
@@ -484,29 +510,29 @@ var Model = class {
|
|
|
484
510
|
return out;
|
|
485
511
|
});
|
|
486
512
|
}
|
|
487
|
-
async #loadMorph(records, resolved, sub, primary) {
|
|
513
|
+
async #loadMorph(records, resolved, sub, primary, options) {
|
|
488
514
|
const foreign = resolved.key ?? "";
|
|
489
515
|
const keys = [...new Set(records.map((record) => this.#field(record, primary)).filter(isDefined))];
|
|
490
516
|
if (keys.length === 0) return records.map(() => []);
|
|
491
|
-
const rows = await this.#database.table(resolved.model).
|
|
517
|
+
const rows = await this.#database.table(resolved.model).records({ conditions: [{
|
|
492
518
|
column: foreign,
|
|
493
519
|
operator: "any",
|
|
494
520
|
values: keys,
|
|
495
521
|
connector: "and"
|
|
496
|
-
}
|
|
522
|
+
}, {
|
|
497
523
|
column: resolved.tag ?? "",
|
|
498
524
|
operator: "equals",
|
|
499
525
|
values: [resolved.label ?? ""],
|
|
500
526
|
connector: "and"
|
|
501
|
-
})
|
|
502
|
-
const groups = this.#group(await this.#nest(resolved.model, rows, sub), foreign);
|
|
527
|
+
}] }, options);
|
|
528
|
+
const groups = this.#group(await this.#nest(resolved.model, rows, sub, options), foreign);
|
|
503
529
|
return records.map((record) => groups.get(String(this.#field(record, primary))) ?? []);
|
|
504
530
|
}
|
|
505
|
-
async #nest(model, rows, sub) {
|
|
531
|
+
async #nest(model, rows, sub, options) {
|
|
506
532
|
if (typeof sub === "boolean" || rows.length === 0) return rows;
|
|
507
533
|
const context = this.#lookup(model);
|
|
508
534
|
if (context === void 0) return rows;
|
|
509
|
-
const props = await this.#populate(rows, sub, context.resolved, context.primary);
|
|
535
|
+
const props = await this.#populate(rows, sub, context.resolved, context.primary, options);
|
|
510
536
|
return rows.map((row, index) => Object.assign({}, row, props[index]));
|
|
511
537
|
}
|
|
512
538
|
#index(rows, column) {
|
|
@@ -534,8 +560,8 @@ var Model = class {
|
|
|
534
560
|
* @remarks
|
|
535
561
|
* Holds the database both at its precise type (to type each model's own table)
|
|
536
562
|
* and at the broad `DatabaseInterface` (to fetch related tables by runtime name
|
|
537
|
-
* while loading).
|
|
538
|
-
*
|
|
563
|
+
* while loading). Construction validates every target and junction against the
|
|
564
|
+
* database's declared tables, so invalid definitions fail before an operation runs.
|
|
539
565
|
*/
|
|
540
566
|
var RelationManager = class {
|
|
541
567
|
#database;
|
|
@@ -546,7 +572,24 @@ var RelationManager = class {
|
|
|
546
572
|
this.#database = options.database;
|
|
547
573
|
this.#broad = options.database;
|
|
548
574
|
this.#relations = options.relations ?? {};
|
|
549
|
-
|
|
575
|
+
const declared = new Set(Object.keys(this.#broad.export()));
|
|
576
|
+
for (const [name, map] of Object.entries(this.#relations)) {
|
|
577
|
+
if (map === void 0) continue;
|
|
578
|
+
const resolved = resolveRelationMap(map);
|
|
579
|
+
for (const [relation, entry] of resolved) {
|
|
580
|
+
if (!declared.has(entry.model)) throw new RelationError("INVALID", `Model '${name}' relation '${relation}' targets undeclared table '${entry.model}'`, {
|
|
581
|
+
model: name,
|
|
582
|
+
relation,
|
|
583
|
+
table: entry.model
|
|
584
|
+
});
|
|
585
|
+
if (entry.relationship === "through" && entry.through !== void 0 && !declared.has(entry.through)) throw new RelationError("INVALID", `Model '${name}' relation '${relation}' uses undeclared junction '${entry.through}'`, {
|
|
586
|
+
model: name,
|
|
587
|
+
relation,
|
|
588
|
+
table: entry.through
|
|
589
|
+
});
|
|
590
|
+
}
|
|
591
|
+
this.#resolved.set(name, resolved);
|
|
592
|
+
}
|
|
550
593
|
}
|
|
551
594
|
get count() {
|
|
552
595
|
return this.#resolved.size;
|