pqb 0.0.1
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/dist/index.d.ts +3630 -0
- package/dist/index.esm.js +4587 -0
- package/dist/index.esm.js.map +1 -0
- package/dist/index.js +4691 -0
- package/dist/index.js.map +1 -0
- package/package.json +59 -0
- package/rollup.config.js +35 -0
- package/src/adapter.test.ts +10 -0
- package/src/adapter.ts +171 -0
- package/src/columnSchema/array.ts +21 -0
- package/src/columnSchema/boolean.ts +10 -0
- package/src/columnSchema/columnType.test.ts +129 -0
- package/src/columnSchema/columnType.ts +77 -0
- package/src/columnSchema/columnTypes.ts +145 -0
- package/src/columnSchema/columnsSchema.test.ts +32 -0
- package/src/columnSchema/columnsSchema.ts +100 -0
- package/src/columnSchema/commonMethods.ts +130 -0
- package/src/columnSchema/dateTime.ts +104 -0
- package/src/columnSchema/enum.ts +13 -0
- package/src/columnSchema/index.ts +11 -0
- package/src/columnSchema/json/array.ts +55 -0
- package/src/columnSchema/json/discriminatedUnion.ts +91 -0
- package/src/columnSchema/json/enum.ts +29 -0
- package/src/columnSchema/json/instanceOf.ts +16 -0
- package/src/columnSchema/json/intersection.ts +23 -0
- package/src/columnSchema/json/lazy.ts +22 -0
- package/src/columnSchema/json/literal.ts +12 -0
- package/src/columnSchema/json/map.ts +29 -0
- package/src/columnSchema/json/nativeEnum.ts +30 -0
- package/src/columnSchema/json/nullable.ts +33 -0
- package/src/columnSchema/json/nullish.ts +30 -0
- package/src/columnSchema/json/object.ts +206 -0
- package/src/columnSchema/json/optional.ts +28 -0
- package/src/columnSchema/json/record.ts +40 -0
- package/src/columnSchema/json/scalarTypes.ts +117 -0
- package/src/columnSchema/json/set.ts +34 -0
- package/src/columnSchema/json/tuple.ts +40 -0
- package/src/columnSchema/json/typeBase.ts +202 -0
- package/src/columnSchema/json/union.ts +16 -0
- package/src/columnSchema/json.ts +64 -0
- package/src/columnSchema/number.ts +122 -0
- package/src/columnSchema/string.ts +222 -0
- package/src/columnSchema/utils.ts +27 -0
- package/src/common.ts +86 -0
- package/src/db.test.ts +67 -0
- package/src/db.ts +212 -0
- package/src/errors.ts +7 -0
- package/src/index.ts +18 -0
- package/src/operators.test.ts +608 -0
- package/src/operators.ts +177 -0
- package/src/query.ts +292 -0
- package/src/queryDataUtils.ts +50 -0
- package/src/queryMethods/aggregate.test.ts +583 -0
- package/src/queryMethods/aggregate.ts +878 -0
- package/src/queryMethods/callbacks.test.ts +69 -0
- package/src/queryMethods/callbacks.ts +55 -0
- package/src/queryMethods/clear.test.ts +64 -0
- package/src/queryMethods/clear.ts +58 -0
- package/src/queryMethods/columnInfo.test.ts +45 -0
- package/src/queryMethods/columnInfo.ts +67 -0
- package/src/queryMethods/delete.test.ts +135 -0
- package/src/queryMethods/delete.ts +50 -0
- package/src/queryMethods/for.test.ts +57 -0
- package/src/queryMethods/for.ts +99 -0
- package/src/queryMethods/from.test.ts +66 -0
- package/src/queryMethods/from.ts +58 -0
- package/src/queryMethods/get.test.ts +66 -0
- package/src/queryMethods/get.ts +88 -0
- package/src/queryMethods/having.test.ts +247 -0
- package/src/queryMethods/having.ts +99 -0
- package/src/queryMethods/insert.test.ts +555 -0
- package/src/queryMethods/insert.ts +453 -0
- package/src/queryMethods/join.test.ts +150 -0
- package/src/queryMethods/join.ts +508 -0
- package/src/queryMethods/json.test.ts +398 -0
- package/src/queryMethods/json.ts +259 -0
- package/src/queryMethods/log.test.ts +172 -0
- package/src/queryMethods/log.ts +123 -0
- package/src/queryMethods/queryMethods.test.ts +629 -0
- package/src/queryMethods/queryMethods.ts +428 -0
- package/src/queryMethods/select.test.ts +479 -0
- package/src/queryMethods/select.ts +249 -0
- package/src/queryMethods/then.ts +236 -0
- package/src/queryMethods/transaction.test.ts +66 -0
- package/src/queryMethods/transaction.ts +66 -0
- package/src/queryMethods/union.test.ts +59 -0
- package/src/queryMethods/union.ts +89 -0
- package/src/queryMethods/update.test.ts +417 -0
- package/src/queryMethods/update.ts +350 -0
- package/src/queryMethods/upsert.test.ts +56 -0
- package/src/queryMethods/upsert.ts +43 -0
- package/src/queryMethods/where.test.ts +1594 -0
- package/src/queryMethods/where.ts +450 -0
- package/src/queryMethods/window.test.ts +66 -0
- package/src/queryMethods/window.ts +108 -0
- package/src/queryMethods/with.test.ts +191 -0
- package/src/queryMethods/with.ts +92 -0
- package/src/quote.ts +36 -0
- package/src/relations.ts +194 -0
- package/src/sql/aggregate.ts +80 -0
- package/src/sql/columnInfo.ts +22 -0
- package/src/sql/common.ts +42 -0
- package/src/sql/delete.ts +41 -0
- package/src/sql/distinct.ts +19 -0
- package/src/sql/fromAndAs.ts +51 -0
- package/src/sql/having.ts +140 -0
- package/src/sql/index.ts +2 -0
- package/src/sql/insert.ts +102 -0
- package/src/sql/join.ts +242 -0
- package/src/sql/orderBy.ts +41 -0
- package/src/sql/select.ts +153 -0
- package/src/sql/toSql.ts +153 -0
- package/src/sql/truncate.ts +13 -0
- package/src/sql/types.ts +355 -0
- package/src/sql/update.ts +62 -0
- package/src/sql/where.ts +314 -0
- package/src/sql/window.ts +38 -0
- package/src/sql/with.ts +32 -0
- package/src/test-utils.ts +172 -0
- package/src/utils.ts +140 -0
- package/tsconfig.build.json +6 -0
- package/tsconfig.json +8 -0
package/src/operators.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import { Query } from './query';
|
|
2
|
+
import { getRaw, isRaw, RawExpression } from './common';
|
|
3
|
+
import { quote } from './quote';
|
|
4
|
+
import { addValue } from './sql/common';
|
|
5
|
+
|
|
6
|
+
type Fn<T> = (key: string, value: T, values: unknown[]) => string;
|
|
7
|
+
|
|
8
|
+
export type Operator<T> = Fn<T> & { type: T };
|
|
9
|
+
|
|
10
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
11
|
+
export type Operators = Record<string, Operator<any>>;
|
|
12
|
+
|
|
13
|
+
export const createOperator = <T>(fn: Fn<T>) => {
|
|
14
|
+
return Object.assign(fn, { type: undefined as unknown as T });
|
|
15
|
+
};
|
|
16
|
+
|
|
17
|
+
const quoteValue = (
|
|
18
|
+
arg: unknown,
|
|
19
|
+
values: unknown[],
|
|
20
|
+
jsonArray?: boolean,
|
|
21
|
+
): string => {
|
|
22
|
+
if (arg && typeof arg === 'object') {
|
|
23
|
+
if (!jsonArray && Array.isArray(arg)) {
|
|
24
|
+
return `(${arg.map((value) => addValue(values, value)).join(', ')})`;
|
|
25
|
+
}
|
|
26
|
+
|
|
27
|
+
if ('toSql' in arg) {
|
|
28
|
+
const sql = (arg as Query).toSql(values);
|
|
29
|
+
return `(${sql.text})`;
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
if (isRaw(arg)) {
|
|
33
|
+
return getRaw(arg, values);
|
|
34
|
+
}
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
return addValue(values, arg);
|
|
38
|
+
};
|
|
39
|
+
|
|
40
|
+
const all = {
|
|
41
|
+
equals: <T>() =>
|
|
42
|
+
createOperator<T | Query | RawExpression>((key, value, values) =>
|
|
43
|
+
value === null
|
|
44
|
+
? `${key} IS NULL`
|
|
45
|
+
: `${key} = ${quoteValue(value, values)}`,
|
|
46
|
+
),
|
|
47
|
+
not: <T>() =>
|
|
48
|
+
createOperator<T | Query | RawExpression>((key, value, values) =>
|
|
49
|
+
value === null
|
|
50
|
+
? `${key} IS NOT NULL`
|
|
51
|
+
: `${key} <> ${quoteValue(value, values)}`,
|
|
52
|
+
),
|
|
53
|
+
in: <T>() =>
|
|
54
|
+
createOperator<T[] | Query | RawExpression>(
|
|
55
|
+
(key, value, values) => `${key} IN ${quoteValue(value, values)}`,
|
|
56
|
+
),
|
|
57
|
+
notIn: <T>() =>
|
|
58
|
+
createOperator<T[] | Query | RawExpression>(
|
|
59
|
+
(key, value, values) => `NOT ${key} IN ${quoteValue(value, values)}`,
|
|
60
|
+
),
|
|
61
|
+
lt: <T>() =>
|
|
62
|
+
createOperator<T | Query | RawExpression>(
|
|
63
|
+
(key, value, values) => `${key} < ${quoteValue(value, values)}`,
|
|
64
|
+
),
|
|
65
|
+
lte: <T>() =>
|
|
66
|
+
createOperator<T | Query | RawExpression>(
|
|
67
|
+
(key, value, values) => `${key} <= ${quoteValue(value, values)}`,
|
|
68
|
+
),
|
|
69
|
+
gt: <T>() =>
|
|
70
|
+
createOperator<T | Query | RawExpression>(
|
|
71
|
+
(key, value, values) => `${key} > ${quoteValue(value, values)}`,
|
|
72
|
+
),
|
|
73
|
+
gte: <T>() =>
|
|
74
|
+
createOperator<T | Query | RawExpression>(
|
|
75
|
+
(key, value, values) => `${key} >= ${quoteValue(value, values)}`,
|
|
76
|
+
),
|
|
77
|
+
contains: <T>() =>
|
|
78
|
+
createOperator<T | Query | RawExpression>(
|
|
79
|
+
(key, value, values) =>
|
|
80
|
+
`${key} LIKE '%' || ${quoteValue(value, values)} || '%'`,
|
|
81
|
+
),
|
|
82
|
+
containsInsensitive: <T>() =>
|
|
83
|
+
createOperator<T | Query | RawExpression>(
|
|
84
|
+
(key, value, values) =>
|
|
85
|
+
`${key} ILIKE '%' || ${quoteValue(value, values)} || '%'`,
|
|
86
|
+
),
|
|
87
|
+
startsWith: <T>() =>
|
|
88
|
+
createOperator<T | Query | RawExpression>(
|
|
89
|
+
(key, value, values) => `${key} LIKE ${quoteValue(value, values)} || '%'`,
|
|
90
|
+
),
|
|
91
|
+
startsWithInsensitive: <T>() =>
|
|
92
|
+
createOperator<T | Query | RawExpression>(
|
|
93
|
+
(key, value, values) =>
|
|
94
|
+
`${key} ILIKE ${quoteValue(value, values)} || '%'`,
|
|
95
|
+
),
|
|
96
|
+
endsWith: <T>() =>
|
|
97
|
+
createOperator<T | Query | RawExpression>(
|
|
98
|
+
(key, value, values) => `${key} LIKE '%' || ${quoteValue(value, values)}`,
|
|
99
|
+
),
|
|
100
|
+
endsWithInsensitive: <T>() =>
|
|
101
|
+
createOperator<T | Query | RawExpression>(
|
|
102
|
+
(key, value, values) =>
|
|
103
|
+
`${key} ILIKE '%' || ${quoteValue(value, values)}`,
|
|
104
|
+
),
|
|
105
|
+
between: <T>() =>
|
|
106
|
+
createOperator<[T | Query | RawExpression, T | Query | RawExpression]>(
|
|
107
|
+
(key, [from, to], values) =>
|
|
108
|
+
`${key} BETWEEN ${quoteValue(from, values)} AND ${quoteValue(
|
|
109
|
+
to,
|
|
110
|
+
values,
|
|
111
|
+
)}`,
|
|
112
|
+
),
|
|
113
|
+
jsonPath: <T>() =>
|
|
114
|
+
createOperator<
|
|
115
|
+
[path: string, op: string, value: T | Query | RawExpression]
|
|
116
|
+
>(
|
|
117
|
+
(key, [path, op, value], values) =>
|
|
118
|
+
`jsonb_path_query_first(${key}, ${quote(
|
|
119
|
+
path,
|
|
120
|
+
)}) #>> '{}' ${op} ${quoteValue(value, values, true)}`,
|
|
121
|
+
),
|
|
122
|
+
jsonSupersetOf: <T>() =>
|
|
123
|
+
createOperator<T | Query | RawExpression>(
|
|
124
|
+
(key, value, values) => `${key} @> ${quoteValue(value, values, true)}`,
|
|
125
|
+
),
|
|
126
|
+
jsonSubsetOf: <T>() =>
|
|
127
|
+
createOperator<T | Query | RawExpression>(
|
|
128
|
+
(key, value, values) => `${key} <@ ${quoteValue(value, values, true)}`,
|
|
129
|
+
),
|
|
130
|
+
};
|
|
131
|
+
|
|
132
|
+
const base = <T>() => ({
|
|
133
|
+
equals: all.equals<T>(),
|
|
134
|
+
not: all.not<T>(),
|
|
135
|
+
in: all.in<T>(),
|
|
136
|
+
notIn: all.notIn<T>(),
|
|
137
|
+
});
|
|
138
|
+
|
|
139
|
+
const numeric = <T>() => ({
|
|
140
|
+
...base<T>(),
|
|
141
|
+
lt: all.lt<T>(),
|
|
142
|
+
lte: all.lte<T>(),
|
|
143
|
+
gt: all.gt<T>(),
|
|
144
|
+
gte: all.gte<T>(),
|
|
145
|
+
between: all.between<T>(),
|
|
146
|
+
});
|
|
147
|
+
|
|
148
|
+
const text = <T>() => ({
|
|
149
|
+
...base<T>(),
|
|
150
|
+
contains: all.contains<T>(),
|
|
151
|
+
containsInsensitive: all.containsInsensitive<T>(),
|
|
152
|
+
startsWith: all.startsWith<T>(),
|
|
153
|
+
startsWithInsensitive: all.startsWithInsensitive<T>(),
|
|
154
|
+
endsWith: all.endsWith<T>(),
|
|
155
|
+
endsWithInsensitive: all.endsWithInsensitive<T>(),
|
|
156
|
+
});
|
|
157
|
+
|
|
158
|
+
const json = <T>() => ({
|
|
159
|
+
...base<T>(),
|
|
160
|
+
jsonPath: all.jsonPath<T>(),
|
|
161
|
+
jsonSupersetOf: all.jsonSupersetOf<T>(),
|
|
162
|
+
jsonSubsetOf: all.jsonSubsetOf<T>(),
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
export const Operators = {
|
|
166
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
167
|
+
any: base<any>(),
|
|
168
|
+
boolean: base<boolean>(),
|
|
169
|
+
number: numeric<number>(),
|
|
170
|
+
date: numeric<Date>(),
|
|
171
|
+
time: numeric<Date>(),
|
|
172
|
+
text: text<string>(),
|
|
173
|
+
json: json<unknown>(),
|
|
174
|
+
// TODO: array operators
|
|
175
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
176
|
+
array: base<any>(),
|
|
177
|
+
};
|
package/src/query.ts
ADDED
|
@@ -0,0 +1,292 @@
|
|
|
1
|
+
import { QueryMethods } from './queryMethods/queryMethods';
|
|
2
|
+
import { QueryData } from './sql';
|
|
3
|
+
import {
|
|
4
|
+
ColumnShapeOutput,
|
|
5
|
+
ColumnsShape,
|
|
6
|
+
ColumnType,
|
|
7
|
+
TableSchema,
|
|
8
|
+
} from './columnSchema';
|
|
9
|
+
import { Spread } from './utils';
|
|
10
|
+
import { AliasOrTable, RawExpression, StringKey } from './common';
|
|
11
|
+
import { ThenResult } from './queryMethods/then';
|
|
12
|
+
import { Db } from './db';
|
|
13
|
+
import { ColumnInfo } from './queryMethods/columnInfo';
|
|
14
|
+
import { RelationQueryBase, RelationsBase } from './relations';
|
|
15
|
+
import { WhereQueryBuilder } from './queryMethods/where';
|
|
16
|
+
import { OnQueryBuilder } from './queryMethods/join';
|
|
17
|
+
import { GetArg } from './queryMethods/get';
|
|
18
|
+
|
|
19
|
+
export type ColumnParser = (input: unknown) => unknown;
|
|
20
|
+
export type ColumnsParsers = Record<string, ColumnParser>;
|
|
21
|
+
|
|
22
|
+
export type SelectableBase = Record<
|
|
23
|
+
PropertyKey,
|
|
24
|
+
{ as: string; column: ColumnType }
|
|
25
|
+
>;
|
|
26
|
+
|
|
27
|
+
export type WithDataItem = { table: string; shape: ColumnsShape };
|
|
28
|
+
export type WithDataBase = Record<never, WithDataItem>;
|
|
29
|
+
|
|
30
|
+
export type QueryBase = {
|
|
31
|
+
query: QueryData;
|
|
32
|
+
table?: string;
|
|
33
|
+
tableAlias?: string;
|
|
34
|
+
clone(): QueryBase;
|
|
35
|
+
selectable: SelectableBase;
|
|
36
|
+
__model: Query;
|
|
37
|
+
relations: RelationsBase;
|
|
38
|
+
withData: WithDataBase;
|
|
39
|
+
};
|
|
40
|
+
|
|
41
|
+
export type defaultsKey = typeof defaultsKey;
|
|
42
|
+
export const defaultsKey: unique symbol = Symbol('defaults');
|
|
43
|
+
|
|
44
|
+
export type Query = QueryMethods & {
|
|
45
|
+
queryBuilder: Db;
|
|
46
|
+
whereQueryBuilder: typeof WhereQueryBuilder;
|
|
47
|
+
onQueryBuilder: typeof OnQueryBuilder;
|
|
48
|
+
table?: string;
|
|
49
|
+
shape: ColumnsShape;
|
|
50
|
+
schema: Omit<TableSchema<ColumnsShape>, 'primaryKeys' | 'primaryTypes'> & {
|
|
51
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
52
|
+
primaryKeys: any[];
|
|
53
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
54
|
+
primaryTypes: any[];
|
|
55
|
+
};
|
|
56
|
+
type: Record<string, unknown>;
|
|
57
|
+
inputType: Record<string, unknown>;
|
|
58
|
+
query: QueryData;
|
|
59
|
+
result: ColumnsShape;
|
|
60
|
+
hasSelect: boolean;
|
|
61
|
+
hasWhere: boolean;
|
|
62
|
+
selectable: SelectableBase;
|
|
63
|
+
returnType: QueryReturnType;
|
|
64
|
+
then: ThenResult<unknown>;
|
|
65
|
+
tableAlias: string | undefined;
|
|
66
|
+
joinedTables: Record<string, Pick<Query, 'result' | 'tableAlias' | 'table'>>;
|
|
67
|
+
windows: PropertyKey[];
|
|
68
|
+
defaultSelectColumns: string[];
|
|
69
|
+
columnsParsers?: ColumnsParsers;
|
|
70
|
+
relations: RelationsBase;
|
|
71
|
+
withData: WithDataBase;
|
|
72
|
+
[defaultsKey]?: string;
|
|
73
|
+
};
|
|
74
|
+
|
|
75
|
+
export type Selectable<T extends QueryBase> = StringKey<keyof T['selectable']>;
|
|
76
|
+
|
|
77
|
+
export type QueryWithTable = Query & { table: string };
|
|
78
|
+
|
|
79
|
+
export type DefaultSelectColumns<S extends ColumnsShape> = {
|
|
80
|
+
[K in keyof S]: S[K]['isHidden'] extends true ? never : K;
|
|
81
|
+
}[StringKey<keyof S>][];
|
|
82
|
+
|
|
83
|
+
export type QueryReturnType =
|
|
84
|
+
| 'all'
|
|
85
|
+
| 'one'
|
|
86
|
+
| 'oneOrThrow'
|
|
87
|
+
| 'rows'
|
|
88
|
+
| 'pluck'
|
|
89
|
+
| 'value'
|
|
90
|
+
| 'valueOrThrow'
|
|
91
|
+
| 'rowCount'
|
|
92
|
+
| 'void';
|
|
93
|
+
|
|
94
|
+
export type JoinedTablesBase = Record<
|
|
95
|
+
string,
|
|
96
|
+
Pick<Query, 'result' | 'tableAlias' | 'table'>
|
|
97
|
+
>;
|
|
98
|
+
|
|
99
|
+
type QueryThen<
|
|
100
|
+
ReturnType extends QueryReturnType,
|
|
101
|
+
Result extends ColumnsShape,
|
|
102
|
+
> = ReturnType extends 'all'
|
|
103
|
+
? ThenResult<ColumnShapeOutput<Result>[]>
|
|
104
|
+
: ReturnType extends 'one'
|
|
105
|
+
? ThenResult<ColumnShapeOutput<Result> | undefined>
|
|
106
|
+
: ReturnType extends 'oneOrThrow'
|
|
107
|
+
? ThenResult<ColumnShapeOutput<Result>>
|
|
108
|
+
: ReturnType extends 'value'
|
|
109
|
+
? Result extends { value: ColumnType }
|
|
110
|
+
? ThenResult<Result['value']['type'] | undefined>
|
|
111
|
+
: never
|
|
112
|
+
: ReturnType extends 'valueOrThrow'
|
|
113
|
+
? Result extends { value: ColumnType }
|
|
114
|
+
? ThenResult<Result['value']['type']>
|
|
115
|
+
: never
|
|
116
|
+
: ReturnType extends 'rows'
|
|
117
|
+
? ThenResult<ColumnShapeOutput<Result>[keyof Result][][]>
|
|
118
|
+
: ReturnType extends 'pluck'
|
|
119
|
+
? Result extends { pluck: ColumnType }
|
|
120
|
+
? ThenResult<Result['pluck']['type'][]>
|
|
121
|
+
: never
|
|
122
|
+
: ReturnType extends 'rowCount'
|
|
123
|
+
? ThenResult<number>
|
|
124
|
+
: ReturnType extends 'void'
|
|
125
|
+
? ThenResult<void>
|
|
126
|
+
: never;
|
|
127
|
+
|
|
128
|
+
export type AddQuerySelect<
|
|
129
|
+
T extends Pick<Query, 'hasSelect' | 'result' | 'then' | 'returnType'>,
|
|
130
|
+
Result extends ColumnsShape,
|
|
131
|
+
> = T['hasSelect'] extends false
|
|
132
|
+
? Omit<T, 'hasSelect' | 'result' | 'then'> & {
|
|
133
|
+
hasSelect: true;
|
|
134
|
+
result: Result;
|
|
135
|
+
then: QueryThen<T['returnType'], Result>;
|
|
136
|
+
}
|
|
137
|
+
: Omit<T, 'result' | 'then'> & {
|
|
138
|
+
result: Spread<[T['result'], Result]>;
|
|
139
|
+
then: QueryThen<T['returnType'], Spread<[T['result'], Result]>>;
|
|
140
|
+
};
|
|
141
|
+
|
|
142
|
+
export type QuerySelectAll<T extends Query> = Omit<
|
|
143
|
+
T,
|
|
144
|
+
'hasSelect' | 'result' | 'then'
|
|
145
|
+
> & {
|
|
146
|
+
hasSelect: true;
|
|
147
|
+
result: T['shape'];
|
|
148
|
+
then: QueryThen<T['returnType'], T['shape']>;
|
|
149
|
+
};
|
|
150
|
+
|
|
151
|
+
export type SetQueryReturns<T extends Query, R extends QueryReturnType> = Omit<
|
|
152
|
+
T,
|
|
153
|
+
'returnType' | 'then'
|
|
154
|
+
> & { returnType: R; then: QueryThen<R, T['result']> };
|
|
155
|
+
|
|
156
|
+
export type SetQueryReturnsAll<T extends Query> = SetQueryReturns<T, 'all'>;
|
|
157
|
+
|
|
158
|
+
export type SetQueryReturnsOneOptional<T extends Query> = SetQueryReturns<
|
|
159
|
+
T,
|
|
160
|
+
'one'
|
|
161
|
+
>;
|
|
162
|
+
|
|
163
|
+
export type SetQueryReturnsOne<T extends Query> = SetQueryReturns<
|
|
164
|
+
T,
|
|
165
|
+
'oneOrThrow'
|
|
166
|
+
>;
|
|
167
|
+
|
|
168
|
+
export type SetQueryReturnsRows<T extends Query> = SetQueryReturns<T, 'rows'>;
|
|
169
|
+
|
|
170
|
+
export type SetQueryReturnsPluck<
|
|
171
|
+
T extends Query,
|
|
172
|
+
S extends keyof T['selectable'] | RawExpression,
|
|
173
|
+
C extends ColumnType = S extends keyof T['selectable']
|
|
174
|
+
? T['selectable'][S]['column']
|
|
175
|
+
: S extends RawExpression
|
|
176
|
+
? S['__column']
|
|
177
|
+
: never,
|
|
178
|
+
> = Omit<T, 'hasSelect' | 'result' | 'returnType' | 'then'> & {
|
|
179
|
+
hasSelect: true;
|
|
180
|
+
result: { pluck: C };
|
|
181
|
+
returnType: 'pluck';
|
|
182
|
+
then: ThenResult<C['type'][]>;
|
|
183
|
+
};
|
|
184
|
+
|
|
185
|
+
export type SetQueryReturnsValueOptional<
|
|
186
|
+
T extends Query,
|
|
187
|
+
Arg extends Exclude<GetArg<T>, RawExpression> | ColumnType,
|
|
188
|
+
Column extends ColumnType = Arg extends ColumnType
|
|
189
|
+
? Arg
|
|
190
|
+
: Arg extends keyof T['selectable']
|
|
191
|
+
? T['selectable'][Arg]['column']
|
|
192
|
+
: Arg extends RelationQueryBase
|
|
193
|
+
? Arg['result']['value']
|
|
194
|
+
: never,
|
|
195
|
+
> = Omit<T, 'hasSelect' | 'result' | 'returnType' | 'then'> & {
|
|
196
|
+
hasSelect: true;
|
|
197
|
+
result: { value: Column };
|
|
198
|
+
returnType: 'value';
|
|
199
|
+
then: ThenResult<Column['type'] | undefined>;
|
|
200
|
+
};
|
|
201
|
+
|
|
202
|
+
export type SetQueryReturnsValue<
|
|
203
|
+
T extends Query,
|
|
204
|
+
Arg extends Exclude<GetArg<T>, RawExpression> | ColumnType,
|
|
205
|
+
Column extends ColumnType = Arg extends ColumnType
|
|
206
|
+
? Arg
|
|
207
|
+
: Arg extends keyof T['selectable']
|
|
208
|
+
? T['selectable'][Arg]['column']
|
|
209
|
+
: Arg extends RelationQueryBase
|
|
210
|
+
? Arg['result']['value']
|
|
211
|
+
: never,
|
|
212
|
+
> = Omit<T, 'hasSelect' | 'result' | 'returnType' | 'then'> & {
|
|
213
|
+
hasSelect: true;
|
|
214
|
+
result: { value: Column };
|
|
215
|
+
returnType: 'valueOrThrow';
|
|
216
|
+
then: ThenResult<Column['type']>;
|
|
217
|
+
};
|
|
218
|
+
|
|
219
|
+
export type SetQueryReturnsRowCount<T extends Query> = SetQueryReturns<
|
|
220
|
+
T,
|
|
221
|
+
'rowCount'
|
|
222
|
+
>;
|
|
223
|
+
|
|
224
|
+
export type SetQueryReturnsVoid<T extends Query> = SetQueryReturns<T, 'void'>;
|
|
225
|
+
|
|
226
|
+
export type SetQueryReturnsColumnInfo<
|
|
227
|
+
T extends Query,
|
|
228
|
+
Column extends keyof T['shape'] | undefined,
|
|
229
|
+
Result = Column extends keyof T['shape']
|
|
230
|
+
? ColumnInfo
|
|
231
|
+
: Record<keyof T['shape'], ColumnInfo>,
|
|
232
|
+
> = Omit<T, 'result' | 'returnType' | 'then'> & {
|
|
233
|
+
result: { value: ColumnType<Result> };
|
|
234
|
+
returnType: 'value';
|
|
235
|
+
then: ThenResult<Result>;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
export type SetQueryTableAlias<
|
|
239
|
+
T extends Query,
|
|
240
|
+
TableAlias extends string,
|
|
241
|
+
> = Omit<T, 'tableAlias' | 'selectable'> & {
|
|
242
|
+
tableAlias: TableAlias;
|
|
243
|
+
selectable: Omit<
|
|
244
|
+
T['selectable'],
|
|
245
|
+
`${AliasOrTable<T>}.${StringKey<keyof T['shape']>}`
|
|
246
|
+
> & {
|
|
247
|
+
[K in keyof T['shape'] as `${TableAlias}.${StringKey<keyof T['shape']>}`]: {
|
|
248
|
+
as: K;
|
|
249
|
+
column: T['shape'][K];
|
|
250
|
+
};
|
|
251
|
+
};
|
|
252
|
+
};
|
|
253
|
+
|
|
254
|
+
export type SetQueryJoinedTables<
|
|
255
|
+
T extends Query,
|
|
256
|
+
Selectable extends Record<string, { as: string; column: ColumnType }>,
|
|
257
|
+
JoinedTables extends JoinedTablesBase,
|
|
258
|
+
> = Omit<T, 'selectable' | 'joinedTables'> & {
|
|
259
|
+
selectable: Selectable;
|
|
260
|
+
joinedTables: JoinedTables;
|
|
261
|
+
};
|
|
262
|
+
|
|
263
|
+
export type AddQueryJoinedTable<
|
|
264
|
+
T extends Query,
|
|
265
|
+
J extends Pick<Query, 'result' | 'tableAlias' | 'table'>,
|
|
266
|
+
> = SetQueryJoinedTables<
|
|
267
|
+
T,
|
|
268
|
+
T['selectable'] & {
|
|
269
|
+
[K in keyof J['result'] as `${AliasOrTable<J>}.${StringKey<K>}`]: {
|
|
270
|
+
as: K;
|
|
271
|
+
column: J['result'][K];
|
|
272
|
+
};
|
|
273
|
+
},
|
|
274
|
+
string extends keyof T['joinedTables']
|
|
275
|
+
? Record<AliasOrTable<J>, J>
|
|
276
|
+
: Spread<[T['joinedTables'], Record<AliasOrTable<J>, J>]>
|
|
277
|
+
>;
|
|
278
|
+
|
|
279
|
+
export type SetQueryWith<
|
|
280
|
+
T extends Query,
|
|
281
|
+
WithData extends Record<string, WithDataItem>,
|
|
282
|
+
> = Omit<T, 'withData'> & { withData: WithData };
|
|
283
|
+
|
|
284
|
+
export type AddQueryWith<
|
|
285
|
+
T extends Query,
|
|
286
|
+
With extends WithDataItem,
|
|
287
|
+
> = SetQueryWith<T, Spread<[T['withData'], { [K in With['table']]: With }]>>;
|
|
288
|
+
|
|
289
|
+
export type SetQueryWindows<T extends Query, W extends PropertyKey[]> = Omit<
|
|
290
|
+
T,
|
|
291
|
+
'windows'
|
|
292
|
+
> & { windows: W };
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { Query } from './query';
|
|
2
|
+
import { QueryData } from './sql';
|
|
3
|
+
|
|
4
|
+
// TODO: remove
|
|
5
|
+
export const removeFromQuery = <T extends Query>(q: T, key: string): T => {
|
|
6
|
+
if (q.query) delete q.query[key as keyof typeof q.query];
|
|
7
|
+
return q;
|
|
8
|
+
};
|
|
9
|
+
|
|
10
|
+
export const pushQueryArray = <T extends { query: QueryData }>(
|
|
11
|
+
q: T,
|
|
12
|
+
key: string,
|
|
13
|
+
value: unknown,
|
|
14
|
+
): T => {
|
|
15
|
+
if (!q.query[key as keyof typeof q.query])
|
|
16
|
+
(q.query as Record<string, unknown>)[key] = value;
|
|
17
|
+
else
|
|
18
|
+
(q.query[key as keyof typeof q.query] as unknown[]).push(
|
|
19
|
+
...(value as unknown[]),
|
|
20
|
+
);
|
|
21
|
+
return q as T;
|
|
22
|
+
};
|
|
23
|
+
|
|
24
|
+
export const pushQueryValue = <T extends { query: QueryData }>(
|
|
25
|
+
q: T,
|
|
26
|
+
key: string,
|
|
27
|
+
value: unknown,
|
|
28
|
+
): T => {
|
|
29
|
+
if (!q.query[key as keyof typeof q.query])
|
|
30
|
+
(q.query as Record<string, unknown>)[key] = [value];
|
|
31
|
+
else (q.query[key as keyof typeof q.query] as unknown[]).push(value);
|
|
32
|
+
return q as unknown as T;
|
|
33
|
+
};
|
|
34
|
+
|
|
35
|
+
export const setQueryObjectValue = <T extends { query: QueryData }>(
|
|
36
|
+
q: T,
|
|
37
|
+
object: string,
|
|
38
|
+
key: string,
|
|
39
|
+
value: unknown,
|
|
40
|
+
): T => {
|
|
41
|
+
if (!q.query[object as keyof typeof q.query])
|
|
42
|
+
(q.query as unknown as Record<string, Record<string, unknown>>)[object] = {
|
|
43
|
+
[key]: value,
|
|
44
|
+
};
|
|
45
|
+
else
|
|
46
|
+
(q.query as unknown as Record<string, Record<string, unknown>>)[object][
|
|
47
|
+
key
|
|
48
|
+
] = value;
|
|
49
|
+
return q as unknown as T;
|
|
50
|
+
};
|