kysely-rizzolver 0.0.2 → 0.0.3
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 +14 -10
- package/dist/cjs/fetch-result-factory.js +1 -0
- package/dist/cjs/fetch-result-factory.js.map +1 -0
- package/dist/cjs/fetch-result.d.ts +11 -8
- package/dist/cjs/fetch-result.js +5 -0
- package/dist/cjs/fetch-result.js.map +1 -0
- package/dist/cjs/fk-gather-result-factory.d.ts +69 -0
- package/dist/cjs/fk-gather-result-factory.js +111 -0
- package/dist/cjs/fk-gather-result-factory.js.map +1 -0
- package/dist/cjs/fk-gather-result.d.ts +115 -0
- package/dist/cjs/fk-gather-result.js +127 -0
- package/dist/cjs/fk-gather-result.js.map +1 -0
- package/dist/cjs/fks.d.ts +111 -0
- package/dist/cjs/fks.js +101 -0
- package/dist/cjs/fks.js.map +1 -0
- package/dist/cjs/index.d.ts +10 -4
- package/dist/cjs/index.js +17 -1
- package/dist/cjs/index.js.map +1 -0
- package/dist/cjs/kysely-rizzolver-builder.d.ts +17 -0
- package/dist/cjs/kysely-rizzolver-builder.js +23 -0
- package/dist/cjs/kysely-rizzolver-builder.js.map +1 -0
- package/dist/cjs/kysely-rizzolver-fk-builder.d.ts +20 -0
- package/dist/cjs/kysely-rizzolver-fk-builder.js +23 -0
- package/dist/cjs/kysely-rizzolver-fk-builder.js.map +1 -0
- package/dist/cjs/kysely-rizzolver.d.ts +64 -85
- package/dist/cjs/kysely-rizzolver.js +47 -90
- package/dist/cjs/kysely-rizzolver.js.map +1 -0
- package/dist/cjs/model-collection.d.ts +5 -1
- package/dist/cjs/model-collection.js +24 -2
- package/dist/cjs/model-collection.js.map +1 -0
- package/dist/cjs/query-builder.d.ts +14 -12
- package/dist/cjs/query-builder.js +10 -4
- package/dist/cjs/query-builder.js.map +1 -0
- package/dist/cjs/selector.d.ts +71 -7
- package/dist/cjs/selector.js +2 -1
- package/dist/cjs/selector.js.map +1 -0
- package/dist/cjs/type-helpers.d.ts +20 -0
- package/dist/cjs/type-helpers.js +1 -0
- package/dist/cjs/type-helpers.js.map +1 -0
- package/dist/esm/fetch-result-factory.js +1 -0
- package/dist/esm/fetch-result-factory.js.map +1 -0
- package/dist/esm/fetch-result.js +5 -0
- package/dist/esm/fetch-result.js.map +1 -0
- package/dist/esm/fk-gather-result-factory.js +108 -0
- package/dist/esm/fk-gather-result-factory.js.map +1 -0
- package/dist/esm/fk-gather-result.js +115 -0
- package/dist/esm/fk-gather-result.js.map +1 -0
- package/dist/esm/fks.js +96 -0
- package/dist/esm/fks.js.map +1 -0
- package/dist/esm/index.js +4 -1
- package/dist/esm/index.js.map +1 -0
- package/dist/esm/kysely-rizzolver-builder.js +20 -0
- package/dist/esm/kysely-rizzolver-builder.js.map +1 -0
- package/dist/esm/kysely-rizzolver-fk-builder.js +20 -0
- package/dist/esm/kysely-rizzolver-fk-builder.js.map +1 -0
- package/dist/esm/kysely-rizzolver.js +47 -90
- package/dist/esm/kysely-rizzolver.js.map +1 -0
- package/dist/esm/model-collection.js +24 -2
- package/dist/esm/model-collection.js.map +1 -0
- package/dist/esm/query-builder.js +10 -4
- package/dist/esm/query-builder.js.map +1 -0
- package/dist/esm/selector.js +2 -1
- package/dist/esm/selector.js.map +1 -0
- package/dist/esm/type-helpers.js +1 -0
- package/dist/esm/type-helpers.js.map +1 -0
- package/dist/types/fetch-result.d.ts +11 -8
- package/dist/types/fk-gather-result-factory.d.ts +69 -0
- package/dist/types/fk-gather-result.d.ts +115 -0
- package/dist/types/fks.d.ts +111 -0
- package/dist/types/index.d.ts +10 -4
- package/dist/types/kysely-rizzolver-builder.d.ts +17 -0
- package/dist/types/kysely-rizzolver-fk-builder.d.ts +20 -0
- package/dist/types/kysely-rizzolver.d.ts +64 -85
- package/dist/types/model-collection.d.ts +5 -1
- package/dist/types/query-builder.d.ts +14 -12
- package/dist/types/selector.d.ts +71 -7
- package/dist/types/type-helpers.d.ts +20 -0
- package/package.json +1 -1
@@ -0,0 +1,108 @@
|
|
1
|
+
import { assertIsGatherOneResult, assertIsGatherOneXResult, assertIsGatherSomeResult, isGatherOneResult, isGatherOneXResult, isGatherSomeResult, newGatherOneResult, newGatherOneXResult, newGatherSomeResult } from './fk-gather-result.js';
|
2
|
+
export function newFkGatherResultFactory() {
|
3
|
+
return {
|
4
|
+
/**
|
5
|
+
* Creates a new {@link GatherOneResult} instance.
|
6
|
+
*/
|
7
|
+
newGatherOne(table, depth, result, models) {
|
8
|
+
return newGatherOneResult(table, depth, result, models);
|
9
|
+
},
|
10
|
+
/**
|
11
|
+
* Creates a new {@link GatherOneXResult} instance.
|
12
|
+
*
|
13
|
+
* Note: it may be counterintuitive, but this function accepts `undefined` as
|
14
|
+
* input. I found it is way more convenient to assert the type once in this
|
15
|
+
* funciton rather than in every caller.
|
16
|
+
*/
|
17
|
+
newGatherOneX(table, depth, result, models) {
|
18
|
+
return newGatherOneXResult(table, depth, result, models);
|
19
|
+
},
|
20
|
+
/**
|
21
|
+
* Creates a new {@link GatherSomeResult} instance.
|
22
|
+
*/
|
23
|
+
newGatherSome(table, depth, result, models) {
|
24
|
+
return newGatherSomeResult(table, depth, result, models);
|
25
|
+
},
|
26
|
+
/**
|
27
|
+
* Checks if a {@link GatherResult} is a {@link GatherOneResult}.
|
28
|
+
*/
|
29
|
+
isGatherOne(table, depth, result) {
|
30
|
+
return isGatherOneResult(result) && result.table === table && result.depth === depth;
|
31
|
+
},
|
32
|
+
/**
|
33
|
+
* Checks if a {@link GatherResult} is a {@link GatherOneXResult}.
|
34
|
+
*/
|
35
|
+
isGatherOneX(table, depth, result) {
|
36
|
+
return isGatherOneXResult(result) && result.table === table && result.depth === depth;
|
37
|
+
},
|
38
|
+
/**
|
39
|
+
* Checks if a {@link GatherResult} is a {@link GatherSomeResult}.
|
40
|
+
*/
|
41
|
+
isGatherSome(table, depth, result) {
|
42
|
+
return isGatherSomeResult(result) && result.table === table && result.depth === depth;
|
43
|
+
},
|
44
|
+
/**
|
45
|
+
* Asserts that a {@link GatherResult} is a {@link GatherOneResult}.
|
46
|
+
*/
|
47
|
+
assertIsGatherOne(table, depth, result) {
|
48
|
+
assertIsGatherOneResult(result);
|
49
|
+
if (result.table !== table) {
|
50
|
+
throw new Error(`Expected a gatherOne result for table ${table}`);
|
51
|
+
}
|
52
|
+
if (result.depth !== depth) {
|
53
|
+
throw new Error(`Expected a gatherOne result for table ${table} of depth ${depth}`);
|
54
|
+
}
|
55
|
+
},
|
56
|
+
/**
|
57
|
+
* Asserts that a {@link GatherResult} is a {@link GatherOneXResult}.
|
58
|
+
*/
|
59
|
+
assertIsGatherOneX(table, depth, result) {
|
60
|
+
assertIsGatherOneXResult(result);
|
61
|
+
if (result.table !== table) {
|
62
|
+
throw new Error(`Expected a gatherOne result with a non-null value for table ${table}`);
|
63
|
+
}
|
64
|
+
if (result.depth !== depth) {
|
65
|
+
throw new Error(`Expected a gatherOne result with a non-null value for table ${table} of depth ${depth}`);
|
66
|
+
}
|
67
|
+
},
|
68
|
+
/**
|
69
|
+
* Asserts that a {@link GatherResult} is a {@link GatherSomeResult}.
|
70
|
+
*/
|
71
|
+
assertIsGatherSome(table, depth, result) {
|
72
|
+
assertIsGatherSomeResult(result);
|
73
|
+
if (result.table !== table) {
|
74
|
+
throw new Error(`Expected a gatherSome result for table ${table}`);
|
75
|
+
}
|
76
|
+
if (result.depth !== depth) {
|
77
|
+
throw new Error(`Expected a gatherSome result for table ${table} of depth ${depth}`);
|
78
|
+
}
|
79
|
+
},
|
80
|
+
/**
|
81
|
+
* Extracts a Kysely {@link Selectable} instance from a gathered model.
|
82
|
+
*
|
83
|
+
* This is useful because Selectables are the lowest common denominators
|
84
|
+
* for any representation of a DB model, so this allows you to convert
|
85
|
+
* any gather result to anything else that might build upon Kysely.
|
86
|
+
*
|
87
|
+
* For example, to convert a gather result to a fetch result:
|
88
|
+
* ```
|
89
|
+
* const userGather: FkGatherOneXResult<DB, 'user'>;
|
90
|
+
* const userModel = userGather.result;
|
91
|
+
* const userSelectable = rizzolver.gatherObjs.toSelectable(userModel);
|
92
|
+
* ```
|
93
|
+
*
|
94
|
+
*/
|
95
|
+
toSelectable(model) {
|
96
|
+
if (model === undefined || model === null) {
|
97
|
+
return model;
|
98
|
+
}
|
99
|
+
const selectable = {
|
100
|
+
...model
|
101
|
+
};
|
102
|
+
delete selectable['__fkDepth'];
|
103
|
+
delete selectable['__table'];
|
104
|
+
return selectable;
|
105
|
+
}
|
106
|
+
};
|
107
|
+
}
|
108
|
+
//# sourceMappingURL=fk-gather-result-factory.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"fk-gather-result-factory.js","sourceRoot":"","sources":["../../src/fk-gather-result-factory.ts"],"names":[],"mappings":"AACA,OAAO,EACN,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EAIxB,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,uBAAuB,CAAC;AAe/B,MAAM,UAAU,wBAAwB;IAGvC,OAAO;QACN;;WAEG;QACH,YAAY,CACX,KAAQ,EACR,KAAY,EACZ,MAAmD,EACnD,MAA4B;YAE5B,OAAO,kBAAkB,CACxB,KAAK,EACL,KAAK,EACL,MAAM,EACN,MAA8B,CAC9B,CAAC;QACH,CAAC;QAED;;;;;;WAMG;QACH,aAAa,CACZ,KAAQ,EACR,KAAY,EACZ,MAAmD,EACnD,MAA4B;YAE5B,OAAO,mBAAmB,CACzB,KAAK,EACL,KAAK,EACL,MAAM,EACN,MAA8B,CAC9B,CAAC;QACH,CAAC;QAED;;WAEG;QACH,aAAa,CACZ,KAAQ,EACR,KAAY,EACZ,MAAyC,EACzC,MAA4B;YAE5B,OAAO,mBAAmB,CACzB,KAAK,EACL,KAAK,EACL,MAAM,EACN,MAA8B,CAC9B,CAAC;QACH,CAAC;QAED;;WAEG;QACH,WAAW,CACV,KAAQ,EACR,KAAY,EACZ,MAAe;YAEf,OAAO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AAC;YACH,CAAC;QACF,CAAC;QAED;;WAEG;QACH,kBAAkB,CACjB,KAAQ,EACR,KAAY,EACZ,MAAe;YAEf,wBAAwB,CAAC,MAAM,CAAC,CAAC;YAEjC,IAAI,MAAM,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;gBAC5B,MAAM,IAAI,KAAK,CAAC,0CAA0C,KAAK,EAAE,CAAC,CAAC;YACpE,CAAC;YAED,IAAI,MAAM,CAAC,KAAK,KAAK,KAAK,EAAE,CAAC;gBAC5B,MAAM,IAAI,KAAK,CAAC,0CAA0C,KAAK,aAAa,KAAK,EAAE,CAAC,CAAC;YACtF,CAAC;QACF,CAAC;QAED;;;;;;;;;;;;;;WAcG;QACH,YAAY,CAAQ,KAAY;YAC/B,IAAI,KAAK,KAAK,SAAS,IAAI,KAAK,KAAK,IAAI,EAAE,CAAC;gBAC3C,OAAO,KAAY,CAAC;YACrB,CAAC;YAED,MAAM,UAAU,GAAG;gBAClB,GAAG,KAAK;aACR,CAAC;YACF,OAAQ,UAAkB,CAAC,WAAW,CAAC,CAAC;YACxC,OAAQ,UAAkB,CAAC,SAAS,CAAC,CAAC;YAEtC,OAAO,UAAiB,CAAC;QAC1B,CAAC;KACD,CAAC;AACH,CAAC"}
|
@@ -0,0 +1,115 @@
|
|
1
|
+
import { MAX_FK_GATHER_DEPTH } from './fks.js';
|
2
|
+
/**
|
3
|
+
* Creates a new {@link FkGatherOneResult} instance.
|
4
|
+
*/
|
5
|
+
export function newGatherOneResult(table, depth, result, models) {
|
6
|
+
const ref = { value: null };
|
7
|
+
const me = {
|
8
|
+
gatherType: 'gatherOne',
|
9
|
+
table,
|
10
|
+
depth,
|
11
|
+
result,
|
12
|
+
models,
|
13
|
+
asGatherOneX() {
|
14
|
+
if (!me.result) {
|
15
|
+
throw new Error('Expected a gatherOneX result');
|
16
|
+
}
|
17
|
+
return ref.value;
|
18
|
+
}
|
19
|
+
};
|
20
|
+
ref.value = me;
|
21
|
+
return me;
|
22
|
+
}
|
23
|
+
/**
|
24
|
+
* Creates a new {@link FkGatherOneXResult} instance.
|
25
|
+
*
|
26
|
+
* Note: it may be counterintuitive, but this function accepts `undefined` as
|
27
|
+
* input. I found it is way more convenient to assert the type once in this
|
28
|
+
* funciton rather than in every caller.
|
29
|
+
*/
|
30
|
+
export function newGatherOneXResult(table, depth, result, models) {
|
31
|
+
if (!result) {
|
32
|
+
throw new Error('Expected a gatherOneX result');
|
33
|
+
}
|
34
|
+
const ref = { value: null };
|
35
|
+
const me = {
|
36
|
+
gatherType: 'gatherOne',
|
37
|
+
table,
|
38
|
+
depth,
|
39
|
+
result,
|
40
|
+
models,
|
41
|
+
asGatherOneX() {
|
42
|
+
return ref.value;
|
43
|
+
}
|
44
|
+
};
|
45
|
+
ref.value = me;
|
46
|
+
return me;
|
47
|
+
}
|
48
|
+
/**
|
49
|
+
* Creates a new {@link FkGatherSomeResult} instance.
|
50
|
+
*/
|
51
|
+
export function newGatherSomeResult(table, depth, result, models) {
|
52
|
+
return {
|
53
|
+
gatherType: 'gatherSome',
|
54
|
+
table,
|
55
|
+
depth,
|
56
|
+
result,
|
57
|
+
models
|
58
|
+
};
|
59
|
+
}
|
60
|
+
export function isGatherResult(result) {
|
61
|
+
return (!!result &&
|
62
|
+
typeof result === 'object' &&
|
63
|
+
'gatherType' in result &&
|
64
|
+
!!result.gatherType &&
|
65
|
+
typeof result.gatherType === 'string' &&
|
66
|
+
'table' in result &&
|
67
|
+
!!result.table &&
|
68
|
+
typeof result.table === 'string' &&
|
69
|
+
'depth' in result &&
|
70
|
+
typeof result.depth === 'number' &&
|
71
|
+
Number.isInteger(result.depth) &&
|
72
|
+
result.depth >= 0 &&
|
73
|
+
result.depth <= MAX_FK_GATHER_DEPTH &&
|
74
|
+
'result' in result &&
|
75
|
+
(result.result === undefined ||
|
76
|
+
typeof result.result === 'object' ||
|
77
|
+
(Array.isArray(result.result) && result.result.every((r) => !!r && typeof r === 'object'))));
|
78
|
+
}
|
79
|
+
export function isGatherOneResult(value) {
|
80
|
+
return isGatherResult(value) && value.gatherType === 'gatherOne' && !Array.isArray(value.result);
|
81
|
+
}
|
82
|
+
export function isGatherOneXResult(value) {
|
83
|
+
return (isGatherResult(value) &&
|
84
|
+
value.gatherType === 'gatherOne' &&
|
85
|
+
!Array.isArray(value.result) &&
|
86
|
+
!!value.result);
|
87
|
+
}
|
88
|
+
export function isGatherSomeResult(value) {
|
89
|
+
return isGatherResult(value) && value.gatherType === 'gatherSome' && Array.isArray(value.result);
|
90
|
+
}
|
91
|
+
/**
|
92
|
+
* Asserts that `value` is a {@link FkGatherOneResult}.
|
93
|
+
*/
|
94
|
+
export function assertIsGatherOneResult(value) {
|
95
|
+
if (!isGatherOneResult(value)) {
|
96
|
+
throw new Error('Expected a gatherOne result');
|
97
|
+
}
|
98
|
+
}
|
99
|
+
/**
|
100
|
+
* Asserts that `value` is a {@link FkGatherOneXResult}.
|
101
|
+
*/
|
102
|
+
export function assertIsGatherOneXResult(value) {
|
103
|
+
if (!isGatherOneXResult(value)) {
|
104
|
+
throw new Error('Expected a gatherOne result with a non-null, non undefined result');
|
105
|
+
}
|
106
|
+
}
|
107
|
+
/**
|
108
|
+
* Asserts that `value` is a {@link FkGatherSomeResult}.
|
109
|
+
*/
|
110
|
+
export function assertIsGatherSomeResult(value) {
|
111
|
+
if (!isGatherSomeResult(value)) {
|
112
|
+
throw new Error('Expected a gatherSome result');
|
113
|
+
}
|
114
|
+
}
|
115
|
+
//# sourceMappingURL=fk-gather-result.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"fk-gather-result.js","sourceRoot":"","sources":["../../src/fk-gather-result.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,mBAAmB,EAAE,MAAM,UAAU,CAAC;AAyG/C;;GAEG;AACH,MAAM,UAAU,kBAAkB,CAKjC,KAAY,EACZ,KAAY,EACZ,MAAuD,EACvD,MAA2C;IAE3C,MAAM,GAAG,GAAG,EAAE,KAAK,EAAE,IAAqD,EAAE,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,CAAC,MAAM,CAAC,CAAC;AAClG,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,uBAAuB,CACtC,KAAc;IAEd,IAAI,CAAC,iBAAiB,CAAC,KAAK,CAAC,EAAE,CAAC;QAC/B,MAAM,IAAI,KAAK,CAAC,6BAA6B,CAAC,CAAC;IAChD,CAAC;AACF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACvC,KAAc;IAEd,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,mEAAmE,CAAC,CAAC;IACtF,CAAC;AACF,CAAC;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB,CACvC,KAAc;IAEd,IAAI,CAAC,kBAAkB,CAAC,KAAK,CAAC,EAAE,CAAC;QAChC,MAAM,IAAI,KAAK,CAAC,8BAA8B,CAAC,CAAC;IACjD,CAAC;AACF,CAAC"}
|
package/dist/esm/fks.js
ADDED
@@ -0,0 +1,96 @@
|
|
1
|
+
/**
|
2
|
+
* The maximum depth to gather FKs in a single gather operation.
|
3
|
+
*
|
4
|
+
* If you change this, you must also change {@link NextDepth}.
|
5
|
+
*/
|
6
|
+
export const MAX_FK_GATHER_DEPTH = 3;
|
7
|
+
export class ModelFkGatherError extends Error {
|
8
|
+
constructor(table, id, reason) {
|
9
|
+
super(`Failed to gather model (table: '${table}', id: ${id}): ${reason}`);
|
10
|
+
}
|
11
|
+
}
|
12
|
+
export async function gatherModelFks(rizzolver, dbInstance, table, where, opts = {}) {
|
13
|
+
const depth = opts.depth ?? MAX_FK_GATHER_DEPTH;
|
14
|
+
const onInvalidModel = opts.onInvalidModel ?? 'omit';
|
15
|
+
const modelCollection = opts.modelCollection ?? null;
|
16
|
+
const fkDefs = rizzolver._types.fkDefs;
|
17
|
+
let qb = rizzolver
|
18
|
+
.newQueryBuilder()
|
19
|
+
.add(table, table, rizzolver._types.fields[table]);
|
20
|
+
const gatherItems = [];
|
21
|
+
qb = _collectGatherData(fkDefs, table, qb, depth, gatherItems);
|
22
|
+
let ky = dbInstance.selectFrom(table);
|
23
|
+
for (const gatherItem of gatherItems) {
|
24
|
+
ky = ky.leftJoin(gatherItem.tableAlias, gatherItem.join);
|
25
|
+
}
|
26
|
+
ky = ky.select(qb.allFields()).where((eb) => where(eb));
|
27
|
+
const rizzult = await qb.run(() => ky.execute());
|
28
|
+
const collection = rizzult.models;
|
29
|
+
modelCollection?.addCollection(collection);
|
30
|
+
const result = rizzult.rows.map((row) => {
|
31
|
+
const baseModel = row.selectors[table];
|
32
|
+
const gatheredModel = _baseToGatheredModel(fkDefs, table, baseModel, collection, MAX_FK_GATHER_DEPTH, onInvalidModel);
|
33
|
+
return gatheredModel;
|
34
|
+
});
|
35
|
+
if (onInvalidModel === 'omit') {
|
36
|
+
return result.filter((model) => model !== null);
|
37
|
+
}
|
38
|
+
return result;
|
39
|
+
}
|
40
|
+
function _collectGatherData(fkDefs, table, qb, depthLeft, result) {
|
41
|
+
if (depthLeft <= 0) {
|
42
|
+
return qb;
|
43
|
+
}
|
44
|
+
const myAlias = qb.numSelectors == 1 ? table : `fk${qb.numSelectors - 1}`;
|
45
|
+
const myFkDefs = fkDefs[table] ?? {};
|
46
|
+
for (const fkDef of Object.values(myFkDefs)) {
|
47
|
+
const otherAlias = `fk${qb.numSelectors}`;
|
48
|
+
qb = qb.add(fkDef.otherTable, otherAlias);
|
49
|
+
const myField = `${myAlias}.${fkDef.myField}`; // qb.selectors[myAlias].field(fkDef.myField).str;
|
50
|
+
const otherField = `${otherAlias}.${fkDef.otherField}`; // qb.selectors[otherAlias].field(fkDef.otherField).str;
|
51
|
+
const join = (join) => join.onRef(myField, '=', otherField);
|
52
|
+
result.push({ tableAlias: qb.table(otherAlias), join });
|
53
|
+
qb = _collectGatherData(fkDefs, fkDef.otherTable, qb, (depthLeft - 1), result);
|
54
|
+
}
|
55
|
+
return qb;
|
56
|
+
}
|
57
|
+
function _baseToGatheredModel(fkDefs, table, baseModel, collection, depth, onInvalidModel) {
|
58
|
+
if (depth <= 0) {
|
59
|
+
return { __fkDepth: 0, __table: table, ...baseModel };
|
60
|
+
}
|
61
|
+
const myId = baseModel['id'];
|
62
|
+
if (!myId || typeof myId !== 'number') {
|
63
|
+
throw new Error('Expected model to have a valid id');
|
64
|
+
}
|
65
|
+
const gatheredModel = { __fkDepth: depth, __table: table, ...baseModel };
|
66
|
+
const myFkDefs = fkDefs[table] ?? {};
|
67
|
+
for (const [fkName, fkDef] of Object.entries(myFkDefs)) {
|
68
|
+
const fkValue = baseModel[fkDef.myField];
|
69
|
+
if (fkValue !== null && fkValue !== undefined && typeof fkValue !== 'number') {
|
70
|
+
throw new ModelFkGatherError(table, myId, `Invalid looking FK value for reference '${fkName}' (field: '${fkDef.myField}'). Expected positive number, got: '${JSON.stringify(fkValue)}'`);
|
71
|
+
}
|
72
|
+
const expectsOtherModel = fkValue !== null && fkValue !== undefined && fkValue !== 0;
|
73
|
+
const otherBaseModel = expectsOtherModel
|
74
|
+
? collection.get(fkDef.otherTable, fkValue) ?? null
|
75
|
+
: null;
|
76
|
+
const isValid = expectsOtherModel === !!otherBaseModel;
|
77
|
+
if (!isValid) {
|
78
|
+
switch (onInvalidModel) {
|
79
|
+
case 'omit':
|
80
|
+
case 'null':
|
81
|
+
return null;
|
82
|
+
case 'throw':
|
83
|
+
throw new ModelFkGatherError(table, myId, `Could not find model for FK reference '${fkName}' (field: '${fkDef.myField}') for id ${fkValue}`);
|
84
|
+
}
|
85
|
+
}
|
86
|
+
if (otherBaseModel) {
|
87
|
+
const otherGatheredModel = _baseToGatheredModel(fkDefs, fkDef.otherTable, otherBaseModel, collection, (depth - 1), onInvalidModel);
|
88
|
+
gatheredModel[fkName] = otherGatheredModel;
|
89
|
+
}
|
90
|
+
else {
|
91
|
+
gatheredModel[fkName] = undefined;
|
92
|
+
}
|
93
|
+
}
|
94
|
+
return gatheredModel;
|
95
|
+
}
|
96
|
+
//# sourceMappingURL=fks.js.map
|
@@ -0,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;YACpB,MAAM,kBAAkB,GAAG,oBAAoB,CAC9C,MAAM,EACN,KAAK,CAAC,UAAU,EAChB,cAAc,EACd,UAAU,EACV,CAAC,KAAK,GAAG,CAAC,CAAiB,EAC3B,cAAc,CACd,CAAC;YAEF,aAAa,CAAC,MAAM,CAAC,GAAG,kBAAkB,CAAC;QAC5C,CAAC;aAAM,CAAC;YACP,aAAa,CAAC,MAAM,CAAC,GAAG,SAAS,CAAC;QACnC,CAAC;IACF,CAAC;IAED,OAAO,aAAa,CAAC;AACtB,CAAC"}
|
package/dist/esm/index.js
CHANGED
@@ -1,5 +1,8 @@
|
|
1
1
|
export { KyselyRizzolver } from './kysely-rizzolver.js';
|
2
|
-
export { assertIsFetchOneResult, assertIsFetchOneXResult, assertIsFetchSomeResult, isFetchOneResult, isFetchOneXResult, isFetchSomeResult, newFetchOneResult, newFetchOneXResult, newFetchSomeResult } from './fetch-result.js';
|
2
|
+
export { assertIsFetchOneResult, assertIsFetchOneXResult, assertIsFetchSomeResult, isFetchOneResult, isFetchOneXResult, isFetchResult, isFetchSomeResult, newFetchOneResult, newFetchOneXResult, newFetchSomeResult } from './fetch-result.js';
|
3
|
+
export { assertIsGatherOneResult, assertIsGatherOneXResult, assertIsGatherSomeResult, isGatherOneResult, isGatherOneXResult, isGatherResult, isGatherSomeResult, newGatherOneResult, newGatherOneXResult, newGatherSomeResult } from './fk-gather-result.js';
|
4
|
+
export { MAX_FK_GATHER_DEPTH, ModelFkGatherError } from './fks.js';
|
3
5
|
export { newSelector } from './selector.js';
|
4
6
|
export { newQueryBuilder } from './query-builder.js';
|
5
7
|
export { newModelCollection } from './model-collection.js';
|
8
|
+
//# sourceMappingURL=index.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../../src/index.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,eAAe,EAAE,MAAM,uBAAuB,CAAC;AAQxD,OAAO,EACN,sBAAsB,EACtB,uBAAuB,EACvB,uBAAuB,EACvB,gBAAgB,EAChB,iBAAiB,EACjB,aAAa,EACb,iBAAiB,EACjB,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,MAAM,mBAAmB,CAAC;AAW3B,OAAO,EACN,uBAAuB,EACvB,wBAAwB,EACxB,wBAAwB,EACxB,iBAAiB,EACjB,kBAAkB,EAClB,cAAc,EACd,kBAAkB,EAClB,kBAAkB,EAClB,mBAAmB,EACnB,mBAAmB,EACnB,MAAM,uBAAuB,CAAC;AAW/B,OAAO,EAAE,mBAAmB,EAAE,kBAAkB,EAAE,MAAM,UAAU,CAAC;AAYnE,OAAO,EAAE,WAAW,EAAE,MAAM,eAAe,CAAC;AAG5C,OAAO,EAAE,eAAe,EAAE,MAAM,oBAAoB,CAAC;AAGrD,OAAO,EAAE,kBAAkB,EAAE,MAAM,uBAAuB,CAAC"}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { newKyselyRizzolverFkBuilder } from './kysely-rizzolver-fk-builder.js';
|
2
|
+
import { KyselyRizzolver } from './kysely-rizzolver.js';
|
3
|
+
export function newKyselyRizzolverBuilder(fields, defaultGatherDepth) {
|
4
|
+
return {
|
5
|
+
table(tableName, tableFields) {
|
6
|
+
return newKyselyRizzolverBuilder({
|
7
|
+
...fields,
|
8
|
+
[tableName]: tableFields
|
9
|
+
});
|
10
|
+
},
|
11
|
+
defaultGatherDepth(depth) {
|
12
|
+
return newKyselyRizzolverBuilder(fields, depth);
|
13
|
+
},
|
14
|
+
build(cb) {
|
15
|
+
const defs = cb?.(newKyselyRizzolverFkBuilder({}));
|
16
|
+
return new KyselyRizzolver(fields, defs ?? {}, defaultGatherDepth);
|
17
|
+
}
|
18
|
+
};
|
19
|
+
}
|
20
|
+
//# sourceMappingURL=kysely-rizzolver-builder.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"kysely-rizzolver-builder.js","sourceRoot":"","sources":["../../src/kysely-rizzolver-builder.ts"],"names":[],"mappings":"AAEA,OAAO,EAEN,2BAA2B,EAC3B,MAAM,kCAAkC,CAAC;AAC1C,OAAO,EAAE,eAAe,EAA2B,MAAM,uBAAuB,CAAC;AAsCjF,MAAM,UAAU,yBAAyB,CAIvC,MAAS,EAAE,kBAAuC;IACnD,OAAO;QACN,KAAK,CACJ,SAAY,EACZ,WAG+D;YAE/D,OAAO,yBAAyB,CAA6C;gBAC5E,GAAG,MAAM;gBACT,CAAC,SAAS,CAAC,EAAE,WAAW;aACxB,CAAQ,CAAC;QACX,CAAC;QAED,kBAAkB,CAAiC,KAAgB;YAClE,OAAO,yBAAyB,CAAmB,MAAM,EAAE,KAAK,CAAC,CAAC;QACnE,CAAC;QAED,KAAK,CAAwC,EAAsC;YAClF,MAAM,IAAI,GAAG,EAAE,EAAE,CAAC,2BAA2B,CAAK,EAAE,CAAC,CAAC,CAAC;YACvD,OAAO,IAAI,eAAe,CAAC,MAAM,EAAE,IAAI,IAAI,EAAE,EAAE,kBAAkB,CAAC,CAAC;QACpE,CAAC;KAC2C,CAAC;AAC/C,CAAC"}
|
@@ -0,0 +1,20 @@
|
|
1
|
+
export function newKyselyRizzolverFkBuilder(fkDefs) {
|
2
|
+
return {
|
3
|
+
add(fromTable, fromField, toTable, toField, fkName, nullable) {
|
4
|
+
const defsForTable = (fkDefs[fromTable] ?? {});
|
5
|
+
const fkEntry = {
|
6
|
+
myField: fromField,
|
7
|
+
otherTable: toTable,
|
8
|
+
otherField: toField,
|
9
|
+
isNullable: nullable ?? false
|
10
|
+
};
|
11
|
+
defsForTable[fkName] = fkEntry;
|
12
|
+
fkDefs[fromTable] = defsForTable;
|
13
|
+
return newKyselyRizzolverFkBuilder(fkDefs);
|
14
|
+
},
|
15
|
+
build() {
|
16
|
+
return fkDefs;
|
17
|
+
}
|
18
|
+
};
|
19
|
+
}
|
20
|
+
//# sourceMappingURL=kysely-rizzolver-fk-builder.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"kysely-rizzolver-fk-builder.js","sourceRoot":"","sources":["../../src/kysely-rizzolver-fk-builder.ts"],"names":[],"mappings":"AAwDA,MAAM,UAAU,2BAA2B,CAC1C,MAAc;IAEd,OAAO;QACN,GAAG,CAQF,SAAoB,EACpB,SAAoB,EACpB,OAAgB,EAChB,OAAgB,EAChB,MAAc,EACd,QAAmB;YAEnB,MAAM,YAAY,GAAG,CAAC,MAAM,CAAC,SAAS,CAAC,IAAI,EAAE,CAAmC,CAAC;YACjF,MAAM,OAAO,GAAG;gBACf,OAAO,EAAE,SAAS;gBAClB,UAAU,EAAE,OAAO;gBACnB,UAAU,EAAE,OAAO;gBACnB,UAAU,EAAE,QAAQ,IAAI,KAAK;aAC6B,CAAC;YAE5D,YAAY,CAAC,MAAM,CAAC,GAAG,OAAO,CAAC;YAC/B,MAAM,CAAC,SAAS,CAAC,GAAG,YAAY,CAAC;YAEjC,OAAO,2BAA2B,CAAU,MAAM,CAAC,CAAC;QACrD,CAAC;QAED,KAAK;YACJ,OAAO,MAAM,CAAC;QACf,CAAC;KACD,CAAC;AACH,CAAC"}
|
@@ -1,7 +1,10 @@
|
|
1
1
|
import { newFetchResultFactory } from './fetch-result-factory.js';
|
2
|
+
import { newFkGatherResultFactory } from './fk-gather-result-factory.js';
|
3
|
+
import { gatherModelFks, MAX_FK_GATHER_DEPTH } from './fks.js';
|
4
|
+
import { newKyselyRizzolverBuilder } from './kysely-rizzolver-builder.js';
|
5
|
+
import { newModelCollection as kyNewModelCollection } from './model-collection.js';
|
2
6
|
import { newQueryBuilder as kyNewQueryBuilder } from './query-builder.js';
|
3
7
|
import { newSelector as kyNewSelector } from './selector.js';
|
4
|
-
import { newModelCollection as kyNewModelCollection } from './model-collection.js';
|
5
8
|
/**
|
6
9
|
* A {@link KyselyRizzolver} is a class that is used to define the structure of
|
7
10
|
* a database schema.
|
@@ -14,11 +17,19 @@ import { newModelCollection as kyNewModelCollection } from './model-collection.j
|
|
14
17
|
* {@link KyselyRizzolver.builderNoSchema|.builderNoSchema()}.
|
15
18
|
*/
|
16
19
|
export class KyselyRizzolver {
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
20
|
+
_types;
|
21
|
+
fetchObjs;
|
22
|
+
gatherObjs;
|
23
|
+
defaultGatherDepth;
|
24
|
+
constructor(fields, fks, defaultGatherDepth) {
|
25
|
+
this._types = Object.freeze({
|
26
|
+
fields,
|
27
|
+
fkDefs: fks,
|
28
|
+
dbfk: null
|
29
|
+
});
|
30
|
+
this.fetchObjs = newFetchResultFactory();
|
31
|
+
this.gatherObjs = newFkGatherResultFactory();
|
32
|
+
this.defaultGatherDepth = defaultGatherDepth ?? MAX_FK_GATHER_DEPTH;
|
22
33
|
}
|
23
34
|
/**
|
24
35
|
* Intantiates a new {@link Selector} for the given table.
|
@@ -38,19 +49,43 @@ export class KyselyRizzolver {
|
|
38
49
|
newModelCollection() {
|
39
50
|
return kyNewModelCollection();
|
40
51
|
}
|
52
|
+
async gatherOne(db, table, where, opts) {
|
53
|
+
const depth = opts?.depth ?? this.defaultGatherDepth;
|
54
|
+
const modelCollection = opts?.modelCollection ?? this.newModelCollection();
|
55
|
+
const gather = await gatherModelFks(this, db, table, where, opts);
|
56
|
+
const result = gather[0] ?? undefined;
|
57
|
+
return this.gatherObjs.newGatherOne(table, depth, result, modelCollection);
|
58
|
+
}
|
59
|
+
async gatherOneX(db, table, where, opts) {
|
60
|
+
const depth = opts?.depth ?? this.defaultGatherDepth;
|
61
|
+
const modelCollection = opts?.modelCollection ?? this.newModelCollection();
|
62
|
+
const gather = await gatherModelFks(this, db, table, where, opts);
|
63
|
+
const result = gather[0] ?? undefined;
|
64
|
+
return this.gatherObjs.newGatherOneX(table, depth, result, modelCollection);
|
65
|
+
}
|
66
|
+
async gatherSome(db, table, where, opts) {
|
67
|
+
const depth = opts?.depth ?? this.defaultGatherDepth;
|
68
|
+
const modelCollection = opts?.modelCollection ?? this.newModelCollection();
|
69
|
+
const gather = await gatherModelFks(this, db, table, where, opts);
|
70
|
+
const result = gather.filter((r) => !!r);
|
71
|
+
if (result.length < gather.length) {
|
72
|
+
throw new Error('Expected no nulls in fkGatherSome result');
|
73
|
+
}
|
74
|
+
return this.gatherObjs.newGatherSome(table, depth, result, modelCollection);
|
75
|
+
}
|
41
76
|
/**
|
42
77
|
* Starts building a new {@link KyselyRizzolver} using a builder pattern for
|
43
78
|
* a schema.
|
44
79
|
*
|
45
|
-
* Call {@link
|
80
|
+
* Call {@link KyselyRizzolverBuilder.table|.table()} for each
|
46
81
|
* table that exists on the `DB` type parameter with all of their column
|
47
82
|
* names as a const array. After all tables have been added, call
|
48
|
-
* {@link
|
83
|
+
* {@link KyselyRizzolverBuilder.build|.build()} to get a new
|
49
84
|
* {@link KyselyRizzolver} instance.
|
50
85
|
*
|
51
86
|
* Example:
|
52
87
|
* ```
|
53
|
-
* const rizzolver = KyselyRizzolver.
|
88
|
+
* const rizzolver = KyselyRizzolver.builder<DB>()
|
54
89
|
* .table('user', ['id', 'name', 'email'] as const)
|
55
90
|
* .table('post', ['id', 'title', 'content', 'authorId'] as const)
|
56
91
|
* .build();
|
@@ -58,86 +93,8 @@ export class KyselyRizzolver {
|
|
58
93
|
*
|
59
94
|
* Note: The `as const` assertion is necessary for correct type inference.
|
60
95
|
*/
|
61
|
-
static
|
62
|
-
return
|
96
|
+
static builder() {
|
97
|
+
return newKyselyRizzolverBuilder({});
|
63
98
|
}
|
64
|
-
/**
|
65
|
-
* Starts building a new {@link KyselyRizzolver} using a builder pattern
|
66
|
-
* without a schema.
|
67
|
-
*
|
68
|
-
* Call {@link KyselyRizzolverBuilderNoSchema.table|.table()} for each
|
69
|
-
* table with all of their column names as a const array.
|
70
|
-
*
|
71
|
-
* Example:
|
72
|
-
* ```
|
73
|
-
* const rizzolver = KyselyRizzolver.builder()
|
74
|
-
* .table('user', ['id', 'name', 'email'] as const) // note `as const` is necessary
|
75
|
-
* .table('post', ['id', 'title', 'content', 'authorId'] as const)
|
76
|
-
* .build();
|
77
|
-
* ```
|
78
|
-
*
|
79
|
-
* Since this version of builder is schemaless, it cannot infer the value
|
80
|
-
* types for the columns. The `user` type will be `{ id: unknown, name:
|
81
|
-
* unknown, email: unknown }`.
|
82
|
-
*
|
83
|
-
* You may call
|
84
|
-
* {@link KyselyRizzolverBuilderNoSchema.asModel|.asModel\<M\>()}
|
85
|
-
* immediately after the .table() call to provide the types, where `M` is an
|
86
|
-
* type like `{ column1: type1, column2: type2, ... }`.
|
87
|
-
*
|
88
|
-
* Example:
|
89
|
-
* ```
|
90
|
-
* const rizzolver = KyselyRizzolver.builder()
|
91
|
-
* .table('user', ['id', 'name', 'email'] as const)
|
92
|
-
* .asModel<{id: number, name: string, email: string}>()
|
93
|
-
* .table('post', ['id', 'title', 'content', 'authorId'] as const)
|
94
|
-
* .asModel<{id: number, title: string, content: string, authorId: number}>()
|
95
|
-
* .build();
|
96
|
-
* ```
|
97
|
-
*
|
98
|
-
* p.s. if your .table() and .asModel() columns differ, it will let you know
|
99
|
-
* at compile time ;)
|
100
|
-
*
|
101
|
-
* Once all tables have been added, call
|
102
|
-
* {@link KyselyRizzolverBuilderNoSchema.build|.build()} to get a new
|
103
|
-
* {@link KyselyRizzolver} instance.
|
104
|
-
*/
|
105
|
-
static builderNoSchema() {
|
106
|
-
return _newKyselyRizzolverBuilderNoSchema({}, null);
|
107
|
-
}
|
108
|
-
}
|
109
|
-
function _newKyselyRizzolverBuilderForSchema(fields) {
|
110
|
-
return {
|
111
|
-
table(tableName, tableFields) {
|
112
|
-
return _newKyselyRizzolverBuilderForSchema({
|
113
|
-
...fields,
|
114
|
-
[tableName]: tableFields
|
115
|
-
});
|
116
|
-
},
|
117
|
-
build() {
|
118
|
-
return new KyselyRizzolver(fields);
|
119
|
-
}
|
120
|
-
};
|
121
|
-
}
|
122
|
-
function _newKyselyRizzolverBuilderNoSchema(fields, last) {
|
123
|
-
return {
|
124
|
-
table(tableName, tableFields) {
|
125
|
-
return _newKyselyRizzolverBuilderNoSchema({
|
126
|
-
...fields,
|
127
|
-
[tableName]: {
|
128
|
-
model: null,
|
129
|
-
columns: tableFields
|
130
|
-
}
|
131
|
-
}, tableName);
|
132
|
-
},
|
133
|
-
asModel() {
|
134
|
-
if (!last) {
|
135
|
-
throw new Error('asModel() must be called after table()');
|
136
|
-
}
|
137
|
-
return _newKyselyRizzolverBuilderNoSchema(fields, null);
|
138
|
-
},
|
139
|
-
build() {
|
140
|
-
return new KyselyRizzolver(Object.fromEntries(Object.entries(fields).map((entry) => [entry[0], entry[1].columns])));
|
141
|
-
}
|
142
|
-
};
|
143
99
|
}
|
100
|
+
//# sourceMappingURL=kysely-rizzolver.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"kysely-rizzolver.js","sourceRoot":"","sources":["../../src/kysely-rizzolver.ts"],"names":[],"mappings":"AACA,OAAO,EAA2B,qBAAqB,EAAE,MAAM,2BAA2B,CAAC;AAE3F,OAAO,EAAyB,wBAAwB,EAAE,MAAM,+BAA+B,CAAC;AAChG,OAAO,EAEN,cAAc,EAGd,mBAAmB,EAInB,MAAM,UAAU,CAAC;AAClB,OAAO,EAAE,yBAAyB,EAAE,MAAM,+BAA+B,CAAC;AAC1E,OAAO,EAEN,kBAAkB,IAAI,oBAAoB,EAC1C,MAAM,uBAAuB,CAAC;AAC/B,OAAO,EAAqB,eAAe,IAAI,iBAAiB,EAAE,MAAM,oBAAoB,CAAC;AAC7F,OAAO,EAAiB,WAAW,IAAI,aAAa,EAAE,MAAM,eAAe,CAAC;AAe5E;;;;;;;;;;GAUG;AACH,MAAM,OAAO,eAAe;IAOX,MAAM,CAA+C;IAErD,SAAS,CAAyB;IAClC,UAAU,CAAoC;IAC9C,kBAAkB,CAAqB;IAEvD,YAAY,MAAS,EAAE,GAAW,EAAE,kBAAuC;QAC1E,IAAI,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC;YAC3B,MAAM;YACN,MAAM,EAAE,GAAG;YACX,IAAI,EAAE,IAAW;SACjB,CAAC,CAAC;QAEH,IA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c,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAClE,MAAM,MAAM,GAAG,MAAM,CAAC,CAAC,CAAC,IAAI,SAAS,CAAC;QAEtC,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC;IAC7E,CAAC;IAiBD,KAAK,CAAC,UAAU,CACf,EAAc,EACd,KAAY,EACZ,KAAuC,EACvC,IAAyC;QAEzC,MAAM,KAAK,GAAG,IAAI,EAAE,KAAK,IAAI,IAAI,CAAC,kBAAkB,CAAC;QACrD,MAAM,eAAe,GAAG,IAAI,EAAE,eAAe,IAAI,IAAI,CAAC,kBAAkB,EAAE,CAAC;QAE3E,MAAM,MAAM,GAAG,MAAM,cAAc,CAAC,IAAI,EAAE,EAAE,EAAE,KAAK,EAAE,KAAK,EAAE,IAAI,CAAC,CAAC;QAClE,MAAM,MAAM,GAAG,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;QACzC,IAAI,MAAM,CAAC,MAAM,GAAG,MAAM,CAAC,MAAM,EAAE,CAAC;YACnC,MAAM,IAAI,KAAK,CAAC,0CAA0C,CAAC,CAAC;QAC7D,CAAC;QAED,OAAO,IAAI,CAAC,UAAU,CAAC,aAAa,CAAC,KAAK,EAAE,KAAK,EAAE,MAAM,EAAE,eAAe,CAAC,CAAC;IAC7E,CAAC;IAED;;;;;;;;;;;;;;;;;;;OAmBG;IACH,MAAM,CAAC,OAAO;QACb,OAAO,yBAAyB,CAAS,EAAE,CAAC,CAAC;IAC9C,CAAC;CACD"}
|
@@ -14,8 +14,30 @@ export function newModelCollection(init = {}) {
|
|
14
14
|
collection[table][selectable.id] = selectable;
|
15
15
|
return this;
|
16
16
|
},
|
17
|
-
|
18
|
-
|
17
|
+
addCollection(collection) {
|
18
|
+
for (const [table, entries] of Object.entries(collection.get())) {
|
19
|
+
for (const [id, model] of Object.entries(entries)) {
|
20
|
+
this.add(table, model);
|
21
|
+
}
|
22
|
+
}
|
23
|
+
return this;
|
24
|
+
},
|
25
|
+
get(table, id) {
|
26
|
+
if (!table) {
|
27
|
+
return collection;
|
28
|
+
}
|
29
|
+
if (!id) {
|
30
|
+
return collection[table] ?? {};
|
31
|
+
}
|
32
|
+
return collection[table]?.[id];
|
33
|
+
},
|
34
|
+
getX(table, id) {
|
35
|
+
const result = this.get(table, id);
|
36
|
+
if (!result) {
|
37
|
+
throw new Error(`Model not found in ModelCollection for table ${table} with id ${id}`);
|
38
|
+
}
|
39
|
+
return result;
|
19
40
|
}
|
20
41
|
};
|
21
42
|
}
|
43
|
+
//# sourceMappingURL=model-collection.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"model-collection.js","sourceRoot":"","sources":["../../src/model-collection.ts"],"names":[],"mappings":"AAkCA,MAAM,UAAU,kBAAkB,CAAK,OAAiB,EAAE;IACzD,MAAM,UAAU,GAAa,EAAE,GAAG,IAAI,EAAE,CAAC;IAEzC,OAAO;QACN,GAAG,CAAkC,KAAY,EAAE,UAAkC;YACpF,IACC,CAAC,UAAU;gBACX,CAAC,CAAC,IAAI,IAAI,UAAU,CAAC;gBACrB,CAAC,UAAU,CAAC,EAAE;gBACd,OAAO,UAAU,CAAC,EAAE,KAAK,QAAQ,EAChC,CAAC;gBACF,OAAO,IAAI,CAAC;YACb,CAAC;YAED,IAAI,CAAC,CAAC,KAAK,IAAI,UAAU,CAAC,EAAE,CAAC;gBAC5B,UAAU,CAAC,KAAK,CAAC,GAAG,EAAE,CAAC;YACxB,CAAC;YAED,UAAU,CAAC,KAAK,CAAE,CAAC,UAAU,CAAC,EAAE,CAAC,GAAG,UAAU,CAAC;YAC/C,OAAO,IAAI,CAAC;QACb,CAAC;QAED,aAAa,CAAC,UAA+B;YAC5C,KAAK,MAAM,CAAC,KAAK,EAAE,OAAO,CAAC,IAA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