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
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { JsonItem, SelectFunctionItem, SelectQueryData } from './types';
|
|
2
|
+
import { Expression, getRaw, isRaw } from '../common';
|
|
3
|
+
import { Query } from '../query';
|
|
4
|
+
import { addValue, q, quoteFullColumn } from './common';
|
|
5
|
+
import { aggregateToSql } from './aggregate';
|
|
6
|
+
import { getQueryAs } from '../utils';
|
|
7
|
+
import { RelationQuery, relationQueryKey } from '../relations';
|
|
8
|
+
|
|
9
|
+
const jsonColumnOrMethodToSql = (
|
|
10
|
+
column: string | JsonItem,
|
|
11
|
+
values: unknown[],
|
|
12
|
+
quotedAs?: string,
|
|
13
|
+
) => {
|
|
14
|
+
return typeof column === 'string'
|
|
15
|
+
? quoteFullColumn(column, quotedAs)
|
|
16
|
+
: jsonToSql(column, values, quotedAs);
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
const jsonToSql = (
|
|
20
|
+
item: JsonItem,
|
|
21
|
+
values: unknown[],
|
|
22
|
+
quotedAs?: string,
|
|
23
|
+
): string => {
|
|
24
|
+
const json = item.__json;
|
|
25
|
+
if (json[0] === 'pathQuery') {
|
|
26
|
+
const [, , , column, path, options] = json;
|
|
27
|
+
return `jsonb_path_query(${jsonColumnOrMethodToSql(
|
|
28
|
+
column,
|
|
29
|
+
values,
|
|
30
|
+
quotedAs,
|
|
31
|
+
)}, ${addValue(values, path)}${
|
|
32
|
+
options?.vars ? `, ${addValue(values, options.vars)}` : ''
|
|
33
|
+
}${options?.silent ? ', true' : ''})`;
|
|
34
|
+
} else if (json[0] === 'set') {
|
|
35
|
+
const [, , , column, path, value, options] = json;
|
|
36
|
+
return `jsonb_set(${jsonColumnOrMethodToSql(
|
|
37
|
+
column,
|
|
38
|
+
values,
|
|
39
|
+
quotedAs,
|
|
40
|
+
)}, '{${path.join(', ')}}', ${addValue(values, JSON.stringify(value))}${
|
|
41
|
+
options?.createIfMissing ? ', true' : ''
|
|
42
|
+
})`;
|
|
43
|
+
} else if (json[0] === 'insert') {
|
|
44
|
+
const [, , , column, path, value, options] = json;
|
|
45
|
+
return `jsonb_insert(${jsonColumnOrMethodToSql(
|
|
46
|
+
column,
|
|
47
|
+
values,
|
|
48
|
+
quotedAs,
|
|
49
|
+
)}, '{${path.join(', ')}}', ${addValue(values, JSON.stringify(value))}${
|
|
50
|
+
options?.insertAfter ? ', true' : ''
|
|
51
|
+
})`;
|
|
52
|
+
} else if (json[0] === 'remove') {
|
|
53
|
+
const [, , , column, path] = json;
|
|
54
|
+
return `${jsonColumnOrMethodToSql(
|
|
55
|
+
column,
|
|
56
|
+
values,
|
|
57
|
+
quotedAs,
|
|
58
|
+
)} #- '{${path.join(', ')}}'`;
|
|
59
|
+
}
|
|
60
|
+
return '';
|
|
61
|
+
};
|
|
62
|
+
|
|
63
|
+
export const pushSelectSql = (
|
|
64
|
+
sql: string[],
|
|
65
|
+
model: Pick<
|
|
66
|
+
Query,
|
|
67
|
+
'whereQueryBuilder' | 'onQueryBuilder' | 'as' | 'shape' | 'relations'
|
|
68
|
+
>,
|
|
69
|
+
query: Pick<SelectQueryData, 'select' | 'join'>,
|
|
70
|
+
values: unknown[],
|
|
71
|
+
quotedAs?: string,
|
|
72
|
+
) => {
|
|
73
|
+
sql.push(selectToSql(model, query, values, quotedAs));
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
export const selectToSql = (
|
|
77
|
+
model: Pick<
|
|
78
|
+
Query,
|
|
79
|
+
'whereQueryBuilder' | 'onQueryBuilder' | 'as' | 'shape' | 'relations'
|
|
80
|
+
>,
|
|
81
|
+
query: Pick<SelectQueryData, 'select' | 'join'>,
|
|
82
|
+
values: unknown[],
|
|
83
|
+
quotedAs?: string,
|
|
84
|
+
): string => {
|
|
85
|
+
if (query.select) {
|
|
86
|
+
const list: string[] = [];
|
|
87
|
+
query.select.forEach((item) => {
|
|
88
|
+
if (typeof item === 'string') {
|
|
89
|
+
list.push(
|
|
90
|
+
item === '*'
|
|
91
|
+
? query.join?.length
|
|
92
|
+
? `${quotedAs}.*`
|
|
93
|
+
: '*'
|
|
94
|
+
: quoteFullColumn(item, quotedAs),
|
|
95
|
+
);
|
|
96
|
+
} else if ((item as Query).query?.[relationQueryKey]) {
|
|
97
|
+
let relationQuery = (item as RelationQuery).clone();
|
|
98
|
+
const as = q(getQueryAs(relationQuery));
|
|
99
|
+
relationQuery._as(relationQuery.query[relationQueryKey] as string);
|
|
100
|
+
|
|
101
|
+
const { returnType } = relationQuery.query;
|
|
102
|
+
if (
|
|
103
|
+
returnType === 'all' ||
|
|
104
|
+
returnType === 'one' ||
|
|
105
|
+
returnType === 'oneOrThrow'
|
|
106
|
+
) {
|
|
107
|
+
relationQuery = relationQuery._json() as unknown as RelationQuery;
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
list.push(`(${relationQuery.toSql(values).text}) AS ${as}`);
|
|
111
|
+
} else {
|
|
112
|
+
if ('selectAs' in item) {
|
|
113
|
+
const obj = item.selectAs as Record<string, Expression | Query>;
|
|
114
|
+
for (const as in obj) {
|
|
115
|
+
const value = obj[as];
|
|
116
|
+
if (typeof value === 'object') {
|
|
117
|
+
if (isRaw(value)) {
|
|
118
|
+
list.push(`${getRaw(value, values)} AS ${q(as)}`);
|
|
119
|
+
} else {
|
|
120
|
+
const sql = (value as Query).json().toSql(values);
|
|
121
|
+
list.push(`(${sql.text}) AS ${q(as)}`);
|
|
122
|
+
}
|
|
123
|
+
} else {
|
|
124
|
+
list.push(
|
|
125
|
+
`${quoteFullColumn(value as string, quotedAs)} AS ${q(as)}`,
|
|
126
|
+
);
|
|
127
|
+
}
|
|
128
|
+
}
|
|
129
|
+
} else if ('__json' in item) {
|
|
130
|
+
list.push(
|
|
131
|
+
`${jsonToSql(item, values, quotedAs)} AS ${q(item.__json[1])}`,
|
|
132
|
+
);
|
|
133
|
+
} else if (isRaw(item)) {
|
|
134
|
+
list.push(getRaw(item, values));
|
|
135
|
+
} else if ('arguments' in item) {
|
|
136
|
+
list.push(
|
|
137
|
+
`${(item as SelectFunctionItem).function}(${selectToSql(
|
|
138
|
+
model,
|
|
139
|
+
{ select: item.arguments },
|
|
140
|
+
values,
|
|
141
|
+
quotedAs,
|
|
142
|
+
)})${item.as ? ` AS ${q((item as { as: string }).as)}` : ''}`,
|
|
143
|
+
);
|
|
144
|
+
} else {
|
|
145
|
+
list.push(aggregateToSql(model, values, item, quotedAs));
|
|
146
|
+
}
|
|
147
|
+
}
|
|
148
|
+
});
|
|
149
|
+
return list.join(', ');
|
|
150
|
+
} else {
|
|
151
|
+
return query.join?.length ? `${quotedAs}.*` : '*';
|
|
152
|
+
}
|
|
153
|
+
};
|
package/src/sql/toSql.ts
ADDED
|
@@ -0,0 +1,153 @@
|
|
|
1
|
+
import { getRaw, isRaw } from '../common';
|
|
2
|
+
import { Query } from '../query';
|
|
3
|
+
import { addValue, q, qc } from './common';
|
|
4
|
+
import { JoinItem, QueryData, Sql } from './types';
|
|
5
|
+
import { pushDistinctSql } from './distinct';
|
|
6
|
+
import { pushSelectSql } from './select';
|
|
7
|
+
import { windowToSql } from './window';
|
|
8
|
+
import { pushJoinSql } from './join';
|
|
9
|
+
import { pushWhereSql } from './where';
|
|
10
|
+
import { pushHavingSql } from './having';
|
|
11
|
+
import { pushWithSql } from './with';
|
|
12
|
+
import { pushFromAndAs } from './fromAndAs';
|
|
13
|
+
import { pushInsertSql } from './insert';
|
|
14
|
+
import { pushUpdateSql } from './update';
|
|
15
|
+
import { pushDeleteSql } from './delete';
|
|
16
|
+
import { pushTruncateSql } from './truncate';
|
|
17
|
+
import { pushColumnInfoSql } from './columnInfo';
|
|
18
|
+
import { pushOrderBySql } from './orderBy';
|
|
19
|
+
|
|
20
|
+
export const toSql = (model: Query, values: unknown[] = []): Sql => {
|
|
21
|
+
const query = model.query;
|
|
22
|
+
const sql: string[] = [];
|
|
23
|
+
|
|
24
|
+
if (query.with) {
|
|
25
|
+
pushWithSql(sql, values, query.with);
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
if (query.type) {
|
|
29
|
+
if (query.type === 'truncate') {
|
|
30
|
+
if (!model.table) throw new Error('Table is missing for truncate');
|
|
31
|
+
|
|
32
|
+
pushTruncateSql(sql, model.table, query);
|
|
33
|
+
return { text: sql.join(' '), values };
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
if (query.type === 'columnInfo') {
|
|
37
|
+
if (!model.table) throw new Error('Table is missing for truncate');
|
|
38
|
+
|
|
39
|
+
pushColumnInfoSql(sql, values, model.table, query);
|
|
40
|
+
return { text: sql.join(' '), values };
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
if (!model.table) throw new Error(`Table is missing for ${query.type}`);
|
|
44
|
+
|
|
45
|
+
const quotedAs = q(query.as || model.table);
|
|
46
|
+
|
|
47
|
+
if (query.type === 'insert') {
|
|
48
|
+
pushInsertSql(sql, values, model, query, q(model.table));
|
|
49
|
+
return { text: sql.join(' '), values };
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
if (query.type === 'update') {
|
|
53
|
+
pushUpdateSql(sql, values, model, query, quotedAs);
|
|
54
|
+
return { text: sql.join(' '), values };
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
if (query.type === 'delete') {
|
|
58
|
+
pushDeleteSql(sql, values, model, query, q(model.table));
|
|
59
|
+
return { text: sql.join(' '), values };
|
|
60
|
+
}
|
|
61
|
+
}
|
|
62
|
+
|
|
63
|
+
const quotedAs = model.table && q(query.as || model.table);
|
|
64
|
+
|
|
65
|
+
sql.push('SELECT');
|
|
66
|
+
|
|
67
|
+
if (query.distinct) {
|
|
68
|
+
pushDistinctSql(sql, values, query.distinct, quotedAs);
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
pushSelectSql(sql, model, query, values, quotedAs);
|
|
72
|
+
|
|
73
|
+
if (model.table || query.from) {
|
|
74
|
+
pushFromAndAs(sql, model, query, values, quotedAs);
|
|
75
|
+
}
|
|
76
|
+
|
|
77
|
+
if (query.join) {
|
|
78
|
+
pushJoinSql(
|
|
79
|
+
sql,
|
|
80
|
+
model,
|
|
81
|
+
query as QueryData & { join: JoinItem[] },
|
|
82
|
+
values,
|
|
83
|
+
quotedAs,
|
|
84
|
+
);
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
if (query.and || query.or) {
|
|
88
|
+
pushWhereSql(sql, model, query, values, quotedAs);
|
|
89
|
+
}
|
|
90
|
+
|
|
91
|
+
if (query.group) {
|
|
92
|
+
const group = query.group.map((item) =>
|
|
93
|
+
typeof item === 'object' && isRaw(item)
|
|
94
|
+
? getRaw(item, values)
|
|
95
|
+
: qc(item as string, quotedAs),
|
|
96
|
+
);
|
|
97
|
+
sql.push(`GROUP BY ${group.join(', ')}`);
|
|
98
|
+
}
|
|
99
|
+
|
|
100
|
+
if (query.having || query.havingOr) {
|
|
101
|
+
pushHavingSql(sql, model, query, values, quotedAs);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
if (query.window) {
|
|
105
|
+
const window: string[] = [];
|
|
106
|
+
query.window.forEach((item) => {
|
|
107
|
+
for (const key in item) {
|
|
108
|
+
window.push(`${q(key)} AS ${windowToSql(item[key], values, quotedAs)}`);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
sql.push(`WINDOW ${window.join(', ')}`);
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
if (query.union) {
|
|
115
|
+
query.union.forEach((item) => {
|
|
116
|
+
let itemSql: string;
|
|
117
|
+
if (isRaw(item.arg)) {
|
|
118
|
+
itemSql = getRaw(item.arg, values);
|
|
119
|
+
} else {
|
|
120
|
+
const argSql = item.arg.toSql(values);
|
|
121
|
+
itemSql = argSql.text;
|
|
122
|
+
}
|
|
123
|
+
sql.push(`${item.kind} ${item.wrap ? `(${itemSql})` : itemSql}`);
|
|
124
|
+
});
|
|
125
|
+
}
|
|
126
|
+
|
|
127
|
+
if (query.order) {
|
|
128
|
+
pushOrderBySql(sql, values, quotedAs, query.order);
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
if (query.take || query.limit !== undefined) {
|
|
132
|
+
sql.push(`LIMIT ${addValue(values, query.take ? 1 : query.limit)}`);
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
if (query.offset) {
|
|
136
|
+
sql.push(`OFFSET ${addValue(values, query.offset)}`);
|
|
137
|
+
}
|
|
138
|
+
|
|
139
|
+
if (query.for) {
|
|
140
|
+
sql.push('FOR', query.for.type);
|
|
141
|
+
const { tableNames } = query.for;
|
|
142
|
+
if (tableNames) {
|
|
143
|
+
if (isRaw(tableNames)) {
|
|
144
|
+
sql.push('OF', getRaw(tableNames, values));
|
|
145
|
+
} else {
|
|
146
|
+
sql.push('OF', tableNames.map(q).join(', '));
|
|
147
|
+
}
|
|
148
|
+
}
|
|
149
|
+
if (query.for.mode) sql.push(query.for.mode);
|
|
150
|
+
}
|
|
151
|
+
|
|
152
|
+
return { text: sql.join(' '), values };
|
|
153
|
+
};
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
import { TruncateQueryData } from './types';
|
|
2
|
+
import { quoteSchemaAndTable } from './common';
|
|
3
|
+
|
|
4
|
+
export const pushTruncateSql = (
|
|
5
|
+
sql: string[],
|
|
6
|
+
table: string,
|
|
7
|
+
query: TruncateQueryData,
|
|
8
|
+
) => {
|
|
9
|
+
sql.push('TRUNCATE', quoteSchemaAndTable(query.schema, table));
|
|
10
|
+
|
|
11
|
+
if (query.restartIdentity) sql.push('RESTART IDENTITY');
|
|
12
|
+
if (query.cascade) sql.push('CASCADE');
|
|
13
|
+
};
|
package/src/sql/types.ts
ADDED
|
@@ -0,0 +1,355 @@
|
|
|
1
|
+
import {
|
|
2
|
+
ColumnsParsers,
|
|
3
|
+
Query,
|
|
4
|
+
QueryBase,
|
|
5
|
+
QueryReturnType,
|
|
6
|
+
QueryWithTable,
|
|
7
|
+
SelectableBase,
|
|
8
|
+
} from '../query';
|
|
9
|
+
import { Expression, RawExpression } from '../common';
|
|
10
|
+
import { ColumnsShape, ColumnType } from '../columnSchema';
|
|
11
|
+
import { RelationQuery, relationQueryKey } from '../relations';
|
|
12
|
+
import { Adapter, QueryResult } from '../adapter';
|
|
13
|
+
import { MaybeArray } from '../utils';
|
|
14
|
+
import { QueryLogger, QueryLogObject } from '../queryMethods/log';
|
|
15
|
+
import { AfterCallback, BeforeCallback } from '../queryMethods/callbacks';
|
|
16
|
+
|
|
17
|
+
export type Sql = {
|
|
18
|
+
text: string;
|
|
19
|
+
values: unknown[];
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
// used in `from` logic to decide if convert query to sql or just write table name
|
|
23
|
+
export const queryKeysOfNotSimpleQuery: (keyof SelectQueryData)[] = [
|
|
24
|
+
'take',
|
|
25
|
+
'with',
|
|
26
|
+
'as',
|
|
27
|
+
'from',
|
|
28
|
+
'and',
|
|
29
|
+
'or',
|
|
30
|
+
'select',
|
|
31
|
+
'distinct',
|
|
32
|
+
'fromOnly',
|
|
33
|
+
'join',
|
|
34
|
+
'group',
|
|
35
|
+
'having',
|
|
36
|
+
'havingOr',
|
|
37
|
+
'window',
|
|
38
|
+
'union',
|
|
39
|
+
'order',
|
|
40
|
+
'limit',
|
|
41
|
+
'offset',
|
|
42
|
+
'for',
|
|
43
|
+
];
|
|
44
|
+
|
|
45
|
+
export type CommonQueryData = {
|
|
46
|
+
adapter: Adapter;
|
|
47
|
+
handleResult(q: Query, result: QueryResult): Promise<unknown>;
|
|
48
|
+
returnType: QueryReturnType;
|
|
49
|
+
[relationQueryKey]?: string;
|
|
50
|
+
inTransaction?: boolean;
|
|
51
|
+
wrapInTransaction?: boolean;
|
|
52
|
+
throwOnNotFound?: boolean;
|
|
53
|
+
take?: boolean;
|
|
54
|
+
with?: WithItem[];
|
|
55
|
+
withShapes?: Record<string, ColumnsShape>;
|
|
56
|
+
schema?: string;
|
|
57
|
+
select?: SelectItem[];
|
|
58
|
+
as?: string;
|
|
59
|
+
from?: string | Query | RawExpression;
|
|
60
|
+
and?: WhereItem[];
|
|
61
|
+
or?: WhereItem[][];
|
|
62
|
+
parsers?: ColumnsParsers;
|
|
63
|
+
defaults?: Record<string, unknown>;
|
|
64
|
+
beforeQuery?: BeforeCallback<Query>[];
|
|
65
|
+
afterQuery?: AfterCallback<Query>[];
|
|
66
|
+
log?: QueryLogObject;
|
|
67
|
+
logger: QueryLogger;
|
|
68
|
+
};
|
|
69
|
+
|
|
70
|
+
export type SelectQueryData = CommonQueryData & {
|
|
71
|
+
type: undefined;
|
|
72
|
+
distinct?: Expression[];
|
|
73
|
+
fromOnly?: boolean;
|
|
74
|
+
join?: JoinItem[];
|
|
75
|
+
joinedParsers?: Record<string, ColumnsParsers>;
|
|
76
|
+
group?: (string | RawExpression)[];
|
|
77
|
+
having?: HavingItem[];
|
|
78
|
+
havingOr?: HavingItem[][];
|
|
79
|
+
window?: WindowItem[];
|
|
80
|
+
union?: { arg: UnionItem; kind: UnionKind; wrap?: boolean }[];
|
|
81
|
+
order?: OrderItem[];
|
|
82
|
+
limit?: number;
|
|
83
|
+
offset?: number;
|
|
84
|
+
for?: {
|
|
85
|
+
type: 'UPDATE' | 'NO KEY UPDATE' | 'SHARE' | 'KEY SHARE';
|
|
86
|
+
tableNames?: string[] | RawExpression;
|
|
87
|
+
mode?: 'NO WAIT' | 'SKIP LOCKED';
|
|
88
|
+
};
|
|
89
|
+
};
|
|
90
|
+
|
|
91
|
+
export type InsertQueryData = CommonQueryData & {
|
|
92
|
+
type: 'insert';
|
|
93
|
+
columns: string[];
|
|
94
|
+
values: unknown[][] | RawExpression;
|
|
95
|
+
using?: JoinItem[];
|
|
96
|
+
join?: JoinItem[];
|
|
97
|
+
joinedParsers?: Record<string, ColumnsParsers>;
|
|
98
|
+
onConflict?:
|
|
99
|
+
| {
|
|
100
|
+
type: 'ignore';
|
|
101
|
+
expr?: OnConflictItem;
|
|
102
|
+
}
|
|
103
|
+
| {
|
|
104
|
+
type: 'merge';
|
|
105
|
+
expr?: OnConflictItem;
|
|
106
|
+
update?: OnConflictMergeUpdate;
|
|
107
|
+
};
|
|
108
|
+
beforeInsert?: BeforeCallback<Query>[];
|
|
109
|
+
afterInsert?: AfterCallback<Query>[];
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
export type UpdateQueryData = CommonQueryData & {
|
|
113
|
+
type: 'update';
|
|
114
|
+
data: (
|
|
115
|
+
| Record<string, RawExpression | { op: string; arg: unknown } | unknown>
|
|
116
|
+
| RawExpression
|
|
117
|
+
)[];
|
|
118
|
+
beforeUpdate?: BeforeCallback<Query>[];
|
|
119
|
+
afterUpdate?: AfterCallback<Query>[];
|
|
120
|
+
};
|
|
121
|
+
|
|
122
|
+
export type DeleteQueryData = CommonQueryData & {
|
|
123
|
+
type: 'delete';
|
|
124
|
+
join?: JoinItem[];
|
|
125
|
+
joinedParsers?: Record<string, ColumnsParsers>;
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
export type TruncateQueryData = CommonQueryData & {
|
|
129
|
+
type: 'truncate';
|
|
130
|
+
restartIdentity?: boolean;
|
|
131
|
+
cascade?: boolean;
|
|
132
|
+
};
|
|
133
|
+
|
|
134
|
+
export type ColumnInfoQueryData = CommonQueryData & {
|
|
135
|
+
type: 'columnInfo';
|
|
136
|
+
column?: string;
|
|
137
|
+
};
|
|
138
|
+
|
|
139
|
+
export type QueryData =
|
|
140
|
+
| SelectQueryData
|
|
141
|
+
| InsertQueryData
|
|
142
|
+
| UpdateQueryData
|
|
143
|
+
| DeleteQueryData
|
|
144
|
+
| TruncateQueryData
|
|
145
|
+
| ColumnInfoQueryData;
|
|
146
|
+
|
|
147
|
+
export type WithItem = [
|
|
148
|
+
as: string,
|
|
149
|
+
options: WithOptions,
|
|
150
|
+
query: Query | RawExpression,
|
|
151
|
+
];
|
|
152
|
+
|
|
153
|
+
export type WithOptions = {
|
|
154
|
+
columns?: string[];
|
|
155
|
+
recursive?: true;
|
|
156
|
+
materialized?: true;
|
|
157
|
+
notMaterialized?: true;
|
|
158
|
+
};
|
|
159
|
+
|
|
160
|
+
export type JsonItem<
|
|
161
|
+
As extends string = string,
|
|
162
|
+
Type extends ColumnType = ColumnType,
|
|
163
|
+
> = {
|
|
164
|
+
__json:
|
|
165
|
+
| [
|
|
166
|
+
kind: 'set',
|
|
167
|
+
as: As,
|
|
168
|
+
type: Type,
|
|
169
|
+
column: string | JsonItem,
|
|
170
|
+
path: Array<string | number>,
|
|
171
|
+
value: unknown,
|
|
172
|
+
options?: {
|
|
173
|
+
createIfMissing?: boolean;
|
|
174
|
+
},
|
|
175
|
+
]
|
|
176
|
+
| [
|
|
177
|
+
kind: 'insert',
|
|
178
|
+
as: As,
|
|
179
|
+
type: Type,
|
|
180
|
+
column: string | JsonItem,
|
|
181
|
+
path: Array<string | number>,
|
|
182
|
+
value: unknown,
|
|
183
|
+
options?: {
|
|
184
|
+
insertAfter?: boolean;
|
|
185
|
+
},
|
|
186
|
+
]
|
|
187
|
+
| [
|
|
188
|
+
kind: 'remove',
|
|
189
|
+
as: As,
|
|
190
|
+
type: Type,
|
|
191
|
+
column: string | JsonItem,
|
|
192
|
+
path: Array<string | number>,
|
|
193
|
+
]
|
|
194
|
+
| [
|
|
195
|
+
kind: 'pathQuery',
|
|
196
|
+
as: As,
|
|
197
|
+
type: Type,
|
|
198
|
+
column: string | JsonItem,
|
|
199
|
+
path: string,
|
|
200
|
+
options?: {
|
|
201
|
+
vars?: string;
|
|
202
|
+
silent?: boolean;
|
|
203
|
+
},
|
|
204
|
+
];
|
|
205
|
+
};
|
|
206
|
+
|
|
207
|
+
export type SelectItem =
|
|
208
|
+
| string
|
|
209
|
+
| RelationQuery
|
|
210
|
+
| AggregateItem
|
|
211
|
+
| { selectAs: Record<string, string | Query | RawExpression> }
|
|
212
|
+
| SelectFunctionItem
|
|
213
|
+
| JsonItem
|
|
214
|
+
| RawExpression;
|
|
215
|
+
|
|
216
|
+
export type SelectFunctionItem = {
|
|
217
|
+
function: string;
|
|
218
|
+
arguments: SelectItem[];
|
|
219
|
+
as?: string;
|
|
220
|
+
};
|
|
221
|
+
|
|
222
|
+
export type JoinItem = {
|
|
223
|
+
type: string;
|
|
224
|
+
args:
|
|
225
|
+
| [relation: string]
|
|
226
|
+
| [
|
|
227
|
+
arg: string | QueryWithTable,
|
|
228
|
+
conditions:
|
|
229
|
+
| Record<string, string | RawExpression>
|
|
230
|
+
| RawExpression
|
|
231
|
+
| ((q: unknown) => QueryBase),
|
|
232
|
+
]
|
|
233
|
+
| [
|
|
234
|
+
arg: string | QueryWithTable,
|
|
235
|
+
leftColumn: string | RawExpression,
|
|
236
|
+
rightColumn: string | RawExpression,
|
|
237
|
+
]
|
|
238
|
+
| [
|
|
239
|
+
arg: string | QueryWithTable,
|
|
240
|
+
leftColumn: string | RawExpression,
|
|
241
|
+
op: string,
|
|
242
|
+
rightColumn: string | RawExpression,
|
|
243
|
+
];
|
|
244
|
+
};
|
|
245
|
+
|
|
246
|
+
export type WhereItem =
|
|
247
|
+
| (Omit<
|
|
248
|
+
Record<
|
|
249
|
+
string,
|
|
250
|
+
| unknown
|
|
251
|
+
| Record<string, unknown | Query | RawExpression>
|
|
252
|
+
| RawExpression
|
|
253
|
+
>,
|
|
254
|
+
'NOT' | 'AND' | 'OR' | 'IN' | 'EXISTS' | 'ON' | 'ON_JSON_PATH_EQUALS'
|
|
255
|
+
> & {
|
|
256
|
+
NOT?: MaybeArray<WhereItem>;
|
|
257
|
+
AND?: MaybeArray<WhereItem>;
|
|
258
|
+
OR?: MaybeArray<WhereItem>[];
|
|
259
|
+
IN?: MaybeArray<WhereInItem>;
|
|
260
|
+
EXISTS?: MaybeArray<JoinItem['args']>;
|
|
261
|
+
ON?: WhereOnItem | WhereJsonPathEqualsItem;
|
|
262
|
+
})
|
|
263
|
+
| ((q: unknown) => QueryBase)
|
|
264
|
+
| Query
|
|
265
|
+
| RawExpression;
|
|
266
|
+
|
|
267
|
+
export type WhereInItem = {
|
|
268
|
+
columns: string[];
|
|
269
|
+
values: unknown[][] | Query | RawExpression;
|
|
270
|
+
};
|
|
271
|
+
|
|
272
|
+
export type WhereJsonPathEqualsItem = [
|
|
273
|
+
leftColumn: string,
|
|
274
|
+
leftPath: string,
|
|
275
|
+
rightColumn: string,
|
|
276
|
+
rightPath: string,
|
|
277
|
+
];
|
|
278
|
+
|
|
279
|
+
export type WhereOnItem = {
|
|
280
|
+
joinFrom: { table?: string; query: { as?: string } } | string;
|
|
281
|
+
joinTo: { table?: string; query: { as?: string } } | string;
|
|
282
|
+
on:
|
|
283
|
+
| [leftFullColumn: string, rightFullColumn: string]
|
|
284
|
+
| [leftFullColumn: string, op: string, rightFullColumn: string];
|
|
285
|
+
};
|
|
286
|
+
|
|
287
|
+
export type AggregateItemOptions = {
|
|
288
|
+
as?: string;
|
|
289
|
+
distinct?: boolean;
|
|
290
|
+
order?: OrderItem[];
|
|
291
|
+
filter?: WhereItem;
|
|
292
|
+
filterOr?: WhereItem[];
|
|
293
|
+
withinGroup?: boolean;
|
|
294
|
+
over?: string;
|
|
295
|
+
window?: WindowItem;
|
|
296
|
+
};
|
|
297
|
+
|
|
298
|
+
export type SortDir = 'ASC' | 'DESC';
|
|
299
|
+
|
|
300
|
+
export type OrderItem =
|
|
301
|
+
| string
|
|
302
|
+
| Record<string, SortDir | { dir: SortDir; nulls: 'FIRST' | 'LAST' }>
|
|
303
|
+
| RawExpression;
|
|
304
|
+
|
|
305
|
+
export type AggregateItemArg =
|
|
306
|
+
| Expression
|
|
307
|
+
| Record<string, Expression>
|
|
308
|
+
| [Expression, string];
|
|
309
|
+
|
|
310
|
+
export type AggregateItem = {
|
|
311
|
+
function: string;
|
|
312
|
+
arg?: AggregateItemArg;
|
|
313
|
+
options: AggregateItemOptions;
|
|
314
|
+
};
|
|
315
|
+
|
|
316
|
+
export type ColumnOperators<
|
|
317
|
+
S extends SelectableBase,
|
|
318
|
+
Column extends keyof S,
|
|
319
|
+
> = {
|
|
320
|
+
[O in keyof S[Column]['column']['operators']]?:
|
|
321
|
+
| S[Column]['column']['operators'][O]['type'];
|
|
322
|
+
};
|
|
323
|
+
|
|
324
|
+
type HavingItemObject = Record<string, unknown>;
|
|
325
|
+
|
|
326
|
+
export type HavingItem =
|
|
327
|
+
| Record<string, HavingItemObject>
|
|
328
|
+
| { count?: number | HavingItemObject }
|
|
329
|
+
| Query
|
|
330
|
+
| RawExpression;
|
|
331
|
+
|
|
332
|
+
export type WindowItem = Record<string, WindowDeclaration | RawExpression>;
|
|
333
|
+
|
|
334
|
+
export type WindowDeclaration = {
|
|
335
|
+
partitionBy?: Expression | Expression[];
|
|
336
|
+
order?: OrderItem;
|
|
337
|
+
};
|
|
338
|
+
|
|
339
|
+
export type UnionItem = Query | RawExpression;
|
|
340
|
+
|
|
341
|
+
type UnionKind =
|
|
342
|
+
| 'UNION'
|
|
343
|
+
| 'UNION ALL'
|
|
344
|
+
| 'INTERSECT'
|
|
345
|
+
| 'INTERSECT ALL'
|
|
346
|
+
| 'EXCEPT'
|
|
347
|
+
| 'EXCEPT ALL';
|
|
348
|
+
|
|
349
|
+
export type OnConflictItem = string | string[] | RawExpression;
|
|
350
|
+
|
|
351
|
+
export type OnConflictMergeUpdate =
|
|
352
|
+
| string
|
|
353
|
+
| string[]
|
|
354
|
+
| Record<string, unknown>
|
|
355
|
+
| RawExpression;
|