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
@@ -5,12 +5,17 @@ export function newQueryBuilder(rizzolver) {
|
|
5
5
|
const selectors = {};
|
6
6
|
return {
|
7
7
|
selectors,
|
8
|
+
get numSelectors() {
|
9
|
+
return Object.keys(selectors).length;
|
10
|
+
},
|
8
11
|
table(tableAlias) {
|
9
12
|
return selectors[tableAlias].selectTable;
|
10
13
|
},
|
11
|
-
allFields
|
12
|
-
.
|
13
|
-
|
14
|
+
allFields() {
|
15
|
+
return Object.values(selectors)
|
16
|
+
.map((selector) => selector.selectFields)
|
17
|
+
.flat();
|
18
|
+
},
|
14
19
|
fieldsOf(...tableAliases) {
|
15
20
|
return tableAliases.map((alias) => selectors[alias].selectFields).flat();
|
16
21
|
},
|
@@ -30,7 +35,7 @@ export function newQueryBuilder(rizzolver) {
|
|
30
35
|
if (!alias) {
|
31
36
|
throw new Error('Must provide an alias when calling QueryBuilder.add with a table name');
|
32
37
|
}
|
33
|
-
selector = newSelector(rizzolver, selectorOrTable, alias, (keys ?? rizzolver.fields[selectorOrTable]));
|
38
|
+
selector = newSelector(rizzolver, selectorOrTable, alias, (keys ?? rizzolver._types.fields[selectorOrTable]));
|
34
39
|
}
|
35
40
|
else {
|
36
41
|
if (alias || keys) {
|
@@ -87,3 +92,4 @@ export function newQueryBuilder(rizzolver) {
|
|
87
92
|
}
|
88
93
|
};
|
89
94
|
}
|
95
|
+
//# sourceMappingURL=query-builder.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"query-builder.js","sourceRoot":"","sources":["../../src/query-builder.ts"],"names":[],"mappings":"AACA,OAAO,EAIN,iBAAiB,EACjB,kBAAkB,EAClB,kBAAkB,EAClB,MAAM,mBAAmB,CAAC;AAQ3B,OAAO,EAAiB,WAAW,EAAoC,MAAM,eAAe,CAAC;AAC7F,OAAO,EAAwB,kBAAkB,EAAE,MAAM,uBAAuB,CAAC;AAmJjF,MAAM,UAAU,eAAe,CAC9B,SAAa;IAEb,MAAM,SAAS,GAAa,EAAE,CAAC;IAE/B,O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AC,CAAC,SAAS,CAAC,aAAa,CAAC,CAAC,CAAC,MAAM,CAAC,CAAC,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,CAAC,CAAC,EAChE,eAAe,CACf,CAAC;oBACH,CAAC;iBACD,CAAC;YACH,CAAC,CAAC,EAAS,CAAC;YAEZ,OAAO,CAAC,iBAAiB,GAAG,CAAmC,aAAgB,EAAE,EAAE,CAClF,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,iBAAiB,CAAC,aAAa,CAAC,CAAC,CAAC;YACnE,OAAO,CAAC,kBAAkB,GAAG,CAAmC,aAAgB,EAAE,EAAE,CACnF,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC,CAAC;YACpE,OAAO,CAAC,kBAAkB,GAAG,CAAmC,aAAgB,EAAE,EAAE,CACnF,OAAO,CAAC,IAAI,CAAC,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,kBAAkB,CAAC,aAAa,CAAC,CAAC,CAAC;YAEpE,OAAO,OAAO,CAAC;QAChB,CAAC;KACD,CAAC;AACH,CAAC"}
|
package/dist/esm/selector.js
CHANGED
@@ -1,5 +1,5 @@
|
|
1
1
|
export function newSelector(rizzolver, tableName, alias, keys) {
|
2
|
-
const effectiveKeys = (keys ?? rizzolver.fields[tableName]);
|
2
|
+
const effectiveKeys = (keys ?? rizzolver._types.fields[tableName]);
|
3
3
|
const tableAlias = `${tableName} as ${alias}`;
|
4
4
|
const fields = effectiveKeys.map((field) => `${alias}.${field} as _${alias}_${field}`);
|
5
5
|
function _rowToModel(result) {
|
@@ -40,3 +40,4 @@ export function newSelector(rizzolver, tableName, alias, keys) {
|
|
40
40
|
};
|
41
41
|
return selector;
|
42
42
|
}
|
43
|
+
//# sourceMappingURL=selector.js.map
|
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"selector.js","s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AsC,IAAW;YACtD,MAAM,IAAI,GAAoD,EAAE,CAAC;YAEjE,KAAK,MAAM,GAAG,IAAI,IAAI,EAAE,CAAC;gBACxB,MAAM,KAAK,GAAG,WAAW,CAAC,GAAG,CAAC,CAAC;gBAC/B,IAAI,CAAC,IAAI,CAAC,EAAE,GAAG,EAAE,KAAK,EAAE,CAAC,CAAC;YAC3B,CAAC;YAED,OAAO,IAAI,CAAC;QACb,CAAC;KACQ,CAAC;IAEX,OAAO,QAAQ,CAAC;AACjB,CAAC"}
|
package/dist/esm/type-helpers.js
CHANGED
@@ -0,0 +1 @@
|
|
1
|
+
{"version":3,"file":"type-helpers.js","sourceRoot":"","sources":["../../src/type-helpers.ts"],"names":[],"mappings":""}
|
@@ -1,5 +1,5 @@
|
|
1
1
|
import type { Selectable } from 'kysely';
|
2
|
-
import type
|
2
|
+
import { type ModelCollection } from './model-collection.js';
|
3
3
|
/**
|
4
4
|
* A {@link FetchResult} is a result of a fetch operation. It can be one of
|
5
5
|
* three types:
|
@@ -10,16 +10,19 @@ import type { ModelCollection } from './model-collection.js';
|
|
10
10
|
* - {@link FetchSomeResult} - A result of a fetch operation that is expected to
|
11
11
|
* return any number of rows.
|
12
12
|
*/
|
13
|
-
export type FetchResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]
|
13
|
+
export type FetchResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]>> = Selectable<DB[T]>> = FetchOneResult<DB, T, R> | FetchOneXResult<DB, T, R> | FetchSomeResult<DB, T, R>;
|
14
14
|
/**
|
15
15
|
* A {@link FetchOneResult} is a result of a fetch operation that is expected to
|
16
16
|
* return up to one row.
|
17
17
|
*/
|
18
|
-
export type FetchOneResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]>>
|
18
|
+
export type FetchOneResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]>> = Selectable<DB[T]>> = {
|
19
19
|
fetchType: 'fetchOne';
|
20
20
|
table: T;
|
21
21
|
result: R | undefined;
|
22
|
-
|
22
|
+
/**
|
23
|
+
* The {@link ModelCollection} that contains the fetched models.
|
24
|
+
*/
|
25
|
+
models: ModelCollection<DB>;
|
23
26
|
/**
|
24
27
|
* Returns this result as a {@link FetchOneXResult}.
|
25
28
|
*
|
@@ -31,11 +34,11 @@ export type FetchOneResult<DB, T extends keyof DB & string, R extends Partial<Se
|
|
31
34
|
* A {@link FetchOneXResult} is a result of a fetch operation that is expected
|
32
35
|
* to return exactly one row.
|
33
36
|
*/
|
34
|
-
export type FetchOneXResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]
|
37
|
+
export type FetchOneXResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]>> = Selectable<DB[T]>> = {
|
35
38
|
fetchType: 'fetchOne';
|
36
39
|
table: T;
|
37
40
|
result: R;
|
38
|
-
models
|
41
|
+
models: ModelCollection<DB>;
|
39
42
|
/**
|
40
43
|
* Returns self. This is a no-op, but it's here to make it possible to
|
41
44
|
* cast this object back to a {@link FetchOneXResult}.
|
@@ -46,11 +49,11 @@ export type FetchOneXResult<DB, T extends keyof DB & string, R extends Partial<S
|
|
46
49
|
* A {@link FetchSomeResult} is a result of a fetch operation that is expected
|
47
50
|
* to return any number of rows.
|
48
51
|
*/
|
49
|
-
export type FetchSomeResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]
|
52
|
+
export type FetchSomeResult<DB, T extends keyof DB & string, R extends Partial<Selectable<DB[T]>> = Selectable<DB[T]>> = {
|
50
53
|
fetchType: 'fetchSome';
|
51
54
|
table: T;
|
52
55
|
result: R[];
|
53
|
-
models
|
56
|
+
models: ModelCollection<DB>;
|
54
57
|
};
|
55
58
|
/**
|
56
59
|
* Used to type juggle between {@link FetchResult} and its subtypes.
|
@@ -0,0 +1,69 @@
|
|
1
|
+
import { type FkGatherOneResult, type FkGatherOneXResult, type FkGatherSomeResult } from './fk-gather-result.js';
|
2
|
+
import type { DBWithFk, ModelFkExtractSelectable, ModelFkInstance, ValidFkDepth } from './fks.js';
|
3
|
+
import type { KyselyRizzolverFKs } from './kysely-rizzolver.js';
|
4
|
+
import { type ModelCollection } from './model-collection.js';
|
5
|
+
/**
|
6
|
+
* A {@link FkGatherResultFactory} exposes variants of gather-related functions, but
|
7
|
+
* with the `DB` type parameter already set.
|
8
|
+
*
|
9
|
+
* This makes it less verbose to work with gather results in a type-safe way.
|
10
|
+
*/
|
11
|
+
export type FkGatherResultFactory<DB, FKDefs extends KyselyRizzolverFKs<DB>> = ReturnType<typeof newFkGatherResultFactory<DB, FKDefs>>;
|
12
|
+
export declare function newFkGatherResultFactory<DB, FKDefs extends KyselyRizzolverFKs<DB>>(): {
|
13
|
+
/**
|
14
|
+
* Creates a new {@link GatherOneResult} instance.
|
15
|
+
*/
|
16
|
+
newGatherOne<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: ModelFkInstance<DBWithFk<DB, FKDefs>, K, Depth> | undefined, models?: ModelCollection<DB>): FkGatherOneResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
17
|
+
/**
|
18
|
+
* Creates a new {@link GatherOneXResult} instance.
|
19
|
+
*
|
20
|
+
* Note: it may be counterintuitive, but this function accepts `undefined` as
|
21
|
+
* input. I found it is way more convenient to assert the type once in this
|
22
|
+
* funciton rather than in every caller.
|
23
|
+
*/
|
24
|
+
newGatherOneX<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: ModelFkInstance<DBWithFk<DB, FKDefs>, K, Depth> | undefined, models?: ModelCollection<DB>): FkGatherOneXResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
25
|
+
/**
|
26
|
+
* Creates a new {@link GatherSomeResult} instance.
|
27
|
+
*/
|
28
|
+
newGatherSome<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: ModelFkInstance<DBWithFk<DB, FKDefs>, K, Depth>[], models?: ModelCollection<DB>): FkGatherSomeResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
29
|
+
/**
|
30
|
+
* Checks if a {@link GatherResult} is a {@link GatherOneResult}.
|
31
|
+
*/
|
32
|
+
isGatherOne<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): result is FkGatherOneResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
33
|
+
/**
|
34
|
+
* Checks if a {@link GatherResult} is a {@link GatherOneXResult}.
|
35
|
+
*/
|
36
|
+
isGatherOneX<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): result is FkGatherOneXResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
37
|
+
/**
|
38
|
+
* Checks if a {@link GatherResult} is a {@link GatherSomeResult}.
|
39
|
+
*/
|
40
|
+
isGatherSome<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): result is FkGatherSomeResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
41
|
+
/**
|
42
|
+
* Asserts that a {@link GatherResult} is a {@link GatherOneResult}.
|
43
|
+
*/
|
44
|
+
assertIsGatherOne<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): asserts result is FkGatherOneResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
45
|
+
/**
|
46
|
+
* Asserts that a {@link GatherResult} is a {@link GatherOneXResult}.
|
47
|
+
*/
|
48
|
+
assertIsGatherOneX<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): asserts result is FkGatherOneXResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
49
|
+
/**
|
50
|
+
* Asserts that a {@link GatherResult} is a {@link GatherSomeResult}.
|
51
|
+
*/
|
52
|
+
assertIsGatherSome<K extends (keyof DB & string) & string, Depth extends ValidFkDepth>(table: K, depth: Depth, result: unknown): asserts result is FkGatherSomeResult<DBWithFk<DB, FKDefs>, K, Depth>;
|
53
|
+
/**
|
54
|
+
* Extracts a Kysely {@link Selectable} instance from a gathered model.
|
55
|
+
*
|
56
|
+
* This is useful because Selectables are the lowest common denominators
|
57
|
+
* for any representation of a DB model, so this allows you to convert
|
58
|
+
* any gather result to anything else that might build upon Kysely.
|
59
|
+
*
|
60
|
+
* For example, to convert a gather result to a fetch result:
|
61
|
+
* ```
|
62
|
+
* const userGather: FkGatherOneXResult<DB, 'user'>;
|
63
|
+
* const userModel = userGather.result;
|
64
|
+
* const userSelectable = rizzolver.gatherObjs.toSelectable(userModel);
|
65
|
+
* ```
|
66
|
+
*
|
67
|
+
*/
|
68
|
+
toSelectable<Model>(model: Model): ModelFkExtractSelectable<DB, Model>;
|
69
|
+
};
|
@@ -0,0 +1,115 @@
|
|
1
|
+
import type { DBWithoutFk, ModelFkInstance, ValidFkDepth } from './fks.js';
|
2
|
+
import { MAX_FK_GATHER_DEPTH } from './fks.js';
|
3
|
+
import type { ModelCollection } from './model-collection.js';
|
4
|
+
/**
|
5
|
+
* A {@link FkGatherResult} is a result of a gather operation. It can be one of
|
6
|
+
* three types:
|
7
|
+
* - {@link FkGatherOneResult} - A result of a gather operation that is expected to
|
8
|
+
* return up to one row,
|
9
|
+
* - {@link FkGatherOneXResult} - A result of a gather operation that is expected to
|
10
|
+
* return exactly one row,
|
11
|
+
* - {@link FkGatherSomeResult} - A result of a gather operation that is expected to
|
12
|
+
* return any number of rows.
|
13
|
+
*/
|
14
|
+
export type FkGatherResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = FkGatherOneResult<DBFk, Table, Depth> | FkGatherOneXResult<DBFk, Table, Depth> | FkGatherSomeResult<DBFk, Table, Depth>;
|
15
|
+
/**
|
16
|
+
* A {@link FkGatherOneResult} is a result of a gather operation that is expected to
|
17
|
+
* return up to one row.
|
18
|
+
*/
|
19
|
+
export type FkGatherOneResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = {
|
20
|
+
gatherType: 'gatherOne';
|
21
|
+
table: Table;
|
22
|
+
depth: Depth;
|
23
|
+
result: ModelFkInstance<DBFk, Table, Depth> | undefined;
|
24
|
+
models?: ModelCollection<DBWithoutFk<DBFk>>;
|
25
|
+
/**
|
26
|
+
* Returns this result as a {@link FkGatherOneXResult}.
|
27
|
+
*
|
28
|
+
* @throws If the result is null or undefined.
|
29
|
+
*/
|
30
|
+
asGatherOneX(): FkGatherOneXResult<DBFk, Table, Depth>;
|
31
|
+
};
|
32
|
+
/**
|
33
|
+
* A {@link FkGatherOneXResult} is a result of a gather operation that is expected
|
34
|
+
* to return exactly one row.
|
35
|
+
*/
|
36
|
+
export type FkGatherOneXResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = {
|
37
|
+
gatherType: 'gatherOne';
|
38
|
+
table: Table;
|
39
|
+
depth: Depth;
|
40
|
+
result: ModelFkInstance<DBFk, Table, Depth>;
|
41
|
+
models?: ModelCollection<DBWithoutFk<DBFk>>;
|
42
|
+
/**
|
43
|
+
* Returns self. This is a no-op, but it's here to make it possible to
|
44
|
+
* cast this object back to a {@link FkGatherOneXResult}.
|
45
|
+
*/
|
46
|
+
asGatherOneX(): FkGatherOneXResult<DBFk, Table, Depth>;
|
47
|
+
};
|
48
|
+
/**
|
49
|
+
* A {@link FkGatherSomeResult} is a result of a gather operation that is expected
|
50
|
+
* to return any number of rows.
|
51
|
+
*/
|
52
|
+
export type FkGatherSomeResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = {
|
53
|
+
gatherType: 'gatherSome';
|
54
|
+
table: Table;
|
55
|
+
depth: Depth;
|
56
|
+
result: ModelFkInstance<DBFk, Table, Depth>[];
|
57
|
+
models?: ModelCollection<DBWithoutFk<DBFk>>;
|
58
|
+
};
|
59
|
+
/**
|
60
|
+
* Used to type juggle between {@link FkGatherResult} and its subtypes.
|
61
|
+
*/
|
62
|
+
export type AsGatherOneResult<T extends FkGatherResult<any, string, ValidFkDepth>> = T extends FkGatherResult<infer DBFk, infer Table, infer D> ? FkGatherOneResult<DBFk, Table, D> : never;
|
63
|
+
/**
|
64
|
+
* Used to type juggle between {@link FkGatherResult} and its subtypes.
|
65
|
+
*/
|
66
|
+
export type AsGatherOneXResult<T extends FkGatherResult<any, string, ValidFkDepth>> = T extends FkGatherResult<infer DBFk, infer Table, infer D> ? FkGatherOneXResult<DBFk, Table, D> : never;
|
67
|
+
/**
|
68
|
+
* Used to type juggle between {@link FkGatherResult} and its subtypes.
|
69
|
+
*/
|
70
|
+
export type AsGatherSomeResult<T extends FkGatherResult<any, string, ValidFkDepth>> = T extends FkGatherResult<infer DBFk, infer Table, infer D> ? FkGatherSomeResult<DBFk, Table, D> : never;
|
71
|
+
/**
|
72
|
+
* Creates a new {@link FkGatherOneResult} instance.
|
73
|
+
*/
|
74
|
+
export declare function newGatherOneResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(table: Table, depth: Depth, result: ModelFkInstance<DBFk, Table, Depth> | undefined, models?: ModelCollection<DBWithoutFk<DBFk>>): FkGatherOneResult<DBFk, Table, Depth>;
|
75
|
+
/**
|
76
|
+
* Creates a new {@link FkGatherOneXResult} instance.
|
77
|
+
*
|
78
|
+
* Note: it may be counterintuitive, but this function accepts `undefined` as
|
79
|
+
* input. I found it is way more convenient to assert the type once in this
|
80
|
+
* funciton rather than in every caller.
|
81
|
+
*/
|
82
|
+
export declare function newGatherOneXResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(table: Table, depth: Depth, result: ModelFkInstance<DBFk, Table, Depth> | undefined, models?: ModelCollection<DBWithoutFk<DBFk>>): FkGatherOneXResult<DBFk, Table, Depth>;
|
83
|
+
/**
|
84
|
+
* Creates a new {@link FkGatherSomeResult} instance.
|
85
|
+
*/
|
86
|
+
export declare function newGatherSomeResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(table: Table, depth: Depth, result: ModelFkInstance<DBFk, Table, Depth>[], models?: ModelCollection<DBWithoutFk<DBFk>>): FkGatherSomeResult<DBFk, Table, Depth>;
|
87
|
+
export declare function isGatherResult(result: unknown): result is FkGatherResult<any, any, ValidFkDepth>;
|
88
|
+
/**
|
89
|
+
* Checks if `value` is a {@link FkGatherOneResult}.
|
90
|
+
*/
|
91
|
+
export declare function isGatherOneResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(value: FkGatherOneResult<DBFk, Table, Depth> | FkGatherOneXResult<DBFk, Table, Depth>): value is typeof value;
|
92
|
+
export declare function isGatherOneResult(value: unknown): value is FkGatherOneResult<any, any, ValidFkDepth>;
|
93
|
+
/**
|
94
|
+
* Checks if `value` is a {@link FkGatherOneXResult}.
|
95
|
+
*/
|
96
|
+
export declare function isGatherOneXResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(value: FkGatherOneResult<DBFk, Table, Depth>): value is FkGatherOneXResult<DBFk, Table, Depth>;
|
97
|
+
export declare function isGatherOneXResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(value: FkGatherOneXResult<DBFk, Table, Depth>): value is typeof value;
|
98
|
+
export declare function isGatherOneXResult(value: unknown): value is FkGatherOneXResult<any, any, ValidFkDepth>;
|
99
|
+
/**
|
100
|
+
* Checks if `value` is a {@link FkGatherSomeResult}.
|
101
|
+
*/
|
102
|
+
export declare function isGatherSomeResult<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth>(value: FkGatherSomeResult<DBFk, Table, Depth>): value is typeof value;
|
103
|
+
export declare function isGatherSomeResult(value: unknown): value is FkGatherSomeResult<any, any, ValidFkDepth>;
|
104
|
+
/**
|
105
|
+
* Asserts that `value` is a {@link FkGatherOneResult}.
|
106
|
+
*/
|
107
|
+
export declare function assertIsGatherOneResult(value: unknown): asserts value is FkGatherOneResult<any, any, ValidFkDepth>;
|
108
|
+
/**
|
109
|
+
* Asserts that `value` is a {@link FkGatherOneXResult}.
|
110
|
+
*/
|
111
|
+
export declare function assertIsGatherOneXResult(value: unknown): asserts value is FkGatherOneXResult<any, any, ValidFkDepth>;
|
112
|
+
/**
|
113
|
+
* Asserts that `value` is a {@link FkGatherSomeResult}.
|
114
|
+
*/
|
115
|
+
export declare function assertIsGatherSomeResult(value: unknown): asserts value is FkGatherSomeResult<any, any, ValidFkDepth>;
|
@@ -0,0 +1,111 @@
|
|
1
|
+
import type { ExpressionBuilder, Kysely, OperandExpression, Selectable, SelectType } from 'kysely';
|
2
|
+
import type { FKDefsEntry } from './kysely-rizzolver-fk-builder.js';
|
3
|
+
import { type KyselyRizzolverBase, type KyselyRizzolverFKs } from './kysely-rizzolver.js';
|
4
|
+
import type { ModelCollection } from './model-collection.js';
|
5
|
+
import type { OmitByValue } from './type-helpers.js';
|
6
|
+
/**
|
7
|
+
* The maximum depth to gather FKs in a single gather operation.
|
8
|
+
*
|
9
|
+
* If you change this, you must also change {@link NextDepth}.
|
10
|
+
*/
|
11
|
+
export declare const MAX_FK_GATHER_DEPTH = 3;
|
12
|
+
/**
|
13
|
+
* For each supported gather depth, assigns the value depth - 1. This is used to recursively construct the types for {@link ModelFkInstance}.
|
14
|
+
*
|
15
|
+
* If you change this, you must also change {@link MAX_FK_GATHER_DEPTH}.
|
16
|
+
*/
|
17
|
+
type NextDepth = {
|
18
|
+
3: 2;
|
19
|
+
2: 1;
|
20
|
+
1: 0;
|
21
|
+
};
|
22
|
+
/**
|
23
|
+
* The depth at which FKs can be gathered.
|
24
|
+
*
|
25
|
+
* A depth of 0 = bare model only. This is equivalent to a fetch without gathers.
|
26
|
+
*/
|
27
|
+
export type ValidFkDepth = (keyof NextDepth & number) | 0;
|
28
|
+
type Fk<DB, Referencing extends keyof DB & string, Referenced extends keyof DB & string, IsNullable extends boolean> = {
|
29
|
+
myTable: Referencing;
|
30
|
+
myField: string;
|
31
|
+
otherTable: Referenced;
|
32
|
+
otherField: string;
|
33
|
+
isNullable: IsNullable;
|
34
|
+
};
|
35
|
+
/**
|
36
|
+
* Omit FK properties from `T`.
|
37
|
+
*/
|
38
|
+
export type OmitFks<T> = OmitByValue<T, Fk<any, any, any, any>>;
|
39
|
+
export declare class ModelFkGatherError extends Error {
|
40
|
+
constructor(table: string, id: number, reason: string);
|
41
|
+
}
|
42
|
+
/**
|
43
|
+
* A model whose FK fields have not been gathered.
|
44
|
+
*/
|
45
|
+
export type ModelFkBare<DBFk, Table extends keyof DBFk & string> = {
|
46
|
+
__fkDepth: 0;
|
47
|
+
__table: Table;
|
48
|
+
} & Selectable<OmitFks<DBFk[Table]>>;
|
49
|
+
/**
|
50
|
+
* A model whose FKs fields have been gathered up to a specific depth.
|
51
|
+
*/
|
52
|
+
export type ModelFkGathered<DBFk, Table extends keyof DBFk & string, Depth extends Exclude<ValidFkDepth, 0>> = {
|
53
|
+
__fkDepth: Depth;
|
54
|
+
__table: Table;
|
55
|
+
} & {
|
56
|
+
[k in keyof DBFk[Table]]: DBFk[Table][k] extends infer U ? U extends Fk<any, any, infer UU, infer NN> ? MaybeNullable<ModelFkInstance<DBFk, UU & keyof DBFk, NextDepth[Depth]>, NN> : SelectType<U> : never;
|
57
|
+
};
|
58
|
+
export type ModelFkInstance<DBFk, Table extends keyof DBFk & string, Depth extends ValidFkDepth> = Depth extends 0 ? ModelFkBare<DBFk, Table> : ModelFkGathered<DBFk, Table, Exclude<Depth, 0>>;
|
59
|
+
type MaybeNullable<T, Nullable extends boolean> = Nullable extends true ? T | null : T;
|
60
|
+
export type ModelFkExtractSelectable<DB, Model> = Model extends ModelFkInstance<any, infer Table, any> ? Table extends keyof DB & string ? Selectable<DB[Table]> : never : Model extends undefined ? undefined : Model extends null ? null : never;
|
61
|
+
/**
|
62
|
+
* A database schema `DB` with FK fields populated according to `FKDefs`.
|
63
|
+
*
|
64
|
+
* `FKDefs` is of type {@link KyselyRizzolverFKs}.
|
65
|
+
*/
|
66
|
+
export type DBWithFk<DB, FKDefs extends KyselyRizzolverFKs<DB>> = {
|
67
|
+
[K in keyof DB & string]: DB[K] & {
|
68
|
+
[F in keyof FKDefs[K] & string]: FKDefs[K][F] extends infer U ? U extends FKDefsEntry<DB, any, any, any, any> ? Fk<DB, K, U['otherTable'], U['isNullable']> : never : never;
|
69
|
+
};
|
70
|
+
};
|
71
|
+
/**
|
72
|
+
* Given a database schema with FKs `DBFK`, returns a database schema without
|
73
|
+
* FKs.
|
74
|
+
*/
|
75
|
+
export type DBWithoutFk<DBFK> = {
|
76
|
+
[k in keyof DBFK & string]: OmitFks<DBFK[k]>;
|
77
|
+
};
|
78
|
+
export type GatherOpts<DB, Depth extends ValidFkDepth> = {
|
79
|
+
/**
|
80
|
+
* The depth to gather FKs to. Default: {@link MAX_FK_GATHER_DEPTH}.
|
81
|
+
*/
|
82
|
+
depth?: Depth;
|
83
|
+
/**
|
84
|
+
* When an invalid model is found, the gather function can either:
|
85
|
+
* - `throw` - throws a {@link ModelFkGatherError}.
|
86
|
+
* - `omit` - omits the model from the result array.
|
87
|
+
* - `null` - puts `null` values in the result array. This is useful if you
|
88
|
+
* want to match the input array to the output array by index while still
|
89
|
+
* indicating that a model is invalid.
|
90
|
+
* - `keep` - keeps the invalid model in the result array.
|
91
|
+
*
|
92
|
+
* An invalid model is a model that has an FK field that is unexpectedly
|
93
|
+
* missing.
|
94
|
+
*
|
95
|
+
* For example, if the model has a `user_id` field whose value is 3 but
|
96
|
+
* there is no user with ID 3 in the database, the model is invalid.
|
97
|
+
*
|
98
|
+
* Default: `omit`
|
99
|
+
*/
|
100
|
+
onInvalidModel?: 'omit' | 'throw' | 'null' | 'keep';
|
101
|
+
/**
|
102
|
+
* If provided, will populate this collection with the gathered models.
|
103
|
+
*/
|
104
|
+
modelCollection?: ModelCollection<DB>;
|
105
|
+
};
|
106
|
+
export type NoNullGatherOpts<DB, Depth extends ValidFkDepth> = Omit<GatherOpts<DB, Depth>, 'onInvalidModel'> & {
|
107
|
+
onInvalidModel?: 'omit' | 'throw' | 'keep';
|
108
|
+
};
|
109
|
+
export type GatherWhereExpression<DB, Table extends keyof DB & string> = (eb: ExpressionBuilder<Pick<DB, Table>, Table>) => OperandExpression<any>;
|
110
|
+
export declare function gatherModelFks<DB, FKDefs extends KyselyRizzolverFKs<DB>, KY extends KyselyRizzolverBase<DB, Record<keyof DB & string, any>, FKDefs>, Table extends keyof DB & string, Depth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH>(rizzolver: KY, dbInstance: Kysely<DB>, table: Table, where: GatherWhereExpression<DB, Table>, opts?: GatherOpts<DB, Depth>): Promise<(ModelFkInstance<DBWithFk<DB, FKDefs>, Table, Depth> | null)[]>;
|
111
|
+
export {};
|
package/dist/types/index.d.ts
CHANGED
@@ -1,10 +1,16 @@
|
|
1
1
|
export { KyselyRizzolver } from './kysely-rizzolver.js';
|
2
|
-
export type { AllTableFields,
|
3
|
-
export { assertIsFetchOneResult, assertIsFetchOneXResult, assertIsFetchSomeResult, isFetchOneResult, isFetchOneXResult, isFetchSomeResult, newFetchOneResult, newFetchOneXResult, newFetchSomeResult } from './fetch-result.js';
|
2
|
+
export type { KyAllTableFields as AllTableFields, KyAnyTableField as AnyTableField, KyDB as RizzolverDB, KyTableName as TableName } from './kysely-rizzolver.js';
|
3
|
+
export { assertIsFetchOneResult, assertIsFetchOneXResult, assertIsFetchSomeResult, isFetchOneResult, isFetchOneXResult, isFetchResult, isFetchSomeResult, newFetchOneResult, newFetchOneXResult, newFetchSomeResult } from './fetch-result.js';
|
4
4
|
export type { AsFetchOneResult, AsFetchOneXResult, AsFetchSomeResult, FetchOneResult, FetchOneXResult, FetchResult, FetchSomeResult } from './fetch-result.js';
|
5
|
+
export { assertIsGatherOneResult, assertIsGatherOneXResult, assertIsGatherSomeResult, isGatherOneResult, isGatherOneXResult, isGatherResult, isGatherSomeResult, newGatherOneResult, newGatherOneXResult, newGatherSomeResult } from './fk-gather-result.js';
|
6
|
+
export type { AsGatherOneResult, AsGatherOneXResult, AsGatherSomeResult, FkGatherOneResult, FkGatherOneXResult, FkGatherResult, FkGatherSomeResult } from './fk-gather-result.js';
|
7
|
+
export { MAX_FK_GATHER_DEPTH, ModelFkGatherError } from './fks.js';
|
8
|
+
export type { ModelFkBare, ModelFkGathered, ModelFkInstance, ValidFkDepth, GatherWhereExpression } from './fks.js';
|
9
|
+
export type { KyselyRizzolverBuilder } from './kysely-rizzolver-builder.js';
|
10
|
+
export type { KyselyRizzolverFkBuilder } from './kysely-rizzolver-fk-builder.js';
|
5
11
|
export { newSelector } from './selector.js';
|
6
|
-
export type { Selector } from './selector.js';
|
12
|
+
export type { AnySelector, Selector } from './selector.js';
|
7
13
|
export { newQueryBuilder } from './query-builder.js';
|
8
|
-
export type { QueryBuilder } from './query-builder.js';
|
14
|
+
export type { AnyQueryBuilder, QueryBuilder } from './query-builder.js';
|
9
15
|
export { newModelCollection } from './model-collection.js';
|
10
16
|
export type { ModelCollection } from './model-collection.js';
|
@@ -0,0 +1,17 @@
|
|
1
|
+
import type { Simplify } from 'kysely';
|
2
|
+
import { MAX_FK_GATHER_DEPTH, type ValidFkDepth } from './fks.js';
|
3
|
+
import { type FkDefsBuilderCallback } from './kysely-rizzolver-fk-builder.js';
|
4
|
+
import { KyselyRizzolver, type KyselyRizzolverFKs } from './kysely-rizzolver.js';
|
5
|
+
export type KyselyRizzolverBuilder<DB, T extends Partial<Record<keyof DB & string, readonly string[]>>, DefaultGatherDepth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = T extends Record<keyof DB & string, readonly string[]> ? _KyselyRizzolverBuilder_Valid<DB, T, DefaultGatherDepth> : _KyselyRizzolverBuilder_InProgress<DB, T, DefaultGatherDepth>;
|
6
|
+
type _KyselyRizzolverBuilder_Valid<DB, T extends Record<keyof DB & string, readonly string[]>, DefaultGatherDepth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = {
|
7
|
+
defaultGatherDepth<NewDepth extends ValidFkDepth>(depth: NewDepth): _KyselyRizzolverBuilder_Valid<DB, T, NewDepth>;
|
8
|
+
build<FKDefs extends KyselyRizzolverFKs<DB>>(fkBuilderCb: FkDefsBuilderCallback<DB, FKDefs>): KyselyRizzolver<DB, T, Simplify<FKDefs>, DefaultGatherDepth>;
|
9
|
+
build(): KyselyRizzolver<DB, T, {}, DefaultGatherDepth>;
|
10
|
+
};
|
11
|
+
type _KyselyRizzolverBuilder_InProgress<DB, T extends Partial<Record<keyof DB & string, readonly string[]>>, DefaultGatherDepth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH> = {
|
12
|
+
table<K extends keyof DB & string, U extends readonly (keyof DB[K])[]>(name: K, fields: U & ([keyof DB[K]] extends [U[number]] ? unknown : `Missing key: ${Exclude<keyof DB[K] & string, U[number]>}`)): KyselyRizzolverBuilder<DB, T & {
|
13
|
+
[key in K]: U;
|
14
|
+
}, DefaultGatherDepth>;
|
15
|
+
};
|
16
|
+
export declare function newKyselyRizzolverBuilder<DB, T extends Partial<Record<keyof DB & string, readonly string[]>>, DefaultGatherDepth extends ValidFkDepth = typeof MAX_FK_GATHER_DEPTH>(fields: T, defaultGatherDepth?: DefaultGatherDepth): KyselyRizzolverBuilder<DB, T>;
|
17
|
+
export {};
|
@@ -0,0 +1,20 @@
|
|
1
|
+
import { KyselyRizzolverFKs } from './kysely-rizzolver.js';
|
2
|
+
import { NumericFields } from './type-helpers.js';
|
3
|
+
export type FkDefsBuilderCallback<DB, T> = (builder: KyselyRizzolverFkBuilder<DB, {}>) => T;
|
4
|
+
export type KyselyRizzolverFkBuilder<DB, FKDefs extends KyselyRizzolverFKs<DB> = {}> = {
|
5
|
+
add<FromTable extends keyof DB & string, FromField extends Exclude<NumericFields<DB[FromTable]> & string, 'id'>, ToTable extends keyof DB & string, ToField extends NumericFields<DB[ToTable]> & string, FkName extends string, Nullable extends boolean = false>(fromTable: FromTable, fromField: FromField, toTable: ToTable, toField: ToField, fkName: FkName, nullable?: Nullable): KyselyRizzolverFkBuilder<DB, FKDefsAddEntry<DB, FKDefs, FromTable, FromField, ToTable, ToField, FkName, Nullable>>;
|
6
|
+
build(): FKDefs;
|
7
|
+
};
|
8
|
+
type FKDefsAddEntry<DB, FKDefs extends KyselyRizzolverFKs<DB>, FromTable extends keyof DB & string, FromField extends string, ToTable extends keyof DB & string, ToField extends string, FkName extends string, Nullable extends boolean> = FKDefs & {
|
9
|
+
[K in FromTable]: {
|
10
|
+
[N in FkName]: FKDefsEntry<DB, FromField, ToTable, ToField, Nullable>;
|
11
|
+
};
|
12
|
+
};
|
13
|
+
export type FKDefsEntry<DB, FromField extends string, ToTable extends keyof DB & string, ToField extends string, Nullable extends boolean> = {
|
14
|
+
myField: FromField;
|
15
|
+
otherTable: ToTable;
|
16
|
+
otherField: ToField;
|
17
|
+
isNullable: Nullable;
|
18
|
+
};
|
19
|
+
export declare function newKyselyRizzolverFkBuilder<DB, FKDefs extends KyselyRizzolverFKs<DB> = {}>(fkDefs: FKDefs): KyselyRizzolverFkBuilder<DB, FKDefs>;
|
20
|
+
export {};
|