pqb 0.57.1 → 0.57.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/dist/index.d.ts +1829 -47
- package/dist/index.js +1864 -528
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1337 -98
- package/dist/index.mjs.map +1 -1
- package/dist/node-postgres.d.ts +1074 -4
- package/dist/node-postgres.js +37 -5
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs +33 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +1076 -6
- package/dist/postgres-js.js +40 -8
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs +33 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +3 -11
package/dist/index.js
CHANGED
|
@@ -1,10 +1,1053 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
-
var
|
|
3
|
+
var url = require('url');
|
|
4
|
+
var path = require('node:path');
|
|
5
|
+
var promises = require('timers/promises');
|
|
4
6
|
var node_util = require('node:util');
|
|
5
7
|
var node_async_hooks = require('node:async_hooks');
|
|
6
8
|
var pqb = require('pqb');
|
|
7
9
|
|
|
10
|
+
const colors = {
|
|
11
|
+
yellow: (s) => `\x1B[33m${s}\x1B[0m`,
|
|
12
|
+
green: (s) => `\x1B[32m${s}\x1B[0m`,
|
|
13
|
+
red: (s) => `\x1B[31m${s}\x1B[0m`,
|
|
14
|
+
blue: (s) => `\x1B[34m${s}\x1B[0m`,
|
|
15
|
+
bright: (s) => `\x1B[1m${s}\x1B[0m`,
|
|
16
|
+
blueBold: (s) => `\x1B[1m\x1B[34m${s}\x1B[0m`,
|
|
17
|
+
yellowBold: (s) => `\x1B[1m\x1B[33m${s}\x1B[0m`,
|
|
18
|
+
greenBold: (s) => `\x1B[1m\x1B[32m${s}\x1B[0m`,
|
|
19
|
+
pale: (s) => `\x1B[2m${s}\x1B[0m`
|
|
20
|
+
};
|
|
21
|
+
|
|
22
|
+
function applyMixins(derivedCtor, constructors) {
|
|
23
|
+
constructors.forEach((baseCtor) => {
|
|
24
|
+
Object.getOwnPropertyNames(baseCtor.prototype).forEach((name) => {
|
|
25
|
+
Object.defineProperty(
|
|
26
|
+
derivedCtor.prototype,
|
|
27
|
+
name,
|
|
28
|
+
Object.getOwnPropertyDescriptor(baseCtor.prototype, name) || /* @__PURE__ */ Object.create(null)
|
|
29
|
+
);
|
|
30
|
+
});
|
|
31
|
+
});
|
|
32
|
+
}
|
|
33
|
+
const joinTruthy = (...strings) => {
|
|
34
|
+
return strings.filter((string) => string).join("");
|
|
35
|
+
};
|
|
36
|
+
const toArray = (item) => Array.isArray(item) ? item : [item];
|
|
37
|
+
const noop = () => {
|
|
38
|
+
};
|
|
39
|
+
const returnArg = (a) => a;
|
|
40
|
+
const emptyObject = {};
|
|
41
|
+
const emptyArray = [];
|
|
42
|
+
const pushOrNewArrayToObjectImmutable = (obj, key, value) => {
|
|
43
|
+
obj[key] = obj[key] ? [...obj[key], value] : [value];
|
|
44
|
+
};
|
|
45
|
+
const setObjectValueImmutable = (q, object, key, value) => {
|
|
46
|
+
q[object] = {
|
|
47
|
+
...q[object],
|
|
48
|
+
[key]: value
|
|
49
|
+
};
|
|
50
|
+
return q;
|
|
51
|
+
};
|
|
52
|
+
const spreadObjectValues = (q, object, value) => {
|
|
53
|
+
q[object] = {
|
|
54
|
+
...q[object],
|
|
55
|
+
...value
|
|
56
|
+
};
|
|
57
|
+
};
|
|
58
|
+
const pushOrNewArray = (arr, value) => {
|
|
59
|
+
if (arr) {
|
|
60
|
+
arr.push(value);
|
|
61
|
+
return arr;
|
|
62
|
+
} else {
|
|
63
|
+
return [value];
|
|
64
|
+
}
|
|
65
|
+
};
|
|
66
|
+
const singleQuote = (s) => {
|
|
67
|
+
return `'${s.replaceAll("\\", "\\\\").replaceAll("'", "\\'")}'`;
|
|
68
|
+
};
|
|
69
|
+
const backtickQuote = (s) => {
|
|
70
|
+
return `\`${s.replaceAll("\\", "\\\\").replaceAll("`", "\\`")}\``;
|
|
71
|
+
};
|
|
72
|
+
const singleQuoteArray = (arr) => {
|
|
73
|
+
return `[${arr.map(singleQuote).join(", ")}]`;
|
|
74
|
+
};
|
|
75
|
+
const quoteObjectKey = (key, toCamel) => {
|
|
76
|
+
if (toCamel) key = toCamelCase(key);
|
|
77
|
+
return /^[a-zA-Z_$][\w$]*$/.test(key) ? key : singleQuote(key);
|
|
78
|
+
};
|
|
79
|
+
const isObjectEmpty = (obj) => !objectHasValues(obj);
|
|
80
|
+
const objectHasValues = (obj) => {
|
|
81
|
+
if (!obj) return false;
|
|
82
|
+
for (const key in obj) {
|
|
83
|
+
if (obj[key] !== void 0) return true;
|
|
84
|
+
}
|
|
85
|
+
return false;
|
|
86
|
+
};
|
|
87
|
+
const pathToLog = (path2) => {
|
|
88
|
+
return process.platform === "win32" ? path2 : url.pathToFileURL(path2).toString();
|
|
89
|
+
};
|
|
90
|
+
const toCamelCase = (str) => {
|
|
91
|
+
return str.replace(/^_+/g, "").replace(/_+./g, (a) => a[a.length - 1].toUpperCase()).replace(/_+$/g, "");
|
|
92
|
+
};
|
|
93
|
+
const toPascalCase = (str) => {
|
|
94
|
+
const camel = toCamelCase(str);
|
|
95
|
+
return camel[0].toUpperCase() + camel.slice(1);
|
|
96
|
+
};
|
|
97
|
+
const toSnakeCase = (str) => {
|
|
98
|
+
return str.replace(/[A-Z]/g, (a) => `_${a.toLowerCase()}`);
|
|
99
|
+
};
|
|
100
|
+
const deepCompare = (a, b) => {
|
|
101
|
+
if (a === b) return true;
|
|
102
|
+
if (typeof a !== typeof b) {
|
|
103
|
+
if (a === void 0 && typeof b === "object") {
|
|
104
|
+
a = emptyObject;
|
|
105
|
+
} else if (typeof a === "object" && b === void 0) {
|
|
106
|
+
b = emptyObject;
|
|
107
|
+
} else {
|
|
108
|
+
return false;
|
|
109
|
+
}
|
|
110
|
+
}
|
|
111
|
+
if (typeof a === "object") {
|
|
112
|
+
if (a === null) return b === null;
|
|
113
|
+
if (Array.isArray(a)) {
|
|
114
|
+
if (!Array.isArray(b) || a.length !== b.length) return false;
|
|
115
|
+
return a.every((item, i) => deepCompare(item, b[i]));
|
|
116
|
+
}
|
|
117
|
+
for (const key in a) {
|
|
118
|
+
if (!deepCompare(a[key], b[key]))
|
|
119
|
+
return false;
|
|
120
|
+
}
|
|
121
|
+
for (const key in b) {
|
|
122
|
+
if (!(key in a) && b[key] !== void 0) return false;
|
|
123
|
+
}
|
|
124
|
+
return true;
|
|
125
|
+
}
|
|
126
|
+
return a === b;
|
|
127
|
+
};
|
|
128
|
+
const getImportPath = (from, to) => {
|
|
129
|
+
const rel = path.relative(path.dirname(from), to).split(path.sep).join(path.posix.sep);
|
|
130
|
+
const importPath = rel.startsWith("./") || rel.startsWith("../") ? rel : `./${rel}`;
|
|
131
|
+
return importPath.replace(/\.[tj]s$/, "");
|
|
132
|
+
};
|
|
133
|
+
const getStackTrace = () => {
|
|
134
|
+
let stack;
|
|
135
|
+
const original = Error.prepareStackTrace;
|
|
136
|
+
Error.prepareStackTrace = (_, s) => stack = s;
|
|
137
|
+
new Error().stack;
|
|
138
|
+
Error.prepareStackTrace = original;
|
|
139
|
+
return stack;
|
|
140
|
+
};
|
|
141
|
+
const getCallerFilePath = (stack = getStackTrace()) => {
|
|
142
|
+
if (stack) {
|
|
143
|
+
const coreLibFile = stack[0]?.getFileName();
|
|
144
|
+
let i = 1;
|
|
145
|
+
if (stack[1]?.getFileName() === coreLibFile) {
|
|
146
|
+
i++;
|
|
147
|
+
}
|
|
148
|
+
const libFile = stack[i]?.getFileName();
|
|
149
|
+
const libDir = libFile && path.dirname(libFile);
|
|
150
|
+
for (; i < stack.length; i++) {
|
|
151
|
+
const item = stack[i];
|
|
152
|
+
let file = item.getFileName();
|
|
153
|
+
if (!file || // skip files in the caller orchid library
|
|
154
|
+
path.dirname(file) === libDir || // skip any files in the node_modules
|
|
155
|
+
/\bnode_modules\b/.test(file)) {
|
|
156
|
+
continue;
|
|
157
|
+
}
|
|
158
|
+
if (/file:\/\/\/\w+:\//.test(file)) {
|
|
159
|
+
file = decodeURI(file.slice(8));
|
|
160
|
+
} else {
|
|
161
|
+
try {
|
|
162
|
+
file = new URL(file).pathname;
|
|
163
|
+
} catch (_) {
|
|
164
|
+
}
|
|
165
|
+
}
|
|
166
|
+
return file;
|
|
167
|
+
}
|
|
168
|
+
}
|
|
169
|
+
return;
|
|
170
|
+
};
|
|
171
|
+
const callWithThis = function(cb) {
|
|
172
|
+
return cb(this);
|
|
173
|
+
};
|
|
174
|
+
const pick = (obj, keys) => {
|
|
175
|
+
const res = {};
|
|
176
|
+
for (const key of keys) {
|
|
177
|
+
res[key] = obj[key];
|
|
178
|
+
}
|
|
179
|
+
return res;
|
|
180
|
+
};
|
|
181
|
+
const omit = (obj, keys) => {
|
|
182
|
+
const res = { ...obj };
|
|
183
|
+
for (const key of keys) {
|
|
184
|
+
delete res[key];
|
|
185
|
+
}
|
|
186
|
+
return res;
|
|
187
|
+
};
|
|
188
|
+
const addValue = (values, value) => {
|
|
189
|
+
values.push(value);
|
|
190
|
+
return `$${values.length}`;
|
|
191
|
+
};
|
|
192
|
+
const getFreeAlias = (obj, as) => {
|
|
193
|
+
if (obj?.[as]) {
|
|
194
|
+
let suffix = 2;
|
|
195
|
+
let name;
|
|
196
|
+
while (obj[name = as + suffix]) {
|
|
197
|
+
suffix++;
|
|
198
|
+
}
|
|
199
|
+
as = name;
|
|
200
|
+
}
|
|
201
|
+
return as;
|
|
202
|
+
};
|
|
203
|
+
const getFreeSetAlias = (set, as, start = 2) => {
|
|
204
|
+
if (set.has(as)) {
|
|
205
|
+
let suffix = start;
|
|
206
|
+
let name;
|
|
207
|
+
while (set.has(name = as + suffix)) {
|
|
208
|
+
suffix++;
|
|
209
|
+
}
|
|
210
|
+
as = name;
|
|
211
|
+
}
|
|
212
|
+
return as;
|
|
213
|
+
};
|
|
214
|
+
const exhaustive = (_) => {
|
|
215
|
+
throw new Error("Condition was not exhaustive");
|
|
216
|
+
};
|
|
217
|
+
const pluralize = (w, count, append = "s") => {
|
|
218
|
+
return count === 1 ? w : w + append;
|
|
219
|
+
};
|
|
220
|
+
const isIterable = (x) => !!(x && typeof x === "object" && typeof x[Symbol.iterator] === "function");
|
|
221
|
+
|
|
222
|
+
const setConnectRetryConfig = (adapter, config) => {
|
|
223
|
+
adapter.connectRetryConfig = {
|
|
224
|
+
attempts: config.attempts ?? 10,
|
|
225
|
+
strategy: typeof config.strategy === "function" ? config.strategy : defaultConnectRetryStrategy(config.strategy ?? emptyObject)
|
|
226
|
+
};
|
|
227
|
+
};
|
|
228
|
+
const wrapAdapterFnWithConnectRetry = (adapter, fn) => {
|
|
229
|
+
return async function(...args) {
|
|
230
|
+
let attempt = 1;
|
|
231
|
+
for (; ; ) {
|
|
232
|
+
try {
|
|
233
|
+
return await fn.call(this, ...args);
|
|
234
|
+
} catch (err) {
|
|
235
|
+
const config = adapter.connectRetryConfig;
|
|
236
|
+
if (!err || typeof err !== "object" || err.code !== "ECONNREFUSED" || !config || attempt >= config.attempts) {
|
|
237
|
+
throw err;
|
|
238
|
+
}
|
|
239
|
+
await config.strategy(attempt, config.attempts);
|
|
240
|
+
attempt++;
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
};
|
|
244
|
+
};
|
|
245
|
+
const defaultConnectRetryStrategy = (param) => {
|
|
246
|
+
return (attempt) => promises.setTimeout((param.factor ?? 1.5) ** (attempt - 1) * (param.delay ?? 50));
|
|
247
|
+
};
|
|
248
|
+
|
|
249
|
+
const getValueKey = Symbol("get");
|
|
250
|
+
const applyTransforms = (queryData, returnType, fns, result) => {
|
|
251
|
+
for (const fn of fns) {
|
|
252
|
+
if ("map" in fn) {
|
|
253
|
+
if (!returnType || returnType === "all" || returnType === "pluck") {
|
|
254
|
+
result = result.map(fn.map, fn.thisArg);
|
|
255
|
+
} else if (result !== void 0) {
|
|
256
|
+
result = result === null ? null : fn.map.call(fn.thisArg, result, 0, result);
|
|
257
|
+
}
|
|
258
|
+
} else {
|
|
259
|
+
result = fn(result, queryData);
|
|
260
|
+
}
|
|
261
|
+
}
|
|
262
|
+
return result;
|
|
263
|
+
};
|
|
264
|
+
const pushQueryValueImmutable = (q, key, value) => {
|
|
265
|
+
pushOrNewArrayToObjectImmutable(
|
|
266
|
+
q.q,
|
|
267
|
+
key,
|
|
268
|
+
value
|
|
269
|
+
);
|
|
270
|
+
return q;
|
|
271
|
+
};
|
|
272
|
+
|
|
273
|
+
const numberMethodNames = ["gt", "gte", "lt", "lte", "step", "int", "finite", "safe"];
|
|
274
|
+
const stringMethodNames = [
|
|
275
|
+
"nonEmpty",
|
|
276
|
+
"min",
|
|
277
|
+
"max",
|
|
278
|
+
"length",
|
|
279
|
+
"email",
|
|
280
|
+
"url",
|
|
281
|
+
"emoji",
|
|
282
|
+
"uuid",
|
|
283
|
+
"cuid",
|
|
284
|
+
"cuid2",
|
|
285
|
+
"ulid",
|
|
286
|
+
"regex",
|
|
287
|
+
"includes",
|
|
288
|
+
"startsWith",
|
|
289
|
+
"endsWith",
|
|
290
|
+
"datetime",
|
|
291
|
+
"ipv4",
|
|
292
|
+
"ipv6",
|
|
293
|
+
"trim",
|
|
294
|
+
"toLowerCase",
|
|
295
|
+
"toUpperCase"
|
|
296
|
+
];
|
|
297
|
+
const dateMethodNames = ["min", "max"];
|
|
298
|
+
const arrayMethodNames = [
|
|
299
|
+
"min",
|
|
300
|
+
"max",
|
|
301
|
+
"length",
|
|
302
|
+
"nonEmpty"
|
|
303
|
+
];
|
|
304
|
+
|
|
305
|
+
class Expression {
|
|
306
|
+
// Produce SQL string by calling `makeSQL` and applying operators from the `q.chain`, push query variables into given `values` array.
|
|
307
|
+
toSQL(ctx, quotedAs) {
|
|
308
|
+
let sql = this.makeSQL(ctx, quotedAs);
|
|
309
|
+
if (this.q.chain) {
|
|
310
|
+
const { chain } = this.q;
|
|
311
|
+
for (let i = 0, len = chain.length; i < len; i += 2) {
|
|
312
|
+
sql = chain[i](
|
|
313
|
+
sql,
|
|
314
|
+
chain[i + 1],
|
|
315
|
+
ctx,
|
|
316
|
+
quotedAs
|
|
317
|
+
);
|
|
318
|
+
}
|
|
319
|
+
}
|
|
320
|
+
return sql;
|
|
321
|
+
}
|
|
322
|
+
}
|
|
323
|
+
const isExpression = (arg) => arg instanceof Expression;
|
|
324
|
+
const isTemplateLiteralArgs = (args) => Array.isArray(args[0]) && "raw" in args[0] && Array.isArray(args[0].raw);
|
|
325
|
+
class ExpressionTypeMethod {
|
|
326
|
+
// Define the resulting column type for the raw SQL.
|
|
327
|
+
type(fn) {
|
|
328
|
+
const column = fn(this.columnTypes);
|
|
329
|
+
this.q.expr.result.value = column;
|
|
330
|
+
Object.assign(
|
|
331
|
+
"baseQuery" in this ? this.baseQuery : this,
|
|
332
|
+
column.operators
|
|
333
|
+
);
|
|
334
|
+
return this;
|
|
335
|
+
}
|
|
336
|
+
}
|
|
337
|
+
class RawSQLBase extends Expression {
|
|
338
|
+
constructor(_sql, _values) {
|
|
339
|
+
super();
|
|
340
|
+
this._sql = _sql;
|
|
341
|
+
this._values = _values;
|
|
342
|
+
this.q = { expr: this };
|
|
343
|
+
}
|
|
344
|
+
// Attach query variables to the raw SQL.
|
|
345
|
+
values(values) {
|
|
346
|
+
this._values = values;
|
|
347
|
+
return this;
|
|
348
|
+
}
|
|
349
|
+
// Convert raw SQL to code for a code generator.
|
|
350
|
+
toCode(t) {
|
|
351
|
+
const { _sql: sql, _values: values } = this;
|
|
352
|
+
let code = `${t}.sql`;
|
|
353
|
+
code += typeof sql === "string" ? `({ raw: '${sql.replace(/'/g, "\\'")}' })` : templateLiteralSQLToCode(sql);
|
|
354
|
+
if (values) {
|
|
355
|
+
code += `.values(${JSON.stringify(values)})`;
|
|
356
|
+
}
|
|
357
|
+
return code;
|
|
358
|
+
}
|
|
359
|
+
}
|
|
360
|
+
const templateLiteralSQLToCode = (sql) => {
|
|
361
|
+
let code = "`";
|
|
362
|
+
const parts = sql[0];
|
|
363
|
+
let i = 0;
|
|
364
|
+
for (let last = parts.length - 1; i < last; i++) {
|
|
365
|
+
code += parts[i] + `\${${sql[i + 1]}}`;
|
|
366
|
+
}
|
|
367
|
+
code += parts[i];
|
|
368
|
+
return code + "`";
|
|
369
|
+
};
|
|
370
|
+
RawSQLBase.prototype.type = ExpressionTypeMethod.prototype.type;
|
|
371
|
+
const isRawSQL = (arg) => arg instanceof RawSQLBase;
|
|
372
|
+
class ValExpression extends Expression {
|
|
373
|
+
constructor(value) {
|
|
374
|
+
super();
|
|
375
|
+
this.value = value;
|
|
376
|
+
// TODO: move unknown column to core and use it here
|
|
377
|
+
this.result = { value: emptyObject };
|
|
378
|
+
this.q = { expr: this };
|
|
379
|
+
}
|
|
380
|
+
makeSQL(ctx) {
|
|
381
|
+
return addValue(ctx.values, this.value);
|
|
382
|
+
}
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
const addCode = (code, add) => {
|
|
386
|
+
if (typeof add === "object") {
|
|
387
|
+
code.push(add);
|
|
388
|
+
} else {
|
|
389
|
+
const last = code.length - 1;
|
|
390
|
+
if (typeof code[last] === "string") {
|
|
391
|
+
code[last] = code[last] + add;
|
|
392
|
+
} else {
|
|
393
|
+
code.push(add);
|
|
394
|
+
}
|
|
395
|
+
}
|
|
396
|
+
};
|
|
397
|
+
const codeToString = (code, tabs, shift) => {
|
|
398
|
+
if (typeof code === "string") return `${tabs}${code}`;
|
|
399
|
+
const lines = [];
|
|
400
|
+
for (const item of code) {
|
|
401
|
+
if (typeof item === "string") {
|
|
402
|
+
lines.push(`${tabs}${item}`);
|
|
403
|
+
} else {
|
|
404
|
+
lines.push(codeToString(item, tabs + shift, shift));
|
|
405
|
+
}
|
|
406
|
+
}
|
|
407
|
+
return lines.length ? lines.join("\n") : "";
|
|
408
|
+
};
|
|
409
|
+
const columnDefaultArgumentToCode = (t, value) => {
|
|
410
|
+
if (typeof value === "object" && value && isRawSQL(value)) {
|
|
411
|
+
return value.toCode(t);
|
|
412
|
+
} else if (typeof value === "function") {
|
|
413
|
+
return value.toString();
|
|
414
|
+
} else if (typeof value === "string") {
|
|
415
|
+
return singleQuote(value);
|
|
416
|
+
} else {
|
|
417
|
+
return JSON.stringify(value);
|
|
418
|
+
}
|
|
419
|
+
};
|
|
420
|
+
const columnMethodsToCode = (methodNames, skip, aliases) => {
|
|
421
|
+
return (data, migration, skipLocal) => {
|
|
422
|
+
return migration ? "" : methodNames.map(
|
|
423
|
+
(key) => (skipLocal || skip)?.[key] || key === "min" && data.nonEmpty && data.min === 1 ? "" : columnMethodToCode(data, key, aliases?.[key])
|
|
424
|
+
).join("");
|
|
425
|
+
};
|
|
426
|
+
};
|
|
427
|
+
const columnMethodToCode = (data, key, name = key) => {
|
|
428
|
+
const param = data[key];
|
|
429
|
+
if (param === void 0) return "";
|
|
430
|
+
const error = data.errors?.[key];
|
|
431
|
+
let params;
|
|
432
|
+
if (typeof param === "object" && param && param?.constructor === Object) {
|
|
433
|
+
const props = [];
|
|
434
|
+
for (const key2 in param) {
|
|
435
|
+
if (key2 === "message") continue;
|
|
436
|
+
const value = param[key2];
|
|
437
|
+
if (value !== void 0) {
|
|
438
|
+
props.push(
|
|
439
|
+
`${key2}: ${typeof value === "string" ? singleQuote(value) : value}`
|
|
440
|
+
);
|
|
441
|
+
}
|
|
442
|
+
}
|
|
443
|
+
if (error) props.push(`message: ${singleQuote(error)}`);
|
|
444
|
+
params = props.length ? `{ ${props.join(", ")} }` : "";
|
|
445
|
+
} else {
|
|
446
|
+
params = param === true ? "" : typeof param === "string" ? singleQuote(param) : param instanceof Date ? `new Date('${param.toISOString()}')` : param;
|
|
447
|
+
if (error) {
|
|
448
|
+
if (param !== true) params += ", ";
|
|
449
|
+
params += singleQuote(error);
|
|
450
|
+
}
|
|
451
|
+
}
|
|
452
|
+
return `.${name}(${params})`;
|
|
453
|
+
};
|
|
454
|
+
const stringDataToCode = columnMethodsToCode(stringMethodNames);
|
|
455
|
+
const numberDataToCode = columnMethodsToCode(
|
|
456
|
+
numberMethodNames,
|
|
457
|
+
void 0,
|
|
458
|
+
{ lte: "max", gte: "min" }
|
|
459
|
+
);
|
|
460
|
+
const dateDataToCode = columnMethodsToCode(dateMethodNames);
|
|
461
|
+
const arrayDataToCode = columnMethodsToCode(arrayMethodNames);
|
|
462
|
+
const columnErrorMessagesToCode = (errors) => {
|
|
463
|
+
const props = [];
|
|
464
|
+
if (errors.required) {
|
|
465
|
+
props.push(`required: ${singleQuote(errors.required)},`);
|
|
466
|
+
}
|
|
467
|
+
if (errors.invalidType) {
|
|
468
|
+
props.push(`invalidType: ${singleQuote(errors.invalidType)},`);
|
|
469
|
+
}
|
|
470
|
+
const code = [];
|
|
471
|
+
if (!props.length) return code;
|
|
472
|
+
addCode(code, ".error({");
|
|
473
|
+
code.push(props);
|
|
474
|
+
addCode(code, "})");
|
|
475
|
+
return code;
|
|
476
|
+
};
|
|
477
|
+
|
|
478
|
+
function makeColumnNullable(column, inputSchema, outputSchema, querySchema) {
|
|
479
|
+
const c = setColumnData(column, "isNullable", true);
|
|
480
|
+
c.inputSchema = inputSchema;
|
|
481
|
+
c.outputSchema = outputSchema;
|
|
482
|
+
c.querySchema = querySchema;
|
|
483
|
+
return c;
|
|
484
|
+
}
|
|
485
|
+
const setColumnData = (q, key, value) => {
|
|
486
|
+
const cloned = Object.create(q);
|
|
487
|
+
cloned.data = { ...q.data, [key]: value };
|
|
488
|
+
return cloned;
|
|
489
|
+
};
|
|
490
|
+
const pushColumnData = (q, key, value) => {
|
|
491
|
+
const arr = q.data[key];
|
|
492
|
+
return setColumnData(
|
|
493
|
+
q,
|
|
494
|
+
key,
|
|
495
|
+
arr ? [...arr, value] : [value]
|
|
496
|
+
);
|
|
497
|
+
};
|
|
498
|
+
const setDataValue = (item, key, value, params) => {
|
|
499
|
+
var _a;
|
|
500
|
+
const cloned = Object.create(item);
|
|
501
|
+
cloned.data = { ...item.data, [key]: value };
|
|
502
|
+
if (params && (typeof params === "string" || params.message)) {
|
|
503
|
+
((_a = cloned.data).errors ?? (_a.errors = {}))[key] = typeof params === "string" ? params : params.message;
|
|
504
|
+
}
|
|
505
|
+
return cloned;
|
|
506
|
+
};
|
|
507
|
+
let currentName;
|
|
508
|
+
function setCurrentColumnName(name) {
|
|
509
|
+
currentName = name;
|
|
510
|
+
}
|
|
511
|
+
const consumeColumnName = () => {
|
|
512
|
+
const name = currentName;
|
|
513
|
+
currentName = void 0;
|
|
514
|
+
return name;
|
|
515
|
+
};
|
|
516
|
+
const defaultNowFn = "now()";
|
|
517
|
+
let currentNowFn = defaultNowFn;
|
|
518
|
+
const setDefaultNowFn = (sql) => {
|
|
519
|
+
currentNowFn = `(${sql})`;
|
|
520
|
+
};
|
|
521
|
+
const getDefaultNowFn = () => currentNowFn;
|
|
522
|
+
let defaultLanguage = "english";
|
|
523
|
+
const setDefaultLanguage = (lang) => {
|
|
524
|
+
defaultLanguage = lang || "english";
|
|
525
|
+
};
|
|
526
|
+
const getDefaultLanguage = () => defaultLanguage;
|
|
527
|
+
class ColumnTypeBase {
|
|
528
|
+
constructor(schema, inputSchema, outputSchema = inputSchema, querySchema = inputSchema) {
|
|
529
|
+
this.inputSchema = inputSchema;
|
|
530
|
+
this.outputSchema = outputSchema;
|
|
531
|
+
this.querySchema = querySchema;
|
|
532
|
+
this.parse = schema.parse;
|
|
533
|
+
this.parseNull = schema.parseNull;
|
|
534
|
+
this.encode = schema.encode;
|
|
535
|
+
this.asType = schema.asType;
|
|
536
|
+
this.narrowType = schema.narrowType;
|
|
537
|
+
this.narrowAllTypes = schema.narrowAllTypes;
|
|
538
|
+
this.nullable = schema.nullable;
|
|
539
|
+
this.error = schema.error;
|
|
540
|
+
const name = consumeColumnName();
|
|
541
|
+
this.data = name ? { name } : {};
|
|
542
|
+
}
|
|
543
|
+
/**
|
|
544
|
+
* Set a default value to a column. Columns that have defaults become optional when creating a record.
|
|
545
|
+
*
|
|
546
|
+
* If you provide a value or a raw SQL, such default should be set on the column in migration to be applied on a database level.
|
|
547
|
+
*
|
|
548
|
+
* Or you can specify a callback that returns a value. This function will be called for each creating record. Such a default won't be applied to a database.
|
|
549
|
+
*
|
|
550
|
+
* ```ts
|
|
551
|
+
* export class Table extends BaseTable {
|
|
552
|
+
* readonly table = 'table';
|
|
553
|
+
* columns = this.setColumns((t) => ({
|
|
554
|
+
* // values as defaults:
|
|
555
|
+
* int: t.integer().default(123),
|
|
556
|
+
* text: t.text().default('text'),
|
|
557
|
+
*
|
|
558
|
+
* // raw SQL default:
|
|
559
|
+
* timestamp: t.timestamp().default(t.sql`now()`),
|
|
560
|
+
*
|
|
561
|
+
* // runtime default, each new records gets a new random value:
|
|
562
|
+
* random: t.numeric().default(() => Math.random()),
|
|
563
|
+
* }));
|
|
564
|
+
* }
|
|
565
|
+
* ```
|
|
566
|
+
*
|
|
567
|
+
* @param value - default value or a function returning a value
|
|
568
|
+
*/
|
|
569
|
+
default(value) {
|
|
570
|
+
return setColumnData(this, "default", value);
|
|
571
|
+
}
|
|
572
|
+
/**
|
|
573
|
+
* Use `hasDefault` to let the column be omitted when creating records.
|
|
574
|
+
*
|
|
575
|
+
* It's better to use {@link default} instead so the value is explicit and serves as a hint.
|
|
576
|
+
*/
|
|
577
|
+
hasDefault() {
|
|
578
|
+
return this;
|
|
579
|
+
}
|
|
580
|
+
/**
|
|
581
|
+
* Set a database-level validation check to a column. `check` accepts a raw SQL.
|
|
582
|
+
*
|
|
583
|
+
* ```ts
|
|
584
|
+
* import { change } from '../dbScript';
|
|
585
|
+
*
|
|
586
|
+
* change(async (db) => {
|
|
587
|
+
* await db.createTable('table', (t) => ({
|
|
588
|
+
* // validate rank to be from 1 to 10
|
|
589
|
+
* rank: t.integer().check(t.sql`1 >= "rank" AND "rank" <= 10`),
|
|
590
|
+
* // constraint name can be passed as a second argument
|
|
591
|
+
* column: t.integer().check(t.sql`...`, 'check_name'),
|
|
592
|
+
* // a single column can have multiple checks
|
|
593
|
+
* multiChecksColumn: t
|
|
594
|
+
* .integer()
|
|
595
|
+
* .check(t.sql`...`)
|
|
596
|
+
* .check(t.sql`...`, 'optional_name'),
|
|
597
|
+
* }));
|
|
598
|
+
* });
|
|
599
|
+
* ```
|
|
600
|
+
*
|
|
601
|
+
* @param sql - raw SQL expression
|
|
602
|
+
* @param name - to specify a constraint name
|
|
603
|
+
*/
|
|
604
|
+
check(sql, name) {
|
|
605
|
+
return pushColumnData(this, "checks", { sql, name });
|
|
606
|
+
}
|
|
607
|
+
/**
|
|
608
|
+
* This method changes a column type without modifying its behavior.
|
|
609
|
+
* This is needed when converting columns to a validation schema, the converter will pick a different type specified by `.as`.
|
|
610
|
+
*
|
|
611
|
+
* Before calling `.as` need to use `.encode` with the input of the same type as the input of the target column,
|
|
612
|
+
* and `.parse` which returns the correct type.
|
|
613
|
+
*
|
|
614
|
+
* ```ts
|
|
615
|
+
* // column has the same type as t.integer()
|
|
616
|
+
* const column = t
|
|
617
|
+
* .string()
|
|
618
|
+
* .encode((input: number) => input)
|
|
619
|
+
* .parse((text) => parseInt(text))
|
|
620
|
+
* .as(t.integer());
|
|
621
|
+
* ```
|
|
622
|
+
*
|
|
623
|
+
* @param column - other column type to inherit from
|
|
624
|
+
*/
|
|
625
|
+
as(column) {
|
|
626
|
+
return setColumnData(
|
|
627
|
+
this,
|
|
628
|
+
"as",
|
|
629
|
+
column
|
|
630
|
+
);
|
|
631
|
+
}
|
|
632
|
+
input(fn) {
|
|
633
|
+
const cloned = Object.create(this);
|
|
634
|
+
cloned.inputSchema = fn(this.inputSchema);
|
|
635
|
+
return cloned;
|
|
636
|
+
}
|
|
637
|
+
output(fn) {
|
|
638
|
+
const cloned = Object.create(this);
|
|
639
|
+
cloned.outputSchema = fn(this.outputSchema);
|
|
640
|
+
return cloned;
|
|
641
|
+
}
|
|
642
|
+
query(fn) {
|
|
643
|
+
const cloned = Object.create(this);
|
|
644
|
+
cloned.querySchema = fn(this.querySchema);
|
|
645
|
+
return cloned;
|
|
646
|
+
}
|
|
647
|
+
/**
|
|
648
|
+
* Set a database column name.
|
|
649
|
+
*
|
|
650
|
+
* @param name - name of the column in database.
|
|
651
|
+
*/
|
|
652
|
+
name(name) {
|
|
653
|
+
return setColumnData(this, "name", name);
|
|
654
|
+
}
|
|
655
|
+
/**
|
|
656
|
+
* Append `select(false)` to a column to exclude it from the default selection.
|
|
657
|
+
* It won't be selected with `selectAll` or `select('*')` as well.
|
|
658
|
+
*
|
|
659
|
+
* ```ts
|
|
660
|
+
* export class UserTable extends BaseTable {
|
|
661
|
+
* readonly table = 'user';
|
|
662
|
+
* columns = this.setColumns((t) => ({
|
|
663
|
+
* id: t.identity().primaryKey(),
|
|
664
|
+
* name: t.string(),
|
|
665
|
+
* password: t.string().select(false),
|
|
666
|
+
* }));
|
|
667
|
+
* }
|
|
668
|
+
*
|
|
669
|
+
* // only id and name are selected, without password
|
|
670
|
+
* const user = await db.user.find(123);
|
|
671
|
+
*
|
|
672
|
+
* // password is still omitted, even with the wildcard
|
|
673
|
+
* const same = await db.user.find(123).select('*');
|
|
674
|
+
*
|
|
675
|
+
* const comment = await db.comment.find(123).select({
|
|
676
|
+
* // password is omitted in the sub-selects as well
|
|
677
|
+
* author: (q) => q.author,
|
|
678
|
+
* });
|
|
679
|
+
*
|
|
680
|
+
* // password is omitted here as well
|
|
681
|
+
* const created = await db.user.create(userData);
|
|
682
|
+
* ```
|
|
683
|
+
*
|
|
684
|
+
* Such a column can only be selected explicitly.
|
|
685
|
+
*
|
|
686
|
+
* ```ts
|
|
687
|
+
* const userWithPassword = await db.user.find(123).select('*', 'password');
|
|
688
|
+
* ```
|
|
689
|
+
*/
|
|
690
|
+
select(value) {
|
|
691
|
+
return setColumnData(this, "explicitSelect", !value);
|
|
692
|
+
}
|
|
693
|
+
/**
|
|
694
|
+
* Forbid the column to be used in [create](/guide/create-update-delete.html#create-insert) and [update](/guide/create-update-delete.html#update) methods.
|
|
695
|
+
*
|
|
696
|
+
* `readOnly` column is still can be set from a [hook](http://localhost:5173/guide/hooks.html#set-values-before-create-or-update).
|
|
697
|
+
*
|
|
698
|
+
* `readOnly` column can be used together with a `default`.
|
|
699
|
+
*
|
|
700
|
+
* ```ts
|
|
701
|
+
* export class Table extends BaseTable {
|
|
702
|
+
* readonly table = 'table';
|
|
703
|
+
* columns = this.setColumns((t) => ({
|
|
704
|
+
* id: t.identity().primaryKey(),
|
|
705
|
+
* column: t.string().default(() => 'default value'),
|
|
706
|
+
* another: t.string().readOnly(),
|
|
707
|
+
* }));
|
|
708
|
+
*
|
|
709
|
+
* init(orm: typeof db) {
|
|
710
|
+
* this.beforeSave(({ set }) => {
|
|
711
|
+
* set({ another: 'value' });
|
|
712
|
+
* });
|
|
713
|
+
* }
|
|
714
|
+
* }
|
|
715
|
+
*
|
|
716
|
+
* // later in the code
|
|
717
|
+
* db.table.create({ column: 'value' }); // TS error, runtime error
|
|
718
|
+
* ```
|
|
719
|
+
*/
|
|
720
|
+
readOnly() {
|
|
721
|
+
return setColumnData(this, "appReadOnly", true);
|
|
722
|
+
}
|
|
723
|
+
/**
|
|
724
|
+
* Set a column value when creating a record.
|
|
725
|
+
* This works for [readOnly](#readonly) columns as well.
|
|
726
|
+
*
|
|
727
|
+
* If no value or undefined is returned, the hook won't have any effect.
|
|
728
|
+
*
|
|
729
|
+
* ```ts
|
|
730
|
+
* export class Table extends BaseTable {
|
|
731
|
+
* readonly table = 'table';
|
|
732
|
+
* columns = this.setColumns((t) => ({
|
|
733
|
+
* id: t.identity().primaryKey(),
|
|
734
|
+
* column: t.string().setOnCreate(() => 'value'),
|
|
735
|
+
* }));
|
|
736
|
+
* }
|
|
737
|
+
* ```
|
|
738
|
+
*/
|
|
739
|
+
setOnCreate(fn) {
|
|
740
|
+
return setColumnData(this, "setOnCreate", fn);
|
|
741
|
+
}
|
|
742
|
+
/**
|
|
743
|
+
* Set a column value when updating a record.
|
|
744
|
+
* This works for [readOnly](#readonly) columns as well.
|
|
745
|
+
*
|
|
746
|
+
* If no value or undefined is returned, the hook won't have any effect.
|
|
747
|
+
*
|
|
748
|
+
* ```ts
|
|
749
|
+
* export class Table extends BaseTable {
|
|
750
|
+
* readonly table = 'table';
|
|
751
|
+
* columns = this.setColumns((t) => ({
|
|
752
|
+
* id: t.identity().primaryKey(),
|
|
753
|
+
* column: t.string().setOnUpdate(() => 'value'),
|
|
754
|
+
* }));
|
|
755
|
+
* }
|
|
756
|
+
* ```
|
|
757
|
+
*/
|
|
758
|
+
setOnUpdate(fn) {
|
|
759
|
+
return setColumnData(this, "setOnUpdate", fn);
|
|
760
|
+
}
|
|
761
|
+
/**
|
|
762
|
+
* Set a column value when creating or updating a record.
|
|
763
|
+
* This works for [readOnly](#readonly) columns as well.
|
|
764
|
+
*
|
|
765
|
+
* If no value or undefined is returned, the hook won't have any effect.
|
|
766
|
+
*
|
|
767
|
+
* ```ts
|
|
768
|
+
* export class Table extends BaseTable {
|
|
769
|
+
* readonly table = 'table';
|
|
770
|
+
* columns = this.setColumns((t) => ({
|
|
771
|
+
* id: t.identity().primaryKey(),
|
|
772
|
+
* column: t.string().setOnSave(() => 'value'),
|
|
773
|
+
* }));
|
|
774
|
+
* }
|
|
775
|
+
* ```
|
|
776
|
+
*/
|
|
777
|
+
setOnSave(fn) {
|
|
778
|
+
return setColumnData(this, "setOnSave", fn);
|
|
779
|
+
}
|
|
780
|
+
}
|
|
781
|
+
|
|
782
|
+
const snakeCaseKey = Symbol("snakeCase");
|
|
783
|
+
|
|
784
|
+
class SimpleRawSQL extends RawSQLBase {
|
|
785
|
+
// Simply returning SQL provided in the constructor.
|
|
786
|
+
makeSQL() {
|
|
787
|
+
return this._sql;
|
|
788
|
+
}
|
|
789
|
+
}
|
|
790
|
+
const raw$1 = (sql) => new SimpleRawSQL(sql);
|
|
791
|
+
const makeTimestamps = (timestamp) => {
|
|
792
|
+
const now = getDefaultNowFn();
|
|
793
|
+
const nowRaw = raw$1(now);
|
|
794
|
+
const updatedAt = timestamp().default(nowRaw);
|
|
795
|
+
let updater;
|
|
796
|
+
updatedAt.data.modifyQuery = (q, column) => {
|
|
797
|
+
if (!updater) {
|
|
798
|
+
const key = column.data.key;
|
|
799
|
+
updater = (data) => {
|
|
800
|
+
if (data.some((item) => {
|
|
801
|
+
return typeof item !== "function" && item[key];
|
|
802
|
+
}))
|
|
803
|
+
return;
|
|
804
|
+
return { [column.data.key]: nowRaw };
|
|
805
|
+
};
|
|
806
|
+
}
|
|
807
|
+
pushOrNewArrayToObjectImmutable(
|
|
808
|
+
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
809
|
+
q.q,
|
|
810
|
+
"updateData",
|
|
811
|
+
updater
|
|
812
|
+
);
|
|
813
|
+
};
|
|
814
|
+
updatedAt.data.defaultTimestamp = "updatedAt";
|
|
815
|
+
const createdAt = timestamp().default(nowRaw);
|
|
816
|
+
createdAt.data.defaultTimestamp = "createdAt";
|
|
817
|
+
return {
|
|
818
|
+
createdAt,
|
|
819
|
+
updatedAt
|
|
820
|
+
};
|
|
821
|
+
};
|
|
822
|
+
const timestampHelpers = {
|
|
823
|
+
timestamps() {
|
|
824
|
+
return makeTimestamps(this.timestamp);
|
|
825
|
+
},
|
|
826
|
+
timestampsNoTZ() {
|
|
827
|
+
return makeTimestamps(this.timestampNoTZ);
|
|
828
|
+
}
|
|
829
|
+
};
|
|
830
|
+
|
|
831
|
+
const logColors = {
|
|
832
|
+
boldCyanBright: (message) => `\x1B[1m\x1B[96m${message}\x1B[39m\x1B[22m`,
|
|
833
|
+
boldBlue: (message) => `\x1B[1m\x1B[34m${message}\x1B[39m\x1B[22m`,
|
|
834
|
+
boldYellow: (message) => `\x1B[1m\x1B[33m${message}\x1B[39m\x1B[22m`,
|
|
835
|
+
boldMagenta: (message) => `\x1B[1m\x1B[33m${message}\x1B[39m\x1B[22m`,
|
|
836
|
+
boldRed: (message) => `\x1B[1m\x1B[31m${message}\x1B[39m\x1B[22m`
|
|
837
|
+
};
|
|
838
|
+
|
|
839
|
+
class QueryHookUtils {
|
|
840
|
+
constructor(query, columns, key) {
|
|
841
|
+
this.query = query;
|
|
842
|
+
this.columns = columns;
|
|
843
|
+
this.key = key;
|
|
844
|
+
this.set = (data) => {
|
|
845
|
+
const set = {};
|
|
846
|
+
for (const key in data) {
|
|
847
|
+
if (data[key] !== void 0) {
|
|
848
|
+
set[key] = data[key];
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
pushQueryValueImmutable(this.query, this.key, set);
|
|
852
|
+
};
|
|
853
|
+
}
|
|
854
|
+
}
|
|
855
|
+
|
|
856
|
+
const queryColumnNameToKey = (q, name) => {
|
|
857
|
+
let map = q.internal.columnNameToKeyMap;
|
|
858
|
+
if (!map) {
|
|
859
|
+
q.internal.columnNameToKeyMap = map = /* @__PURE__ */ new Map();
|
|
860
|
+
const { shape } = q;
|
|
861
|
+
for (const key in q.shape) {
|
|
862
|
+
const column = shape[key];
|
|
863
|
+
map.set(column.data.name ?? key, key);
|
|
864
|
+
}
|
|
865
|
+
}
|
|
866
|
+
return map.get(name);
|
|
867
|
+
};
|
|
868
|
+
|
|
869
|
+
var __typeError = (msg) => {
|
|
870
|
+
throw TypeError(msg);
|
|
871
|
+
};
|
|
872
|
+
var __accessCheck = (obj, member, msg) => member.has(obj) || __typeError("Cannot " + msg);
|
|
873
|
+
var __privateGet = (obj, member, getter) => (__accessCheck(obj, member, "read from private field"), getter ? getter.call(obj) : member.get(obj));
|
|
874
|
+
var __privateAdd = (obj, member, value) => member.has(obj) ? __typeError("Cannot add the same private member more than once") : member instanceof WeakSet ? member.add(obj) : member.set(obj, value);
|
|
875
|
+
var __privateSet = (obj, member, value, setter) => (__accessCheck(obj, member, "write to private field"), member.set(obj, value), value);
|
|
876
|
+
var _query, _query2;
|
|
877
|
+
class OrchidOrmError extends Error {
|
|
878
|
+
}
|
|
879
|
+
class NotFoundError extends OrchidOrmError {
|
|
880
|
+
constructor(query, message = "Record is not found") {
|
|
881
|
+
super(message);
|
|
882
|
+
// `#query` is private to prevent it from serializing to not cause problems to test runner reports
|
|
883
|
+
__privateAdd(this, _query);
|
|
884
|
+
__privateSet(this, _query, query);
|
|
885
|
+
}
|
|
886
|
+
get query() {
|
|
887
|
+
return __privateGet(this, _query);
|
|
888
|
+
}
|
|
889
|
+
}
|
|
890
|
+
_query = new WeakMap();
|
|
891
|
+
class OrchidOrmInternalError extends Error {
|
|
892
|
+
constructor(query, message, data) {
|
|
893
|
+
super(message);
|
|
894
|
+
this.data = data;
|
|
895
|
+
// `#query` is private to prevent it from serializing to not cause problems to test runner reports
|
|
896
|
+
__privateAdd(this, _query2);
|
|
897
|
+
__privateSet(this, _query2, query);
|
|
898
|
+
}
|
|
899
|
+
get query() {
|
|
900
|
+
return __privateGet(this, _query2);
|
|
901
|
+
}
|
|
902
|
+
}
|
|
903
|
+
_query2 = new WeakMap();
|
|
904
|
+
class QueryError extends OrchidOrmInternalError {
|
|
905
|
+
get isUnique() {
|
|
906
|
+
return this.code === "23505";
|
|
907
|
+
}
|
|
908
|
+
get columns() {
|
|
909
|
+
if (this.columnsCache) return this.columnsCache;
|
|
910
|
+
const columns = {};
|
|
911
|
+
if (this.detail) {
|
|
912
|
+
const list = this.detail.match(/\((.*)\)=/)?.[1];
|
|
913
|
+
if (list) {
|
|
914
|
+
list.split(", ").forEach((item) => {
|
|
915
|
+
const column = item.startsWith('"') ? item.slice(1, -1) : item;
|
|
916
|
+
const key = queryColumnNameToKey(this.query, column) ?? column;
|
|
917
|
+
columns[key] = true;
|
|
918
|
+
});
|
|
919
|
+
}
|
|
920
|
+
}
|
|
921
|
+
return this.columnsCache = columns;
|
|
922
|
+
}
|
|
923
|
+
}
|
|
924
|
+
class MoreThanOneRowError extends OrchidOrmInternalError {
|
|
925
|
+
constructor(query, message) {
|
|
926
|
+
super(query, message);
|
|
927
|
+
}
|
|
928
|
+
}
|
|
929
|
+
class UnhandledTypeError extends OrchidOrmInternalError {
|
|
930
|
+
constructor(query, value) {
|
|
931
|
+
super(query, `Unhandled type: ${JSON.stringify(value)} received`);
|
|
932
|
+
}
|
|
933
|
+
}
|
|
934
|
+
|
|
935
|
+
const getPrimaryKeys = (q) => {
|
|
936
|
+
var _a;
|
|
937
|
+
return (_a = q.internal).primaryKeys ?? (_a.primaryKeys = collectPrimaryKeys(q));
|
|
938
|
+
};
|
|
939
|
+
const requirePrimaryKeys = (q, message) => {
|
|
940
|
+
const primaryKeys = getPrimaryKeys(q);
|
|
941
|
+
if (!primaryKeys.length) {
|
|
942
|
+
throw new OrchidOrmInternalError(q, message);
|
|
943
|
+
}
|
|
944
|
+
return primaryKeys;
|
|
945
|
+
};
|
|
946
|
+
const collectPrimaryKeys = (q) => {
|
|
947
|
+
const primaryKeys = [];
|
|
948
|
+
const { shape } = q.q;
|
|
949
|
+
for (const key in shape) {
|
|
950
|
+
if (shape[key].data.primaryKey) {
|
|
951
|
+
primaryKeys.push(key);
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
const pkey = q.internal.tableData.primaryKey;
|
|
955
|
+
if (pkey) {
|
|
956
|
+
primaryKeys.push(...pkey.columns);
|
|
957
|
+
}
|
|
958
|
+
return primaryKeys;
|
|
959
|
+
};
|
|
960
|
+
|
|
961
|
+
const newDelayedRelationSelect = (query) => ({
|
|
962
|
+
query
|
|
963
|
+
});
|
|
964
|
+
const setDelayedRelation = (d, as, value) => {
|
|
965
|
+
(d.value ?? (d.value = {}))[as] = value;
|
|
966
|
+
};
|
|
967
|
+
|
|
968
|
+
const isRelationQuery = (q) => "joinQuery" in q;
|
|
969
|
+
|
|
970
|
+
const _getQueryAs = (q) => q.q.as;
|
|
971
|
+
const _getQueryFreeAlias = (q, as) => q.aliases ? getFreeAlias(q.aliases, as) : as;
|
|
972
|
+
const _checkIfAliased = (q, as, name) => {
|
|
973
|
+
return q.q.aliases?.[as] === name;
|
|
974
|
+
};
|
|
975
|
+
const _getQueryAliasOrName = (q, as) => {
|
|
976
|
+
return q.aliases?.[as] || as;
|
|
977
|
+
};
|
|
978
|
+
const _getQueryOuterAliases = (q) => {
|
|
979
|
+
return q.outerAliases;
|
|
980
|
+
};
|
|
981
|
+
const _setQueryAs = (self, as) => {
|
|
982
|
+
const { q } = self;
|
|
983
|
+
q.as = as;
|
|
984
|
+
q.aliases = {
|
|
985
|
+
...q.aliases,
|
|
986
|
+
[as]: _getQueryFreeAlias(q, as)
|
|
987
|
+
};
|
|
988
|
+
return self;
|
|
989
|
+
};
|
|
990
|
+
const _setQueryAlias = (q, name, as) => {
|
|
991
|
+
q.q.aliases = { ...q.q.aliases, [as]: name };
|
|
992
|
+
};
|
|
993
|
+
const _setSubQueryAliases = (q) => {
|
|
994
|
+
q.q.outerAliases = q.q.aliases;
|
|
995
|
+
};
|
|
996
|
+
const _applyRelationAliases = (query, relQueryData) => {
|
|
997
|
+
const aliases = query.q.as ? { ...query.q.aliases } : { ...query.q.aliases, [query.table]: query.table };
|
|
998
|
+
const relAliases = relQueryData.aliases;
|
|
999
|
+
for (const as in relAliases) {
|
|
1000
|
+
aliases[as] = getFreeAlias(aliases, as);
|
|
1001
|
+
}
|
|
1002
|
+
relQueryData.as = aliases[relQueryData.as];
|
|
1003
|
+
relQueryData.aliases = aliases;
|
|
1004
|
+
};
|
|
1005
|
+
const _copyQueryAliasToQuery = (fromQuery, toQuery, key) => {
|
|
1006
|
+
const name = _getQueryAliasOrName(fromQuery.q, key);
|
|
1007
|
+
if (name !== key) {
|
|
1008
|
+
_setQueryAlias(toQuery, name, key);
|
|
1009
|
+
}
|
|
1010
|
+
return name;
|
|
1011
|
+
};
|
|
1012
|
+
|
|
1013
|
+
const setParserToQuery = (query, key, parser) => {
|
|
1014
|
+
if (query.parsers) query.parsers[key] = parser;
|
|
1015
|
+
else query.parsers = { [key]: parser };
|
|
1016
|
+
};
|
|
1017
|
+
const getQueryParsers = (q, hookSelect) => {
|
|
1018
|
+
if (hookSelect) {
|
|
1019
|
+
const parsers = { ...q.q.parsers };
|
|
1020
|
+
const { defaultParsers } = q.q;
|
|
1021
|
+
if (defaultParsers) {
|
|
1022
|
+
for (const [key, value] of hookSelect) {
|
|
1023
|
+
const parser = defaultParsers[key];
|
|
1024
|
+
if (parser) {
|
|
1025
|
+
parsers[value.as || key] = parser;
|
|
1026
|
+
}
|
|
1027
|
+
}
|
|
1028
|
+
}
|
|
1029
|
+
return parsers;
|
|
1030
|
+
}
|
|
1031
|
+
return q.q.select ? q.q.parsers : q.q.defaultParsers;
|
|
1032
|
+
};
|
|
1033
|
+
|
|
1034
|
+
const _addToHookSelect = (query, selects) => {
|
|
1035
|
+
const map = query.q.hookSelect = new Map(
|
|
1036
|
+
query.q.hookSelect
|
|
1037
|
+
);
|
|
1038
|
+
for (const key of selects) {
|
|
1039
|
+
map.set(key, { select: key });
|
|
1040
|
+
}
|
|
1041
|
+
};
|
|
1042
|
+
const _addToHookSelectWithTable = (query, selects, table) => {
|
|
1043
|
+
const map = query.q.hookSelect = new Map(
|
|
1044
|
+
query.q.hookSelect
|
|
1045
|
+
);
|
|
1046
|
+
for (const column of selects) {
|
|
1047
|
+
map.set(column, { select: `${table}.${column}` });
|
|
1048
|
+
}
|
|
1049
|
+
};
|
|
1050
|
+
|
|
8
1051
|
const used = [];
|
|
9
1052
|
const literalValues = [];
|
|
10
1053
|
const templateLiteralToSQL = (template, ctx, quotedAs) => {
|
|
@@ -16,7 +1059,7 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
|
|
|
16
1059
|
for (let last = parts.length - 1; i < last; i++) {
|
|
17
1060
|
sql += parts[i];
|
|
18
1061
|
const value = template[i + 1];
|
|
19
|
-
if (value instanceof
|
|
1062
|
+
if (value instanceof Expression) {
|
|
20
1063
|
sql += value.toSQL(ctx, quotedAs);
|
|
21
1064
|
} else {
|
|
22
1065
|
values.push(value);
|
|
@@ -26,7 +1069,7 @@ const templateLiteralToSQL = (template, ctx, quotedAs) => {
|
|
|
26
1069
|
}
|
|
27
1070
|
return sql + parts[i];
|
|
28
1071
|
};
|
|
29
|
-
class RawSQL extends
|
|
1072
|
+
class RawSQL extends RawSQLBase {
|
|
30
1073
|
constructor(sql, values, type) {
|
|
31
1074
|
super(sql, values);
|
|
32
1075
|
this.result = { value: type };
|
|
@@ -84,11 +1127,11 @@ class RawSQL extends orchidCore.RawSQLBase {
|
|
|
84
1127
|
return arr.join("'");
|
|
85
1128
|
}
|
|
86
1129
|
}
|
|
87
|
-
class DynamicRawSQL extends
|
|
1130
|
+
class DynamicRawSQL extends Expression {
|
|
88
1131
|
constructor(fn) {
|
|
89
1132
|
super();
|
|
90
1133
|
this.fn = fn;
|
|
91
|
-
this.result =
|
|
1134
|
+
this.result = emptyObject;
|
|
92
1135
|
this.q = { expr: this };
|
|
93
1136
|
}
|
|
94
1137
|
// Calls the given function to get SQL from it.
|
|
@@ -96,9 +1139,9 @@ class DynamicRawSQL extends orchidCore.Expression {
|
|
|
96
1139
|
return this.fn(raw).toSQL(ctx, quotedAs);
|
|
97
1140
|
}
|
|
98
1141
|
}
|
|
99
|
-
DynamicRawSQL.prototype.type =
|
|
1142
|
+
DynamicRawSQL.prototype.type = ExpressionTypeMethod.prototype.type;
|
|
100
1143
|
function raw(...args) {
|
|
101
|
-
return
|
|
1144
|
+
return isTemplateLiteralArgs(args) ? new RawSQL(args) : typeof args[0] === "function" ? new DynamicRawSQL(args[0]) : new RawSQL(args[0].raw, args[0].values);
|
|
102
1145
|
}
|
|
103
1146
|
const countSelect = [new RawSQL("count(*)")];
|
|
104
1147
|
function sqlQueryArgsToExpression(args) {
|
|
@@ -118,7 +1161,7 @@ const sqlFn = (...args) => {
|
|
|
118
1161
|
return (...args2) => new RawSQL(args2, arg);
|
|
119
1162
|
};
|
|
120
1163
|
|
|
121
|
-
class ColumnType extends
|
|
1164
|
+
class ColumnType extends ColumnTypeBase {
|
|
122
1165
|
/**
|
|
123
1166
|
* Mark the column as a primary key.
|
|
124
1167
|
* This column type becomes an argument of the `.find` method.
|
|
@@ -144,10 +1187,10 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
144
1187
|
* @param name - to specify a constraint name
|
|
145
1188
|
*/
|
|
146
1189
|
primaryKey(name) {
|
|
147
|
-
return
|
|
1190
|
+
return setColumnData(this, "primaryKey", name ?? true);
|
|
148
1191
|
}
|
|
149
|
-
foreignKey(fnOrTable, column, options =
|
|
150
|
-
return
|
|
1192
|
+
foreignKey(fnOrTable, column, options = emptyObject) {
|
|
1193
|
+
return pushColumnData(this, "foreignKeys", {
|
|
151
1194
|
fnOrTable,
|
|
152
1195
|
foreignColumns: [column],
|
|
153
1196
|
options
|
|
@@ -206,8 +1249,8 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
206
1249
|
*/
|
|
207
1250
|
index(...args) {
|
|
208
1251
|
const a = args;
|
|
209
|
-
return
|
|
210
|
-
options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ??
|
|
1252
|
+
return pushColumnData(this, "indexes", {
|
|
1253
|
+
options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ?? emptyObject
|
|
211
1254
|
});
|
|
212
1255
|
}
|
|
213
1256
|
/**
|
|
@@ -314,7 +1357,7 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
314
1357
|
*/
|
|
315
1358
|
searchIndex(...args) {
|
|
316
1359
|
const a = args;
|
|
317
|
-
return
|
|
1360
|
+
return pushColumnData(this, "indexes", {
|
|
318
1361
|
options: {
|
|
319
1362
|
...typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0],
|
|
320
1363
|
...this.dataType === "tsvector" ? { using: "GIN" } : { tsVector: true }
|
|
@@ -323,7 +1366,7 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
323
1366
|
}
|
|
324
1367
|
unique(...args) {
|
|
325
1368
|
const a = args;
|
|
326
|
-
return
|
|
1369
|
+
return pushColumnData(this, "indexes", {
|
|
327
1370
|
options: {
|
|
328
1371
|
...typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0],
|
|
329
1372
|
unique: true
|
|
@@ -377,22 +1420,22 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
377
1420
|
*/
|
|
378
1421
|
exclude(op, ...args) {
|
|
379
1422
|
const a = args;
|
|
380
|
-
return
|
|
1423
|
+
return pushColumnData(this, "excludes", {
|
|
381
1424
|
with: op,
|
|
382
|
-
options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ??
|
|
1425
|
+
options: (typeof a[0] === "string" ? { ...a[1], name: a[0] } : a[0]) ?? emptyObject
|
|
383
1426
|
});
|
|
384
1427
|
}
|
|
385
1428
|
comment(comment) {
|
|
386
|
-
return
|
|
1429
|
+
return setColumnData(this, "comment", comment);
|
|
387
1430
|
}
|
|
388
1431
|
compression(compression) {
|
|
389
|
-
return
|
|
1432
|
+
return setColumnData(this, "compression", compression);
|
|
390
1433
|
}
|
|
391
1434
|
collate(collate) {
|
|
392
|
-
return
|
|
1435
|
+
return setColumnData(this, "collate", collate);
|
|
393
1436
|
}
|
|
394
1437
|
modifyQuery(cb) {
|
|
395
|
-
return
|
|
1438
|
+
return setColumnData(
|
|
396
1439
|
this,
|
|
397
1440
|
"modifyQuery",
|
|
398
1441
|
cb
|
|
@@ -415,14 +1458,14 @@ class ColumnType extends orchidCore.ColumnTypeBase {
|
|
|
415
1458
|
*/
|
|
416
1459
|
generated(...args) {
|
|
417
1460
|
const sql = raw(...args);
|
|
418
|
-
const column =
|
|
1461
|
+
const column = setColumnData(this, "generated", {
|
|
419
1462
|
toSQL(ctx, quoted) {
|
|
420
1463
|
return sql.toSQL(ctx, quoted);
|
|
421
1464
|
},
|
|
422
1465
|
toCode() {
|
|
423
1466
|
let sql2 = ".generated";
|
|
424
1467
|
if (Array.isArray(args[0])) {
|
|
425
|
-
sql2 +=
|
|
1468
|
+
sql2 += templateLiteralSQLToCode(args);
|
|
426
1469
|
} else {
|
|
427
1470
|
const { raw: raw2, values } = args[0];
|
|
428
1471
|
sql2 += `({ raw: '${raw2.replace(/'/g, "\\'")}'${values ? `, values: ${JSON.stringify(values)}` : ""} })`;
|
|
@@ -464,7 +1507,7 @@ const assignDbDataToColumn = (column, params) => {
|
|
|
464
1507
|
const isDefaultTimeStamp = (item) => {
|
|
465
1508
|
if (item.dataType !== "timestamptz") return false;
|
|
466
1509
|
const def = item.data.default;
|
|
467
|
-
if (!(def instanceof
|
|
1510
|
+
if (!(def instanceof RawSQLBase)) return false;
|
|
468
1511
|
return typeof def._sql === "string" && def._sql.startsWith("now()");
|
|
469
1512
|
};
|
|
470
1513
|
const combineCodeElements = (input) => {
|
|
@@ -494,8 +1537,8 @@ const columnsShapeToCode = (ctx, shape) => {
|
|
|
494
1537
|
if (name === key) column.data.name = void 0;
|
|
495
1538
|
code.push(
|
|
496
1539
|
...combineCodeElements([
|
|
497
|
-
`${
|
|
498
|
-
...
|
|
1540
|
+
`${quoteObjectKey(key, ctx.snakeCase)}: `,
|
|
1541
|
+
...toArray(shape[key].toCode(ctx, key)),
|
|
499
1542
|
","
|
|
500
1543
|
])
|
|
501
1544
|
);
|
|
@@ -509,9 +1552,9 @@ const columnsShapeToCode = (ctx, shape) => {
|
|
|
509
1552
|
const pushTableDataCode = (code, ast) => {
|
|
510
1553
|
const lines = [
|
|
511
1554
|
ast.primaryKey && [primaryKeyInnerToCode(ast.primaryKey, "t") + ","],
|
|
512
|
-
...ast.indexes?.map((x) => indexToCode(x, "t")) ||
|
|
513
|
-
...ast.excludes?.map((x) => excludeToCode(x, "t")) ||
|
|
514
|
-
...ast.constraints?.map((x) => constraintToCode(x, "t", true)) ||
|
|
1555
|
+
...ast.indexes?.map((x) => indexToCode(x, "t")) || emptyArray,
|
|
1556
|
+
...ast.excludes?.map((x) => excludeToCode(x, "t")) || emptyArray,
|
|
1557
|
+
...ast.constraints?.map((x) => constraintToCode(x, "t", true)) || emptyArray
|
|
515
1558
|
].filter((x) => !!x);
|
|
516
1559
|
if (lines.length > 1) {
|
|
517
1560
|
code.push("(t) => [", ...lines, "],");
|
|
@@ -524,13 +1567,13 @@ const pushTableDataCode = (code, ast) => {
|
|
|
524
1567
|
};
|
|
525
1568
|
const primaryKeyInnerToCode = (primaryKey, t) => {
|
|
526
1569
|
const name = primaryKey.name;
|
|
527
|
-
return `${t}.primaryKey([${primaryKey.columns.map(
|
|
1570
|
+
return `${t}.primaryKey([${primaryKey.columns.map(singleQuote).join(", ")}]${name ? `, ${singleQuote(name)}` : ""})`;
|
|
528
1571
|
};
|
|
529
1572
|
const indexOrExcludeToCode = (innerToCode) => (item, t, prefix) => {
|
|
530
1573
|
const code = innerToCode(item, t);
|
|
531
1574
|
if (prefix) code[0] = prefix + code[0];
|
|
532
1575
|
const last = code[code.length - 1];
|
|
533
|
-
if (typeof last === "string" && !last.endsWith(","))
|
|
1576
|
+
if (typeof last === "string" && !last.endsWith(",")) addCode(code, ",");
|
|
534
1577
|
return code;
|
|
535
1578
|
};
|
|
536
1579
|
const indexInnerToCode = (index, t) => {
|
|
@@ -570,17 +1613,17 @@ const indexInnerToCode = (index, t) => {
|
|
|
570
1613
|
}
|
|
571
1614
|
}
|
|
572
1615
|
if (!hasOptions2) {
|
|
573
|
-
objects.push(`${
|
|
1616
|
+
objects.push(`${singleQuote(expr)},`);
|
|
574
1617
|
} else {
|
|
575
1618
|
const props = [
|
|
576
|
-
`${"expression" in column ? "expression" : "column"}: ${
|
|
1619
|
+
`${"expression" in column ? "expression" : "column"}: ${singleQuote(
|
|
577
1620
|
expr
|
|
578
1621
|
)},`
|
|
579
1622
|
];
|
|
580
1623
|
for (const key of columnOptions) {
|
|
581
1624
|
const value = column[key];
|
|
582
1625
|
if (value !== void 0) {
|
|
583
|
-
props.push(`${key}: ${
|
|
1626
|
+
props.push(`${key}: ${singleQuote(value)},`);
|
|
584
1627
|
}
|
|
585
1628
|
}
|
|
586
1629
|
objects.push("{", props, "},");
|
|
@@ -588,16 +1631,16 @@ const indexInnerToCode = (index, t) => {
|
|
|
588
1631
|
}
|
|
589
1632
|
code.push(["[", objects, hasOptions ? "]," : "]"]);
|
|
590
1633
|
} else {
|
|
591
|
-
|
|
1634
|
+
addCode(
|
|
592
1635
|
code,
|
|
593
|
-
`[${index.columns.map((it) =>
|
|
1636
|
+
`[${index.columns.map((it) => singleQuote(it.column)).join(", ")}]`
|
|
594
1637
|
);
|
|
595
1638
|
}
|
|
596
1639
|
if (hasOptions) {
|
|
597
1640
|
if (columnsMultiline) {
|
|
598
1641
|
code.push(["{"]);
|
|
599
1642
|
} else {
|
|
600
|
-
|
|
1643
|
+
addCode(code, ", {");
|
|
601
1644
|
}
|
|
602
1645
|
const options = [];
|
|
603
1646
|
for (const key of indexOptionsKeys) {
|
|
@@ -605,20 +1648,20 @@ const indexInnerToCode = (index, t) => {
|
|
|
605
1648
|
const value = index.options[key];
|
|
606
1649
|
if (value === null || value === void 0) continue;
|
|
607
1650
|
options.push(
|
|
608
|
-
`${key}: ${Array.isArray(value) ?
|
|
1651
|
+
`${key}: ${Array.isArray(value) ? singleQuoteArray(value) : typeof value === "string" ? singleQuote(value) : value},`
|
|
609
1652
|
);
|
|
610
1653
|
}
|
|
611
1654
|
if (columnsMultiline) {
|
|
612
1655
|
code.push([options, "},"]);
|
|
613
1656
|
} else {
|
|
614
1657
|
code.push(options);
|
|
615
|
-
|
|
1658
|
+
addCode(code, "}");
|
|
616
1659
|
}
|
|
617
1660
|
}
|
|
618
1661
|
if (columnsMultiline) {
|
|
619
1662
|
code.push("),");
|
|
620
1663
|
} else {
|
|
621
|
-
|
|
1664
|
+
addCode(code, ")");
|
|
622
1665
|
}
|
|
623
1666
|
return code;
|
|
624
1667
|
};
|
|
@@ -640,14 +1683,14 @@ const excludeInnerToCode = (item, t) => {
|
|
|
640
1683
|
for (const column of item.columns) {
|
|
641
1684
|
const expr = "expression" in column ? column.expression : column.column;
|
|
642
1685
|
const props = [
|
|
643
|
-
`${"expression" in column ? "expression" : "column"}: ${
|
|
1686
|
+
`${"expression" in column ? "expression" : "column"}: ${singleQuote(
|
|
644
1687
|
expr
|
|
645
1688
|
)},`
|
|
646
1689
|
];
|
|
647
1690
|
for (const key of columnOptions) {
|
|
648
1691
|
const value = column[key];
|
|
649
1692
|
if (value !== void 0) {
|
|
650
|
-
props.push(`${key}: ${
|
|
1693
|
+
props.push(`${key}: ${singleQuote(value)},`);
|
|
651
1694
|
}
|
|
652
1695
|
}
|
|
653
1696
|
objects.push("{", props, "},");
|
|
@@ -661,7 +1704,7 @@ const excludeInnerToCode = (item, t) => {
|
|
|
661
1704
|
const value = item.options[key];
|
|
662
1705
|
if (value === null || value === void 0) continue;
|
|
663
1706
|
options.push(
|
|
664
|
-
`${key}: ${Array.isArray(value) ?
|
|
1707
|
+
`${key}: ${Array.isArray(value) ? singleQuoteArray(value) : typeof value === "string" ? singleQuote(value) : value},`
|
|
665
1708
|
);
|
|
666
1709
|
}
|
|
667
1710
|
code.push([options, "},"]);
|
|
@@ -687,7 +1730,7 @@ const constraintInnerToCode = (item, t, m) => {
|
|
|
687
1730
|
];
|
|
688
1731
|
}
|
|
689
1732
|
return [
|
|
690
|
-
`${t}.check(${item.check.toCode(t)}${item.name ? `, ${
|
|
1733
|
+
`${t}.check(${item.check.toCode(t)}${item.name ? `, ${singleQuote(item.name)}` : ""})`
|
|
691
1734
|
];
|
|
692
1735
|
};
|
|
693
1736
|
const referencesArgsToCode = ({
|
|
@@ -697,22 +1740,22 @@ const referencesArgsToCode = ({
|
|
|
697
1740
|
options
|
|
698
1741
|
}, name = options?.name || false, m) => {
|
|
699
1742
|
const args = [];
|
|
700
|
-
args.push(`${
|
|
1743
|
+
args.push(`${singleQuoteArray(columns)},`);
|
|
701
1744
|
if (m && typeof fnOrTable !== "string") {
|
|
702
1745
|
const { schema, table } = new (fnOrTable())();
|
|
703
1746
|
fnOrTable = schema ? `${schema}.${table}` : table;
|
|
704
1747
|
}
|
|
705
1748
|
args.push(
|
|
706
|
-
`${typeof fnOrTable === "string" ?
|
|
1749
|
+
`${typeof fnOrTable === "string" ? singleQuote(fnOrTable) : fnOrTable.toString()},`
|
|
707
1750
|
);
|
|
708
|
-
args.push(`${
|
|
709
|
-
if (
|
|
1751
|
+
args.push(`${singleQuoteArray(foreignColumns)},`);
|
|
1752
|
+
if (objectHasValues(options) || name) {
|
|
710
1753
|
const lines = [];
|
|
711
|
-
if (name) lines.push(`name: ${
|
|
1754
|
+
if (name) lines.push(`name: ${singleQuote(name)},`);
|
|
712
1755
|
for (const key in options) {
|
|
713
1756
|
if (key === "name") continue;
|
|
714
1757
|
const value = options[key];
|
|
715
|
-
if (value) lines.push(`${key}: ${
|
|
1758
|
+
if (value) lines.push(`${key}: ${singleQuote(value)},`);
|
|
716
1759
|
}
|
|
717
1760
|
args.push("{", lines, "},");
|
|
718
1761
|
}
|
|
@@ -721,18 +1764,18 @@ const referencesArgsToCode = ({
|
|
|
721
1764
|
const columnForeignKeysToCode = (foreignKeys, migration) => {
|
|
722
1765
|
const code = [];
|
|
723
1766
|
for (const foreignKey of foreignKeys) {
|
|
724
|
-
|
|
1767
|
+
addCode(code, `.foreignKey(`);
|
|
725
1768
|
for (const part of foreignKeyArgumentToCode(foreignKey, migration)) {
|
|
726
|
-
|
|
1769
|
+
addCode(code, part);
|
|
727
1770
|
}
|
|
728
|
-
|
|
1771
|
+
addCode(code, ")");
|
|
729
1772
|
}
|
|
730
1773
|
return code;
|
|
731
1774
|
};
|
|
732
1775
|
const foreignKeyArgumentToCode = ({
|
|
733
1776
|
fnOrTable,
|
|
734
1777
|
foreignColumns,
|
|
735
|
-
options =
|
|
1778
|
+
options = emptyObject
|
|
736
1779
|
}, migration) => {
|
|
737
1780
|
const code = [];
|
|
738
1781
|
if (migration && typeof fnOrTable !== "string") {
|
|
@@ -740,70 +1783,70 @@ const foreignKeyArgumentToCode = ({
|
|
|
740
1783
|
fnOrTable = schema ? `${schema}.${table}` : table;
|
|
741
1784
|
}
|
|
742
1785
|
code.push(
|
|
743
|
-
typeof fnOrTable === "string" ?
|
|
1786
|
+
typeof fnOrTable === "string" ? singleQuote(fnOrTable) : fnOrTable.toString()
|
|
744
1787
|
);
|
|
745
|
-
|
|
1788
|
+
addCode(code, `, ${singleQuote(foreignColumns[0])}`);
|
|
746
1789
|
const hasOptions = options.name || options.match || options.onUpdate || options.onDelete;
|
|
747
1790
|
if (hasOptions) {
|
|
748
1791
|
const arr = [];
|
|
749
|
-
if (options.name) arr.push(`name: ${
|
|
750
|
-
if (options.match) arr.push(`match: ${
|
|
1792
|
+
if (options.name) arr.push(`name: ${singleQuote(options.name)},`);
|
|
1793
|
+
if (options.match) arr.push(`match: ${singleQuote(options.match)},`);
|
|
751
1794
|
if (options.onUpdate)
|
|
752
|
-
arr.push(`onUpdate: ${
|
|
1795
|
+
arr.push(`onUpdate: ${singleQuote(options.onUpdate)},`);
|
|
753
1796
|
if (options.onDelete)
|
|
754
|
-
arr.push(`onDelete: ${
|
|
755
|
-
|
|
1797
|
+
arr.push(`onDelete: ${singleQuote(options.onDelete)},`);
|
|
1798
|
+
addCode(code, ", {");
|
|
756
1799
|
code.push(arr);
|
|
757
|
-
|
|
1800
|
+
addCode(code, "}");
|
|
758
1801
|
}
|
|
759
1802
|
return code;
|
|
760
1803
|
};
|
|
761
1804
|
const columnIndexesToCode = (items) => {
|
|
762
1805
|
const code = [];
|
|
763
1806
|
for (const { options } of items) {
|
|
764
|
-
|
|
1807
|
+
addCode(code, `.${options.unique ? "unique" : "index"}(`);
|
|
765
1808
|
const arr = [
|
|
766
|
-
options.name && `name: ${
|
|
767
|
-
options.collate && `collate: ${
|
|
768
|
-
options.opclass && `opclass: ${
|
|
769
|
-
options.order && `order: ${
|
|
770
|
-
options.using && `using: ${
|
|
771
|
-
options.include && `include: ${typeof options.include === "string" ?
|
|
1809
|
+
options.name && `name: ${singleQuote(options.name)},`,
|
|
1810
|
+
options.collate && `collate: ${singleQuote(options.collate)},`,
|
|
1811
|
+
options.opclass && `opclass: ${singleQuote(options.opclass)},`,
|
|
1812
|
+
options.order && `order: ${singleQuote(options.order)},`,
|
|
1813
|
+
options.using && `using: ${singleQuote(options.using)},`,
|
|
1814
|
+
options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
|
|
772
1815
|
options.nullsNotDistinct && `nullsNotDistinct: true,`,
|
|
773
|
-
options.with && `with: ${
|
|
774
|
-
options.tablespace && `tablespace: ${
|
|
775
|
-
options.where && `where: ${
|
|
1816
|
+
options.with && `with: ${singleQuote(options.with)},`,
|
|
1817
|
+
options.tablespace && `tablespace: ${singleQuote(options.tablespace)},`,
|
|
1818
|
+
options.where && `where: ${singleQuote(options.where)},`
|
|
776
1819
|
].filter((x) => !!x);
|
|
777
1820
|
if (arr.length) {
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
1821
|
+
addCode(code, "{");
|
|
1822
|
+
addCode(code, arr);
|
|
1823
|
+
addCode(code, "}");
|
|
781
1824
|
}
|
|
782
|
-
|
|
1825
|
+
addCode(code, ")");
|
|
783
1826
|
}
|
|
784
1827
|
return code;
|
|
785
1828
|
};
|
|
786
1829
|
const columnExcludesToCode = (items) => {
|
|
787
1830
|
const code = [];
|
|
788
1831
|
for (const { options, with: w } of items) {
|
|
789
|
-
|
|
1832
|
+
addCode(code, `.exclude('${w}'`);
|
|
790
1833
|
const arr = [
|
|
791
|
-
options.name && `name: ${
|
|
792
|
-
options.collate && `collate: ${
|
|
793
|
-
options.opclass && `opclass: ${
|
|
794
|
-
options.order && `order: ${
|
|
795
|
-
options.using && `using: ${
|
|
796
|
-
options.include && `include: ${typeof options.include === "string" ?
|
|
797
|
-
options.with && `with: ${
|
|
798
|
-
options.tablespace && `tablespace: ${
|
|
799
|
-
options.where && `where: ${
|
|
1834
|
+
options.name && `name: ${singleQuote(options.name)},`,
|
|
1835
|
+
options.collate && `collate: ${singleQuote(options.collate)},`,
|
|
1836
|
+
options.opclass && `opclass: ${singleQuote(options.opclass)},`,
|
|
1837
|
+
options.order && `order: ${singleQuote(options.order)},`,
|
|
1838
|
+
options.using && `using: ${singleQuote(options.using)},`,
|
|
1839
|
+
options.include && `include: ${typeof options.include === "string" ? singleQuote(options.include) : `[${options.include.map(singleQuote).join(", ")}]`},`,
|
|
1840
|
+
options.with && `with: ${singleQuote(options.with)},`,
|
|
1841
|
+
options.tablespace && `tablespace: ${singleQuote(options.tablespace)},`,
|
|
1842
|
+
options.where && `where: ${singleQuote(options.where)},`
|
|
800
1843
|
].filter((x) => !!x);
|
|
801
1844
|
if (arr.length) {
|
|
802
|
-
|
|
803
|
-
|
|
804
|
-
|
|
1845
|
+
addCode(code, ", {");
|
|
1846
|
+
addCode(code, arr);
|
|
1847
|
+
addCode(code, "}");
|
|
805
1848
|
}
|
|
806
|
-
|
|
1849
|
+
addCode(code, ")");
|
|
807
1850
|
}
|
|
808
1851
|
return code;
|
|
809
1852
|
};
|
|
@@ -831,20 +1874,20 @@ const identityToCode = (identity, dataType) => {
|
|
|
831
1874
|
props.push(`cache: ${identity.cache},`);
|
|
832
1875
|
if (identity.cycle) props.push(`cycle: true,`);
|
|
833
1876
|
if (props.length) {
|
|
834
|
-
|
|
1877
|
+
addCode(code, "{");
|
|
835
1878
|
code.push(props, "}");
|
|
836
1879
|
}
|
|
837
|
-
|
|
1880
|
+
addCode(code, ")");
|
|
838
1881
|
return code;
|
|
839
1882
|
};
|
|
840
1883
|
const columnCode = (type, ctx, key, code) => {
|
|
841
1884
|
const { data } = type;
|
|
842
|
-
code =
|
|
1885
|
+
code = toArray(code);
|
|
843
1886
|
let prepend = `${ctx.t}.`;
|
|
844
|
-
const keyName = ctx.snakeCase ?
|
|
1887
|
+
const keyName = ctx.snakeCase ? toSnakeCase(key) : key;
|
|
845
1888
|
const name = data.name ?? keyName;
|
|
846
1889
|
if (name !== keyName) {
|
|
847
|
-
prepend += `name(${
|
|
1890
|
+
prepend += `name(${singleQuote(name)}).`;
|
|
848
1891
|
}
|
|
849
1892
|
if (typeof code[0] === "string") {
|
|
850
1893
|
code[0] = `${prepend}${code[0]}`;
|
|
@@ -852,12 +1895,12 @@ const columnCode = (type, ctx, key, code) => {
|
|
|
852
1895
|
code[0].unshift(prepend);
|
|
853
1896
|
}
|
|
854
1897
|
if (data.generated) {
|
|
855
|
-
|
|
1898
|
+
addCode(code, data.generated.toCode());
|
|
856
1899
|
}
|
|
857
1900
|
if (data.primaryKey) {
|
|
858
|
-
|
|
1901
|
+
addCode(
|
|
859
1902
|
code,
|
|
860
|
-
`.primaryKey(${data.primaryKey === true ? "" :
|
|
1903
|
+
`.primaryKey(${data.primaryKey === true ? "" : singleQuote(data.primaryKey)})`
|
|
861
1904
|
);
|
|
862
1905
|
}
|
|
863
1906
|
if (data.foreignKeys) {
|
|
@@ -865,51 +1908,51 @@ const columnCode = (type, ctx, key, code) => {
|
|
|
865
1908
|
data.foreignKeys,
|
|
866
1909
|
ctx.migration
|
|
867
1910
|
)) {
|
|
868
|
-
|
|
1911
|
+
addCode(code, part);
|
|
869
1912
|
}
|
|
870
1913
|
}
|
|
871
|
-
if (data.explicitSelect)
|
|
872
|
-
if (data.isNullable)
|
|
1914
|
+
if (data.explicitSelect) addCode(code, ".select(false)");
|
|
1915
|
+
if (data.isNullable) addCode(code, ".nullable()");
|
|
873
1916
|
if (data.as && !ctx.migration) {
|
|
874
|
-
|
|
1917
|
+
addCode(code, `.as(${data.as.toCode(ctx, key)})`);
|
|
875
1918
|
}
|
|
876
1919
|
if (data.default !== void 0 && data.default !== data.defaultDefault && (!ctx.migration || typeof data.default !== "function")) {
|
|
877
|
-
|
|
1920
|
+
addCode(
|
|
878
1921
|
code,
|
|
879
|
-
`.default(${
|
|
1922
|
+
`.default(${columnDefaultArgumentToCode(ctx.t, data.default)})`
|
|
880
1923
|
);
|
|
881
1924
|
}
|
|
882
1925
|
if (data.indexes) {
|
|
883
1926
|
for (const part of columnIndexesToCode(data.indexes)) {
|
|
884
|
-
|
|
1927
|
+
addCode(code, part);
|
|
885
1928
|
}
|
|
886
1929
|
}
|
|
887
1930
|
if (data.excludes) {
|
|
888
1931
|
for (const part of columnExcludesToCode(data.excludes)) {
|
|
889
|
-
|
|
1932
|
+
addCode(code, part);
|
|
890
1933
|
}
|
|
891
1934
|
}
|
|
892
|
-
if (data.comment)
|
|
1935
|
+
if (data.comment) addCode(code, `.comment(${singleQuote(data.comment)})`);
|
|
893
1936
|
if (data.checks) {
|
|
894
|
-
|
|
1937
|
+
addCode(code, columnCheckToCode(ctx, data.checks));
|
|
895
1938
|
}
|
|
896
1939
|
if (data.errors) {
|
|
897
|
-
for (const part of
|
|
898
|
-
|
|
1940
|
+
for (const part of columnErrorMessagesToCode(data.errors)) {
|
|
1941
|
+
addCode(code, part);
|
|
899
1942
|
}
|
|
900
1943
|
}
|
|
901
1944
|
const { validationDefault } = data;
|
|
902
1945
|
if (validationDefault) {
|
|
903
|
-
|
|
1946
|
+
addCode(
|
|
904
1947
|
code,
|
|
905
|
-
`.validationDefault(${typeof validationDefault === "function" ? validationDefault.toString() : typeof validationDefault === "string" ?
|
|
1948
|
+
`.validationDefault(${typeof validationDefault === "function" ? validationDefault.toString() : typeof validationDefault === "string" ? singleQuote(validationDefault) : JSON.stringify(validationDefault)})`
|
|
906
1949
|
);
|
|
907
1950
|
}
|
|
908
1951
|
if (data.compression)
|
|
909
|
-
|
|
910
|
-
if (data.collate)
|
|
1952
|
+
addCode(code, `.compression(${singleQuote(data.compression)})`);
|
|
1953
|
+
if (data.collate) addCode(code, `.collate(${singleQuote(data.collate)})`);
|
|
911
1954
|
if (data.modifyQuery)
|
|
912
|
-
|
|
1955
|
+
addCode(code, `.modifyQuery(${data.modifyQuery.toString()})`);
|
|
913
1956
|
return code.length === 1 && typeof code[0] === "string" ? code[0] : code;
|
|
914
1957
|
};
|
|
915
1958
|
|
|
@@ -942,13 +1985,13 @@ class CustomTypeColumn extends ColumnType {
|
|
|
942
1985
|
this,
|
|
943
1986
|
ctx,
|
|
944
1987
|
key,
|
|
945
|
-
`type(${
|
|
1988
|
+
`type(${singleQuote(
|
|
946
1989
|
(dataType.startsWith(ctx.currentSchema) ? dataType.slice(ctx.currentSchema.length + 1) : dataType) + (typmod !== void 0 && typmod !== -1 && !dataType.includes("(") ? `(${typmod})` : "")
|
|
947
1990
|
)})`
|
|
948
1991
|
);
|
|
949
1992
|
}
|
|
950
1993
|
as(column) {
|
|
951
|
-
const c =
|
|
1994
|
+
const c = setColumnData(
|
|
952
1995
|
this,
|
|
953
1996
|
"as",
|
|
954
1997
|
column
|
|
@@ -961,7 +2004,7 @@ class CustomTypeColumn extends ColumnType {
|
|
|
961
2004
|
}
|
|
962
2005
|
class DomainColumn extends CustomTypeColumn {
|
|
963
2006
|
toCode(ctx, key) {
|
|
964
|
-
return columnCode(this, ctx, key, `domain(${
|
|
2007
|
+
return columnCode(this, ctx, key, `domain(${singleQuote(this.dataType)})`);
|
|
965
2008
|
}
|
|
966
2009
|
}
|
|
967
2010
|
|
|
@@ -987,7 +2030,7 @@ class EnumColumn extends ColumnType {
|
|
|
987
2030
|
|
|
988
2031
|
const addColumnParserToQuery = (q, key, column) => {
|
|
989
2032
|
if (column._parse) {
|
|
990
|
-
|
|
2033
|
+
setObjectValueImmutable(
|
|
991
2034
|
q,
|
|
992
2035
|
"parsers",
|
|
993
2036
|
key,
|
|
@@ -1039,8 +2082,8 @@ const make = (_op) => {
|
|
|
1039
2082
|
function(value) {
|
|
1040
2083
|
const { q } = this;
|
|
1041
2084
|
(q.chain ?? (q.chain = [])).push(_op, value);
|
|
1042
|
-
if (q.parsers?.[
|
|
1043
|
-
|
|
2085
|
+
if (q.parsers?.[getValueKey]) {
|
|
2086
|
+
setObjectValueImmutable(q, "parsers", getValueKey, void 0);
|
|
1044
2087
|
}
|
|
1045
2088
|
return setQueryOperators(this, boolean);
|
|
1046
2089
|
},
|
|
@@ -1057,8 +2100,8 @@ const makeVarArg = (_op) => {
|
|
|
1057
2100
|
function(...args) {
|
|
1058
2101
|
const { q } = this;
|
|
1059
2102
|
(q.chain ?? (q.chain = [])).push(_op, args);
|
|
1060
|
-
if (q.parsers?.[
|
|
1061
|
-
|
|
2103
|
+
if (q.parsers?.[getValueKey]) {
|
|
2104
|
+
setObjectValueImmutable(q, "parsers", getValueKey, void 0);
|
|
1062
2105
|
}
|
|
1063
2106
|
return setQueryOperators(this, boolean);
|
|
1064
2107
|
},
|
|
@@ -1071,10 +2114,10 @@ const makeVarArg = (_op) => {
|
|
|
1071
2114
|
};
|
|
1072
2115
|
const quoteValue = (arg, ctx, quotedAs, IN) => {
|
|
1073
2116
|
if (arg && typeof arg === "object") {
|
|
1074
|
-
if (IN &&
|
|
1075
|
-
return `(${(Array.isArray(arg) ? arg : [...arg]).map((value) =>
|
|
2117
|
+
if (IN && isIterable(arg)) {
|
|
2118
|
+
return `(${(Array.isArray(arg) ? arg : [...arg]).map((value) => addValue(ctx.values, value)).join(", ")})`;
|
|
1076
2119
|
}
|
|
1077
|
-
if (
|
|
2120
|
+
if (isExpression(arg)) {
|
|
1078
2121
|
return arg.toSQL(ctx, quotedAs);
|
|
1079
2122
|
}
|
|
1080
2123
|
if ("toSQL" in arg) {
|
|
@@ -1084,14 +2127,14 @@ const quoteValue = (arg, ctx, quotedAs, IN) => {
|
|
|
1084
2127
|
arg = JSON.stringify(arg);
|
|
1085
2128
|
}
|
|
1086
2129
|
}
|
|
1087
|
-
return
|
|
2130
|
+
return addValue(ctx.values, arg);
|
|
1088
2131
|
};
|
|
1089
2132
|
const quoteLikeValue = (arg, ctx, quotedAs, jsonArray) => {
|
|
1090
2133
|
if (arg && typeof arg === "object") {
|
|
1091
2134
|
if (Array.isArray(arg)) {
|
|
1092
|
-
return `(${arg.map((value) =>
|
|
2135
|
+
return `(${arg.map((value) => addValue(ctx.values, value)).join(", ")})`;
|
|
1093
2136
|
}
|
|
1094
|
-
if (
|
|
2137
|
+
if (isExpression(arg)) {
|
|
1095
2138
|
return arg.toSQL(ctx, quotedAs);
|
|
1096
2139
|
}
|
|
1097
2140
|
if ("toSQL" in arg) {
|
|
@@ -1100,7 +2143,7 @@ const quoteLikeValue = (arg, ctx, quotedAs, jsonArray) => {
|
|
|
1100
2143
|
)}), '%', '\\\\%'), '_', '\\\\_')`;
|
|
1101
2144
|
}
|
|
1102
2145
|
}
|
|
1103
|
-
return
|
|
2146
|
+
return addValue(ctx.values, arg.replace(/[%_]/g, "\\$&"));
|
|
1104
2147
|
};
|
|
1105
2148
|
const base = {
|
|
1106
2149
|
equals: make(
|
|
@@ -1172,32 +2215,32 @@ const ordinalText = {
|
|
|
1172
2215
|
...ord,
|
|
1173
2216
|
...text
|
|
1174
2217
|
};
|
|
1175
|
-
const encodeJsonPath = (ctx, path) =>
|
|
1176
|
-
const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${
|
|
2218
|
+
const encodeJsonPath = (ctx, path) => addValue(ctx.values, `{${Array.isArray(path) ? path.join(", ") : path}}`);
|
|
2219
|
+
const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${addValue(ctx.values, path)}${options?.vars ? `, ${addValue(ctx.values, JSON.stringify(options.vars))}${options.silent ? ", true" : ""}` : options?.silent ? ", NULL, true" : ""})`;
|
|
1177
2220
|
const quoteJsonValue = (arg, ctx, quotedAs, IN) => {
|
|
1178
2221
|
if (arg && typeof arg === "object") {
|
|
1179
2222
|
if (IN && Array.isArray(arg)) {
|
|
1180
|
-
return `(${arg.map((value) =>
|
|
2223
|
+
return `(${arg.map((value) => addValue(ctx.values, JSON.stringify(value)) + "::jsonb").join(", ")})`;
|
|
1181
2224
|
}
|
|
1182
|
-
if (
|
|
2225
|
+
if (isExpression(arg)) {
|
|
1183
2226
|
return "to_jsonb(" + arg.toSQL(ctx, quotedAs) + ")";
|
|
1184
2227
|
}
|
|
1185
2228
|
if ("toSQL" in arg) {
|
|
1186
2229
|
return `to_jsonb((${getSqlText(arg.toSQL(ctx))}))`;
|
|
1187
2230
|
}
|
|
1188
2231
|
}
|
|
1189
|
-
return
|
|
2232
|
+
return addValue(ctx.values, JSON.stringify(arg)) + "::jsonb";
|
|
1190
2233
|
};
|
|
1191
2234
|
const serializeJsonValue = (arg, ctx, quotedAs) => {
|
|
1192
2235
|
if (arg && typeof arg === "object") {
|
|
1193
|
-
if (
|
|
2236
|
+
if (isExpression(arg)) {
|
|
1194
2237
|
return "to_jsonb(" + arg.toSQL(ctx, quotedAs) + ")";
|
|
1195
2238
|
}
|
|
1196
2239
|
if ("toSQL" in arg) {
|
|
1197
2240
|
return `to_jsonb((${getSqlText(arg.toSQL(ctx))}))`;
|
|
1198
2241
|
}
|
|
1199
2242
|
}
|
|
1200
|
-
return
|
|
2243
|
+
return addValue(ctx.values, JSON.stringify(arg));
|
|
1201
2244
|
};
|
|
1202
2245
|
const json = {
|
|
1203
2246
|
...ord,
|
|
@@ -1218,15 +2261,15 @@ const json = {
|
|
|
1218
2261
|
const { q, columnTypes } = this;
|
|
1219
2262
|
const chain = q.chain ?? (q.chain = []);
|
|
1220
2263
|
chain.push(jsonPathQueryOp, [path, options]);
|
|
1221
|
-
if (q.parsers?.[
|
|
1222
|
-
|
|
2264
|
+
if (q.parsers?.[getValueKey]) {
|
|
2265
|
+
setObjectValueImmutable(q, "parsers", getValueKey, void 0);
|
|
1223
2266
|
}
|
|
1224
2267
|
if (options?.type) {
|
|
1225
2268
|
const type = options.type(columnTypes);
|
|
1226
|
-
addColumnParserToQuery(q,
|
|
2269
|
+
addColumnParserToQuery(q, getValueKey, type);
|
|
1227
2270
|
chain.push = (...args) => {
|
|
1228
2271
|
chain.push = Array.prototype.push;
|
|
1229
|
-
chain.push((s) => `${s}::${type.dataType}`,
|
|
2272
|
+
chain.push((s) => `${s}::${type.dataType}`, emptyArray);
|
|
1230
2273
|
return chain.push(...args);
|
|
1231
2274
|
};
|
|
1232
2275
|
return setQueryOperators(this, type.operators);
|
|
@@ -1329,7 +2372,7 @@ class DateColumn extends DateBaseColumn {
|
|
|
1329
2372
|
this,
|
|
1330
2373
|
ctx,
|
|
1331
2374
|
key,
|
|
1332
|
-
`date()${
|
|
2375
|
+
`date()${dateDataToCode(this.data, ctx.migration)}`
|
|
1333
2376
|
);
|
|
1334
2377
|
}
|
|
1335
2378
|
}
|
|
@@ -1339,7 +2382,7 @@ class DateTimeBaseClass extends DateBaseColumn {
|
|
|
1339
2382
|
this.data.dateTimePrecision = dateTimePrecision;
|
|
1340
2383
|
}
|
|
1341
2384
|
toSQL() {
|
|
1342
|
-
return
|
|
2385
|
+
return joinTruthy(
|
|
1343
2386
|
this.dataType,
|
|
1344
2387
|
this.data.dateTimePrecision !== void 0 && `(${this.data.dateTimePrecision})`
|
|
1345
2388
|
);
|
|
@@ -1347,7 +2390,7 @@ class DateTimeBaseClass extends DateBaseColumn {
|
|
|
1347
2390
|
}
|
|
1348
2391
|
class DateTimeTzBaseClass extends DateTimeBaseClass {
|
|
1349
2392
|
toSQL() {
|
|
1350
|
-
return
|
|
2393
|
+
return joinTruthy(
|
|
1351
2394
|
this.baseDataType,
|
|
1352
2395
|
this.data.dateTimePrecision !== void 0 && `(${this.data.dateTimePrecision})`,
|
|
1353
2396
|
" with time zone"
|
|
@@ -1367,7 +2410,7 @@ const timestampToCode = (self, ctx, key) => {
|
|
|
1367
2410
|
self,
|
|
1368
2411
|
ctx,
|
|
1369
2412
|
key,
|
|
1370
|
-
`timestamps${noTz}(${p && p !== 6 ? p : ""}).${defaultTimestamp}${
|
|
2413
|
+
`timestamps${noTz}(${p && p !== 6 ? p : ""}).${defaultTimestamp}${dateDataToCode(self.data, ctx.migration)}`
|
|
1371
2414
|
);
|
|
1372
2415
|
self.data.default = def;
|
|
1373
2416
|
self.data.modifyQuery = modifyQuery;
|
|
@@ -1377,7 +2420,7 @@ const timestampToCode = (self, ctx, key) => {
|
|
|
1377
2420
|
self,
|
|
1378
2421
|
ctx,
|
|
1379
2422
|
key,
|
|
1380
|
-
`${self instanceof TimestampColumn ? "timestampNoTZ" : "timestamp"}(${p && p !== 6 ? p : ""})${
|
|
2423
|
+
`${self instanceof TimestampColumn ? "timestampNoTZ" : "timestamp"}(${p && p !== 6 ? p : ""})${dateDataToCode(self.data, ctx.migration)}`
|
|
1381
2424
|
);
|
|
1382
2425
|
}
|
|
1383
2426
|
};
|
|
@@ -1413,7 +2456,7 @@ class TimeColumn extends ColumnType {
|
|
|
1413
2456
|
this,
|
|
1414
2457
|
ctx,
|
|
1415
2458
|
key,
|
|
1416
|
-
`time(${dateTimePrecision || ""})${
|
|
2459
|
+
`time(${dateTimePrecision || ""})${dateDataToCode(
|
|
1417
2460
|
this.data,
|
|
1418
2461
|
ctx.migration
|
|
1419
2462
|
)}`
|
|
@@ -1438,7 +2481,7 @@ class IntervalColumn extends ColumnType {
|
|
|
1438
2481
|
);
|
|
1439
2482
|
}
|
|
1440
2483
|
toSQL() {
|
|
1441
|
-
return
|
|
2484
|
+
return joinTruthy(
|
|
1442
2485
|
this.dataType,
|
|
1443
2486
|
this.data.fields && ` ${this.data.fields}`,
|
|
1444
2487
|
this.data.precision !== void 0 && ` (${this.data.precision})`
|
|
@@ -1471,9 +2514,9 @@ class ArrayColumn extends ColumnType {
|
|
|
1471
2514
|
const { item } = this.data;
|
|
1472
2515
|
const { isNullable } = item.data;
|
|
1473
2516
|
delete item.data.isNullable;
|
|
1474
|
-
|
|
2517
|
+
addCode(code, item.toCode(ctx, key));
|
|
1475
2518
|
item.data.isNullable = isNullable;
|
|
1476
|
-
|
|
2519
|
+
addCode(code, `${close}${arrayDataToCode(this.data, ctx.migration)}`);
|
|
1477
2520
|
return columnCode(this, ctx, key, code);
|
|
1478
2521
|
}
|
|
1479
2522
|
}
|
|
@@ -1601,7 +2644,7 @@ class DecimalColumn extends NumberAsStringBaseColumn {
|
|
|
1601
2644
|
}
|
|
1602
2645
|
toSQL() {
|
|
1603
2646
|
const { numericPrecision, numericScale } = this.data;
|
|
1604
|
-
return
|
|
2647
|
+
return joinTruthy(
|
|
1605
2648
|
this.dataType,
|
|
1606
2649
|
numericPrecision ? numericScale ? `(${numericPrecision}, ${numericScale})` : `(${numericPrecision})` : void 0
|
|
1607
2650
|
);
|
|
@@ -1615,9 +2658,9 @@ const intToCode = (column, ctx, key, alias) => {
|
|
|
1615
2658
|
} else {
|
|
1616
2659
|
code = [`${alias}()`];
|
|
1617
2660
|
}
|
|
1618
|
-
|
|
2661
|
+
addCode(
|
|
1619
2662
|
code,
|
|
1620
|
-
|
|
2663
|
+
numberDataToCode(column.data, ctx.migration, skipNumberMethods)
|
|
1621
2664
|
);
|
|
1622
2665
|
return columnCode(column, ctx, key, code);
|
|
1623
2666
|
};
|
|
@@ -1632,7 +2675,7 @@ class SmallIntColumn extends IntegerBaseColumn {
|
|
|
1632
2675
|
return intToCode(this, ctx, key, "smallint");
|
|
1633
2676
|
}
|
|
1634
2677
|
identity(options = {}) {
|
|
1635
|
-
return
|
|
2678
|
+
return setColumnData(this, "identity", options);
|
|
1636
2679
|
}
|
|
1637
2680
|
}
|
|
1638
2681
|
class IntegerColumn extends IntegerBaseColumn {
|
|
@@ -1646,7 +2689,7 @@ class IntegerColumn extends IntegerBaseColumn {
|
|
|
1646
2689
|
return intToCode(this, ctx, key, "integer");
|
|
1647
2690
|
}
|
|
1648
2691
|
identity(options = {}) {
|
|
1649
|
-
return
|
|
2692
|
+
return setColumnData(this, "identity", options);
|
|
1650
2693
|
}
|
|
1651
2694
|
}
|
|
1652
2695
|
class BigIntColumn extends NumberAsStringBaseColumn {
|
|
@@ -1659,7 +2702,7 @@ class BigIntColumn extends NumberAsStringBaseColumn {
|
|
|
1659
2702
|
return intToCode(this, ctx, key, "bigint");
|
|
1660
2703
|
}
|
|
1661
2704
|
identity(options = {}) {
|
|
1662
|
-
return
|
|
2705
|
+
return setColumnData(this, "identity", options);
|
|
1663
2706
|
}
|
|
1664
2707
|
}
|
|
1665
2708
|
class RealColumn extends NumberBaseColumn {
|
|
@@ -1674,7 +2717,7 @@ class RealColumn extends NumberBaseColumn {
|
|
|
1674
2717
|
this,
|
|
1675
2718
|
ctx,
|
|
1676
2719
|
key,
|
|
1677
|
-
`real()${
|
|
2720
|
+
`real()${numberDataToCode(this.data, ctx.migration)}`
|
|
1678
2721
|
);
|
|
1679
2722
|
}
|
|
1680
2723
|
}
|
|
@@ -1704,7 +2747,7 @@ class SmallSerialColumn extends IntegerBaseColumn {
|
|
|
1704
2747
|
this,
|
|
1705
2748
|
ctx,
|
|
1706
2749
|
key,
|
|
1707
|
-
`smallSerial()${
|
|
2750
|
+
`smallSerial()${numberDataToCode(
|
|
1708
2751
|
this.data,
|
|
1709
2752
|
ctx.migration,
|
|
1710
2753
|
skipNumberMethods
|
|
@@ -1728,7 +2771,7 @@ class SerialColumn extends IntegerBaseColumn {
|
|
|
1728
2771
|
this,
|
|
1729
2772
|
ctx,
|
|
1730
2773
|
key,
|
|
1731
|
-
`serial()${
|
|
2774
|
+
`serial()${numberDataToCode(
|
|
1732
2775
|
this.data,
|
|
1733
2776
|
ctx.migration,
|
|
1734
2777
|
skipNumberMethods
|
|
@@ -1761,7 +2804,7 @@ const defaultSchemaConfig = {
|
|
|
1761
2804
|
},
|
|
1762
2805
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
1763
2806
|
encode(fn) {
|
|
1764
|
-
return
|
|
2807
|
+
return setColumnData(this, "encode", fn);
|
|
1765
2808
|
},
|
|
1766
2809
|
asType() {
|
|
1767
2810
|
return this;
|
|
@@ -1784,27 +2827,27 @@ const defaultSchemaConfig = {
|
|
|
1784
2827
|
array(item) {
|
|
1785
2828
|
return new ArrayColumn(defaultSchemaConfig, item, void 0);
|
|
1786
2829
|
},
|
|
1787
|
-
boolean:
|
|
1788
|
-
buffer:
|
|
1789
|
-
unknown:
|
|
1790
|
-
never:
|
|
1791
|
-
stringSchema:
|
|
1792
|
-
stringMin:
|
|
1793
|
-
stringMax:
|
|
1794
|
-
stringMinMax:
|
|
1795
|
-
number:
|
|
1796
|
-
int:
|
|
1797
|
-
stringNumberDate:
|
|
1798
|
-
timeInterval:
|
|
1799
|
-
bit:
|
|
1800
|
-
uuid:
|
|
2830
|
+
boolean: noop,
|
|
2831
|
+
buffer: noop,
|
|
2832
|
+
unknown: noop,
|
|
2833
|
+
never: noop,
|
|
2834
|
+
stringSchema: noop,
|
|
2835
|
+
stringMin: noop,
|
|
2836
|
+
stringMax: noop,
|
|
2837
|
+
stringMinMax: noop,
|
|
2838
|
+
number: noop,
|
|
2839
|
+
int: noop,
|
|
2840
|
+
stringNumberDate: noop,
|
|
2841
|
+
timeInterval: noop,
|
|
2842
|
+
bit: noop,
|
|
2843
|
+
uuid: noop,
|
|
1801
2844
|
nullable() {
|
|
1802
|
-
return
|
|
2845
|
+
return setColumnData(this, "isNullable", true);
|
|
1803
2846
|
},
|
|
1804
2847
|
json() {
|
|
1805
2848
|
return new JSONColumn(defaultSchemaConfig, void 0);
|
|
1806
2849
|
},
|
|
1807
|
-
setErrors:
|
|
2850
|
+
setErrors: noop,
|
|
1808
2851
|
smallint: () => new SmallIntColumn(defaultSchemaConfig),
|
|
1809
2852
|
integer: () => new IntegerColumn(defaultSchemaConfig),
|
|
1810
2853
|
real: () => new RealColumn(defaultSchemaConfig),
|
|
@@ -1822,7 +2865,7 @@ const defaultSchemaConfig = {
|
|
|
1822
2865
|
date: () => new DateColumn(defaultSchemaConfig),
|
|
1823
2866
|
timestampNoTZ: (precision) => new TimestampColumn(defaultSchemaConfig, precision),
|
|
1824
2867
|
timestamp: (precision) => new TimestampTZColumn(defaultSchemaConfig, precision),
|
|
1825
|
-
geographyPointSchema:
|
|
2868
|
+
geographyPointSchema: noop
|
|
1826
2869
|
};
|
|
1827
2870
|
|
|
1828
2871
|
class TextBaseColumn extends ColumnType {
|
|
@@ -1841,7 +2884,7 @@ class LimitedTextBaseColumn extends TextBaseColumn {
|
|
|
1841
2884
|
this.data.maxChars = limit;
|
|
1842
2885
|
}
|
|
1843
2886
|
toSQL() {
|
|
1844
|
-
return
|
|
2887
|
+
return joinTruthy(
|
|
1845
2888
|
this.dataType,
|
|
1846
2889
|
this.data.maxChars !== void 0 && `(${this.data.maxChars})`
|
|
1847
2890
|
);
|
|
@@ -1858,7 +2901,7 @@ class VarCharColumn extends LimitedTextBaseColumn {
|
|
|
1858
2901
|
this,
|
|
1859
2902
|
ctx,
|
|
1860
2903
|
key,
|
|
1861
|
-
`varchar(${maxChars ?? ""})${
|
|
2904
|
+
`varchar(${maxChars ?? ""})${stringDataToCode(this.data, ctx.migration)}`
|
|
1862
2905
|
);
|
|
1863
2906
|
}
|
|
1864
2907
|
}
|
|
@@ -1873,7 +2916,7 @@ class StringColumn extends VarCharColumn {
|
|
|
1873
2916
|
this,
|
|
1874
2917
|
ctx,
|
|
1875
2918
|
key,
|
|
1876
|
-
`string(${max ?? ""})${
|
|
2919
|
+
`string(${max ?? ""})${stringDataToCode(this.data, ctx.migration)}`
|
|
1877
2920
|
);
|
|
1878
2921
|
}
|
|
1879
2922
|
}
|
|
@@ -1897,7 +2940,7 @@ const textColumnToCode = (column, ctx, key) => {
|
|
|
1897
2940
|
column,
|
|
1898
2941
|
ctx,
|
|
1899
2942
|
key,
|
|
1900
|
-
`${column.dataType}(${args})${
|
|
2943
|
+
`${column.dataType}(${args})${stringDataToCode(data, ctx.migration)}`
|
|
1901
2944
|
);
|
|
1902
2945
|
};
|
|
1903
2946
|
class TextColumn extends TextBaseColumn {
|
|
@@ -2066,7 +3109,7 @@ class BitColumn extends ColumnType {
|
|
|
2066
3109
|
return columnCode(this, ctx, key, `bit(${length})`);
|
|
2067
3110
|
}
|
|
2068
3111
|
toSQL() {
|
|
2069
|
-
return
|
|
3112
|
+
return joinTruthy(
|
|
2070
3113
|
this.dataType,
|
|
2071
3114
|
this.data.length !== void 0 && `(${this.data.length})`
|
|
2072
3115
|
);
|
|
@@ -2085,14 +3128,14 @@ class BitVaryingColumn extends ColumnType {
|
|
|
2085
3128
|
return columnCode(this, ctx, key, `bitVarying(${length ?? ""})`);
|
|
2086
3129
|
}
|
|
2087
3130
|
toSQL() {
|
|
2088
|
-
return
|
|
3131
|
+
return joinTruthy(
|
|
2089
3132
|
this.dataType,
|
|
2090
3133
|
this.data.length !== void 0 && `(${this.data.length})`
|
|
2091
3134
|
);
|
|
2092
3135
|
}
|
|
2093
3136
|
}
|
|
2094
3137
|
class TsVectorColumn extends ColumnType {
|
|
2095
|
-
constructor(schema, defaultLanguage =
|
|
3138
|
+
constructor(schema, defaultLanguage = getDefaultLanguage()) {
|
|
2096
3139
|
super(schema, schema.stringSchema());
|
|
2097
3140
|
this.defaultLanguage = defaultLanguage;
|
|
2098
3141
|
this.dataType = "tsvector";
|
|
@@ -2137,13 +3180,13 @@ class TsVectorColumn extends ColumnType {
|
|
|
2137
3180
|
const { snakeCase } = ctx;
|
|
2138
3181
|
let sql;
|
|
2139
3182
|
if (Array.isArray(target)) {
|
|
2140
|
-
const columns = target.length === 1 ? `"${snakeCase ?
|
|
2141
|
-
(column2) => `coalesce("${snakeCase ?
|
|
3183
|
+
const columns = target.length === 1 ? `"${snakeCase ? toSnakeCase(target[0]) : target[0]}"` : target.map(
|
|
3184
|
+
(column2) => `coalesce("${snakeCase ? toSnakeCase(column2) : column2}", '')`
|
|
2142
3185
|
).join(` || ' ' || `);
|
|
2143
3186
|
sql = `to_tsvector('${language}', ${columns})`;
|
|
2144
3187
|
} else {
|
|
2145
3188
|
for (const key in target) {
|
|
2146
|
-
sql = (sql ? sql + " || " : "(") + `setweight(to_tsvector('${language}', coalesce("${snakeCase ?
|
|
3189
|
+
sql = (sql ? sql + " || " : "(") + `setweight(to_tsvector('${language}', coalesce("${snakeCase ? toSnakeCase(key) : key}", '')), '${target[key]}')`;
|
|
2147
3190
|
}
|
|
2148
3191
|
if (sql) {
|
|
2149
3192
|
sql += ")";
|
|
@@ -2169,14 +3212,14 @@ class TsVectorColumn extends ColumnType {
|
|
|
2169
3212
|
const pairs = [];
|
|
2170
3213
|
for (const key in target) {
|
|
2171
3214
|
pairs.push(
|
|
2172
|
-
`${
|
|
3215
|
+
`${quoteObjectKey(key, false)}: '${target[key]}'`
|
|
2173
3216
|
);
|
|
2174
3217
|
}
|
|
2175
3218
|
code += `{ ${pairs.join(", ")} }`;
|
|
2176
3219
|
}
|
|
2177
3220
|
return code + ")";
|
|
2178
3221
|
};
|
|
2179
|
-
const column =
|
|
3222
|
+
const column = setColumnData(this, "generated", {
|
|
2180
3223
|
toSQL,
|
|
2181
3224
|
toCode
|
|
2182
3225
|
});
|
|
@@ -2356,8 +3399,8 @@ function uint8ArrToHex(arr) {
|
|
|
2356
3399
|
}
|
|
2357
3400
|
|
|
2358
3401
|
const getColumnTypes = (types, fn, nowSQL, language) => {
|
|
2359
|
-
if (nowSQL)
|
|
2360
|
-
if (language)
|
|
3402
|
+
if (nowSQL) setDefaultNowFn(nowSQL);
|
|
3403
|
+
if (language) setDefaultLanguage(language);
|
|
2361
3404
|
return fn(types);
|
|
2362
3405
|
};
|
|
2363
3406
|
const makeColumnTypes = (schema) => {
|
|
@@ -2366,7 +3409,7 @@ const makeColumnTypes = (schema) => {
|
|
|
2366
3409
|
enum: schema.enum,
|
|
2367
3410
|
array: schema.array,
|
|
2368
3411
|
name(name) {
|
|
2369
|
-
|
|
3412
|
+
setCurrentColumnName(name);
|
|
2370
3413
|
return this;
|
|
2371
3414
|
},
|
|
2372
3415
|
sql: sqlFn,
|
|
@@ -2471,7 +3514,7 @@ const makeColumnTypes = (schema) => {
|
|
|
2471
3514
|
return new PostgisGeographyPointColumn(schema);
|
|
2472
3515
|
}
|
|
2473
3516
|
},
|
|
2474
|
-
...
|
|
3517
|
+
...timestampHelpers
|
|
2475
3518
|
};
|
|
2476
3519
|
};
|
|
2477
3520
|
RawSQL.prototype.columnTypes = makeColumnTypes;
|
|
@@ -2560,7 +3603,7 @@ const makeColumnsByType = (schema) => {
|
|
|
2560
3603
|
};
|
|
2561
3604
|
};
|
|
2562
3605
|
|
|
2563
|
-
const anyShape = new Proxy(
|
|
3606
|
+
const anyShape = new Proxy(emptyObject, {
|
|
2564
3607
|
get() {
|
|
2565
3608
|
return UnknownColumn.instance;
|
|
2566
3609
|
}
|
|
@@ -2628,7 +3671,7 @@ const columnWithDotToSql = (ctx, data, shape, column, index, quotedAs, select) =
|
|
|
2628
3671
|
const shape2 = data.joinedShapes?.[table];
|
|
2629
3672
|
return shape2 ? select ? makeRowToJson(table, shape2, true) : `"${table}".*` : column;
|
|
2630
3673
|
}
|
|
2631
|
-
const tableName =
|
|
3674
|
+
const tableName = _getQueryAliasOrName(data, table);
|
|
2632
3675
|
const quoted = `"${table}"`;
|
|
2633
3676
|
const col = quoted === quotedAs ? shape[key] : data.joinedShapes?.[tableName]?.[key];
|
|
2634
3677
|
if (col) {
|
|
@@ -2667,7 +3710,7 @@ const tableColumnToSqlWithAs = (ctx, data, column, table, key, as, quotedAs, sel
|
|
|
2667
3710
|
}
|
|
2668
3711
|
return column;
|
|
2669
3712
|
}
|
|
2670
|
-
const tableName =
|
|
3713
|
+
const tableName = _getQueryAliasOrName(data, table);
|
|
2671
3714
|
const quoted = `"${table}"`;
|
|
2672
3715
|
const col = quoted === quotedAs ? data.shape[key] : data.joinedShapes?.[tableName][key];
|
|
2673
3716
|
if (jsonList) jsonList[as] = col;
|
|
@@ -2799,7 +3842,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2799
3842
|
}
|
|
2800
3843
|
return;
|
|
2801
3844
|
}
|
|
2802
|
-
if (
|
|
3845
|
+
if (isExpression(data)) {
|
|
2803
3846
|
ands.push(`(${data.toSQL(ctx, quotedAs)})`);
|
|
2804
3847
|
return;
|
|
2805
3848
|
}
|
|
@@ -2807,11 +3850,11 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2807
3850
|
const value = data[key];
|
|
2808
3851
|
if (value === void 0) continue;
|
|
2809
3852
|
if (key === "AND") {
|
|
2810
|
-
const arr =
|
|
3853
|
+
const arr = toArray(value);
|
|
2811
3854
|
const sql = processAnds(arr, ctx, table, query, quotedAs);
|
|
2812
3855
|
if (sql) ands.push(sql);
|
|
2813
3856
|
} else if (key === "OR") {
|
|
2814
|
-
const arr = value.map(
|
|
3857
|
+
const arr = value.map(toArray);
|
|
2815
3858
|
const sqls = arr.reduce((acc, and) => {
|
|
2816
3859
|
const sql = processAnds(and, ctx, table, query, quotedAs);
|
|
2817
3860
|
if (sql) acc.push(sql);
|
|
@@ -2819,7 +3862,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2819
3862
|
}, []);
|
|
2820
3863
|
if (sqls.length) ands.push(`(${sqls.join(" OR ")})`);
|
|
2821
3864
|
} else if (key === "NOT") {
|
|
2822
|
-
const arr =
|
|
3865
|
+
const arr = toArray(value);
|
|
2823
3866
|
ands.push(`NOT ${processAnds(arr, ctx, table, query, quotedAs, true)}`);
|
|
2824
3867
|
} else if (key === "ON") {
|
|
2825
3868
|
if (Array.isArray(value)) {
|
|
@@ -2841,10 +3884,10 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2841
3884
|
);
|
|
2842
3885
|
const rightPath = item[3];
|
|
2843
3886
|
ands.push(
|
|
2844
|
-
`jsonb_path_query_first(${leftColumn}, ${
|
|
3887
|
+
`jsonb_path_query_first(${leftColumn}, ${addValue(
|
|
2845
3888
|
ctx.values,
|
|
2846
3889
|
leftPath
|
|
2847
|
-
)}) = jsonb_path_query_first(${rightColumn}, ${
|
|
3890
|
+
)}) = jsonb_path_query_first(${rightColumn}, ${addValue(
|
|
2848
3891
|
ctx.values,
|
|
2849
3892
|
rightPath
|
|
2850
3893
|
)})`
|
|
@@ -2854,7 +3897,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2854
3897
|
const joinAs = `"${getJoinItemSource(item.joinFrom)}"`;
|
|
2855
3898
|
const q = item.useOuterAliases ? {
|
|
2856
3899
|
joinedShapes: query.joinedShapes,
|
|
2857
|
-
aliases:
|
|
3900
|
+
aliases: _getQueryOuterAliases(query),
|
|
2858
3901
|
shape: query.shape
|
|
2859
3902
|
} : query;
|
|
2860
3903
|
ands.push(
|
|
@@ -2862,7 +3905,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2862
3905
|
);
|
|
2863
3906
|
}
|
|
2864
3907
|
} else if (key === "IN") {
|
|
2865
|
-
|
|
3908
|
+
toArray(value).forEach((item) => {
|
|
2866
3909
|
pushIn(ctx, query, ands, quotedAs, item);
|
|
2867
3910
|
});
|
|
2868
3911
|
} else if (key === "EXISTS") {
|
|
@@ -2887,7 +3930,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2887
3930
|
const search = value;
|
|
2888
3931
|
ands.push(`${search.vectorSQL} @@ "${search.as}"`);
|
|
2889
3932
|
} else if (typeof value === "object" && value && !(value instanceof Date) && !Array.isArray(value)) {
|
|
2890
|
-
if (
|
|
3933
|
+
if (isExpression(value)) {
|
|
2891
3934
|
ands.push(
|
|
2892
3935
|
`${columnToSql(
|
|
2893
3936
|
ctx,
|
|
@@ -2947,7 +3990,7 @@ const processWhere = (ands, ctx, table, query, data, quotedAs) => {
|
|
|
2947
3990
|
ands.push(`${column} = ${expr.toSQL(ctx, quotedAs)}`);
|
|
2948
3991
|
} else {
|
|
2949
3992
|
ands.push(
|
|
2950
|
-
`${column} ${value === null ? "IS NULL" : `= ${
|
|
3993
|
+
`${column} ${value === null ? "IS NULL" : `= ${addValue(ctx.values, value)}`}`
|
|
2951
3994
|
);
|
|
2952
3995
|
}
|
|
2953
3996
|
}
|
|
@@ -2962,10 +4005,10 @@ const pushIn = (ctx, query, ands, quotedAs, arg) => {
|
|
|
2962
4005
|
let value;
|
|
2963
4006
|
if (Array.isArray(arg.values)) {
|
|
2964
4007
|
value = `${arg.values.map(
|
|
2965
|
-
multiple ? (arr) => `(${arr.map((value2) =>
|
|
4008
|
+
multiple ? (arr) => `(${arr.map((value2) => addValue(ctx.values, value2)).join(", ")})` : (arr) => `${arr.map((value2) => addValue(ctx.values, value2)).join(", ")}`
|
|
2966
4009
|
).join(", ")}`;
|
|
2967
4010
|
value = `(${value})`;
|
|
2968
|
-
} else if (
|
|
4011
|
+
} else if (isExpression(arg.values)) {
|
|
2969
4012
|
value = arg.values.toSQL(ctx, quotedAs);
|
|
2970
4013
|
} else {
|
|
2971
4014
|
value = `(${getSqlText(toSQL(arg.values, ctx))})`;
|
|
@@ -2981,7 +4024,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
2981
4024
|
if ("l" in args) {
|
|
2982
4025
|
const { aliasValue } = ctx;
|
|
2983
4026
|
ctx.aliasValue = true;
|
|
2984
|
-
target = `(${getSqlText(args.l.toSQL(ctx))}) "${
|
|
4027
|
+
target = `(${getSqlText(args.l.toSQL(ctx))}) "${_getQueryAliasOrName(
|
|
2985
4028
|
query,
|
|
2986
4029
|
args.a
|
|
2987
4030
|
)}"`;
|
|
@@ -3028,7 +4071,7 @@ const processJoinItem = (ctx, table, query, args, quotedAs) => {
|
|
|
3028
4071
|
target = `(VALUES ${args.d.map((x) => {
|
|
3029
4072
|
return "(" + Object.entries(shape).map(([key, column]) => {
|
|
3030
4073
|
const value = x[key];
|
|
3031
|
-
return
|
|
4074
|
+
return addValue(
|
|
3032
4075
|
values,
|
|
3033
4076
|
value === null || value === void 0 ? null : column.data.encode ? column.data.encode(value) : value
|
|
3034
4077
|
) + "::" + column.dataType;
|
|
@@ -3135,7 +4178,7 @@ const getConditionsFor3Or4LengthItem = (ctx, query, target, quotedAs, args, join
|
|
|
3135
4178
|
const getObjectOrRawConditions = (ctx, query, data, quotedAs, joinAs, joinShape) => {
|
|
3136
4179
|
if (data === true) {
|
|
3137
4180
|
return "true";
|
|
3138
|
-
} else if (
|
|
4181
|
+
} else if (isExpression(data)) {
|
|
3139
4182
|
return data.toSQL(ctx, quotedAs);
|
|
3140
4183
|
} else {
|
|
3141
4184
|
const pairs = [];
|
|
@@ -3276,7 +4319,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery, shape, whereExists,
|
|
|
3276
4319
|
};
|
|
3277
4320
|
}
|
|
3278
4321
|
}
|
|
3279
|
-
const args0 = args.length ? args[0] :
|
|
4322
|
+
const args0 = args.length ? args[0] : returnArg;
|
|
3280
4323
|
if (typeof args0 === "function") {
|
|
3281
4324
|
let q = first;
|
|
3282
4325
|
if (q.joinQueryAfterCallback) {
|
|
@@ -3357,7 +4400,7 @@ const noneResult = (q, queryData, type) => {
|
|
|
3357
4400
|
} else if (type === "valueOrThrow" && queryData.returning) {
|
|
3358
4401
|
return 0;
|
|
3359
4402
|
} else {
|
|
3360
|
-
throw new
|
|
4403
|
+
throw new NotFoundError(q);
|
|
3361
4404
|
}
|
|
3362
4405
|
};
|
|
3363
4406
|
const noneMethods = {
|
|
@@ -3367,7 +4410,7 @@ const noneMethods = {
|
|
|
3367
4410
|
try {
|
|
3368
4411
|
let result = noneResult(this, this.q, this.q.returnType);
|
|
3369
4412
|
if (this.q.transform) {
|
|
3370
|
-
result =
|
|
4413
|
+
result = applyTransforms(
|
|
3371
4414
|
this.q,
|
|
3372
4415
|
this.q.returnType,
|
|
3373
4416
|
this.q.transform,
|
|
@@ -3386,8 +4429,8 @@ const noneMethods = {
|
|
|
3386
4429
|
const _queryNone = (q) => {
|
|
3387
4430
|
if (isQueryNone(q)) return q;
|
|
3388
4431
|
q = extendQuery(q, noneMethods);
|
|
3389
|
-
|
|
3390
|
-
|
|
4432
|
+
pushQueryValueImmutable(q, "and", new RawSQL("false"));
|
|
4433
|
+
pushQueryValueImmutable(
|
|
3391
4434
|
q,
|
|
3392
4435
|
"transform",
|
|
3393
4436
|
(_, queryData) => noneResult(q, queryData, queryData.returnType)
|
|
@@ -3404,7 +4447,7 @@ const resolveCallbacksInArgs = (q, args) => {
|
|
|
3404
4447
|
qb.q = getClonedQueryData(q.q);
|
|
3405
4448
|
qb.q.and = qb.q.or = qb.q.scopes = void 0;
|
|
3406
4449
|
qb.q.subQuery = 1;
|
|
3407
|
-
|
|
4450
|
+
_setSubQueryAliases(qb);
|
|
3408
4451
|
args[i] = resolveSubQueryCallbackV2(qb, arg);
|
|
3409
4452
|
}
|
|
3410
4453
|
}
|
|
@@ -3426,21 +4469,21 @@ const validateFindBy = (q, arg, method) => {
|
|
|
3426
4469
|
for (const key in arg) {
|
|
3427
4470
|
nonEmpty = true;
|
|
3428
4471
|
if (arg[key] === void 0) {
|
|
3429
|
-
throw new
|
|
4472
|
+
throw new OrchidOrmInternalError(
|
|
3430
4473
|
q,
|
|
3431
4474
|
`${method} was called with undefined value`
|
|
3432
4475
|
);
|
|
3433
4476
|
}
|
|
3434
4477
|
}
|
|
3435
4478
|
if (!nonEmpty) {
|
|
3436
|
-
throw new
|
|
4479
|
+
throw new OrchidOrmInternalError(
|
|
3437
4480
|
q,
|
|
3438
4481
|
`${method} was called with empty object`
|
|
3439
4482
|
);
|
|
3440
4483
|
}
|
|
3441
4484
|
};
|
|
3442
4485
|
const _queryWhereSql = (q, args) => {
|
|
3443
|
-
return
|
|
4486
|
+
return pushQueryValueImmutable(
|
|
3444
4487
|
q,
|
|
3445
4488
|
"and",
|
|
3446
4489
|
sqlQueryArgsToExpression(args)
|
|
@@ -3448,24 +4491,24 @@ const _queryWhereSql = (q, args) => {
|
|
|
3448
4491
|
};
|
|
3449
4492
|
const _queryWhereNot = (q, args) => {
|
|
3450
4493
|
resolveCallbacksInArgs(q, args);
|
|
3451
|
-
return
|
|
4494
|
+
return pushQueryValueImmutable(q, "and", {
|
|
3452
4495
|
NOT: args
|
|
3453
4496
|
});
|
|
3454
4497
|
};
|
|
3455
4498
|
const _queryWhereNotSql = (q, args) => {
|
|
3456
|
-
return
|
|
4499
|
+
return pushQueryValueImmutable(q, "and", {
|
|
3457
4500
|
NOT: sqlQueryArgsToExpression(args)
|
|
3458
4501
|
});
|
|
3459
4502
|
};
|
|
3460
4503
|
const _queryWhereOneOf = (q, args) => {
|
|
3461
4504
|
resolveCallbacksInArgs(q, args);
|
|
3462
|
-
return
|
|
4505
|
+
return pushQueryValueImmutable(q, "and", {
|
|
3463
4506
|
OR: args
|
|
3464
4507
|
});
|
|
3465
4508
|
};
|
|
3466
4509
|
const _queryWhereNotOneOf = (q, args) => {
|
|
3467
4510
|
resolveCallbacksInArgs(q, args);
|
|
3468
|
-
return
|
|
4511
|
+
return pushQueryValueImmutable(q, "and", {
|
|
3469
4512
|
NOT: { OR: args }
|
|
3470
4513
|
});
|
|
3471
4514
|
};
|
|
@@ -3490,7 +4533,7 @@ const _queryOrNot = (q, args) => {
|
|
|
3490
4533
|
const _queryWhereIn = (q, and, arg, values, not) => {
|
|
3491
4534
|
let item;
|
|
3492
4535
|
if (values) {
|
|
3493
|
-
if (
|
|
4536
|
+
if (isIterable(values)) values = [...values];
|
|
3494
4537
|
if ("length" in values && !values.length) {
|
|
3495
4538
|
return _queryNone(q);
|
|
3496
4539
|
}
|
|
@@ -3516,9 +4559,9 @@ const _queryWhereIn = (q, and, arg, values, not) => {
|
|
|
3516
4559
|
}
|
|
3517
4560
|
if (not) item = { NOT: item };
|
|
3518
4561
|
if (and) {
|
|
3519
|
-
|
|
4562
|
+
pushQueryValueImmutable(q, "and", item);
|
|
3520
4563
|
} else {
|
|
3521
|
-
|
|
4564
|
+
pushQueryValueImmutable(q, "or", [item]);
|
|
3522
4565
|
}
|
|
3523
4566
|
return q;
|
|
3524
4567
|
};
|
|
@@ -4227,7 +5270,7 @@ const _chain = (fromQuery, toQuery, rel) => {
|
|
|
4227
5270
|
} else {
|
|
4228
5271
|
q.relChain = [{ query: self, rel }];
|
|
4229
5272
|
}
|
|
4230
|
-
|
|
5273
|
+
_applyRelationAliases(self, q);
|
|
4231
5274
|
q.joinedShapes = {
|
|
4232
5275
|
[getQueryAs(self)]: self.q.shape,
|
|
4233
5276
|
...self.q.joinedShapes
|
|
@@ -4269,10 +5312,10 @@ const resolveSubQueryCallbackV2 = (q, cb) => {
|
|
|
4269
5312
|
arg.q = getClonedQueryData(q.q);
|
|
4270
5313
|
arg.q.subQuery = 1;
|
|
4271
5314
|
arg.q.with = arg.q.relChain = void 0;
|
|
4272
|
-
|
|
5315
|
+
_setSubQueryAliases(arg);
|
|
4273
5316
|
return cb(arg);
|
|
4274
5317
|
};
|
|
4275
|
-
const joinSubQuery = (q, sub) =>
|
|
5318
|
+
const joinSubQuery = (q, sub) => isRelationQuery(sub) ? sub.joinQuery(sub, q) : sub;
|
|
4276
5319
|
|
|
4277
5320
|
const _clone = (q) => q.clone();
|
|
4278
5321
|
const pushQueryArrayImmutable = (q, key, value) => {
|
|
@@ -4289,7 +5332,7 @@ const setQueryObjectValueImmutable = (q, object, key, value) => {
|
|
|
4289
5332
|
};
|
|
4290
5333
|
const throwIfNoWhere = (q, method) => {
|
|
4291
5334
|
if (!q.q.or && !q.q.and && !q.q.scopes && !q.q.all) {
|
|
4292
|
-
throw new
|
|
5335
|
+
throw new OrchidOrmInternalError(
|
|
4293
5336
|
q,
|
|
4294
5337
|
`Dangerous ${method} without conditions`
|
|
4295
5338
|
);
|
|
@@ -4297,7 +5340,7 @@ const throwIfNoWhere = (q, method) => {
|
|
|
4297
5340
|
};
|
|
4298
5341
|
const throwIfJoinLateral = (q, method) => {
|
|
4299
5342
|
if (q.q.join?.some((x) => Array.isArray(x) || "s" in x.args && x.args.s)) {
|
|
4300
|
-
throw new
|
|
5343
|
+
throw new OrchidOrmInternalError(
|
|
4301
5344
|
q,
|
|
4302
5345
|
`Cannot join a complex query in ${method}`
|
|
4303
5346
|
);
|
|
@@ -4305,8 +5348,8 @@ const throwIfJoinLateral = (q, method) => {
|
|
|
4305
5348
|
};
|
|
4306
5349
|
const saveAliasedShape = (q, as, key) => {
|
|
4307
5350
|
const shapes = q.q[key];
|
|
4308
|
-
as =
|
|
4309
|
-
setQueryObjectValueImmutable(q, key, as,
|
|
5351
|
+
as = getFreeAlias(shapes, as);
|
|
5352
|
+
setQueryObjectValueImmutable(q, key, as, emptyObject);
|
|
4310
5353
|
return as;
|
|
4311
5354
|
};
|
|
4312
5355
|
const extendQuery = (q, methods) => {
|
|
@@ -4366,7 +5409,7 @@ const _queryRows = (q) => {
|
|
|
4366
5409
|
};
|
|
4367
5410
|
const getFullColumnTable = (q, column, index, as) => {
|
|
4368
5411
|
const table = column.slice(0, index);
|
|
4369
|
-
return as && table !== as &&
|
|
5412
|
+
return as && table !== as && _checkIfAliased(q, table, as) ? as : table;
|
|
4370
5413
|
};
|
|
4371
5414
|
|
|
4372
5415
|
const _joinReturningArgs = (query, require, first, args, forbidLateral) => {
|
|
@@ -4393,7 +5436,7 @@ const _joinReturningArgs = (query, require, first, args, forbidLateral) => {
|
|
|
4393
5436
|
joinKey = q.q.as || q.table;
|
|
4394
5437
|
if (joinKey) {
|
|
4395
5438
|
shape = getShapeFromSelect(q, joinSubQuery && !!q.q.select);
|
|
4396
|
-
parsers =
|
|
5439
|
+
parsers = getQueryParsers(q);
|
|
4397
5440
|
batchParsers = q.q.batchParsers;
|
|
4398
5441
|
computeds = q.q.runtimeComputeds;
|
|
4399
5442
|
if (joinSubQuery) {
|
|
@@ -4407,7 +5450,7 @@ const _joinReturningArgs = (query, require, first, args, forbidLateral) => {
|
|
|
4407
5450
|
if (relation) {
|
|
4408
5451
|
shape = getShapeFromSelect(relation.query);
|
|
4409
5452
|
const r = relation.query;
|
|
4410
|
-
parsers =
|
|
5453
|
+
parsers = getQueryParsers(r);
|
|
4411
5454
|
batchParsers = r.q.batchParsers;
|
|
4412
5455
|
computeds = r.q.runtimeComputeds;
|
|
4413
5456
|
} else {
|
|
@@ -4442,17 +5485,17 @@ const _joinReturningArgs = (query, require, first, args, forbidLateral) => {
|
|
|
4442
5485
|
if ("r" in joinArgs && joinArgs.r) {
|
|
4443
5486
|
joinArgs.c = shape = getShapeFromSelect(j, true);
|
|
4444
5487
|
}
|
|
4445
|
-
|
|
4446
|
-
|
|
5488
|
+
setObjectValueImmutable(q, "joinedShapes", joinKey, shape);
|
|
5489
|
+
setObjectValueImmutable(q, "joinedParsers", joinKey, getQueryParsers(j));
|
|
4447
5490
|
if (jq.batchParsers) {
|
|
4448
|
-
|
|
5491
|
+
setObjectValueImmutable(
|
|
4449
5492
|
jq,
|
|
4450
5493
|
"joinedBatchParsers",
|
|
4451
5494
|
joinKey,
|
|
4452
5495
|
jq.batchParsers
|
|
4453
5496
|
);
|
|
4454
5497
|
}
|
|
4455
|
-
|
|
5498
|
+
setObjectValueImmutable(
|
|
4456
5499
|
q,
|
|
4457
5500
|
"joinedComputeds",
|
|
4458
5501
|
joinKey,
|
|
@@ -4485,7 +5528,7 @@ const _join = (query, require, type, first, args) => {
|
|
|
4485
5528
|
if (!joinArgs) {
|
|
4486
5529
|
return _queryNone(query);
|
|
4487
5530
|
}
|
|
4488
|
-
|
|
5531
|
+
pushQueryValueImmutable(query, "join", {
|
|
4489
5532
|
type,
|
|
4490
5533
|
args: joinArgs
|
|
4491
5534
|
});
|
|
@@ -4500,12 +5543,12 @@ const _join = (query, require, type, first, args) => {
|
|
|
4500
5543
|
const addAllShapesAndParsers = (query, joinKey, shape, parsers, batchParsers, computeds) => {
|
|
4501
5544
|
if (!joinKey) return;
|
|
4502
5545
|
const { q } = query;
|
|
4503
|
-
|
|
4504
|
-
|
|
5546
|
+
setObjectValueImmutable(q, "joinedShapes", joinKey, shape);
|
|
5547
|
+
setObjectValueImmutable(q, "joinedParsers", joinKey, parsers);
|
|
4505
5548
|
if (batchParsers) {
|
|
4506
|
-
|
|
5549
|
+
setObjectValueImmutable(q, "joinedBatchParsers", joinKey, batchParsers);
|
|
4507
5550
|
}
|
|
4508
|
-
|
|
5551
|
+
setObjectValueImmutable(q, "joinedComputeds", joinKey, computeds);
|
|
4509
5552
|
};
|
|
4510
5553
|
const _joinLateralProcessArg = (q, arg, cb) => {
|
|
4511
5554
|
let relation;
|
|
@@ -4545,19 +5588,19 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
|
|
|
4545
5588
|
const q = self;
|
|
4546
5589
|
arg.q.joinTo = q;
|
|
4547
5590
|
const joinedAs = getQueryAs(q);
|
|
4548
|
-
|
|
5591
|
+
setObjectValueImmutable(arg.q, "joinedShapes", joinedAs, q.q.shape);
|
|
4549
5592
|
const joinKey = as || arg.q.as || arg.table;
|
|
4550
5593
|
if (joinKey) {
|
|
4551
5594
|
const shape = getShapeFromSelect(arg, true);
|
|
4552
|
-
|
|
4553
|
-
|
|
5595
|
+
setObjectValueImmutable(q.q, "joinedShapes", joinKey, shape);
|
|
5596
|
+
setObjectValueImmutable(
|
|
4554
5597
|
q.q,
|
|
4555
5598
|
"joinedParsers",
|
|
4556
5599
|
joinKey,
|
|
4557
|
-
|
|
5600
|
+
getQueryParsers(arg)
|
|
4558
5601
|
);
|
|
4559
5602
|
if (arg.q.batchParsers) {
|
|
4560
|
-
|
|
5603
|
+
setObjectValueImmutable(
|
|
4561
5604
|
q.q,
|
|
4562
5605
|
"joinedBatchParsers",
|
|
4563
5606
|
joinKey,
|
|
@@ -4566,7 +5609,7 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
|
|
|
4566
5609
|
}
|
|
4567
5610
|
}
|
|
4568
5611
|
as || (as = getQueryAs(arg));
|
|
4569
|
-
|
|
5612
|
+
setObjectValueImmutable(q.q, "joinedComputeds", as, arg.q.runtimeComputeds);
|
|
4570
5613
|
const joinArgs = {
|
|
4571
5614
|
l: arg,
|
|
4572
5615
|
a: as,
|
|
@@ -4594,7 +5637,7 @@ const _joinLateral = (self, type, arg, as, innerJoinLateral) => {
|
|
|
4594
5637
|
map.set(dedupKey, { q: arg, a: as });
|
|
4595
5638
|
}
|
|
4596
5639
|
}
|
|
4597
|
-
|
|
5640
|
+
pushQueryValueImmutable(q, "join", {
|
|
4598
5641
|
type: `${type} LATERAL`,
|
|
4599
5642
|
args: joinArgs
|
|
4600
5643
|
});
|
|
@@ -4638,11 +5681,11 @@ const logParamToLogObject = (logger, log) => {
|
|
|
4638
5681
|
logger.log(
|
|
4639
5682
|
makeMessage(
|
|
4640
5683
|
colors,
|
|
4641
|
-
|
|
5684
|
+
logColors.boldCyanBright,
|
|
4642
5685
|
time,
|
|
4643
|
-
|
|
5686
|
+
logColors.boldBlue,
|
|
4644
5687
|
sql.text,
|
|
4645
|
-
|
|
5688
|
+
logColors.boldYellow,
|
|
4646
5689
|
sql.values
|
|
4647
5690
|
)
|
|
4648
5691
|
);
|
|
@@ -4652,13 +5695,13 @@ const logParamToLogObject = (logger, log) => {
|
|
|
4652
5695
|
logger.error(
|
|
4653
5696
|
`${makeMessage(
|
|
4654
5697
|
colors,
|
|
4655
|
-
|
|
5698
|
+
logColors.boldMagenta,
|
|
4656
5699
|
time,
|
|
4657
|
-
|
|
5700
|
+
logColors.boldRed,
|
|
4658
5701
|
sql.text,
|
|
4659
|
-
|
|
5702
|
+
logColors.boldYellow,
|
|
4660
5703
|
sql.values
|
|
4661
|
-
)} ${colors ?
|
|
5704
|
+
)} ${colors ? logColors.boldRed(message) : message}`
|
|
4662
5705
|
);
|
|
4663
5706
|
}
|
|
4664
5707
|
},
|
|
@@ -4681,7 +5724,7 @@ const commitSql = {
|
|
|
4681
5724
|
const rollbackSql = {
|
|
4682
5725
|
text: "ROLLBACK"
|
|
4683
5726
|
};
|
|
4684
|
-
class AfterCommitError extends
|
|
5727
|
+
class AfterCommitError extends OrchidOrmError {
|
|
4685
5728
|
constructor(result, hookResults) {
|
|
4686
5729
|
super("After commit hooks have failed");
|
|
4687
5730
|
this.result = result;
|
|
@@ -4712,14 +5755,14 @@ class Transaction {
|
|
|
4712
5755
|
let options;
|
|
4713
5756
|
let fn;
|
|
4714
5757
|
if (typeof cbOrOptions === "function") {
|
|
4715
|
-
options =
|
|
5758
|
+
options = emptyObject;
|
|
4716
5759
|
fn = cbOrOptions;
|
|
4717
5760
|
} else {
|
|
4718
5761
|
options = typeof cbOrOptions === "object" ? cbOrOptions : { level: cbOrOptions };
|
|
4719
5762
|
fn = cb;
|
|
4720
5763
|
}
|
|
4721
5764
|
const sql = {
|
|
4722
|
-
values:
|
|
5765
|
+
values: emptyArray
|
|
4723
5766
|
};
|
|
4724
5767
|
const log = options.log !== void 0 ? this.q.log ?? logParamToLogObject(this.q.logger, options.log) : this.q.log;
|
|
4725
5768
|
let logData;
|
|
@@ -4919,7 +5962,7 @@ const runAfterCommit = (afterCommit, result) => {
|
|
|
4919
5962
|
const applyBatchTransforms = (q, batches) => {
|
|
4920
5963
|
if (q.transform) {
|
|
4921
5964
|
for (const item of batches) {
|
|
4922
|
-
item.parent[item.key] =
|
|
5965
|
+
item.parent[item.key] = applyTransforms(
|
|
4923
5966
|
q,
|
|
4924
5967
|
q.returnType,
|
|
4925
5968
|
q.transform,
|
|
@@ -5209,18 +6252,19 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5209
6252
|
try {
|
|
5210
6253
|
if (beforeHooks || query.before) {
|
|
5211
6254
|
await Promise.all(
|
|
5212
|
-
[...beforeHooks ||
|
|
5213
|
-
|
|
6255
|
+
[...beforeHooks || emptyArray, ...query.before || emptyArray].map(
|
|
6256
|
+
callWithThis,
|
|
5214
6257
|
q
|
|
5215
6258
|
)
|
|
5216
6259
|
);
|
|
5217
6260
|
}
|
|
5218
6261
|
const localSql = sql = q.toSQL();
|
|
5219
|
-
const {
|
|
6262
|
+
const { tableHook, delayedRelationSelect } = sql;
|
|
5220
6263
|
const { returnType = "all" } = query;
|
|
5221
|
-
const tempReturnType =
|
|
6264
|
+
const tempReturnType = tableHook?.select || returnType === "rows" && q.q.batchParsers || delayedRelationSelect?.value ? "all" : returnType;
|
|
5222
6265
|
let result;
|
|
5223
6266
|
let queryResult;
|
|
6267
|
+
let cteData;
|
|
5224
6268
|
if ("text" in sql) {
|
|
5225
6269
|
if (query.autoPreparedStatements) {
|
|
5226
6270
|
sql.name = queriesNames[sql.text] || (queriesNames[sql.text] = (nameI++).toString(36));
|
|
@@ -5237,8 +6281,20 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5237
6281
|
log.afterQuery(sql, logData);
|
|
5238
6282
|
sql = void 0;
|
|
5239
6283
|
}
|
|
6284
|
+
if (localSql.cteHooks?.hasSelect) {
|
|
6285
|
+
const lastRowI = queryResult.rows.length - 1;
|
|
6286
|
+
const lastFieldI = queryResult.fields.length - 1;
|
|
6287
|
+
const fieldName = queryResult.fields[lastFieldI].name;
|
|
6288
|
+
cteData = queryResult.rows[lastRowI][fieldName];
|
|
6289
|
+
queryResult.fields.length = lastFieldI;
|
|
6290
|
+
queryResult.rowCount--;
|
|
6291
|
+
queryResult.rows.length = lastRowI;
|
|
6292
|
+
for (const row of queryResult.rows) {
|
|
6293
|
+
delete row[fieldName];
|
|
6294
|
+
}
|
|
6295
|
+
}
|
|
5240
6296
|
if (query.patchResult) {
|
|
5241
|
-
await query.patchResult(q,
|
|
6297
|
+
await query.patchResult(q, tableHook?.select, queryResult);
|
|
5242
6298
|
}
|
|
5243
6299
|
result = query.handleResult(q, tempReturnType, queryResult, localSql);
|
|
5244
6300
|
} else {
|
|
@@ -5276,15 +6332,15 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5276
6332
|
if (log) log.afterQuery(commitSql, logData);
|
|
5277
6333
|
}
|
|
5278
6334
|
if (query.patchResult) {
|
|
5279
|
-
await query.patchResult(q,
|
|
6335
|
+
await query.patchResult(q, tableHook?.select, queryResult);
|
|
5280
6336
|
}
|
|
5281
6337
|
result = query.handleResult(q, tempReturnType, queryResult, localSql);
|
|
5282
6338
|
}
|
|
5283
6339
|
let tempColumns;
|
|
5284
6340
|
let renames;
|
|
5285
|
-
if (
|
|
5286
|
-
for (const column of
|
|
5287
|
-
const { as, temp } =
|
|
6341
|
+
if (tableHook?.select) {
|
|
6342
|
+
for (const column of tableHook.select.keys()) {
|
|
6343
|
+
const { as, temp } = tableHook.select.get(column);
|
|
5288
6344
|
if (as) {
|
|
5289
6345
|
(renames ?? (renames = {}))[column] = as;
|
|
5290
6346
|
}
|
|
@@ -5306,42 +6362,103 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5306
6362
|
if (promise2) await promise2;
|
|
5307
6363
|
}
|
|
5308
6364
|
}
|
|
5309
|
-
|
|
6365
|
+
let cteAfterHooks;
|
|
6366
|
+
let cteAfterCommitHooks;
|
|
6367
|
+
if (localSql.cteHooks) {
|
|
6368
|
+
for (const cteName in localSql.cteHooks.tableHooks) {
|
|
6369
|
+
const hook = localSql.cteHooks.tableHooks[cteName];
|
|
6370
|
+
const data = cteData?.[cteName];
|
|
6371
|
+
if (data) {
|
|
6372
|
+
let hasParsers;
|
|
6373
|
+
const parsers = {};
|
|
6374
|
+
for (const key in hook.shape) {
|
|
6375
|
+
if (hook.shape[key]._parse) {
|
|
6376
|
+
hasParsers = true;
|
|
6377
|
+
parsers[key] = hook.shape[key]._parse;
|
|
6378
|
+
}
|
|
6379
|
+
}
|
|
6380
|
+
if (hasParsers) {
|
|
6381
|
+
for (const row of data) {
|
|
6382
|
+
parseRecord(parsers, row);
|
|
6383
|
+
}
|
|
6384
|
+
}
|
|
6385
|
+
}
|
|
6386
|
+
if (hook.tableHook.after) {
|
|
6387
|
+
(cteAfterHooks ?? (cteAfterHooks = [])).push(
|
|
6388
|
+
...hook.tableHook.after.map(
|
|
6389
|
+
(fn) => () => fn(cteData?.[cteName], q)
|
|
6390
|
+
)
|
|
6391
|
+
);
|
|
6392
|
+
}
|
|
6393
|
+
if (hook.tableHook.afterCommit) {
|
|
6394
|
+
(cteAfterCommitHooks ?? (cteAfterCommitHooks = [])).push(
|
|
6395
|
+
...hook.tableHook.afterCommit.map(
|
|
6396
|
+
(fn) => () => fn(cteData?.[cteName], q)
|
|
6397
|
+
)
|
|
6398
|
+
);
|
|
6399
|
+
}
|
|
6400
|
+
}
|
|
6401
|
+
}
|
|
6402
|
+
const hasAfterHook = afterHooks || afterCommitHooks || query.after || cteAfterHooks || cteAfterCommitHooks;
|
|
5310
6403
|
if (hasAfterHook) {
|
|
5311
6404
|
if (queryResult.rowCount) {
|
|
5312
|
-
if (afterHooks || query.after) {
|
|
6405
|
+
if (afterHooks || query.after || cteAfterHooks) {
|
|
5313
6406
|
const args = [result, q];
|
|
5314
6407
|
await Promise.all(
|
|
5315
|
-
[
|
|
5316
|
-
|
|
5317
|
-
|
|
5318
|
-
|
|
6408
|
+
[
|
|
6409
|
+
...afterHooks || emptyArray,
|
|
6410
|
+
...query.after || emptyArray,
|
|
6411
|
+
...cteAfterHooks || emptyArray
|
|
6412
|
+
].map(callAfterHook, args)
|
|
5319
6413
|
);
|
|
5320
6414
|
}
|
|
5321
|
-
if (afterCommitHooks) {
|
|
6415
|
+
if (afterCommitHooks || cteAfterCommitHooks) {
|
|
5322
6416
|
if (isInUserTransaction(trx)) {
|
|
5323
|
-
|
|
5324
|
-
|
|
5325
|
-
|
|
5326
|
-
|
|
5327
|
-
|
|
6417
|
+
if (afterCommitHooks) {
|
|
6418
|
+
(trx.afterCommit ?? (trx.afterCommit = [])).push(
|
|
6419
|
+
result,
|
|
6420
|
+
q,
|
|
6421
|
+
afterCommitHooks
|
|
6422
|
+
);
|
|
6423
|
+
}
|
|
6424
|
+
if (cteAfterCommitHooks) {
|
|
6425
|
+
(trx.afterCommit ?? (trx.afterCommit = [])).push(
|
|
6426
|
+
result,
|
|
6427
|
+
q,
|
|
6428
|
+
cteAfterCommitHooks
|
|
6429
|
+
);
|
|
6430
|
+
}
|
|
5328
6431
|
} else {
|
|
5329
6432
|
const localResult = result;
|
|
5330
6433
|
queueMicrotask(async () => {
|
|
5331
6434
|
const promises = [];
|
|
5332
|
-
|
|
5333
|
-
|
|
5334
|
-
|
|
5335
|
-
|
|
5336
|
-
|
|
5337
|
-
|
|
5338
|
-
|
|
6435
|
+
if (afterCommitHooks) {
|
|
6436
|
+
for (const fn of afterCommitHooks) {
|
|
6437
|
+
try {
|
|
6438
|
+
promises.push(
|
|
6439
|
+
fn(localResult, q)
|
|
6440
|
+
);
|
|
6441
|
+
} catch (err) {
|
|
6442
|
+
promises.push(Promise.reject(err));
|
|
6443
|
+
}
|
|
6444
|
+
}
|
|
6445
|
+
}
|
|
6446
|
+
if (cteAfterCommitHooks) {
|
|
6447
|
+
for (const fn of cteAfterCommitHooks) {
|
|
6448
|
+
try {
|
|
6449
|
+
promises.push(fn());
|
|
6450
|
+
} catch (err) {
|
|
6451
|
+
promises.push(Promise.reject(err));
|
|
6452
|
+
}
|
|
5339
6453
|
}
|
|
5340
6454
|
}
|
|
5341
6455
|
await _runAfterCommitHooks(
|
|
5342
6456
|
localResult,
|
|
5343
6457
|
promises,
|
|
5344
|
-
() =>
|
|
6458
|
+
() => [
|
|
6459
|
+
...afterCommitHooks || emptyArray,
|
|
6460
|
+
...cteAfterCommitHooks || emptyArray
|
|
6461
|
+
].map((h) => h.name),
|
|
5345
6462
|
q.q.catchAfterCommitErrors
|
|
5346
6463
|
);
|
|
5347
6464
|
});
|
|
@@ -5354,7 +6471,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5354
6471
|
}
|
|
5355
6472
|
if (delayedRelationSelect?.value) {
|
|
5356
6473
|
const q2 = delayedRelationSelect.query;
|
|
5357
|
-
const primaryKeys =
|
|
6474
|
+
const primaryKeys = requirePrimaryKeys(
|
|
5358
6475
|
q2,
|
|
5359
6476
|
"Cannot select a relation of a table that has no primary keys"
|
|
5360
6477
|
);
|
|
@@ -5372,7 +6489,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5372
6489
|
const select = [{ selectAs }];
|
|
5373
6490
|
const relationKeyAliases = primaryKeys.map((key) => {
|
|
5374
6491
|
if (key in selectAs) {
|
|
5375
|
-
const as =
|
|
6492
|
+
const as = getFreeAlias(selectAs, key);
|
|
5376
6493
|
selectAs[as] = key;
|
|
5377
6494
|
return as;
|
|
5378
6495
|
} else {
|
|
@@ -5402,7 +6519,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5402
6519
|
}
|
|
5403
6520
|
const promise = parseBatch(q, queryResult, delayedRelationSelect);
|
|
5404
6521
|
if (promise) await promise;
|
|
5405
|
-
if (
|
|
6522
|
+
if (tableHook?.select || tempReturnType !== returnType) {
|
|
5406
6523
|
if (renames) {
|
|
5407
6524
|
const renamedResult = Array.from({
|
|
5408
6525
|
length: result.length
|
|
@@ -5428,7 +6545,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
5428
6545
|
);
|
|
5429
6546
|
}
|
|
5430
6547
|
if (query.transform) {
|
|
5431
|
-
result =
|
|
6548
|
+
result = applyTransforms(query, returnType, query.transform, result);
|
|
5432
6549
|
}
|
|
5433
6550
|
return resolve ? resolve(result) : result;
|
|
5434
6551
|
} catch (err) {
|
|
@@ -5470,11 +6587,11 @@ const execQuery = (adapter, method, sql) => {
|
|
|
5470
6587
|
});
|
|
5471
6588
|
};
|
|
5472
6589
|
const handleResult = (q, returnType, result, sql, isSubQuery) => {
|
|
5473
|
-
const parsers =
|
|
6590
|
+
const parsers = getQueryParsers(q, sql.tableHook?.select);
|
|
5474
6591
|
switch (returnType) {
|
|
5475
6592
|
case "all": {
|
|
5476
6593
|
if (q.q.throwOnNotFound && result.rows.length === 0)
|
|
5477
|
-
throw new
|
|
6594
|
+
throw new NotFoundError(q);
|
|
5478
6595
|
const { rows } = result;
|
|
5479
6596
|
if (parsers) {
|
|
5480
6597
|
for (const row of rows) {
|
|
@@ -5491,7 +6608,7 @@ const handleResult = (q, returnType, result, sql, isSubQuery) => {
|
|
|
5491
6608
|
}
|
|
5492
6609
|
case "oneOrThrow": {
|
|
5493
6610
|
const { rows } = result;
|
|
5494
|
-
if (!rows.length) throw new
|
|
6611
|
+
if (!rows.length) throw new NotFoundError(q);
|
|
5495
6612
|
if (parsers) parseRecord(parsers, rows[0]);
|
|
5496
6613
|
return rows[0];
|
|
5497
6614
|
}
|
|
@@ -5514,12 +6631,12 @@ const handleResult = (q, returnType, result, sql, isSubQuery) => {
|
|
|
5514
6631
|
case "valueOrThrow": {
|
|
5515
6632
|
if (q.q.returning) {
|
|
5516
6633
|
if (q.q.throwOnNotFound && result.rowCount === 0) {
|
|
5517
|
-
throw new
|
|
6634
|
+
throw new NotFoundError(q);
|
|
5518
6635
|
}
|
|
5519
6636
|
return result.rowCount;
|
|
5520
6637
|
}
|
|
5521
6638
|
const { rows } = result;
|
|
5522
|
-
if (rows[0]?.[0] === void 0) throw new
|
|
6639
|
+
if (rows[0]?.[0] === void 0) throw new NotFoundError(q);
|
|
5523
6640
|
return parseValue(rows[0][0], parsers);
|
|
5524
6641
|
}
|
|
5525
6642
|
case "void": {
|
|
@@ -5568,7 +6685,7 @@ const parsePluck = (parsers, isSubQuery, rows) => {
|
|
|
5568
6685
|
}
|
|
5569
6686
|
};
|
|
5570
6687
|
const parseValue = (value, parsers) => {
|
|
5571
|
-
const parser = parsers?.[
|
|
6688
|
+
const parser = parsers?.[getValueKey];
|
|
5572
6689
|
return parser ? parser(value) : value;
|
|
5573
6690
|
};
|
|
5574
6691
|
const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterHook) => {
|
|
@@ -5578,7 +6695,7 @@ const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterH
|
|
|
5578
6695
|
if (returnType === "oneOrThrow" || returnType === "one") {
|
|
5579
6696
|
let row = result[0];
|
|
5580
6697
|
if (!row) {
|
|
5581
|
-
if (returnType === "oneOrThrow") throw new
|
|
6698
|
+
if (returnType === "oneOrThrow") throw new NotFoundError(q);
|
|
5582
6699
|
return void 0;
|
|
5583
6700
|
} else if (!tempColumns?.size) {
|
|
5584
6701
|
return row;
|
|
@@ -5598,7 +6715,7 @@ const filterResult = (q, returnType, queryResult, result, tempColumns, hasAfterH
|
|
|
5598
6715
|
return queryResult.rowCount;
|
|
5599
6716
|
}
|
|
5600
6717
|
const row = result[0];
|
|
5601
|
-
if (!row) throw new
|
|
6718
|
+
if (!row) throw new NotFoundError(q);
|
|
5602
6719
|
return row[getFirstResultKey(q, queryResult)];
|
|
5603
6720
|
}
|
|
5604
6721
|
if (returnType === "pluck") {
|
|
@@ -5670,10 +6787,10 @@ function queryFrom(self, arg) {
|
|
|
5670
6787
|
const parsers = {};
|
|
5671
6788
|
joinedParsers[item] = parsers;
|
|
5672
6789
|
addWithParsers(w, parsers);
|
|
5673
|
-
} else if (!
|
|
6790
|
+
} else if (!isExpression(item)) {
|
|
5674
6791
|
Object.assign(shape, getShapeFromSelect(item, true));
|
|
5675
6792
|
const key = getQueryAs(item);
|
|
5676
|
-
joinedParsers[key] =
|
|
6793
|
+
joinedParsers[key] = getQueryParsers(item);
|
|
5677
6794
|
}
|
|
5678
6795
|
}
|
|
5679
6796
|
data.joinedParsers = joinedParsers;
|
|
@@ -5681,7 +6798,7 @@ function queryFrom(self, arg) {
|
|
|
5681
6798
|
const q = arg;
|
|
5682
6799
|
data.as || (data.as = q.q.as || q.table || "t");
|
|
5683
6800
|
data.shape = getShapeFromSelect(q, true);
|
|
5684
|
-
data.defaultParsers =
|
|
6801
|
+
data.defaultParsers = getQueryParsers(q);
|
|
5685
6802
|
data.batchParsers = q.q.batchParsers;
|
|
5686
6803
|
}
|
|
5687
6804
|
data.from = arg;
|
|
@@ -5765,7 +6882,7 @@ class FromMethods {
|
|
|
5765
6882
|
}
|
|
5766
6883
|
|
|
5767
6884
|
function queryWrap(self, query, as = "t") {
|
|
5768
|
-
return
|
|
6885
|
+
return _setQueryAs(queryFrom(query, self), as);
|
|
5769
6886
|
}
|
|
5770
6887
|
function cloneQueryBaseUnscoped(query) {
|
|
5771
6888
|
const q = query.baseQuery.clone();
|
|
@@ -5779,7 +6896,7 @@ const addParserForRawExpression = (q, key, raw) => {
|
|
|
5779
6896
|
const addParsersForSelectJoined = (q, arg, as = arg) => {
|
|
5780
6897
|
const parsers = q.q.joinedParsers?.[arg];
|
|
5781
6898
|
if (parsers) {
|
|
5782
|
-
|
|
6899
|
+
setParserToQuery(q.q, as, (row) => parseRecord(parsers, row));
|
|
5783
6900
|
}
|
|
5784
6901
|
const batchParsers = q.q.joinedBatchParsers?.[arg];
|
|
5785
6902
|
if (batchParsers) {
|
|
@@ -5806,9 +6923,9 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5806
6923
|
}))
|
|
5807
6924
|
);
|
|
5808
6925
|
}
|
|
5809
|
-
const parsers =
|
|
6926
|
+
const parsers = isExpression(arg) ? void 0 : getQueryParsers(arg);
|
|
5810
6927
|
if (parsers || query.hookSelect || query.transform || query.returnType === "oneOrThrow" || query.returnType === "valueOrThrow" || query.returnType === "one" || query.returnType === "value") {
|
|
5811
|
-
|
|
6928
|
+
pushQueryValueImmutable(q, "batchParsers", {
|
|
5812
6929
|
path: [key],
|
|
5813
6930
|
fn: (path, queryResult) => {
|
|
5814
6931
|
const { rows } = queryResult;
|
|
@@ -5850,7 +6967,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5850
6967
|
}
|
|
5851
6968
|
} else {
|
|
5852
6969
|
for (const { data } of batches) {
|
|
5853
|
-
if (!data) throw new
|
|
6970
|
+
if (!data) throw new NotFoundError(arg);
|
|
5854
6971
|
parseRecord(parsers, data);
|
|
5855
6972
|
}
|
|
5856
6973
|
}
|
|
@@ -5861,7 +6978,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5861
6978
|
}
|
|
5862
6979
|
} else {
|
|
5863
6980
|
for (const { data } of batches) {
|
|
5864
|
-
if (!data) throw new
|
|
6981
|
+
if (!data) throw new NotFoundError(arg);
|
|
5865
6982
|
}
|
|
5866
6983
|
}
|
|
5867
6984
|
if (hookSelect) {
|
|
@@ -5885,7 +7002,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5885
7002
|
case "value":
|
|
5886
7003
|
case "valueOrThrow": {
|
|
5887
7004
|
const notNullable = !query.getColumn?.data.isNullable;
|
|
5888
|
-
const parse = parsers?.[
|
|
7005
|
+
const parse = parsers?.[getValueKey];
|
|
5889
7006
|
if (parse) {
|
|
5890
7007
|
if (returnType === "value") {
|
|
5891
7008
|
for (const item of batches) {
|
|
@@ -5894,7 +7011,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5894
7011
|
} else {
|
|
5895
7012
|
for (const item of batches) {
|
|
5896
7013
|
if (notNullable && item.data === null) {
|
|
5897
|
-
throw new
|
|
7014
|
+
throw new NotFoundError(arg);
|
|
5898
7015
|
}
|
|
5899
7016
|
item.parent[item.key] = item.data = parse(item.data);
|
|
5900
7017
|
}
|
|
@@ -5907,7 +7024,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5907
7024
|
}
|
|
5908
7025
|
} else if (notNullable) {
|
|
5909
7026
|
for (const { data } of batches) {
|
|
5910
|
-
if (data === null) throw new
|
|
7027
|
+
if (data === null) throw new NotFoundError(arg);
|
|
5911
7028
|
}
|
|
5912
7029
|
}
|
|
5913
7030
|
if (hookSelect) {
|
|
@@ -5967,7 +7084,7 @@ const addParserForSelectItem = (q, as, key, arg, columnAlias, joinQuery) => {
|
|
|
5967
7084
|
if (!joinQuery && arg.q?.subQuery && arg.q.expr) {
|
|
5968
7085
|
arg = arg.q.expr;
|
|
5969
7086
|
}
|
|
5970
|
-
if (
|
|
7087
|
+
if (isExpression(arg)) {
|
|
5971
7088
|
addParserForRawExpression(q, key, arg);
|
|
5972
7089
|
return arg;
|
|
5973
7090
|
}
|
|
@@ -6025,7 +7142,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
6025
7142
|
return false;
|
|
6026
7143
|
}
|
|
6027
7144
|
}
|
|
6028
|
-
if (!
|
|
7145
|
+
if (!isExpression(value) && isRelationQuery(value) && // `subQuery = 1` case is when callback returns the same query as it gets,
|
|
6029
7146
|
// for example `q => q.get('name')`.
|
|
6030
7147
|
value.q.subQuery !== 1) {
|
|
6031
7148
|
query.q.selectRelation = joinQuery = true;
|
|
@@ -6066,7 +7183,7 @@ const processSelectArg = (q, as, arg, columnAs) => {
|
|
|
6066
7183
|
innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow"
|
|
6067
7184
|
);
|
|
6068
7185
|
if (as2) {
|
|
6069
|
-
value.q.joinedForSelect =
|
|
7186
|
+
value.q.joinedForSelect = _copyQueryAliasToQuery(
|
|
6070
7187
|
value,
|
|
6071
7188
|
q,
|
|
6072
7189
|
as2
|
|
@@ -6101,7 +7218,7 @@ const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
|
|
|
6101
7218
|
return selectColumn(query, q, column, columnAs, columnAlias);
|
|
6102
7219
|
}
|
|
6103
7220
|
const parser = q.joinedParsers?.[table]?.[column];
|
|
6104
|
-
if (parser)
|
|
7221
|
+
if (parser) setParserToQuery(q, columnAs || column, parser);
|
|
6105
7222
|
const batchParsers = q.joinedBatchParsers?.[table];
|
|
6106
7223
|
if (batchParsers) {
|
|
6107
7224
|
let cloned = false;
|
|
@@ -6118,8 +7235,8 @@ const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
|
|
|
6118
7235
|
const computeds = q.joinedComputeds?.[table];
|
|
6119
7236
|
if (computeds?.[column]) {
|
|
6120
7237
|
const computed = computeds[column];
|
|
6121
|
-
|
|
6122
|
-
|
|
7238
|
+
_addToHookSelectWithTable(query, computed.deps, table);
|
|
7239
|
+
setObjectValueImmutable(q, "selectedComputeds", column, computed);
|
|
6123
7240
|
return;
|
|
6124
7241
|
}
|
|
6125
7242
|
return arg;
|
|
@@ -6128,14 +7245,14 @@ const selectColumn = (query, q, key, columnAs, columnAlias) => {
|
|
|
6128
7245
|
if (key === "*") {
|
|
6129
7246
|
const { defaultParsers } = query.q;
|
|
6130
7247
|
if (defaultParsers) {
|
|
6131
|
-
|
|
7248
|
+
spreadObjectValues(query.q, "parsers", defaultParsers);
|
|
6132
7249
|
}
|
|
6133
7250
|
} else {
|
|
6134
7251
|
const parser = query.q.defaultParsers?.[key];
|
|
6135
|
-
if (parser)
|
|
7252
|
+
if (parser) setObjectValueImmutable(q, "parsers", columnAs || key, parser);
|
|
6136
7253
|
if (q.runtimeComputeds?.[key]) {
|
|
6137
7254
|
const computed = q.runtimeComputeds[key];
|
|
6138
|
-
|
|
7255
|
+
_addToHookSelect(query, computed.deps);
|
|
6139
7256
|
query.q.selectedComputeds = {
|
|
6140
7257
|
...query.q.selectedComputeds,
|
|
6141
7258
|
[columnAlias || key]: computed
|
|
@@ -6164,7 +7281,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
6164
7281
|
for (const key in shape) {
|
|
6165
7282
|
const column = shape[key];
|
|
6166
7283
|
if (!column.data.explicitSelect) {
|
|
6167
|
-
result[key] = column.data.name ?
|
|
7284
|
+
result[key] = column.data.name ? setColumnData(column, "name", void 0) : column;
|
|
6168
7285
|
}
|
|
6169
7286
|
}
|
|
6170
7287
|
} else {
|
|
@@ -6175,7 +7292,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
6175
7292
|
for (const item of select) {
|
|
6176
7293
|
if (typeof item === "string") {
|
|
6177
7294
|
addColumnToShapeFromSelect(q, item, shape, query, result, isSubQuery);
|
|
6178
|
-
} else if (
|
|
7295
|
+
} else if (isExpression(item)) {
|
|
6179
7296
|
result.value = item.result.value;
|
|
6180
7297
|
} else if (item && "selectAs" in item) {
|
|
6181
7298
|
for (const key in item.selectAs) {
|
|
@@ -6190,7 +7307,7 @@ const getShapeFromSelect = (q, isSubQuery) => {
|
|
|
6190
7307
|
isSubQuery,
|
|
6191
7308
|
key
|
|
6192
7309
|
);
|
|
6193
|
-
} else if (
|
|
7310
|
+
} else if (isExpression(it)) {
|
|
6194
7311
|
result[key] = it.result.value || UnknownColumn.instance;
|
|
6195
7312
|
} else if (it) {
|
|
6196
7313
|
const { returnType } = it.q;
|
|
@@ -6311,7 +7428,7 @@ class Select {
|
|
|
6311
7428
|
}
|
|
6312
7429
|
}
|
|
6313
7430
|
|
|
6314
|
-
class SelectItemExpression extends
|
|
7431
|
+
class SelectItemExpression extends Expression {
|
|
6315
7432
|
constructor(query, item, value) {
|
|
6316
7433
|
super();
|
|
6317
7434
|
this.query = query;
|
|
@@ -6355,13 +7472,13 @@ const _get = (query, returnType, arg) => {
|
|
|
6355
7472
|
query,
|
|
6356
7473
|
arg,
|
|
6357
7474
|
getQueryAs(query),
|
|
6358
|
-
|
|
7475
|
+
getValueKey
|
|
6359
7476
|
);
|
|
6360
7477
|
q.select = selected ? [q.expr = new SelectItemExpression(query, selected, type)] : void 0;
|
|
6361
7478
|
} else {
|
|
6362
7479
|
type = arg.result.value;
|
|
6363
7480
|
q.getColumn = type;
|
|
6364
|
-
addParserForRawExpression(query,
|
|
7481
|
+
addParserForRawExpression(query, getValueKey, arg);
|
|
6365
7482
|
q.select = [q.expr = arg];
|
|
6366
7483
|
}
|
|
6367
7484
|
return setQueryOperators(
|
|
@@ -6376,7 +7493,7 @@ function _queryGetOptional(self, arg) {
|
|
|
6376
7493
|
return _get(self, "value", arg);
|
|
6377
7494
|
}
|
|
6378
7495
|
|
|
6379
|
-
class RowToJsonExpression extends
|
|
7496
|
+
class RowToJsonExpression extends Expression {
|
|
6380
7497
|
constructor(from, one, coalesce) {
|
|
6381
7498
|
super();
|
|
6382
7499
|
this.from = from;
|
|
@@ -6395,6 +7512,7 @@ class RowToJsonExpression extends orchidCore.Expression {
|
|
|
6395
7512
|
q.q,
|
|
6396
7513
|
`"${getQueryAs(q)}"`,
|
|
6397
7514
|
q.q.hookSelect,
|
|
7515
|
+
void 0,
|
|
6398
7516
|
aliases,
|
|
6399
7517
|
void 0,
|
|
6400
7518
|
jsonList
|
|
@@ -6435,11 +7553,11 @@ const withToSql = (ctx, items) => {
|
|
|
6435
7553
|
if (!item) continue;
|
|
6436
7554
|
let inner;
|
|
6437
7555
|
if (item.q) {
|
|
6438
|
-
inner =
|
|
7556
|
+
inner = toSubSqlText(item.q, item.n, ctx);
|
|
6439
7557
|
} else {
|
|
6440
7558
|
inner = item.s.toSQL(ctx, `"${item.n}"`);
|
|
6441
7559
|
}
|
|
6442
|
-
const o = item.o ??
|
|
7560
|
+
const o = item.o ?? emptyObject;
|
|
6443
7561
|
sqls.push(
|
|
6444
7562
|
`${o.recursive ? "RECURSIVE " : ""}"${item.n}"${o.columns ? `(${o.columns.map((x) => `"${x}"`).join(", ")})` : ""} AS ${o.materialized ? "MATERIALIZED " : o.notMaterialized ? "NOT MATERIALIZED " : ""}(${inner})`
|
|
6445
7563
|
);
|
|
@@ -6463,7 +7581,7 @@ const pushOrAppendWithSql = (ctx, query, items) => {
|
|
|
6463
7581
|
}
|
|
6464
7582
|
};
|
|
6465
7583
|
|
|
6466
|
-
const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
7584
|
+
const makeInsertSql = (ctx, q, query, quotedAs, isSubSql) => {
|
|
6467
7585
|
let { columns } = query;
|
|
6468
7586
|
const { shape, inCTE, hookCreateSet } = query;
|
|
6469
7587
|
const QueryClass = ctx.qb.constructor;
|
|
@@ -6484,14 +7602,17 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6484
7602
|
(column) => `"${shape[column]?.data.name || column}"`
|
|
6485
7603
|
);
|
|
6486
7604
|
let runtimeDefaults;
|
|
7605
|
+
let runtimeDefaultColumns;
|
|
6487
7606
|
if (q.internal.runtimeDefaultColumns) {
|
|
6488
7607
|
runtimeDefaults = [];
|
|
7608
|
+
runtimeDefaultColumns = [];
|
|
6489
7609
|
for (const key of q.internal.runtimeDefaultColumns) {
|
|
6490
7610
|
if (!columns.includes(key)) {
|
|
6491
7611
|
const column = shape[key];
|
|
6492
7612
|
columns.push(key);
|
|
6493
7613
|
quotedColumns.push(`"${column.data.name || key}"`);
|
|
6494
7614
|
runtimeDefaults.push(column.data.runtimeDefault);
|
|
7615
|
+
runtimeDefaultColumns.push(key);
|
|
6495
7616
|
}
|
|
6496
7617
|
}
|
|
6497
7618
|
}
|
|
@@ -6553,7 +7674,10 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6553
7674
|
const name = shape[merge]?.data.name || merge;
|
|
6554
7675
|
sql = `DO UPDATE SET "${name}" = excluded."${name}"`;
|
|
6555
7676
|
} else if ("except" in merge) {
|
|
6556
|
-
sql = mergeColumnsSql(columns, quotedColumns, target,
|
|
7677
|
+
sql = mergeColumnsSql(columns, quotedColumns, target, [
|
|
7678
|
+
...toArray(merge.except),
|
|
7679
|
+
...runtimeDefaultColumns || emptyArray
|
|
7680
|
+
]);
|
|
6557
7681
|
} else {
|
|
6558
7682
|
sql = `DO UPDATE SET ${merge.reduce((sql2, item, i) => {
|
|
6559
7683
|
const name = shape[item]?.data.name || item;
|
|
@@ -6561,7 +7685,12 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6561
7685
|
}, "")}`;
|
|
6562
7686
|
}
|
|
6563
7687
|
} else {
|
|
6564
|
-
sql = mergeColumnsSql(
|
|
7688
|
+
sql = mergeColumnsSql(
|
|
7689
|
+
columns,
|
|
7690
|
+
quotedColumns,
|
|
7691
|
+
target,
|
|
7692
|
+
runtimeDefaultColumns
|
|
7693
|
+
);
|
|
6565
7694
|
}
|
|
6566
7695
|
ctx.sql.push(sql);
|
|
6567
7696
|
} else if (query.onConflict.set) {
|
|
@@ -6569,7 +7698,7 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6569
7698
|
const arr = [];
|
|
6570
7699
|
for (const key in set) {
|
|
6571
7700
|
const val = set[key];
|
|
6572
|
-
const value =
|
|
7701
|
+
const value = isExpression(val) ? val.toSQL(ctx, quotedAs) : addValue(ctx.values, val);
|
|
6573
7702
|
arr.push(`"${shape[key]?.data.name || key}" = ${value}`);
|
|
6574
7703
|
}
|
|
6575
7704
|
ctx.sql.push("DO UPDATE SET", arr.join(", "));
|
|
@@ -6578,32 +7707,17 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6578
7707
|
}
|
|
6579
7708
|
}
|
|
6580
7709
|
pushWhereStatementSql(ctx, q, query, quotedAs);
|
|
6581
|
-
let returning;
|
|
6582
7710
|
let delayedRelationSelect;
|
|
6583
|
-
if (inCTE) {
|
|
6584
|
-
|
|
6585
|
-
returning = {
|
|
6586
|
-
select: inCTE.selectNum || !select ? select ? "1, " + select : "1" : select,
|
|
6587
|
-
hookSelect: inCTE.returning?.hookSelect
|
|
6588
|
-
};
|
|
6589
|
-
} else {
|
|
6590
|
-
delayedRelationSelect = q.q.selectRelation ? orchidCore.newDelayedRelationSelect(q) : void 0;
|
|
6591
|
-
returning = makeReturningSql(
|
|
6592
|
-
ctx,
|
|
6593
|
-
q,
|
|
6594
|
-
query,
|
|
6595
|
-
quotedAs,
|
|
6596
|
-
delayedRelationSelect,
|
|
6597
|
-
2
|
|
6598
|
-
);
|
|
7711
|
+
if (!inCTE) {
|
|
7712
|
+
delayedRelationSelect = q.q.selectRelation ? newDelayedRelationSelect(q) : void 0;
|
|
6599
7713
|
}
|
|
6600
|
-
|
|
7714
|
+
const returningPos = ctx.sql.length;
|
|
6601
7715
|
let insertManyFromValuesAs;
|
|
6602
7716
|
if (insertFrom) {
|
|
6603
7717
|
if (values.length < 2) {
|
|
6604
7718
|
const q2 = insertFrom.clone();
|
|
6605
7719
|
if (values[0]?.length) {
|
|
6606
|
-
|
|
7720
|
+
pushQueryValueImmutable(
|
|
6607
7721
|
q2,
|
|
6608
7722
|
"select",
|
|
6609
7723
|
new RawSQL(
|
|
@@ -6666,7 +7780,18 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6666
7780
|
addWithSqls(ctx, hasWith, withSqls, valuesPos, insertSql);
|
|
6667
7781
|
ctx.sql[valuesPos] = startingKeyword + valuesSql.join(", ") + valuesAppend;
|
|
6668
7782
|
ctxValues.length = currentValuesLen;
|
|
6669
|
-
|
|
7783
|
+
const returning2 = makeInsertReturning(
|
|
7784
|
+
ctx,
|
|
7785
|
+
q,
|
|
7786
|
+
query,
|
|
7787
|
+
quotedAs,
|
|
7788
|
+
delayedRelationSelect,
|
|
7789
|
+
isSubSql
|
|
7790
|
+
);
|
|
7791
|
+
if (returning2.select) {
|
|
7792
|
+
ctx.sql[returningPos] = "RETURNING " + returning2.select;
|
|
7793
|
+
}
|
|
7794
|
+
batch = pushOrNewArray(batch, {
|
|
6670
7795
|
text: ctx.sql.join(" "),
|
|
6671
7796
|
values: ctxValues
|
|
6672
7797
|
});
|
|
@@ -6683,7 +7808,7 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6683
7808
|
addWithSqls(ctx, hasWith, withSqls, valuesPos, insertSql);
|
|
6684
7809
|
if (batch) {
|
|
6685
7810
|
if (hasNonSelect) {
|
|
6686
|
-
throw new
|
|
7811
|
+
throw new OrchidOrmInternalError(
|
|
6687
7812
|
q,
|
|
6688
7813
|
`Cannot insert many records when having a non-select sub-query`
|
|
6689
7814
|
);
|
|
@@ -6693,8 +7818,19 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6693
7818
|
text: ctx.sql.join(" "),
|
|
6694
7819
|
values: ctxValues
|
|
6695
7820
|
});
|
|
7821
|
+
const returning2 = makeInsertReturning(
|
|
7822
|
+
ctx,
|
|
7823
|
+
q,
|
|
7824
|
+
query,
|
|
7825
|
+
quotedAs,
|
|
7826
|
+
delayedRelationSelect,
|
|
7827
|
+
isSubSql
|
|
7828
|
+
);
|
|
7829
|
+
if (returning2.select) {
|
|
7830
|
+
ctx.sql[returningPos] = "RETURNING " + returning2.select;
|
|
7831
|
+
}
|
|
6696
7832
|
return {
|
|
6697
|
-
|
|
7833
|
+
tableHook: returning2.tableHook,
|
|
6698
7834
|
delayedRelationSelect,
|
|
6699
7835
|
batch
|
|
6700
7836
|
};
|
|
@@ -6705,13 +7841,47 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6705
7841
|
ctx.sql[valuesPos] += ' WHERE NOT EXISTS (SELECT 1 FROM "f")';
|
|
6706
7842
|
}
|
|
6707
7843
|
}
|
|
7844
|
+
const returning = makeInsertReturning(
|
|
7845
|
+
ctx,
|
|
7846
|
+
q,
|
|
7847
|
+
query,
|
|
7848
|
+
quotedAs,
|
|
7849
|
+
delayedRelationSelect,
|
|
7850
|
+
isSubSql
|
|
7851
|
+
);
|
|
7852
|
+
if (returning.select) {
|
|
7853
|
+
ctx.sql[returningPos] = "RETURNING " + returning.select;
|
|
7854
|
+
}
|
|
6708
7855
|
return {
|
|
6709
|
-
|
|
7856
|
+
tableHook: returning.tableHook,
|
|
6710
7857
|
delayedRelationSelect,
|
|
6711
7858
|
text: ctx.sql.join(" "),
|
|
6712
7859
|
values: ctx.values
|
|
6713
7860
|
};
|
|
6714
7861
|
};
|
|
7862
|
+
const makeInsertReturning = (ctx, q, query, quotedAs, delayedRelationSelect, isSubSql) => {
|
|
7863
|
+
const { inCTE } = query;
|
|
7864
|
+
if (inCTE) {
|
|
7865
|
+
const select = inCTE.returning?.select;
|
|
7866
|
+
return {
|
|
7867
|
+
select: inCTE.selectNum || !select ? select ? "1, " + select : "1" : select,
|
|
7868
|
+
tableHook: inCTE.returning?.hookSelect && {
|
|
7869
|
+
select: inCTE.returning?.hookSelect
|
|
7870
|
+
}
|
|
7871
|
+
};
|
|
7872
|
+
} else {
|
|
7873
|
+
return makeReturningSql(
|
|
7874
|
+
ctx,
|
|
7875
|
+
q,
|
|
7876
|
+
query,
|
|
7877
|
+
quotedAs,
|
|
7878
|
+
delayedRelationSelect,
|
|
7879
|
+
"Create",
|
|
7880
|
+
void 0,
|
|
7881
|
+
isSubSql
|
|
7882
|
+
);
|
|
7883
|
+
}
|
|
7884
|
+
};
|
|
6715
7885
|
const addWithSqls = (ctx, hasWith, withSqls, valuesPos, insertSql) => {
|
|
6716
7886
|
if (withSqls.length) {
|
|
6717
7887
|
if (hasWith) {
|
|
@@ -6877,7 +8047,7 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs, h
|
|
|
6877
8047
|
);
|
|
6878
8048
|
if (runtimeDefaults) {
|
|
6879
8049
|
for (const fn of runtimeDefaults) {
|
|
6880
|
-
arr.push(
|
|
8050
|
+
arr.push(addValue(values, fn()));
|
|
6881
8051
|
}
|
|
6882
8052
|
}
|
|
6883
8053
|
if (hookSetSql) arr.push(hookSetSql);
|
|
@@ -6885,36 +8055,35 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs, h
|
|
|
6885
8055
|
};
|
|
6886
8056
|
const encodeValue = (ctx, values, q, QueryClass, value, quotedAs) => {
|
|
6887
8057
|
if (value && typeof value === "object") {
|
|
6888
|
-
if (value instanceof
|
|
8058
|
+
if (value instanceof Expression) {
|
|
6889
8059
|
return value.toSQL(ctx, quotedAs);
|
|
6890
8060
|
} else if (value instanceof QueryClass) {
|
|
6891
|
-
return `(${
|
|
8061
|
+
return `(${toSubSqlText(
|
|
8062
|
+
joinSubQuery(q, value),
|
|
8063
|
+
value.q.as,
|
|
8064
|
+
ctx
|
|
8065
|
+
)})`;
|
|
6892
8066
|
} else if ("fromHook" in value) {
|
|
6893
8067
|
return value.fromHook;
|
|
6894
8068
|
}
|
|
6895
8069
|
}
|
|
6896
|
-
return value === void 0 ? "DEFAULT" :
|
|
8070
|
+
return value === void 0 ? "DEFAULT" : addValue(values, value);
|
|
6897
8071
|
};
|
|
6898
|
-
const
|
|
6899
|
-
null,
|
|
6900
|
-
"afterUpdateSelect",
|
|
6901
|
-
"afterCreateSelect",
|
|
6902
|
-
"afterDeleteSelect"
|
|
6903
|
-
];
|
|
6904
|
-
const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSelectI, addHookSelectI) => {
|
|
8072
|
+
const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookPurpose, addHookPurpose, isSubSql) => {
|
|
6905
8073
|
if (data.inCTE) {
|
|
6906
|
-
if (
|
|
8074
|
+
if (hookPurpose !== "Create") {
|
|
6907
8075
|
const returning = makeReturningSql(
|
|
6908
8076
|
ctx,
|
|
6909
8077
|
q,
|
|
6910
8078
|
data,
|
|
6911
8079
|
quotedAs,
|
|
6912
8080
|
delayedRelationSelect,
|
|
6913
|
-
|
|
6914
|
-
|
|
8081
|
+
"Create",
|
|
8082
|
+
hookPurpose,
|
|
8083
|
+
isSubSql
|
|
6915
8084
|
);
|
|
6916
|
-
if (returning.
|
|
6917
|
-
for (const [key, value] of returning.
|
|
8085
|
+
if (returning.tableHook?.select) {
|
|
8086
|
+
for (const [key, value] of returning.tableHook.select) {
|
|
6918
8087
|
data.inCTE.targetHookSelect.set(key, value);
|
|
6919
8088
|
}
|
|
6920
8089
|
}
|
|
@@ -6924,12 +8093,18 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6924
8093
|
return data.inCTE.returning;
|
|
6925
8094
|
}
|
|
6926
8095
|
}
|
|
6927
|
-
const hookSelect =
|
|
8096
|
+
const hookSelect = hookPurpose && data[`after${hookPurpose}Select`];
|
|
6928
8097
|
const { select } = data;
|
|
6929
|
-
if (!q.q.hookSelect && !hookSelect?.size && !select?.length && !
|
|
6930
|
-
|
|
8098
|
+
if (!q.q.hookSelect && !hookSelect?.size && !select?.length && !hookPurpose) {
|
|
8099
|
+
const select2 = hookSelect && /* @__PURE__ */ new Map();
|
|
8100
|
+
return {
|
|
8101
|
+
select: !isSubSql && ctx.cteHooks?.hasSelect ? "NULL" : void 0,
|
|
8102
|
+
tableHook: select2 && {
|
|
8103
|
+
select: select2
|
|
8104
|
+
}
|
|
8105
|
+
};
|
|
6931
8106
|
}
|
|
6932
|
-
const otherCTEHookSelect =
|
|
8107
|
+
const otherCTEHookSelect = addHookPurpose && data[`after${addHookPurpose}Select`];
|
|
6933
8108
|
let tempSelect;
|
|
6934
8109
|
if (q.q.hookSelect || hookSelect || otherCTEHookSelect || q.q.selectRelation) {
|
|
6935
8110
|
tempSelect = new Map(q.q.hookSelect);
|
|
@@ -6944,7 +8119,7 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6944
8119
|
}
|
|
6945
8120
|
}
|
|
6946
8121
|
if (q.q.selectRelation) {
|
|
6947
|
-
for (const column of
|
|
8122
|
+
for (const column of getPrimaryKeys(q)) {
|
|
6948
8123
|
tempSelect.set(column, { select: column });
|
|
6949
8124
|
}
|
|
6950
8125
|
}
|
|
@@ -6958,18 +8133,25 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6958
8133
|
quotedAs,
|
|
6959
8134
|
tempSelect,
|
|
6960
8135
|
void 0,
|
|
8136
|
+
void 0,
|
|
6961
8137
|
true,
|
|
6962
8138
|
void 0,
|
|
6963
8139
|
delayedRelationSelect
|
|
6964
8140
|
);
|
|
6965
8141
|
}
|
|
8142
|
+
const after = hookPurpose && data[`after${hookPurpose}`];
|
|
8143
|
+
const afterCommit = hookPurpose && data[`after${hookPurpose}Commit`];
|
|
6966
8144
|
return {
|
|
6967
|
-
select: sql,
|
|
6968
|
-
|
|
8145
|
+
select: !isSubSql && ctx.cteHooks?.hasSelect ? sql ? "NULL, " + sql : "NULL" : sql,
|
|
8146
|
+
tableHook: (tempSelect || after || afterCommit) && {
|
|
8147
|
+
select: tempSelect,
|
|
8148
|
+
after: data.after && after ? [...data.after, ...after] : after ? after : data.after,
|
|
8149
|
+
afterCommit
|
|
8150
|
+
}
|
|
6969
8151
|
};
|
|
6970
8152
|
};
|
|
6971
8153
|
|
|
6972
|
-
const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
|
|
8154
|
+
const pushSelectSql = (ctx, table, query, quotedAs, isSubSql, aliases) => {
|
|
6973
8155
|
if (query.selectCache) {
|
|
6974
8156
|
ctx.sql.push(query.selectCache.sql);
|
|
6975
8157
|
if (aliases) aliases.push(...query.selectCache.aliases);
|
|
@@ -6980,12 +8162,13 @@ const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
|
|
|
6980
8162
|
query,
|
|
6981
8163
|
quotedAs,
|
|
6982
8164
|
query.hookSelect,
|
|
8165
|
+
isSubSql,
|
|
6983
8166
|
aliases
|
|
6984
8167
|
);
|
|
6985
8168
|
if (sql) ctx.sql.push(sql);
|
|
6986
8169
|
}
|
|
6987
8170
|
};
|
|
6988
|
-
const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, aliases, skipCTE, jsonList, delayedRelationSelect) => {
|
|
8171
|
+
const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, isSubSql, aliases, skipCTE, jsonList, delayedRelationSelect) => {
|
|
6989
8172
|
if (query.inCTE && !skipCTE) {
|
|
6990
8173
|
const { select } = makeReturningSql(
|
|
6991
8174
|
ctx,
|
|
@@ -7060,14 +8243,14 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7060
8243
|
}
|
|
7061
8244
|
const value = obj[as];
|
|
7062
8245
|
if (typeof value === "object") {
|
|
7063
|
-
if (
|
|
8246
|
+
if (isExpression(value)) {
|
|
7064
8247
|
list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
|
|
7065
8248
|
if (jsonList) {
|
|
7066
8249
|
jsonList[as] = value.result.value;
|
|
7067
8250
|
}
|
|
7068
8251
|
aliases?.push(as);
|
|
7069
|
-
} else if (delayedRelationSelect &&
|
|
7070
|
-
|
|
8252
|
+
} else if (delayedRelationSelect && isRelationQuery(value)) {
|
|
8253
|
+
setDelayedRelation(delayedRelationSelect, as, value);
|
|
7071
8254
|
} else {
|
|
7072
8255
|
pushSubQuerySql(ctx, query, value, as, list, quotedAs, aliases);
|
|
7073
8256
|
if (jsonList) {
|
|
@@ -7132,7 +8315,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7132
8315
|
hookSelect.delete(column);
|
|
7133
8316
|
continue;
|
|
7134
8317
|
}
|
|
7135
|
-
name =
|
|
8318
|
+
name = getFreeAlias(selected, column);
|
|
7136
8319
|
item.as = name;
|
|
7137
8320
|
item.temp = name;
|
|
7138
8321
|
sql += ` "${name}"`;
|
|
@@ -7150,6 +8333,10 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7150
8333
|
list.push(sql);
|
|
7151
8334
|
}
|
|
7152
8335
|
}
|
|
8336
|
+
if (!isSubSql && ctx.cteHooks?.hasSelect) {
|
|
8337
|
+
const count = ctx.selectedCount = list.length || query.selectAllColumns?.length || 0;
|
|
8338
|
+
return count ? (list.length ? list.join(", ") : selectAllSql(query, quotedAs, jsonList)) + ", NULL" : "";
|
|
8339
|
+
}
|
|
7153
8340
|
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs, jsonList);
|
|
7154
8341
|
};
|
|
7155
8342
|
const selectAllSql = (query, quotedAs, jsonList) => {
|
|
@@ -7181,7 +8368,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7181
8368
|
sql = `'[]'::json`;
|
|
7182
8369
|
break;
|
|
7183
8370
|
default:
|
|
7184
|
-
throw new
|
|
8371
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7185
8372
|
}
|
|
7186
8373
|
list.push(`${sql} "${as}"`);
|
|
7187
8374
|
aliases?.push(as);
|
|
@@ -7210,7 +8397,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7210
8397
|
case "void":
|
|
7211
8398
|
return;
|
|
7212
8399
|
default:
|
|
7213
|
-
throw new
|
|
8400
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7214
8401
|
}
|
|
7215
8402
|
if (sql) {
|
|
7216
8403
|
list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
|
|
@@ -7230,7 +8417,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7230
8417
|
if (!first && query.q.runtimeComputeds?.[as]) {
|
|
7231
8418
|
query = queryJson(query);
|
|
7232
8419
|
} else if (!first) {
|
|
7233
|
-
throw new
|
|
8420
|
+
throw new OrchidOrmInternalError(
|
|
7234
8421
|
query,
|
|
7235
8422
|
`Nothing was selected for pluck`
|
|
7236
8423
|
);
|
|
@@ -7252,7 +8439,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7252
8439
|
case "void":
|
|
7253
8440
|
break;
|
|
7254
8441
|
default:
|
|
7255
|
-
throw new
|
|
8442
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7256
8443
|
}
|
|
7257
8444
|
list.push(
|
|
7258
8445
|
`${coalesce(
|
|
@@ -7266,7 +8453,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7266
8453
|
const coalesce = (ctx, query, sql, quotedAs) => {
|
|
7267
8454
|
const { coalesceValue } = query.q;
|
|
7268
8455
|
if (coalesceValue !== void 0) {
|
|
7269
|
-
const value =
|
|
8456
|
+
const value = isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : addValue(ctx.values, coalesceValue);
|
|
7270
8457
|
return `COALESCE(${sql}, ${value})`;
|
|
7271
8458
|
}
|
|
7272
8459
|
return sql;
|
|
@@ -7281,7 +8468,7 @@ const orderByToSql = (ctx, data, order, quotedAs) => {
|
|
|
7281
8468
|
if (typeof order === "string") {
|
|
7282
8469
|
return addOrder(ctx, data, order, quotedAs);
|
|
7283
8470
|
}
|
|
7284
|
-
if (
|
|
8471
|
+
if (isExpression(order)) {
|
|
7285
8472
|
return order.toSQL(ctx, quotedAs);
|
|
7286
8473
|
}
|
|
7287
8474
|
const sql = [];
|
|
@@ -7294,15 +8481,15 @@ const orderByToSql = (ctx, data, order, quotedAs) => {
|
|
|
7294
8481
|
const addOrder = (ctx, data, column, quotedAs, dir) => {
|
|
7295
8482
|
if (data.sources?.[column]) {
|
|
7296
8483
|
const search = data.sources[column];
|
|
7297
|
-
const order = dir || (!search.order || search.order === true ?
|
|
7298
|
-
return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${
|
|
8484
|
+
const order = dir || (!search.order || search.order === true ? emptyObject : search.order);
|
|
8485
|
+
return `${order.coverDensity ? "ts_rank_cd" : "ts_rank"}(${order.weights ? `${addValue(ctx.values, `{${order.weights}}`)}, ` : ""}${search.vectorSQL}, "${column}"${order.normalization !== void 0 ? `, ${addValue(ctx.values, order.normalization)}` : ""}) ${order.dir || "DESC"}`;
|
|
7299
8486
|
}
|
|
7300
8487
|
return `${maybeSelectedColumnToSql(ctx, data, column, quotedAs)} ${dir || "ASC"}`;
|
|
7301
8488
|
};
|
|
7302
8489
|
|
|
7303
8490
|
const windowToSql = (ctx, data, window, quotedAs) => {
|
|
7304
8491
|
if (typeof window === "string") return `"${window}"`;
|
|
7305
|
-
if (
|
|
8492
|
+
if (isExpression(window)) return `(${window.toSQL(ctx, quotedAs)})`;
|
|
7306
8493
|
const sql = [];
|
|
7307
8494
|
if (window.partitionBy) {
|
|
7308
8495
|
sql.push(
|
|
@@ -7412,7 +8599,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
7412
8599
|
let only;
|
|
7413
8600
|
let sql;
|
|
7414
8601
|
if (typeof from === "object") {
|
|
7415
|
-
if (
|
|
8602
|
+
if (isExpression(from)) {
|
|
7416
8603
|
sql = from.toSQL(ctx, quotedAs) + " " + quotedAs;
|
|
7417
8604
|
} else {
|
|
7418
8605
|
only = from.q.only;
|
|
@@ -7439,7 +8626,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
7439
8626
|
return (only === void 0 ? data.only : only) ? `ONLY ${sql}` : sql;
|
|
7440
8627
|
};
|
|
7441
8628
|
const getSearchLang = (ctx, data, source, quotedAs) => {
|
|
7442
|
-
return source.langSQL ?? (source.langSQL = "languageColumn" in source ? columnToSql(ctx, data, data.shape, source.languageColumn, quotedAs) :
|
|
8629
|
+
return source.langSQL ?? (source.langSQL = "languageColumn" in source ? columnToSql(ctx, data, data.shape, source.languageColumn, quotedAs) : isRawSQL(source.language) ? source.language.toSQL(ctx) : addValue(ctx.values, source.language || data.language || "english"));
|
|
7443
8630
|
};
|
|
7444
8631
|
const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
7445
8632
|
let sql = source.textSQL;
|
|
@@ -7464,7 +8651,7 @@ const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
|
7464
8651
|
sql = columnToSql(ctx, data, data.shape, source.vector, quotedAs);
|
|
7465
8652
|
} else {
|
|
7466
8653
|
if (typeof source.text === "string") {
|
|
7467
|
-
sql =
|
|
8654
|
+
sql = addValue(ctx.values, source.text);
|
|
7468
8655
|
} else {
|
|
7469
8656
|
sql = source.text.toSQL(ctx, quotedAs);
|
|
7470
8657
|
}
|
|
@@ -7480,7 +8667,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
7480
8667
|
let tsVector = "";
|
|
7481
8668
|
let i = 0;
|
|
7482
8669
|
for (const key in source.in) {
|
|
7483
|
-
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${
|
|
8670
|
+
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${addValue(
|
|
7484
8671
|
ctx.values,
|
|
7485
8672
|
source.in[key]
|
|
7486
8673
|
)})`;
|
|
@@ -7494,7 +8681,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
7494
8681
|
}
|
|
7495
8682
|
};
|
|
7496
8683
|
|
|
7497
|
-
const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
8684
|
+
const pushUpdateSql = (ctx, table, query, quotedAs, isSubSql) => {
|
|
7498
8685
|
const quotedTable = quoteSchemaAndTable(
|
|
7499
8686
|
query.schema,
|
|
7500
8687
|
table.table || query.from
|
|
@@ -7506,26 +8693,27 @@ const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
|
7506
8693
|
Object.assign(hookSet, item);
|
|
7507
8694
|
}
|
|
7508
8695
|
} else {
|
|
7509
|
-
hookSet =
|
|
8696
|
+
hookSet = emptyObject;
|
|
7510
8697
|
}
|
|
7511
8698
|
const set = [];
|
|
7512
8699
|
processData(ctx, table, set, query.updateData, hookSet, quotedAs);
|
|
7513
8700
|
if (query.hookUpdateSet) {
|
|
7514
|
-
applySet(ctx, table, set, hookSet,
|
|
8701
|
+
applySet(ctx, table, set, hookSet, emptyObject, quotedAs);
|
|
7515
8702
|
}
|
|
7516
|
-
let
|
|
7517
|
-
const delayedRelationSelect = query.selectRelation ?
|
|
8703
|
+
let tableHook;
|
|
8704
|
+
const delayedRelationSelect = query.selectRelation ? newDelayedRelationSelect(table) : void 0;
|
|
7518
8705
|
if (!set.length) {
|
|
7519
8706
|
if (!query.select) {
|
|
7520
8707
|
query.select = countSelect;
|
|
7521
8708
|
}
|
|
7522
|
-
|
|
8709
|
+
tableHook = pushUpdateReturning(
|
|
7523
8710
|
ctx,
|
|
7524
8711
|
table,
|
|
7525
8712
|
query,
|
|
7526
8713
|
quotedAs,
|
|
7527
8714
|
"SELECT",
|
|
7528
|
-
delayedRelationSelect
|
|
8715
|
+
delayedRelationSelect,
|
|
8716
|
+
isSubSql
|
|
7529
8717
|
);
|
|
7530
8718
|
ctx.sql.push(`FROM ${quotedTable}`);
|
|
7531
8719
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
@@ -7576,43 +8764,45 @@ const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
|
7576
8764
|
if (whereSql) {
|
|
7577
8765
|
ctx.sql.push("WHERE", whereSql);
|
|
7578
8766
|
}
|
|
7579
|
-
|
|
8767
|
+
tableHook = pushUpdateReturning(
|
|
7580
8768
|
ctx,
|
|
7581
8769
|
table,
|
|
7582
8770
|
query,
|
|
7583
8771
|
quotedAs,
|
|
7584
8772
|
"RETURNING",
|
|
7585
|
-
delayedRelationSelect
|
|
8773
|
+
delayedRelationSelect,
|
|
8774
|
+
isSubSql
|
|
7586
8775
|
);
|
|
7587
8776
|
}
|
|
7588
8777
|
return {
|
|
7589
|
-
|
|
8778
|
+
tableHook,
|
|
7590
8779
|
delayedRelationSelect,
|
|
7591
8780
|
text: ctx.sql.join(" "),
|
|
7592
8781
|
values: ctx.values
|
|
7593
8782
|
};
|
|
7594
8783
|
};
|
|
7595
|
-
const pushUpdateReturning = (ctx, table, query, quotedAs, keyword, delayedRelationSelect) => {
|
|
8784
|
+
const pushUpdateReturning = (ctx, table, query, quotedAs, keyword, delayedRelationSelect, isSubSql) => {
|
|
7596
8785
|
const { inCTE } = query;
|
|
7597
|
-
const { select,
|
|
8786
|
+
const { select, tableHook } = makeReturningSql(
|
|
7598
8787
|
ctx,
|
|
7599
8788
|
table,
|
|
7600
8789
|
query,
|
|
7601
8790
|
quotedAs,
|
|
7602
8791
|
delayedRelationSelect,
|
|
7603
|
-
|
|
7604
|
-
inCTE &&
|
|
8792
|
+
"Update",
|
|
8793
|
+
inCTE && "Create",
|
|
8794
|
+
isSubSql
|
|
7605
8795
|
);
|
|
7606
8796
|
const s = inCTE && (inCTE.selectNum || !select) ? select ? "0, " + select : "0" : select;
|
|
7607
8797
|
if (s) ctx.sql.push(keyword, s);
|
|
7608
|
-
return
|
|
8798
|
+
return tableHook;
|
|
7609
8799
|
};
|
|
7610
8800
|
const processData = (ctx, table, set, data, hookSet, quotedAs) => {
|
|
7611
8801
|
let append;
|
|
7612
8802
|
for (const item of data) {
|
|
7613
8803
|
if (typeof item === "function") {
|
|
7614
8804
|
const result = item(data);
|
|
7615
|
-
if (result) append =
|
|
8805
|
+
if (result) append = pushOrNewArray(append, result);
|
|
7616
8806
|
} else {
|
|
7617
8807
|
applySet(ctx, table, set, item, hookSet, quotedAs);
|
|
7618
8808
|
}
|
|
@@ -7639,7 +8829,7 @@ const applySet = (ctx, table, set, item, hookSet, quotedAs) => {
|
|
|
7639
8829
|
};
|
|
7640
8830
|
const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
7641
8831
|
if (value && typeof value === "object") {
|
|
7642
|
-
if (
|
|
8832
|
+
if (isExpression(value)) {
|
|
7643
8833
|
return value.toSQL(ctx, quotedAs);
|
|
7644
8834
|
} else if (value instanceof QueryClass) {
|
|
7645
8835
|
if (value.q.subQuery === 1) {
|
|
@@ -7649,13 +8839,13 @@ const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
|
7649
8839
|
joinSubQuery(table, value).toSQL(ctx)
|
|
7650
8840
|
)})`;
|
|
7651
8841
|
} else if ("op" in value && "arg" in value) {
|
|
7652
|
-
return `"${table.q.shape[key].data.name || key}" ${value.op} ${
|
|
8842
|
+
return `"${table.q.shape[key].data.name || key}" ${value.op} ${addValue(ctx.values, value.arg)}`;
|
|
7653
8843
|
}
|
|
7654
8844
|
}
|
|
7655
|
-
return
|
|
8845
|
+
return addValue(ctx.values, value);
|
|
7656
8846
|
};
|
|
7657
8847
|
|
|
7658
|
-
const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
8848
|
+
const pushDeleteSql = (ctx, table, query, quotedAs, isSubSql) => {
|
|
7659
8849
|
const from = `"${table.table || query.from}"`;
|
|
7660
8850
|
ctx.sql.push(`DELETE FROM ${from}`);
|
|
7661
8851
|
if (from !== quotedAs) {
|
|
@@ -7669,10 +8859,10 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
7669
8859
|
for (const item of query.join) {
|
|
7670
8860
|
const lateral = "l" in item.args && item.args.l;
|
|
7671
8861
|
if (lateral) {
|
|
7672
|
-
if (
|
|
8862
|
+
if (isRelationQuery(lateral)) {
|
|
7673
8863
|
continue;
|
|
7674
8864
|
}
|
|
7675
|
-
throw new
|
|
8865
|
+
throw new OrchidOrmInternalError(
|
|
7676
8866
|
table,
|
|
7677
8867
|
"Join lateral is not supported in delete"
|
|
7678
8868
|
);
|
|
@@ -7699,18 +8889,20 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
7699
8889
|
ctx.sql.push("WHERE", conditions);
|
|
7700
8890
|
}
|
|
7701
8891
|
}
|
|
7702
|
-
const delayedRelationSelect = query.selectRelation ?
|
|
8892
|
+
const delayedRelationSelect = query.selectRelation ? newDelayedRelationSelect(table) : void 0;
|
|
7703
8893
|
const returning = makeReturningSql(
|
|
7704
8894
|
ctx,
|
|
7705
8895
|
table,
|
|
7706
8896
|
query,
|
|
7707
8897
|
quotedAs,
|
|
7708
8898
|
delayedRelationSelect,
|
|
7709
|
-
|
|
8899
|
+
"Delete",
|
|
8900
|
+
void 0,
|
|
8901
|
+
isSubSql
|
|
7710
8902
|
);
|
|
7711
8903
|
if (returning.select) ctx.sql.push("RETURNING", returning.select);
|
|
7712
8904
|
return {
|
|
7713
|
-
|
|
8905
|
+
tableHook: returning.tableHook,
|
|
7714
8906
|
delayedRelationSelect,
|
|
7715
8907
|
text: ctx.sql.join(" "),
|
|
7716
8908
|
values: ctx.values
|
|
@@ -7725,14 +8917,14 @@ const pushTruncateSql = (ctx, table, query) => {
|
|
|
7725
8917
|
|
|
7726
8918
|
const pushColumnInfoSql = (ctx, table, query) => {
|
|
7727
8919
|
ctx.sql.push(
|
|
7728
|
-
`SELECT * FROM information_schema.columns WHERE table_name = ${
|
|
8920
|
+
`SELECT * FROM information_schema.columns WHERE table_name = ${addValue(
|
|
7729
8921
|
ctx.values,
|
|
7730
8922
|
table.table
|
|
7731
8923
|
)} AND table_catalog = current_database() AND table_schema = ${query.schema || "current_schema()"}`
|
|
7732
8924
|
);
|
|
7733
8925
|
if (query.column) {
|
|
7734
8926
|
ctx.sql.push(
|
|
7735
|
-
`AND column_name = ${
|
|
8927
|
+
`AND column_name = ${addValue(
|
|
7736
8928
|
ctx.values,
|
|
7737
8929
|
table.q.shape[query.column]?.data.name || query.column
|
|
7738
8930
|
)}`
|
|
@@ -7776,7 +8968,34 @@ const pushCopySql = (ctx, table, query, quotedAs) => {
|
|
|
7776
8968
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
7777
8969
|
};
|
|
7778
8970
|
|
|
7779
|
-
const
|
|
8971
|
+
const toSubSqlText = (q, cteName, options) => getSqlText(subToSql(q, cteName, options));
|
|
8972
|
+
const subToSql = (q, cteName, options) => {
|
|
8973
|
+
var _a;
|
|
8974
|
+
const sql = toSQL(q, options, true);
|
|
8975
|
+
if (sql.tableHook && (sql.tableHook.after || sql.tableHook.afterCommit) && !q.q.inCTE) {
|
|
8976
|
+
const shape = {};
|
|
8977
|
+
if (sql.tableHook.select) {
|
|
8978
|
+
for (const key of sql.tableHook.select.keys()) {
|
|
8979
|
+
shape[key] = q.shape[key];
|
|
8980
|
+
}
|
|
8981
|
+
}
|
|
8982
|
+
const item = {
|
|
8983
|
+
shape,
|
|
8984
|
+
tableHook: sql.tableHook
|
|
8985
|
+
};
|
|
8986
|
+
if (options.cteHooks) {
|
|
8987
|
+
if (sql.tableHook.select) options.cteHooks.hasSelect = true;
|
|
8988
|
+
(_a = options.cteHooks.tableHooks)[cteName] ?? (_a[cteName] = item);
|
|
8989
|
+
} else {
|
|
8990
|
+
options.cteHooks = {
|
|
8991
|
+
hasSelect: !!sql.tableHook.select,
|
|
8992
|
+
tableHooks: { [cteName]: item }
|
|
8993
|
+
};
|
|
8994
|
+
}
|
|
8995
|
+
}
|
|
8996
|
+
return sql;
|
|
8997
|
+
};
|
|
8998
|
+
const toSQL = (table, options, isSubSql) => {
|
|
7780
8999
|
const query = table.q;
|
|
7781
9000
|
const sql = [];
|
|
7782
9001
|
const values = options?.values || [];
|
|
@@ -7787,7 +9006,8 @@ const toSQL = (table, options) => {
|
|
|
7787
9006
|
values,
|
|
7788
9007
|
aliasValue: options?.aliasValue,
|
|
7789
9008
|
skipBatchCheck: options?.skipBatchCheck,
|
|
7790
|
-
hasNonSelect: options?.hasNonSelect
|
|
9009
|
+
hasNonSelect: options?.hasNonSelect,
|
|
9010
|
+
cteHooks: options?.cteHooks
|
|
7791
9011
|
};
|
|
7792
9012
|
if (query.with) {
|
|
7793
9013
|
pushWithSql(ctx, query.with);
|
|
@@ -7806,11 +9026,11 @@ const toSQL = (table, options) => {
|
|
|
7806
9026
|
} else {
|
|
7807
9027
|
const quotedAs = `"${query.as || tableName}"`;
|
|
7808
9028
|
if (query.type === "insert") {
|
|
7809
|
-
result = makeInsertSql(ctx, table, query, `"${tableName}"
|
|
9029
|
+
result = makeInsertSql(ctx, table, query, `"${tableName}"`, isSubSql);
|
|
7810
9030
|
} else if (query.type === "update") {
|
|
7811
|
-
result = pushUpdateSql(ctx, table, query, quotedAs);
|
|
9031
|
+
result = pushUpdateSql(ctx, table, query, quotedAs, isSubSql);
|
|
7812
9032
|
} else if (query.type === "delete") {
|
|
7813
|
-
result = pushDeleteSql(ctx, table, query, quotedAs);
|
|
9033
|
+
result = pushDeleteSql(ctx, table, query, quotedAs, isSubSql);
|
|
7814
9034
|
} else if (query.type === "copy") {
|
|
7815
9035
|
pushCopySql(ctx, table, query, quotedAs);
|
|
7816
9036
|
result = { text: sql.join(" "), values };
|
|
@@ -7826,7 +9046,7 @@ const toSQL = (table, options) => {
|
|
|
7826
9046
|
const s = getSqlText(firstSql);
|
|
7827
9047
|
sql.push(query.union.p ? s : `(${s})`);
|
|
7828
9048
|
for (const u of query.union.u) {
|
|
7829
|
-
const s2 =
|
|
9049
|
+
const s2 = isExpression(u.a) ? u.a.toSQL(ctx, quotedAs) : getSqlText(toSQL(u.a, ctx));
|
|
7830
9050
|
sql.push(`${u.k} ${u.p ? s2 : "(" + s2 + ")"}`);
|
|
7831
9051
|
}
|
|
7832
9052
|
} else {
|
|
@@ -7835,7 +9055,7 @@ const toSQL = (table, options) => {
|
|
|
7835
9055
|
pushDistinctSql(ctx, table, query.distinct, quotedAs);
|
|
7836
9056
|
}
|
|
7837
9057
|
const aliases = query.group ? [] : void 0;
|
|
7838
|
-
pushSelectSql(ctx, table, query, quotedAs, aliases);
|
|
9058
|
+
pushSelectSql(ctx, table, query, quotedAs, isSubSql, aliases);
|
|
7839
9059
|
fromQuery = (table.table || query.from) && pushFromAndAs(ctx, table, query, quotedAs) || void 0;
|
|
7840
9060
|
if (query.join) {
|
|
7841
9061
|
pushJoinSql(
|
|
@@ -7850,7 +9070,7 @@ const toSQL = (table, options) => {
|
|
|
7850
9070
|
}
|
|
7851
9071
|
if (query.group) {
|
|
7852
9072
|
const group = query.group.map((item) => {
|
|
7853
|
-
if (
|
|
9073
|
+
if (isExpression(item)) {
|
|
7854
9074
|
return item.toSQL(ctx, quotedAs);
|
|
7855
9075
|
} else {
|
|
7856
9076
|
const i = aliases.indexOf(item);
|
|
@@ -7884,15 +9104,15 @@ const toSQL = (table, options) => {
|
|
|
7884
9104
|
if (query.useFromLimitOffset) {
|
|
7885
9105
|
const q = fromQuery?.q;
|
|
7886
9106
|
if (q.limit) {
|
|
7887
|
-
sql.push(`LIMIT ${
|
|
9107
|
+
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
7888
9108
|
}
|
|
7889
9109
|
if (q.offset) {
|
|
7890
|
-
sql.push(`OFFSET ${
|
|
9110
|
+
sql.push(`OFFSET ${addValue(values, q.offset)}`);
|
|
7891
9111
|
}
|
|
7892
9112
|
} else {
|
|
7893
9113
|
pushLimitSQL(sql, values, query);
|
|
7894
9114
|
if (query.offset && !query.returnsOne) {
|
|
7895
|
-
sql.push(`OFFSET ${
|
|
9115
|
+
sql.push(`OFFSET ${addValue(values, query.offset)}`);
|
|
7896
9116
|
}
|
|
7897
9117
|
}
|
|
7898
9118
|
if (query.for) {
|
|
@@ -7901,7 +9121,7 @@ const toSQL = (table, options) => {
|
|
|
7901
9121
|
if (tableNames) {
|
|
7902
9122
|
sql.push(
|
|
7903
9123
|
"OF",
|
|
7904
|
-
|
|
9124
|
+
isExpression(tableNames) ? tableNames.toSQL(ctx, quotedAs) : tableNames.map((x) => `"${x}"`).join(", ")
|
|
7905
9125
|
);
|
|
7906
9126
|
}
|
|
7907
9127
|
if (query.for.mode) sql.push(query.for.mode);
|
|
@@ -7909,13 +9129,29 @@ const toSQL = (table, options) => {
|
|
|
7909
9129
|
result = {
|
|
7910
9130
|
text: sql.join(" "),
|
|
7911
9131
|
values,
|
|
7912
|
-
|
|
9132
|
+
tableHook: query.hookSelect && {
|
|
9133
|
+
select: query.hookSelect
|
|
9134
|
+
},
|
|
7913
9135
|
delayedRelationSelect: ctx.delayedRelationSelect
|
|
7914
9136
|
};
|
|
7915
9137
|
}
|
|
7916
9138
|
if (options && (query.type || ctx.hasNonSelect)) {
|
|
7917
9139
|
options.hasNonSelect = true;
|
|
7918
9140
|
}
|
|
9141
|
+
if (!isSubSql && ctx.cteHooks && "text" in result) {
|
|
9142
|
+
result.cteHooks = ctx.cteHooks;
|
|
9143
|
+
if (ctx.cteHooks.hasSelect) {
|
|
9144
|
+
result.text += ` UNION ALL SELECT ${"NULL, ".repeat(
|
|
9145
|
+
ctx.selectedCount || 0
|
|
9146
|
+
)}json_build_object(${Object.entries(ctx.cteHooks.tableHooks).map(
|
|
9147
|
+
([cteName, data]) => `'${cteName}', (SELECT json_agg(${makeRowToJson(
|
|
9148
|
+
cteName,
|
|
9149
|
+
data.shape,
|
|
9150
|
+
false
|
|
9151
|
+
)}) FROM "${cteName}")`
|
|
9152
|
+
).join(", ")})`;
|
|
9153
|
+
}
|
|
9154
|
+
}
|
|
7919
9155
|
return result;
|
|
7920
9156
|
};
|
|
7921
9157
|
function pushLimitSQL(sql, values, q) {
|
|
@@ -7923,12 +9159,12 @@ function pushLimitSQL(sql, values, q) {
|
|
|
7923
9159
|
if (queryTypeWithLimitOne[q.returnType] && !q.returning) {
|
|
7924
9160
|
sql.push(`LIMIT 1`);
|
|
7925
9161
|
} else if (q.limit) {
|
|
7926
|
-
sql.push(`LIMIT ${
|
|
9162
|
+
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
7927
9163
|
}
|
|
7928
9164
|
}
|
|
7929
9165
|
}
|
|
7930
9166
|
|
|
7931
|
-
class FnExpression extends
|
|
9167
|
+
class FnExpression extends Expression {
|
|
7932
9168
|
/**
|
|
7933
9169
|
* @param query - query object.
|
|
7934
9170
|
* @param fn - SQL function name.
|
|
@@ -7936,7 +9172,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7936
9172
|
* @param options - aggregate options.
|
|
7937
9173
|
* @param value - column type of the function result.
|
|
7938
9174
|
*/
|
|
7939
|
-
constructor(query, fn, args, options =
|
|
9175
|
+
constructor(query, fn, args, options = emptyObject, value) {
|
|
7940
9176
|
super();
|
|
7941
9177
|
this.query = query;
|
|
7942
9178
|
this.fn = fn;
|
|
@@ -7949,7 +9185,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7949
9185
|
query.q.returnsOne = true;
|
|
7950
9186
|
query.q.getColumn = value;
|
|
7951
9187
|
query.q.select = [this];
|
|
7952
|
-
addColumnParserToQuery(query.q,
|
|
9188
|
+
addColumnParserToQuery(query.q, getValueKey, value);
|
|
7953
9189
|
}
|
|
7954
9190
|
// Builds function SQL.
|
|
7955
9191
|
makeSQL(ctx, quotedAs) {
|
|
@@ -7961,7 +9197,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7961
9197
|
this.args.map((arg) => {
|
|
7962
9198
|
if (typeof arg === "string") {
|
|
7963
9199
|
return arg === "*" ? "*" : columnToSql(ctx, this.q, this.q.shape, arg, quotedAs);
|
|
7964
|
-
} else if (arg instanceof
|
|
9200
|
+
} else if (arg instanceof Expression) {
|
|
7965
9201
|
return arg.toSQL(ctx, quotedAs);
|
|
7966
9202
|
} else if ("pairs" in arg) {
|
|
7967
9203
|
const args = [];
|
|
@@ -7969,7 +9205,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7969
9205
|
for (const key in pairs) {
|
|
7970
9206
|
args.push(
|
|
7971
9207
|
// ::text is needed to bypass "could not determine data type of parameter" postgres error
|
|
7972
|
-
`${
|
|
9208
|
+
`${addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
7973
9209
|
ctx,
|
|
7974
9210
|
this.q,
|
|
7975
9211
|
pairs[key],
|
|
@@ -7979,7 +9215,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7979
9215
|
}
|
|
7980
9216
|
return args.join(", ");
|
|
7981
9217
|
} else {
|
|
7982
|
-
return
|
|
9218
|
+
return addValue(values, arg.value);
|
|
7983
9219
|
}
|
|
7984
9220
|
}).join(", ")
|
|
7985
9221
|
);
|
|
@@ -7990,7 +9226,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7990
9226
|
{ ...ctx, sql },
|
|
7991
9227
|
this.q,
|
|
7992
9228
|
quotedAs,
|
|
7993
|
-
|
|
9229
|
+
toArray(options.order)
|
|
7994
9230
|
);
|
|
7995
9231
|
}
|
|
7996
9232
|
sql.push(")");
|
|
@@ -8020,7 +9256,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
8020
9256
|
}
|
|
8021
9257
|
function makeFnExpression(self, type, fn, args, options) {
|
|
8022
9258
|
const q = extendQuery(self, type.operators);
|
|
8023
|
-
q.baseQuery.type =
|
|
9259
|
+
q.baseQuery.type = ExpressionTypeMethod.prototype.type;
|
|
8024
9260
|
new FnExpression(
|
|
8025
9261
|
q,
|
|
8026
9262
|
fn,
|
|
@@ -8512,7 +9748,7 @@ class AggregateMethods {
|
|
|
8512
9748
|
* @param over - OVER clause config
|
|
8513
9749
|
*/
|
|
8514
9750
|
rowNumber(over) {
|
|
8515
|
-
return makeFnExpression(this, intNullable, "row_number",
|
|
9751
|
+
return makeFnExpression(this, intNullable, "row_number", emptyArray, {
|
|
8516
9752
|
over
|
|
8517
9753
|
});
|
|
8518
9754
|
}
|
|
@@ -8535,7 +9771,7 @@ class AggregateMethods {
|
|
|
8535
9771
|
* @param over - OVER clause config
|
|
8536
9772
|
*/
|
|
8537
9773
|
rank(over) {
|
|
8538
|
-
return makeFnExpression(this, intNullable, "rank",
|
|
9774
|
+
return makeFnExpression(this, intNullable, "rank", emptyArray, {
|
|
8539
9775
|
over
|
|
8540
9776
|
});
|
|
8541
9777
|
}
|
|
@@ -8558,7 +9794,7 @@ class AggregateMethods {
|
|
|
8558
9794
|
* @param over - OVER clause config
|
|
8559
9795
|
*/
|
|
8560
9796
|
denseRank(over) {
|
|
8561
|
-
return makeFnExpression(this, intNullable, "dense_rank",
|
|
9797
|
+
return makeFnExpression(this, intNullable, "dense_rank", emptyArray, {
|
|
8562
9798
|
over
|
|
8563
9799
|
});
|
|
8564
9800
|
}
|
|
@@ -8581,7 +9817,7 @@ class AggregateMethods {
|
|
|
8581
9817
|
* @param over - OVER clause config
|
|
8582
9818
|
*/
|
|
8583
9819
|
percentRank(over) {
|
|
8584
|
-
return makeFnExpression(this, intNullable, "percent_rank",
|
|
9820
|
+
return makeFnExpression(this, intNullable, "percent_rank", emptyArray, {
|
|
8585
9821
|
over
|
|
8586
9822
|
});
|
|
8587
9823
|
}
|
|
@@ -8604,7 +9840,7 @@ class AggregateMethods {
|
|
|
8604
9840
|
* @param over - OVER clause config
|
|
8605
9841
|
*/
|
|
8606
9842
|
cumeDist(over) {
|
|
8607
|
-
return makeFnExpression(this, floatNullable, "cume_dist",
|
|
9843
|
+
return makeFnExpression(this, floatNullable, "cume_dist", emptyArray, {
|
|
8608
9844
|
over
|
|
8609
9845
|
});
|
|
8610
9846
|
}
|
|
@@ -8624,7 +9860,7 @@ class QueryAsMethods {
|
|
|
8624
9860
|
* @param as - alias for the table of this query
|
|
8625
9861
|
*/
|
|
8626
9862
|
as(as) {
|
|
8627
|
-
return
|
|
9863
|
+
return _setQueryAs(_clone(this), as);
|
|
8628
9864
|
}
|
|
8629
9865
|
}
|
|
8630
9866
|
|
|
@@ -8638,7 +9874,7 @@ class Clear {
|
|
|
8638
9874
|
} else if (clear === "counters") {
|
|
8639
9875
|
if ("type" in q.q && q.q.type === "update") {
|
|
8640
9876
|
q.q.updateData = q.q.updateData.filter((item) => {
|
|
8641
|
-
if (!
|
|
9877
|
+
if (!isExpression(item) && typeof item !== "function") {
|
|
8642
9878
|
let removed = false;
|
|
8643
9879
|
for (const key in item) {
|
|
8644
9880
|
const value = item[key];
|
|
@@ -8813,7 +10049,7 @@ const _addWith = (query, withStore, item, key = "with") => {
|
|
|
8813
10049
|
if (item.q) {
|
|
8814
10050
|
item.q.q.with?.forEach((item2, i, arr) => {
|
|
8815
10051
|
if (item2?.q?.q.type) {
|
|
8816
|
-
|
|
10052
|
+
pushOrNewArrayToObjectImmutable(withStore, key, item2);
|
|
8817
10053
|
arr[i] = void 0;
|
|
8818
10054
|
}
|
|
8819
10055
|
});
|
|
@@ -8824,7 +10060,7 @@ const _addWith = (query, withStore, item, key = "with") => {
|
|
|
8824
10060
|
q.with = q.with ? [...q.with, ...values] : values;
|
|
8825
10061
|
}
|
|
8826
10062
|
}
|
|
8827
|
-
|
|
10063
|
+
pushOrNewArrayToObjectImmutable(withStore, key, item);
|
|
8828
10064
|
};
|
|
8829
10065
|
const moveQueryValueToWith = (q, withStore, value, withKey, set, key) => {
|
|
8830
10066
|
if (value.q.type) {
|
|
@@ -8839,7 +10075,10 @@ const moveQueryValueToWith = (q, withStore, value, withKey, set, key) => {
|
|
|
8839
10075
|
withKey
|
|
8840
10076
|
);
|
|
8841
10077
|
if (set) {
|
|
8842
|
-
|
|
10078
|
+
const sub = _clone(value.baseQuery);
|
|
10079
|
+
sub.q.select = value.q.select;
|
|
10080
|
+
sub.q.as = sub.q.from = as;
|
|
10081
|
+
set[key] = sub;
|
|
8843
10082
|
}
|
|
8844
10083
|
return as;
|
|
8845
10084
|
}
|
|
@@ -8897,7 +10136,7 @@ class WithMethods {
|
|
|
8897
10136
|
const q = _clone(this);
|
|
8898
10137
|
const [options, shapeFn, sql] = args.length === 2 ? [void 0, args[0], args[1]] : args;
|
|
8899
10138
|
const shape = shapeFn(this.columnTypes);
|
|
8900
|
-
|
|
10139
|
+
pushQueryValueImmutable(q, "with", {
|
|
8901
10140
|
n: name,
|
|
8902
10141
|
o: { ...options, columns: Object.keys(shape) },
|
|
8903
10142
|
s: sql(q)
|
|
@@ -9199,7 +10438,7 @@ const processCreateItem = (q, item, rowIndex, ctx, encoders) => {
|
|
|
9199
10438
|
};
|
|
9200
10439
|
const throwOnReadOnly$1 = (q, column, key) => {
|
|
9201
10440
|
if (column.data.appReadOnly || column.data.readOnly) {
|
|
9202
|
-
throw new
|
|
10441
|
+
throw new OrchidOrmInternalError(
|
|
9203
10442
|
q,
|
|
9204
10443
|
"Trying to insert a readonly column",
|
|
9205
10444
|
{ column: key }
|
|
@@ -9222,7 +10461,7 @@ const handleOneData = (q, data, ctx) => {
|
|
|
9222
10461
|
columns.map(
|
|
9223
10462
|
(key) => (
|
|
9224
10463
|
// undefined values were stripped and no need to check for them
|
|
9225
|
-
encoders[key] && !
|
|
10464
|
+
encoders[key] && !isExpression(data[key]) && data[key] !== null ? encoders[key](data[key]) : data[key]
|
|
9226
10465
|
)
|
|
9227
10466
|
)
|
|
9228
10467
|
];
|
|
@@ -9241,7 +10480,7 @@ const handleManyData = (q, data, ctx) => {
|
|
|
9241
10480
|
const columns = Array.from(ctx.columns.keys());
|
|
9242
10481
|
data.forEach((item, i) => {
|
|
9243
10482
|
values[i] = columns.map(
|
|
9244
|
-
(key) => encoders[key] && item[key] !== void 0 && !
|
|
10483
|
+
(key) => encoders[key] && item[key] !== void 0 && !isExpression(item[key]) ? encoders[key](item[key]) : item[key]
|
|
9245
10484
|
);
|
|
9246
10485
|
});
|
|
9247
10486
|
return { columns, values };
|
|
@@ -9276,8 +10515,8 @@ const insert = (self, {
|
|
|
9276
10515
|
q.queryColumnsCount = obj.queryColumnsCount;
|
|
9277
10516
|
}
|
|
9278
10517
|
if (values.length > 1) {
|
|
9279
|
-
const insertValuesAs =
|
|
9280
|
-
|
|
10518
|
+
const insertValuesAs = _getQueryFreeAlias(q, "v");
|
|
10519
|
+
_setQueryAlias(self, "v", insertValuesAs);
|
|
9281
10520
|
q.insertValuesAs = insertValuesAs;
|
|
9282
10521
|
}
|
|
9283
10522
|
}
|
|
@@ -9921,7 +11160,7 @@ class Having {
|
|
|
9921
11160
|
*/
|
|
9922
11161
|
having(...args) {
|
|
9923
11162
|
const q = _clone(this);
|
|
9924
|
-
return
|
|
11163
|
+
return pushQueryValueImmutable(
|
|
9925
11164
|
q,
|
|
9926
11165
|
"having",
|
|
9927
11166
|
args.map((arg) => arg(q).q.expr)
|
|
@@ -9937,14 +11176,14 @@ class Having {
|
|
|
9937
11176
|
* @param args - SQL expression
|
|
9938
11177
|
*/
|
|
9939
11178
|
havingSql(...args) {
|
|
9940
|
-
return
|
|
11179
|
+
return pushQueryValueImmutable(_clone(this), "having", args);
|
|
9941
11180
|
}
|
|
9942
11181
|
}
|
|
9943
11182
|
|
|
9944
|
-
const before = (q, key, cb) =>
|
|
11183
|
+
const before = (q, key, cb) => pushQueryValueImmutable(q, `before${key}`, cb);
|
|
9945
11184
|
const after = (query, key, select, cb, commit) => {
|
|
9946
11185
|
const q = query;
|
|
9947
|
-
|
|
11186
|
+
pushQueryValueImmutable(
|
|
9948
11187
|
q,
|
|
9949
11188
|
`after${key}${commit ? "Commit" : ""}`,
|
|
9950
11189
|
cb
|
|
@@ -9957,16 +11196,16 @@ const after = (query, key, select, cb, commit) => {
|
|
|
9957
11196
|
return query;
|
|
9958
11197
|
};
|
|
9959
11198
|
const _queryHookBeforeQuery = (q, cb) => {
|
|
9960
|
-
return
|
|
11199
|
+
return pushQueryValueImmutable(q, "before", cb);
|
|
9961
11200
|
};
|
|
9962
11201
|
const _queryHookAfterQuery = (q, cb) => {
|
|
9963
|
-
return
|
|
11202
|
+
return pushQueryValueImmutable(q, "after", cb);
|
|
9964
11203
|
};
|
|
9965
11204
|
const _queryHookBeforeCreate = (q, cb) => {
|
|
9966
11205
|
return before(
|
|
9967
11206
|
q,
|
|
9968
11207
|
"Create",
|
|
9969
|
-
(q2) => cb(new
|
|
11208
|
+
(q2) => cb(new QueryHookUtils(q2, q2.q.columns, "hookCreateSet"))
|
|
9970
11209
|
);
|
|
9971
11210
|
};
|
|
9972
11211
|
const _queryHookAfterCreate = (q, select, cb) => {
|
|
@@ -9983,7 +11222,7 @@ const _queryHookBeforeUpdate = (q, cb) => {
|
|
|
9983
11222
|
columns.push(...Object.keys(item));
|
|
9984
11223
|
}
|
|
9985
11224
|
}
|
|
9986
|
-
return cb(new
|
|
11225
|
+
return cb(new QueryHookUtils(q2, columns, "hookUpdateSet"));
|
|
9987
11226
|
});
|
|
9988
11227
|
};
|
|
9989
11228
|
const _queryHookAfterUpdate = (q, select, cb) => {
|
|
@@ -10198,7 +11437,7 @@ class QueryHooks {
|
|
|
10198
11437
|
*/
|
|
10199
11438
|
catchAfterCommitError(fn) {
|
|
10200
11439
|
const q = _clone(this);
|
|
10201
|
-
|
|
11440
|
+
pushQueryValueImmutable(q, "catchAfterCommitErrors", fn);
|
|
10202
11441
|
return q;
|
|
10203
11442
|
}
|
|
10204
11443
|
}
|
|
@@ -10863,9 +12102,9 @@ class Join {
|
|
|
10863
12102
|
Object.entries(shape).map(([key, column]) => [key, column._parse])
|
|
10864
12103
|
);
|
|
10865
12104
|
const { q } = query;
|
|
10866
|
-
|
|
10867
|
-
|
|
10868
|
-
|
|
12105
|
+
setObjectValueImmutable(q, "joinedShapes", as, shape);
|
|
12106
|
+
setObjectValueImmutable(q, "joinedParsers", as, parsers);
|
|
12107
|
+
pushOrNewArrayToObjectImmutable(q, "join", {
|
|
10869
12108
|
type: "JOIN",
|
|
10870
12109
|
args: { a: as, c: shape, d: data }
|
|
10871
12110
|
});
|
|
@@ -10882,7 +12121,7 @@ const makeOnItem = (joinTo, joinFrom, args) => ({
|
|
|
10882
12121
|
}
|
|
10883
12122
|
});
|
|
10884
12123
|
const pushQueryOnForOuter = (q, joinFrom, joinTo, leftColumn, rightColumn) => {
|
|
10885
|
-
return
|
|
12124
|
+
return pushQueryValueImmutable(q, "and", {
|
|
10886
12125
|
ON: {
|
|
10887
12126
|
joinFrom: joinTo,
|
|
10888
12127
|
from: leftColumn,
|
|
@@ -10893,21 +12132,21 @@ const pushQueryOnForOuter = (q, joinFrom, joinTo, leftColumn, rightColumn) => {
|
|
|
10893
12132
|
});
|
|
10894
12133
|
};
|
|
10895
12134
|
const pushQueryOn = (q, joinFrom, joinTo, ...on) => {
|
|
10896
|
-
return
|
|
12135
|
+
return pushQueryValueImmutable(
|
|
10897
12136
|
q,
|
|
10898
12137
|
"and",
|
|
10899
12138
|
makeOnItem(joinFrom, joinTo, on)
|
|
10900
12139
|
);
|
|
10901
12140
|
};
|
|
10902
12141
|
const pushQueryOrOn = (q, joinFrom, joinTo, ...on) => {
|
|
10903
|
-
return
|
|
12142
|
+
return pushQueryValueImmutable(q, "or", [
|
|
10904
12143
|
makeOnItem(joinFrom, joinTo, on)
|
|
10905
12144
|
]);
|
|
10906
12145
|
};
|
|
10907
12146
|
const addQueryOn = (query, joinFrom, joinTo, ...args) => {
|
|
10908
12147
|
const cloned = _clone(query);
|
|
10909
12148
|
const { q } = cloned;
|
|
10910
|
-
|
|
12149
|
+
setObjectValueImmutable(
|
|
10911
12150
|
q,
|
|
10912
12151
|
"joinedShapes",
|
|
10913
12152
|
joinFrom.q.as || joinFrom.table,
|
|
@@ -10932,7 +12171,7 @@ const _queryJoinOrOn = (q, args) => {
|
|
|
10932
12171
|
);
|
|
10933
12172
|
};
|
|
10934
12173
|
const _queryJoinOnJsonPathEquals = (q, args) => {
|
|
10935
|
-
return
|
|
12174
|
+
return pushQueryValueImmutable(q, "and", {
|
|
10936
12175
|
ON: args
|
|
10937
12176
|
});
|
|
10938
12177
|
};
|
|
@@ -11051,7 +12290,7 @@ class MergeQueryMethods {
|
|
|
11051
12290
|
|
|
11052
12291
|
const throwOnReadOnly = (q, column, key) => {
|
|
11053
12292
|
if (column.data.appReadOnly || column.data.readOnly) {
|
|
11054
|
-
throw new
|
|
12293
|
+
throw new OrchidOrmInternalError(
|
|
11055
12294
|
q,
|
|
11056
12295
|
"Trying to update a readonly column",
|
|
11057
12296
|
{ column: key }
|
|
@@ -11086,7 +12325,7 @@ const _queryChangeCounter = (self, op, data) => {
|
|
|
11086
12325
|
throwOnReadOnly(self, column, data);
|
|
11087
12326
|
}
|
|
11088
12327
|
}
|
|
11089
|
-
|
|
12328
|
+
pushQueryValueImmutable(self, "updateData", map);
|
|
11090
12329
|
return self;
|
|
11091
12330
|
};
|
|
11092
12331
|
const _queryUpdate = (updateSelf, arg) => {
|
|
@@ -11095,7 +12334,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11095
12334
|
q.type = "update";
|
|
11096
12335
|
const returnCount = !q.select;
|
|
11097
12336
|
const set = { ...arg };
|
|
11098
|
-
|
|
12337
|
+
pushQueryValueImmutable(query, "updateData", set);
|
|
11099
12338
|
const { shape } = q;
|
|
11100
12339
|
const ctx = {};
|
|
11101
12340
|
let selectQuery;
|
|
@@ -11119,14 +12358,14 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11119
12358
|
value
|
|
11120
12359
|
);
|
|
11121
12360
|
if (value instanceof Db && value.q.type && value.q.subQuery) {
|
|
11122
|
-
throw new
|
|
12361
|
+
throw new OrchidOrmInternalError(
|
|
11123
12362
|
value,
|
|
11124
12363
|
`Only selecting queries are allowed inside a callback of update, ${value.q.type} is given instead.`
|
|
11125
12364
|
);
|
|
11126
12365
|
}
|
|
11127
12366
|
set[key] = value;
|
|
11128
12367
|
}
|
|
11129
|
-
if (value !== null && value !== void 0 && !
|
|
12368
|
+
if (value !== null && value !== void 0 && !isExpression(value)) {
|
|
11130
12369
|
if (value instanceof Db) {
|
|
11131
12370
|
moveQueryValueToWith(query, q, value, "with", set, key);
|
|
11132
12371
|
} else {
|
|
@@ -11138,7 +12377,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11138
12377
|
}
|
|
11139
12378
|
const { queries } = ctx;
|
|
11140
12379
|
if (queries) {
|
|
11141
|
-
const primaryKeys =
|
|
12380
|
+
const primaryKeys = requirePrimaryKeys(
|
|
11142
12381
|
query,
|
|
11143
12382
|
"Cannot perform complex update on a table without primary keys"
|
|
11144
12383
|
);
|
|
@@ -11147,7 +12386,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11147
12386
|
hookSelect.set(column, { select: column });
|
|
11148
12387
|
}
|
|
11149
12388
|
q.patchResult = async (_, _h, queryResult) => {
|
|
11150
|
-
await Promise.all(queries.map(
|
|
12389
|
+
await Promise.all(queries.map(callWithThis, queryResult));
|
|
11151
12390
|
if (ctx.collect) {
|
|
11152
12391
|
const t = query.baseQuery.clone();
|
|
11153
12392
|
_queryWhereIn(
|
|
@@ -11563,20 +12802,20 @@ class Update {
|
|
|
11563
12802
|
}
|
|
11564
12803
|
}
|
|
11565
12804
|
|
|
11566
|
-
class Headline extends
|
|
12805
|
+
class Headline extends Expression {
|
|
11567
12806
|
constructor(q, source, params) {
|
|
11568
12807
|
super();
|
|
11569
12808
|
this.q = q;
|
|
11570
12809
|
this.source = source;
|
|
11571
12810
|
this.params = params;
|
|
11572
|
-
this.result =
|
|
12811
|
+
this.result = emptyObject;
|
|
11573
12812
|
q.expr = this;
|
|
11574
12813
|
}
|
|
11575
12814
|
makeSQL(ctx, quotedAs) {
|
|
11576
12815
|
const { q, source, params } = this;
|
|
11577
12816
|
const lang = getSearchLang(ctx, q, source, quotedAs);
|
|
11578
|
-
const text = params?.text ? params.text instanceof
|
|
11579
|
-
const options = params?.options ? `, ${params.options instanceof
|
|
12817
|
+
const text = params?.text ? params.text instanceof Expression ? params.text.toSQL(ctx, quotedAs) : columnToSql(ctx, q, q.shape, params.text, quotedAs) : getSearchText(ctx, q, source, quotedAs, true);
|
|
12818
|
+
const options = params?.options ? `, ${params.options instanceof Expression ? params.options.toSQL(ctx, quotedAs) : addValue(ctx.values, params.options)}` : "";
|
|
11580
12819
|
return `ts_headline(${lang}, ${text}, "${source.as}"${options})`;
|
|
11581
12820
|
}
|
|
11582
12821
|
}
|
|
@@ -11585,7 +12824,7 @@ AggregateMethods.prototype.headline = function(search, params) {
|
|
|
11585
12824
|
const q = this;
|
|
11586
12825
|
const source = q.q.sources?.[search];
|
|
11587
12826
|
if (!source)
|
|
11588
|
-
throw new
|
|
12827
|
+
throw new OrchidOrmInternalError(q, `Search \`${search}\` is not defined`);
|
|
11589
12828
|
return new Headline(
|
|
11590
12829
|
q.q,
|
|
11591
12830
|
source,
|
|
@@ -11792,9 +13031,9 @@ class SearchMethods {
|
|
|
11792
13031
|
}
|
|
11793
13032
|
setQueryObjectValueImmutable(q, "sources", arg.as, arg);
|
|
11794
13033
|
if (arg.order) {
|
|
11795
|
-
|
|
13034
|
+
pushQueryValueImmutable(q, "order", arg.as);
|
|
11796
13035
|
}
|
|
11797
|
-
return
|
|
13036
|
+
return pushQueryValueImmutable(q, "and", { SEARCH: arg });
|
|
11798
13037
|
}
|
|
11799
13038
|
}
|
|
11800
13039
|
|
|
@@ -11827,7 +13066,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11827
13066
|
const inCTE = {
|
|
11828
13067
|
selectNum: !!(hasAfterCallback || hasAfterCommitCallback),
|
|
11829
13068
|
targetHookSelect: hookSelect,
|
|
11830
|
-
delayedRelationSelect:
|
|
13069
|
+
delayedRelationSelect: newDelayedRelationSelect(
|
|
11831
13070
|
query
|
|
11832
13071
|
)
|
|
11833
13072
|
};
|
|
@@ -11879,7 +13118,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11879
13118
|
await q22;
|
|
11880
13119
|
created = true;
|
|
11881
13120
|
} else if (queryResult.rowCount > 1) {
|
|
11882
|
-
throw new
|
|
13121
|
+
throw new MoreThanOneRowError(
|
|
11883
13122
|
q2,
|
|
11884
13123
|
`Only one row was expected to find, found ${queryResult.rowCount} rows.`
|
|
11885
13124
|
);
|
|
@@ -11903,7 +13142,7 @@ const QueryUpsert = {
|
|
|
11903
13142
|
} else {
|
|
11904
13143
|
updateData = data.update;
|
|
11905
13144
|
}
|
|
11906
|
-
if (!
|
|
13145
|
+
if (!isObjectEmpty(updateData)) {
|
|
11907
13146
|
_queryUpdate(q, updateData);
|
|
11908
13147
|
}
|
|
11909
13148
|
return orCreate(q, data.create, updateData, mergeData);
|
|
@@ -11973,7 +13212,7 @@ class TransformMethods {
|
|
|
11973
13212
|
* @param fn - function to transform query result with
|
|
11974
13213
|
*/
|
|
11975
13214
|
transform(fn) {
|
|
11976
|
-
return
|
|
13215
|
+
return pushQueryValueImmutable(_clone(this), "transform", fn);
|
|
11977
13216
|
}
|
|
11978
13217
|
}
|
|
11979
13218
|
|
|
@@ -12020,7 +13259,7 @@ class QueryMap {
|
|
|
12020
13259
|
* @param thisArg - same as in the native array map
|
|
12021
13260
|
*/
|
|
12022
13261
|
map(fn, thisArg) {
|
|
12023
|
-
return
|
|
13262
|
+
return pushQueryValueImmutable(_clone(this), "transform", {
|
|
12024
13263
|
map: fn,
|
|
12025
13264
|
thisArg
|
|
12026
13265
|
});
|
|
@@ -12045,7 +13284,7 @@ class ScopeMethods {
|
|
|
12045
13284
|
if (!q.q.scopes?.[scope]) {
|
|
12046
13285
|
const s = q.internal.scopes[scope];
|
|
12047
13286
|
if (!s) throw new Error(`Scope ${scope} is not defined`);
|
|
12048
|
-
|
|
13287
|
+
setObjectValueImmutable(q.q, "scopes", scope, s);
|
|
12049
13288
|
}
|
|
12050
13289
|
return q;
|
|
12051
13290
|
}
|
|
@@ -12087,7 +13326,7 @@ function enableSoftDelete(query, table, shape, softDelete, scopes) {
|
|
|
12087
13326
|
};
|
|
12088
13327
|
scopes.deleted = scope;
|
|
12089
13328
|
const { q } = query;
|
|
12090
|
-
|
|
13329
|
+
setObjectValueImmutable(q, "scopes", "nonDeleted", scope);
|
|
12091
13330
|
const _del = _softDelete(
|
|
12092
13331
|
column,
|
|
12093
13332
|
query.internal.nowSQL
|
|
@@ -12126,7 +13365,7 @@ class SoftDeleteMethods {
|
|
|
12126
13365
|
}
|
|
12127
13366
|
}
|
|
12128
13367
|
|
|
12129
|
-
class ColumnRefExpression extends
|
|
13368
|
+
class ColumnRefExpression extends Expression {
|
|
12130
13369
|
constructor(value, name) {
|
|
12131
13370
|
super();
|
|
12132
13371
|
this.name = name;
|
|
@@ -12143,7 +13382,7 @@ class ColumnRefExpression extends orchidCore.Expression {
|
|
|
12143
13382
|
);
|
|
12144
13383
|
}
|
|
12145
13384
|
}
|
|
12146
|
-
class RefExpression extends
|
|
13385
|
+
class RefExpression extends Expression {
|
|
12147
13386
|
constructor(value, query, ref) {
|
|
12148
13387
|
super();
|
|
12149
13388
|
this.ref = ref;
|
|
@@ -12157,7 +13396,7 @@ class RefExpression extends orchidCore.Expression {
|
|
|
12157
13396
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, as && `"${as}"`);
|
|
12158
13397
|
}
|
|
12159
13398
|
}
|
|
12160
|
-
class OrExpression extends
|
|
13399
|
+
class OrExpression extends Expression {
|
|
12161
13400
|
constructor(args) {
|
|
12162
13401
|
super();
|
|
12163
13402
|
this.args = args;
|
|
@@ -12167,7 +13406,7 @@ class OrExpression extends orchidCore.Expression {
|
|
|
12167
13406
|
const res = [];
|
|
12168
13407
|
for (const arg of this.args) {
|
|
12169
13408
|
if (arg) {
|
|
12170
|
-
if (
|
|
13409
|
+
if (isExpression(arg)) {
|
|
12171
13410
|
const sql = arg.toSQL(ctx, quotedAs);
|
|
12172
13411
|
if (sql) res.push(sql);
|
|
12173
13412
|
} else {
|
|
@@ -12253,7 +13492,7 @@ class ExpressionMethods {
|
|
|
12253
13492
|
return new RefExpression(column || UnknownColumn.instance, q, arg);
|
|
12254
13493
|
}
|
|
12255
13494
|
val(value) {
|
|
12256
|
-
return new
|
|
13495
|
+
return new ValExpression(value);
|
|
12257
13496
|
}
|
|
12258
13497
|
/**
|
|
12259
13498
|
* `fn` allows to call an arbitrary SQL function.
|
|
@@ -12292,7 +13531,7 @@ class ExpressionMethods {
|
|
|
12292
13531
|
* @param options
|
|
12293
13532
|
*/
|
|
12294
13533
|
fn(fn, args, options) {
|
|
12295
|
-
return makeFnExpression(this,
|
|
13534
|
+
return makeFnExpression(this, emptyObject, fn, args, options);
|
|
12296
13535
|
}
|
|
12297
13536
|
or(...args) {
|
|
12298
13537
|
return new OrExpression(args);
|
|
@@ -12467,7 +13706,7 @@ class QueryMethods {
|
|
|
12467
13706
|
find(value) {
|
|
12468
13707
|
const q = _clone(this);
|
|
12469
13708
|
if (value === null || value === void 0) {
|
|
12470
|
-
throw new
|
|
13709
|
+
throw new OrchidOrmInternalError(
|
|
12471
13710
|
q,
|
|
12472
13711
|
`${value} is not allowed in the find method`
|
|
12473
13712
|
);
|
|
@@ -12651,7 +13890,7 @@ class QueryMethods {
|
|
|
12651
13890
|
* @param arg - window config
|
|
12652
13891
|
*/
|
|
12653
13892
|
window(arg) {
|
|
12654
|
-
return
|
|
13893
|
+
return pushQueryValueImmutable(_clone(this), "window", arg);
|
|
12655
13894
|
}
|
|
12656
13895
|
wrap(query, as) {
|
|
12657
13896
|
return queryWrap(this, _clone(query), as);
|
|
@@ -12706,7 +13945,7 @@ class QueryMethods {
|
|
|
12706
13945
|
* @param args - SQL expression
|
|
12707
13946
|
*/
|
|
12708
13947
|
orderSql(...args) {
|
|
12709
|
-
return
|
|
13948
|
+
return pushQueryValueImmutable(
|
|
12710
13949
|
_clone(this),
|
|
12711
13950
|
"order",
|
|
12712
13951
|
sqlQueryArgsToExpression(args)
|
|
@@ -12893,9 +14132,9 @@ class QueryMethods {
|
|
|
12893
14132
|
const helperAs = this.q.as || this.table;
|
|
12894
14133
|
return (query, ...args) => {
|
|
12895
14134
|
const q = _clone(query);
|
|
12896
|
-
const as =
|
|
14135
|
+
const as = _getQueryAs(q);
|
|
12897
14136
|
if (as) {
|
|
12898
|
-
|
|
14137
|
+
_setQueryAlias(q, as, helperAs);
|
|
12899
14138
|
}
|
|
12900
14139
|
return fn(q, ...args);
|
|
12901
14140
|
};
|
|
@@ -13024,7 +14263,7 @@ class QueryMethods {
|
|
|
13024
14263
|
}
|
|
13025
14264
|
Object.assign(QueryMethods.prototype, QueryUpsert);
|
|
13026
14265
|
Object.assign(QueryMethods.prototype, QueryOrCreate);
|
|
13027
|
-
|
|
14266
|
+
applyMixins(QueryMethods, [
|
|
13028
14267
|
QueryAsMethods,
|
|
13029
14268
|
AggregateMethods,
|
|
13030
14269
|
Select,
|
|
@@ -13162,7 +14401,7 @@ const parseIndexOrExclude = (item) => {
|
|
|
13162
14401
|
const performQuery = async (q, args, method) => {
|
|
13163
14402
|
const trx = q.internal.transactionStorage.getStore();
|
|
13164
14403
|
let sql;
|
|
13165
|
-
if (
|
|
14404
|
+
if (isRawSQL(args[0])) {
|
|
13166
14405
|
const values = [];
|
|
13167
14406
|
sql = {
|
|
13168
14407
|
text: args[0].toSQL({ values }),
|
|
@@ -13208,7 +14447,7 @@ class Db extends QueryMethods {
|
|
|
13208
14447
|
this.columnTypes = columnTypes;
|
|
13209
14448
|
const self = this;
|
|
13210
14449
|
const { softDelete } = options;
|
|
13211
|
-
const scopes = options.scopes || softDelete ? {} :
|
|
14450
|
+
const scopes = options.scopes || softDelete ? {} : emptyObject;
|
|
13212
14451
|
this.baseQuery = this;
|
|
13213
14452
|
this.relations = {};
|
|
13214
14453
|
this.relationQueries = {};
|
|
@@ -13230,7 +14469,7 @@ class Db extends QueryMethods {
|
|
|
13230
14469
|
if (column.data.name) {
|
|
13231
14470
|
prepareSelectAll = true;
|
|
13232
14471
|
} else if (snakeCase) {
|
|
13233
|
-
const snakeName =
|
|
14472
|
+
const snakeName = toSnakeCase(key);
|
|
13234
14473
|
if (snakeName !== key) {
|
|
13235
14474
|
prepareSelectAll = true;
|
|
13236
14475
|
column.data.name = snakeName;
|
|
@@ -13241,7 +14480,7 @@ class Db extends QueryMethods {
|
|
|
13241
14480
|
}
|
|
13242
14481
|
const { modifyQuery: mq } = column.data;
|
|
13243
14482
|
if (mq) {
|
|
13244
|
-
modifyQuery =
|
|
14483
|
+
modifyQuery = pushOrNewArray(modifyQuery, (q) => mq(q, column));
|
|
13245
14484
|
}
|
|
13246
14485
|
if (typeof column.data.default === "function") {
|
|
13247
14486
|
if (!runtimeDefaultColumns) runtimeDefaultColumns = [key];
|
|
@@ -13320,7 +14559,7 @@ class Db extends QueryMethods {
|
|
|
13320
14559
|
cb(this);
|
|
13321
14560
|
}
|
|
13322
14561
|
}
|
|
13323
|
-
this.error = class extends
|
|
14562
|
+
this.error = class extends QueryError {
|
|
13324
14563
|
constructor(message) {
|
|
13325
14564
|
super(self, message);
|
|
13326
14565
|
}
|
|
@@ -13420,7 +14659,7 @@ class Db extends QueryMethods {
|
|
|
13420
14659
|
const {
|
|
13421
14660
|
rows: [row]
|
|
13422
14661
|
} = await performQuery(q, args, "query");
|
|
13423
|
-
if (!row) throw new
|
|
14662
|
+
if (!row) throw new NotFoundError(q);
|
|
13424
14663
|
return row;
|
|
13425
14664
|
},
|
|
13426
14665
|
async takeOptional(...args) {
|
|
@@ -13439,7 +14678,7 @@ class Db extends QueryMethods {
|
|
|
13439
14678
|
const {
|
|
13440
14679
|
rows: [row]
|
|
13441
14680
|
} = await performQuery(q, args, "arrays");
|
|
13442
|
-
if (!row) throw new
|
|
14681
|
+
if (!row) throw new NotFoundError(q);
|
|
13443
14682
|
return row[0];
|
|
13444
14683
|
},
|
|
13445
14684
|
async getOptional(...args) {
|
|
@@ -13471,7 +14710,7 @@ class Db extends QueryMethods {
|
|
|
13471
14710
|
return performQuery(this, args, "arrays");
|
|
13472
14711
|
}
|
|
13473
14712
|
}
|
|
13474
|
-
|
|
14713
|
+
applyMixins(Db, [QueryMethods]);
|
|
13475
14714
|
Db.prototype.constructor = Db;
|
|
13476
14715
|
const createDbWithAdapter = ({
|
|
13477
14716
|
log,
|
|
@@ -13492,7 +14731,7 @@ const createDbWithAdapter = ({
|
|
|
13492
14731
|
};
|
|
13493
14732
|
const ct = typeof ctOrFn === "function" ? ctOrFn(makeColumnTypes(schemaConfig)) : ctOrFn;
|
|
13494
14733
|
if (snakeCase) {
|
|
13495
|
-
ct[
|
|
14734
|
+
ct[snakeCaseKey] = true;
|
|
13496
14735
|
}
|
|
13497
14736
|
const transactionStorage = new node_async_hooks.AsyncLocalStorage();
|
|
13498
14737
|
const qb = _initQueryBuilder(
|
|
@@ -13694,6 +14933,7 @@ exports.CitextColumn = CitextColumn;
|
|
|
13694
14933
|
exports.Clear = Clear;
|
|
13695
14934
|
exports.ColumnRefExpression = ColumnRefExpression;
|
|
13696
14935
|
exports.ColumnType = ColumnType;
|
|
14936
|
+
exports.ColumnTypeBase = ColumnTypeBase;
|
|
13697
14937
|
exports.ComputedColumn = ComputedColumn;
|
|
13698
14938
|
exports.CustomTypeColumn = CustomTypeColumn;
|
|
13699
14939
|
exports.DateBaseColumn = DateBaseColumn;
|
|
@@ -13707,7 +14947,9 @@ exports.DomainColumn = DomainColumn;
|
|
|
13707
14947
|
exports.DoublePrecisionColumn = DoublePrecisionColumn;
|
|
13708
14948
|
exports.DynamicRawSQL = DynamicRawSQL;
|
|
13709
14949
|
exports.EnumColumn = EnumColumn;
|
|
14950
|
+
exports.Expression = Expression;
|
|
13710
14951
|
exports.ExpressionMethods = ExpressionMethods;
|
|
14952
|
+
exports.ExpressionTypeMethod = ExpressionTypeMethod;
|
|
13711
14953
|
exports.FnExpression = FnExpression;
|
|
13712
14954
|
exports.For = For;
|
|
13713
14955
|
exports.FromMethods = FromMethods;
|
|
@@ -13727,22 +14969,29 @@ exports.MacAddr8Column = MacAddr8Column;
|
|
|
13727
14969
|
exports.MacAddrColumn = MacAddrColumn;
|
|
13728
14970
|
exports.MergeQueryMethods = MergeQueryMethods;
|
|
13729
14971
|
exports.MoneyColumn = MoneyColumn;
|
|
14972
|
+
exports.MoreThanOneRowError = MoreThanOneRowError;
|
|
14973
|
+
exports.NotFoundError = NotFoundError;
|
|
13730
14974
|
exports.NumberAsStringBaseColumn = NumberAsStringBaseColumn;
|
|
13731
14975
|
exports.NumberBaseColumn = NumberBaseColumn;
|
|
13732
14976
|
exports.OnMethods = OnMethods;
|
|
13733
14977
|
exports.Operators = Operators;
|
|
13734
14978
|
exports.OrExpression = OrExpression;
|
|
14979
|
+
exports.OrchidOrmError = OrchidOrmError;
|
|
14980
|
+
exports.OrchidOrmInternalError = OrchidOrmInternalError;
|
|
13735
14981
|
exports.PathColumn = PathColumn;
|
|
13736
14982
|
exports.PointColumn = PointColumn;
|
|
13737
14983
|
exports.PolygonColumn = PolygonColumn;
|
|
13738
14984
|
exports.PostgisGeographyPointColumn = PostgisGeographyPointColumn;
|
|
13739
14985
|
exports.QueryAsMethods = QueryAsMethods;
|
|
14986
|
+
exports.QueryError = QueryError;
|
|
13740
14987
|
exports.QueryGet = QueryGet;
|
|
14988
|
+
exports.QueryHookUtils = QueryHookUtils;
|
|
13741
14989
|
exports.QueryHooks = QueryHooks;
|
|
13742
14990
|
exports.QueryLog = QueryLog;
|
|
13743
14991
|
exports.QueryMethods = QueryMethods;
|
|
13744
14992
|
exports.QueryUpsert = QueryUpsert;
|
|
13745
14993
|
exports.RawSQL = RawSQL;
|
|
14994
|
+
exports.RawSQLBase = RawSQLBase;
|
|
13746
14995
|
exports.RealColumn = RealColumn;
|
|
13747
14996
|
exports.RefExpression = RefExpression;
|
|
13748
14997
|
exports.SearchMethods = SearchMethods;
|
|
@@ -13763,16 +15012,27 @@ exports.TransformMethods = TransformMethods;
|
|
|
13763
15012
|
exports.TsQueryColumn = TsQueryColumn;
|
|
13764
15013
|
exports.TsVectorColumn = TsVectorColumn;
|
|
13765
15014
|
exports.UUIDColumn = UUIDColumn;
|
|
15015
|
+
exports.UnhandledTypeError = UnhandledTypeError;
|
|
13766
15016
|
exports.Union = Union;
|
|
13767
15017
|
exports.UnknownColumn = UnknownColumn;
|
|
13768
15018
|
exports.Update = Update;
|
|
15019
|
+
exports.ValExpression = ValExpression;
|
|
13769
15020
|
exports.VarCharColumn = VarCharColumn;
|
|
13770
15021
|
exports.VirtualColumn = VirtualColumn;
|
|
13771
15022
|
exports.Where = Where;
|
|
13772
15023
|
exports.WithMethods = WithMethods;
|
|
13773
15024
|
exports.XMLColumn = XMLColumn;
|
|
15025
|
+
exports._addToHookSelect = _addToHookSelect;
|
|
15026
|
+
exports._addToHookSelectWithTable = _addToHookSelectWithTable;
|
|
13774
15027
|
exports._addWith = _addWith;
|
|
15028
|
+
exports._applyRelationAliases = _applyRelationAliases;
|
|
15029
|
+
exports._checkIfAliased = _checkIfAliased;
|
|
13775
15030
|
exports._clone = _clone;
|
|
15031
|
+
exports._copyQueryAliasToQuery = _copyQueryAliasToQuery;
|
|
15032
|
+
exports._getQueryAliasOrName = _getQueryAliasOrName;
|
|
15033
|
+
exports._getQueryAs = _getQueryAs;
|
|
15034
|
+
exports._getQueryFreeAlias = _getQueryFreeAlias;
|
|
15035
|
+
exports._getQueryOuterAliases = _getQueryOuterAliases;
|
|
13776
15036
|
exports._getSelectableColumn = _getSelectableColumn;
|
|
13777
15037
|
exports._initQueryBuilder = _initQueryBuilder;
|
|
13778
15038
|
exports._queryAfterSaveCommit = _queryAfterSaveCommit;
|
|
@@ -13830,17 +15090,31 @@ exports._queryWhereNotSql = _queryWhereNotSql;
|
|
|
13830
15090
|
exports._queryWhereOneOf = _queryWhereOneOf;
|
|
13831
15091
|
exports._queryWhereSql = _queryWhereSql;
|
|
13832
15092
|
exports._runAfterCommitHooks = _runAfterCommitHooks;
|
|
15093
|
+
exports._setQueryAlias = _setQueryAlias;
|
|
15094
|
+
exports._setQueryAs = _setQueryAs;
|
|
15095
|
+
exports._setSubQueryAliases = _setSubQueryAliases;
|
|
15096
|
+
exports.addCode = addCode;
|
|
13833
15097
|
exports.addColumnParserToQuery = addColumnParserToQuery;
|
|
13834
15098
|
exports.addParserForRawExpression = addParserForRawExpression;
|
|
13835
15099
|
exports.addParserForSelectItem = addParserForSelectItem;
|
|
13836
15100
|
exports.addQueryOn = addQueryOn;
|
|
15101
|
+
exports.addValue = addValue;
|
|
13837
15102
|
exports.anyShape = anyShape;
|
|
13838
15103
|
exports.applyComputedColumns = applyComputedColumns;
|
|
15104
|
+
exports.applyMixins = applyMixins;
|
|
15105
|
+
exports.applyTransforms = applyTransforms;
|
|
15106
|
+
exports.arrayDataToCode = arrayDataToCode;
|
|
13839
15107
|
exports.assignDbDataToColumn = assignDbDataToColumn;
|
|
15108
|
+
exports.backtickQuote = backtickQuote;
|
|
15109
|
+
exports.callWithThis = callWithThis;
|
|
13840
15110
|
exports.checkIfASimpleQuery = checkIfASimpleQuery;
|
|
13841
15111
|
exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
|
|
15112
|
+
exports.codeToString = codeToString;
|
|
15113
|
+
exports.colors = colors;
|
|
13842
15114
|
exports.columnCheckToCode = columnCheckToCode;
|
|
13843
15115
|
exports.columnCode = columnCode;
|
|
15116
|
+
exports.columnDefaultArgumentToCode = columnDefaultArgumentToCode;
|
|
15117
|
+
exports.columnErrorMessagesToCode = columnErrorMessagesToCode;
|
|
13844
15118
|
exports.columnExcludesToCode = columnExcludesToCode;
|
|
13845
15119
|
exports.columnForeignKeysToCode = columnForeignKeysToCode;
|
|
13846
15120
|
exports.columnIndexesToCode = columnIndexesToCode;
|
|
@@ -13848,79 +15122,141 @@ exports.columnsShapeToCode = columnsShapeToCode;
|
|
|
13848
15122
|
exports.commitSql = commitSql;
|
|
13849
15123
|
exports.constraintInnerToCode = constraintInnerToCode;
|
|
13850
15124
|
exports.constraintToCode = constraintToCode;
|
|
15125
|
+
exports.consumeColumnName = consumeColumnName;
|
|
13851
15126
|
exports.copyTableData = copyTableData;
|
|
13852
15127
|
exports.countSelect = countSelect;
|
|
13853
15128
|
exports.createDbWithAdapter = createDbWithAdapter;
|
|
15129
|
+
exports.dateDataToCode = dateDataToCode;
|
|
15130
|
+
exports.deepCompare = deepCompare;
|
|
13854
15131
|
exports.defaultSchemaConfig = defaultSchemaConfig;
|
|
15132
|
+
exports.emptyArray = emptyArray;
|
|
15133
|
+
exports.emptyObject = emptyObject;
|
|
13855
15134
|
exports.escapeForLog = escapeForLog;
|
|
13856
15135
|
exports.escapeForMigration = escapeForMigration;
|
|
13857
15136
|
exports.escapeString = escapeString;
|
|
13858
15137
|
exports.excludeInnerToCode = excludeInnerToCode;
|
|
13859
15138
|
exports.excludeToCode = excludeToCode;
|
|
15139
|
+
exports.exhaustive = exhaustive;
|
|
13860
15140
|
exports.extendQuery = extendQuery;
|
|
13861
15141
|
exports.filterResult = filterResult;
|
|
13862
15142
|
exports.foreignKeyArgumentToCode = foreignKeyArgumentToCode;
|
|
15143
|
+
exports.getCallerFilePath = getCallerFilePath;
|
|
13863
15144
|
exports.getClonedQueryData = getClonedQueryData;
|
|
13864
15145
|
exports.getColumnBaseType = getColumnBaseType;
|
|
13865
15146
|
exports.getColumnInfo = getColumnInfo;
|
|
13866
15147
|
exports.getColumnTypes = getColumnTypes;
|
|
15148
|
+
exports.getDefaultLanguage = getDefaultLanguage;
|
|
15149
|
+
exports.getFreeAlias = getFreeAlias;
|
|
15150
|
+
exports.getFreeSetAlias = getFreeSetAlias;
|
|
13867
15151
|
exports.getFullColumnTable = getFullColumnTable;
|
|
15152
|
+
exports.getImportPath = getImportPath;
|
|
15153
|
+
exports.getPrimaryKeys = getPrimaryKeys;
|
|
13868
15154
|
exports.getQueryAs = getQueryAs;
|
|
15155
|
+
exports.getQueryParsers = getQueryParsers;
|
|
13869
15156
|
exports.getShapeFromSelect = getShapeFromSelect;
|
|
13870
15157
|
exports.getSqlText = getSqlText;
|
|
15158
|
+
exports.getStackTrace = getStackTrace;
|
|
15159
|
+
exports.getValueKey = getValueKey;
|
|
13871
15160
|
exports.handleResult = handleResult;
|
|
13872
15161
|
exports.identityToCode = identityToCode;
|
|
13873
15162
|
exports.indexInnerToCode = indexInnerToCode;
|
|
13874
15163
|
exports.indexToCode = indexToCode;
|
|
13875
15164
|
exports.isDefaultTimeStamp = isDefaultTimeStamp;
|
|
15165
|
+
exports.isExpression = isExpression;
|
|
13876
15166
|
exports.isInUserTransaction = isInUserTransaction;
|
|
15167
|
+
exports.isIterable = isIterable;
|
|
15168
|
+
exports.isObjectEmpty = isObjectEmpty;
|
|
13877
15169
|
exports.isQueryReturnsAll = isQueryReturnsAll;
|
|
15170
|
+
exports.isRawSQL = isRawSQL;
|
|
15171
|
+
exports.isRelationQuery = isRelationQuery;
|
|
13878
15172
|
exports.isSelectingCount = isSelectingCount;
|
|
15173
|
+
exports.isTemplateLiteralArgs = isTemplateLiteralArgs;
|
|
13879
15174
|
exports.joinSubQuery = joinSubQuery;
|
|
15175
|
+
exports.joinTruthy = joinTruthy;
|
|
15176
|
+
exports.logColors = logColors;
|
|
13880
15177
|
exports.logParamToLogObject = logParamToLogObject;
|
|
15178
|
+
exports.makeColumnNullable = makeColumnNullable;
|
|
13881
15179
|
exports.makeColumnTypes = makeColumnTypes;
|
|
13882
15180
|
exports.makeColumnsByType = makeColumnsByType;
|
|
13883
15181
|
exports.makeFnExpression = makeFnExpression;
|
|
13884
15182
|
exports.moveQueryValueToWith = moveQueryValueToWith;
|
|
15183
|
+
exports.newDelayedRelationSelect = newDelayedRelationSelect;
|
|
15184
|
+
exports.noop = noop;
|
|
15185
|
+
exports.numberDataToCode = numberDataToCode;
|
|
15186
|
+
exports.objectHasValues = objectHasValues;
|
|
15187
|
+
exports.omit = omit;
|
|
13885
15188
|
exports.parseRecord = parseRecord;
|
|
13886
15189
|
exports.parseTableData = parseTableData;
|
|
13887
15190
|
exports.parseTableDataInput = parseTableDataInput;
|
|
15191
|
+
exports.pathToLog = pathToLog;
|
|
15192
|
+
exports.pick = pick;
|
|
15193
|
+
exports.pluralize = pluralize;
|
|
13888
15194
|
exports.postgisTypmodToSql = postgisTypmodToSql;
|
|
13889
15195
|
exports.primaryKeyInnerToCode = primaryKeyInnerToCode;
|
|
13890
15196
|
exports.processComputedBatches = processComputedBatches;
|
|
13891
15197
|
exports.processComputedResult = processComputedResult;
|
|
13892
15198
|
exports.processSelectArg = processSelectArg;
|
|
15199
|
+
exports.pushColumnData = pushColumnData;
|
|
13893
15200
|
exports.pushLimitSQL = pushLimitSQL;
|
|
15201
|
+
exports.pushOrNewArray = pushOrNewArray;
|
|
15202
|
+
exports.pushOrNewArrayToObjectImmutable = pushOrNewArrayToObjectImmutable;
|
|
13894
15203
|
exports.pushQueryArrayImmutable = pushQueryArrayImmutable;
|
|
13895
15204
|
exports.pushQueryOn = pushQueryOn;
|
|
13896
15205
|
exports.pushQueryOnForOuter = pushQueryOnForOuter;
|
|
13897
15206
|
exports.pushQueryOrOn = pushQueryOrOn;
|
|
15207
|
+
exports.pushQueryValueImmutable = pushQueryValueImmutable;
|
|
13898
15208
|
exports.pushTableDataCode = pushTableDataCode;
|
|
15209
|
+
exports.queryColumnNameToKey = queryColumnNameToKey;
|
|
13899
15210
|
exports.queryFrom = queryFrom;
|
|
13900
15211
|
exports.queryFromSql = queryFromSql;
|
|
13901
15212
|
exports.queryJson = queryJson;
|
|
13902
15213
|
exports.queryMethodByReturnType = queryMethodByReturnType;
|
|
13903
15214
|
exports.queryTypeWithLimitOne = queryTypeWithLimitOne;
|
|
13904
15215
|
exports.queryWrap = queryWrap;
|
|
15216
|
+
exports.quoteObjectKey = quoteObjectKey;
|
|
13905
15217
|
exports.raw = raw;
|
|
13906
15218
|
exports.referencesArgsToCode = referencesArgsToCode;
|
|
15219
|
+
exports.requirePrimaryKeys = requirePrimaryKeys;
|
|
13907
15220
|
exports.resolveSubQueryCallbackV2 = resolveSubQueryCallbackV2;
|
|
15221
|
+
exports.returnArg = returnArg;
|
|
13908
15222
|
exports.rollbackSql = rollbackSql;
|
|
13909
15223
|
exports.saveAliasedShape = saveAliasedShape;
|
|
15224
|
+
exports.setColumnData = setColumnData;
|
|
13910
15225
|
exports.setColumnDefaultParse = setColumnDefaultParse;
|
|
13911
15226
|
exports.setColumnEncode = setColumnEncode;
|
|
13912
15227
|
exports.setColumnParse = setColumnParse;
|
|
13913
15228
|
exports.setColumnParseNull = setColumnParseNull;
|
|
15229
|
+
exports.setConnectRetryConfig = setConnectRetryConfig;
|
|
15230
|
+
exports.setCurrentColumnName = setCurrentColumnName;
|
|
15231
|
+
exports.setDataValue = setDataValue;
|
|
15232
|
+
exports.setDefaultLanguage = setDefaultLanguage;
|
|
15233
|
+
exports.setDefaultNowFn = setDefaultNowFn;
|
|
15234
|
+
exports.setDelayedRelation = setDelayedRelation;
|
|
15235
|
+
exports.setObjectValueImmutable = setObjectValueImmutable;
|
|
13914
15236
|
exports.setParserForSelectedString = setParserForSelectedString;
|
|
15237
|
+
exports.setParserToQuery = setParserToQuery;
|
|
13915
15238
|
exports.setQueryObjectValueImmutable = setQueryObjectValueImmutable;
|
|
13916
15239
|
exports.setQueryOperators = setQueryOperators;
|
|
13917
15240
|
exports.simplifyColumnDefault = simplifyColumnDefault;
|
|
15241
|
+
exports.singleQuote = singleQuote;
|
|
15242
|
+
exports.singleQuoteArray = singleQuoteArray;
|
|
15243
|
+
exports.snakeCaseKey = snakeCaseKey;
|
|
15244
|
+
exports.spreadObjectValues = spreadObjectValues;
|
|
13918
15245
|
exports.sqlFn = sqlFn;
|
|
13919
15246
|
exports.sqlQueryArgsToExpression = sqlQueryArgsToExpression;
|
|
15247
|
+
exports.stringDataToCode = stringDataToCode;
|
|
13920
15248
|
exports.tableDataMethods = tableDataMethods;
|
|
15249
|
+
exports.templateLiteralSQLToCode = templateLiteralSQLToCode;
|
|
13921
15250
|
exports.templateLiteralToSQL = templateLiteralToSQL;
|
|
13922
15251
|
exports.testTransaction = testTransaction;
|
|
13923
15252
|
exports.throwIfJoinLateral = throwIfJoinLateral;
|
|
13924
15253
|
exports.throwIfNoWhere = throwIfNoWhere;
|
|
15254
|
+
exports.timestampHelpers = timestampHelpers;
|
|
15255
|
+
exports.toArray = toArray;
|
|
15256
|
+
exports.toCamelCase = toCamelCase;
|
|
15257
|
+
exports.toPascalCase = toPascalCase;
|
|
13925
15258
|
exports.toSQL = toSQL;
|
|
15259
|
+
exports.toSnakeCase = toSnakeCase;
|
|
15260
|
+
exports.toSubSqlText = toSubSqlText;
|
|
15261
|
+
exports.wrapAdapterFnWithConnectRetry = wrapAdapterFnWithConnectRetry;
|
|
13926
15262
|
//# sourceMappingURL=index.js.map
|