pqb 0.57.1 → 0.57.2
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 +1851 -526
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +1324 -96
- 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;
|
|
@@ -6569,7 +7687,7 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6569
7687
|
const arr = [];
|
|
6570
7688
|
for (const key in set) {
|
|
6571
7689
|
const val = set[key];
|
|
6572
|
-
const value =
|
|
7690
|
+
const value = isExpression(val) ? val.toSQL(ctx, quotedAs) : addValue(ctx.values, val);
|
|
6573
7691
|
arr.push(`"${shape[key]?.data.name || key}" = ${value}`);
|
|
6574
7692
|
}
|
|
6575
7693
|
ctx.sql.push("DO UPDATE SET", arr.join(", "));
|
|
@@ -6578,32 +7696,17 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6578
7696
|
}
|
|
6579
7697
|
}
|
|
6580
7698
|
pushWhereStatementSql(ctx, q, query, quotedAs);
|
|
6581
|
-
let returning;
|
|
6582
7699
|
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
|
-
);
|
|
7700
|
+
if (!inCTE) {
|
|
7701
|
+
delayedRelationSelect = q.q.selectRelation ? newDelayedRelationSelect(q) : void 0;
|
|
6599
7702
|
}
|
|
6600
|
-
|
|
7703
|
+
const returningPos = ctx.sql.length;
|
|
6601
7704
|
let insertManyFromValuesAs;
|
|
6602
7705
|
if (insertFrom) {
|
|
6603
7706
|
if (values.length < 2) {
|
|
6604
7707
|
const q2 = insertFrom.clone();
|
|
6605
7708
|
if (values[0]?.length) {
|
|
6606
|
-
|
|
7709
|
+
pushQueryValueImmutable(
|
|
6607
7710
|
q2,
|
|
6608
7711
|
"select",
|
|
6609
7712
|
new RawSQL(
|
|
@@ -6666,7 +7769,18 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6666
7769
|
addWithSqls(ctx, hasWith, withSqls, valuesPos, insertSql);
|
|
6667
7770
|
ctx.sql[valuesPos] = startingKeyword + valuesSql.join(", ") + valuesAppend;
|
|
6668
7771
|
ctxValues.length = currentValuesLen;
|
|
6669
|
-
|
|
7772
|
+
const returning2 = makeInsertReturning(
|
|
7773
|
+
ctx,
|
|
7774
|
+
q,
|
|
7775
|
+
query,
|
|
7776
|
+
quotedAs,
|
|
7777
|
+
delayedRelationSelect,
|
|
7778
|
+
isSubSql
|
|
7779
|
+
);
|
|
7780
|
+
if (returning2.select) {
|
|
7781
|
+
ctx.sql[returningPos] = "RETURNING " + returning2.select;
|
|
7782
|
+
}
|
|
7783
|
+
batch = pushOrNewArray(batch, {
|
|
6670
7784
|
text: ctx.sql.join(" "),
|
|
6671
7785
|
values: ctxValues
|
|
6672
7786
|
});
|
|
@@ -6683,7 +7797,7 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6683
7797
|
addWithSqls(ctx, hasWith, withSqls, valuesPos, insertSql);
|
|
6684
7798
|
if (batch) {
|
|
6685
7799
|
if (hasNonSelect) {
|
|
6686
|
-
throw new
|
|
7800
|
+
throw new OrchidOrmInternalError(
|
|
6687
7801
|
q,
|
|
6688
7802
|
`Cannot insert many records when having a non-select sub-query`
|
|
6689
7803
|
);
|
|
@@ -6693,8 +7807,19 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6693
7807
|
text: ctx.sql.join(" "),
|
|
6694
7808
|
values: ctxValues
|
|
6695
7809
|
});
|
|
7810
|
+
const returning2 = makeInsertReturning(
|
|
7811
|
+
ctx,
|
|
7812
|
+
q,
|
|
7813
|
+
query,
|
|
7814
|
+
quotedAs,
|
|
7815
|
+
delayedRelationSelect,
|
|
7816
|
+
isSubSql
|
|
7817
|
+
);
|
|
7818
|
+
if (returning2.select) {
|
|
7819
|
+
ctx.sql[returningPos] = "RETURNING " + returning2.select;
|
|
7820
|
+
}
|
|
6696
7821
|
return {
|
|
6697
|
-
|
|
7822
|
+
tableHook: returning2.tableHook,
|
|
6698
7823
|
delayedRelationSelect,
|
|
6699
7824
|
batch
|
|
6700
7825
|
};
|
|
@@ -6705,13 +7830,47 @@ const makeInsertSql = (ctx, q, query, quotedAs) => {
|
|
|
6705
7830
|
ctx.sql[valuesPos] += ' WHERE NOT EXISTS (SELECT 1 FROM "f")';
|
|
6706
7831
|
}
|
|
6707
7832
|
}
|
|
7833
|
+
const returning = makeInsertReturning(
|
|
7834
|
+
ctx,
|
|
7835
|
+
q,
|
|
7836
|
+
query,
|
|
7837
|
+
quotedAs,
|
|
7838
|
+
delayedRelationSelect,
|
|
7839
|
+
isSubSql
|
|
7840
|
+
);
|
|
7841
|
+
if (returning.select) {
|
|
7842
|
+
ctx.sql[returningPos] = "RETURNING " + returning.select;
|
|
7843
|
+
}
|
|
6708
7844
|
return {
|
|
6709
|
-
|
|
7845
|
+
tableHook: returning.tableHook,
|
|
6710
7846
|
delayedRelationSelect,
|
|
6711
7847
|
text: ctx.sql.join(" "),
|
|
6712
7848
|
values: ctx.values
|
|
6713
7849
|
};
|
|
6714
7850
|
};
|
|
7851
|
+
const makeInsertReturning = (ctx, q, query, quotedAs, delayedRelationSelect, isSubSql) => {
|
|
7852
|
+
const { inCTE } = query;
|
|
7853
|
+
if (inCTE) {
|
|
7854
|
+
const select = inCTE.returning?.select;
|
|
7855
|
+
return {
|
|
7856
|
+
select: inCTE.selectNum || !select ? select ? "1, " + select : "1" : select,
|
|
7857
|
+
tableHook: inCTE.returning?.hookSelect && {
|
|
7858
|
+
select: inCTE.returning?.hookSelect
|
|
7859
|
+
}
|
|
7860
|
+
};
|
|
7861
|
+
} else {
|
|
7862
|
+
return makeReturningSql(
|
|
7863
|
+
ctx,
|
|
7864
|
+
q,
|
|
7865
|
+
query,
|
|
7866
|
+
quotedAs,
|
|
7867
|
+
delayedRelationSelect,
|
|
7868
|
+
"Create",
|
|
7869
|
+
void 0,
|
|
7870
|
+
isSubSql
|
|
7871
|
+
);
|
|
7872
|
+
}
|
|
7873
|
+
};
|
|
6715
7874
|
const addWithSqls = (ctx, hasWith, withSqls, valuesPos, insertSql) => {
|
|
6716
7875
|
if (withSqls.length) {
|
|
6717
7876
|
if (hasWith) {
|
|
@@ -6877,7 +8036,7 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs, h
|
|
|
6877
8036
|
);
|
|
6878
8037
|
if (runtimeDefaults) {
|
|
6879
8038
|
for (const fn of runtimeDefaults) {
|
|
6880
|
-
arr.push(
|
|
8039
|
+
arr.push(addValue(values, fn()));
|
|
6881
8040
|
}
|
|
6882
8041
|
}
|
|
6883
8042
|
if (hookSetSql) arr.push(hookSetSql);
|
|
@@ -6885,36 +8044,35 @@ const encodeRow = (ctx, values, q, QueryClass, row, runtimeDefaults, quotedAs, h
|
|
|
6885
8044
|
};
|
|
6886
8045
|
const encodeValue = (ctx, values, q, QueryClass, value, quotedAs) => {
|
|
6887
8046
|
if (value && typeof value === "object") {
|
|
6888
|
-
if (value instanceof
|
|
8047
|
+
if (value instanceof Expression) {
|
|
6889
8048
|
return value.toSQL(ctx, quotedAs);
|
|
6890
8049
|
} else if (value instanceof QueryClass) {
|
|
6891
|
-
return `(${
|
|
8050
|
+
return `(${toSubSqlText(
|
|
8051
|
+
joinSubQuery(q, value),
|
|
8052
|
+
value.q.as,
|
|
8053
|
+
ctx
|
|
8054
|
+
)})`;
|
|
6892
8055
|
} else if ("fromHook" in value) {
|
|
6893
8056
|
return value.fromHook;
|
|
6894
8057
|
}
|
|
6895
8058
|
}
|
|
6896
|
-
return value === void 0 ? "DEFAULT" :
|
|
8059
|
+
return value === void 0 ? "DEFAULT" : addValue(values, value);
|
|
6897
8060
|
};
|
|
6898
|
-
const
|
|
6899
|
-
null,
|
|
6900
|
-
"afterUpdateSelect",
|
|
6901
|
-
"afterCreateSelect",
|
|
6902
|
-
"afterDeleteSelect"
|
|
6903
|
-
];
|
|
6904
|
-
const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSelectI, addHookSelectI) => {
|
|
8061
|
+
const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookPurpose, addHookPurpose, isSubSql) => {
|
|
6905
8062
|
if (data.inCTE) {
|
|
6906
|
-
if (
|
|
8063
|
+
if (hookPurpose !== "Create") {
|
|
6907
8064
|
const returning = makeReturningSql(
|
|
6908
8065
|
ctx,
|
|
6909
8066
|
q,
|
|
6910
8067
|
data,
|
|
6911
8068
|
quotedAs,
|
|
6912
8069
|
delayedRelationSelect,
|
|
6913
|
-
|
|
6914
|
-
|
|
8070
|
+
"Create",
|
|
8071
|
+
hookPurpose,
|
|
8072
|
+
isSubSql
|
|
6915
8073
|
);
|
|
6916
|
-
if (returning.
|
|
6917
|
-
for (const [key, value] of returning.
|
|
8074
|
+
if (returning.tableHook?.select) {
|
|
8075
|
+
for (const [key, value] of returning.tableHook.select) {
|
|
6918
8076
|
data.inCTE.targetHookSelect.set(key, value);
|
|
6919
8077
|
}
|
|
6920
8078
|
}
|
|
@@ -6924,12 +8082,18 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6924
8082
|
return data.inCTE.returning;
|
|
6925
8083
|
}
|
|
6926
8084
|
}
|
|
6927
|
-
const hookSelect =
|
|
8085
|
+
const hookSelect = hookPurpose && data[`after${hookPurpose}Select`];
|
|
6928
8086
|
const { select } = data;
|
|
6929
|
-
if (!q.q.hookSelect && !hookSelect?.size && !select?.length && !
|
|
6930
|
-
|
|
8087
|
+
if (!q.q.hookSelect && !hookSelect?.size && !select?.length && !hookPurpose) {
|
|
8088
|
+
const select2 = hookSelect && /* @__PURE__ */ new Map();
|
|
8089
|
+
return {
|
|
8090
|
+
select: !isSubSql && ctx.cteHooks?.hasSelect ? "NULL" : void 0,
|
|
8091
|
+
tableHook: select2 && {
|
|
8092
|
+
select: select2
|
|
8093
|
+
}
|
|
8094
|
+
};
|
|
6931
8095
|
}
|
|
6932
|
-
const otherCTEHookSelect =
|
|
8096
|
+
const otherCTEHookSelect = addHookPurpose && data[`after${addHookPurpose}Select`];
|
|
6933
8097
|
let tempSelect;
|
|
6934
8098
|
if (q.q.hookSelect || hookSelect || otherCTEHookSelect || q.q.selectRelation) {
|
|
6935
8099
|
tempSelect = new Map(q.q.hookSelect);
|
|
@@ -6944,7 +8108,7 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6944
8108
|
}
|
|
6945
8109
|
}
|
|
6946
8110
|
if (q.q.selectRelation) {
|
|
6947
|
-
for (const column of
|
|
8111
|
+
for (const column of getPrimaryKeys(q)) {
|
|
6948
8112
|
tempSelect.set(column, { select: column });
|
|
6949
8113
|
}
|
|
6950
8114
|
}
|
|
@@ -6958,18 +8122,25 @@ const makeReturningSql = (ctx, q, data, quotedAs, delayedRelationSelect, hookSel
|
|
|
6958
8122
|
quotedAs,
|
|
6959
8123
|
tempSelect,
|
|
6960
8124
|
void 0,
|
|
8125
|
+
void 0,
|
|
6961
8126
|
true,
|
|
6962
8127
|
void 0,
|
|
6963
8128
|
delayedRelationSelect
|
|
6964
8129
|
);
|
|
6965
8130
|
}
|
|
8131
|
+
const after = hookPurpose && data[`after${hookPurpose}`];
|
|
8132
|
+
const afterCommit = hookPurpose && data[`after${hookPurpose}Commit`];
|
|
6966
8133
|
return {
|
|
6967
|
-
select: sql,
|
|
6968
|
-
|
|
8134
|
+
select: !isSubSql && ctx.cteHooks?.hasSelect ? sql ? "NULL, " + sql : "NULL" : sql,
|
|
8135
|
+
tableHook: (tempSelect || after || afterCommit) && {
|
|
8136
|
+
select: tempSelect,
|
|
8137
|
+
after: data.after && after ? [...data.after, ...after] : after ? after : data.after,
|
|
8138
|
+
afterCommit
|
|
8139
|
+
}
|
|
6969
8140
|
};
|
|
6970
8141
|
};
|
|
6971
8142
|
|
|
6972
|
-
const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
|
|
8143
|
+
const pushSelectSql = (ctx, table, query, quotedAs, isSubSql, aliases) => {
|
|
6973
8144
|
if (query.selectCache) {
|
|
6974
8145
|
ctx.sql.push(query.selectCache.sql);
|
|
6975
8146
|
if (aliases) aliases.push(...query.selectCache.aliases);
|
|
@@ -6980,12 +8151,13 @@ const pushSelectSql = (ctx, table, query, quotedAs, aliases) => {
|
|
|
6980
8151
|
query,
|
|
6981
8152
|
quotedAs,
|
|
6982
8153
|
query.hookSelect,
|
|
8154
|
+
isSubSql,
|
|
6983
8155
|
aliases
|
|
6984
8156
|
);
|
|
6985
8157
|
if (sql) ctx.sql.push(sql);
|
|
6986
8158
|
}
|
|
6987
8159
|
};
|
|
6988
|
-
const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, aliases, skipCTE, jsonList, delayedRelationSelect) => {
|
|
8160
|
+
const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect, isSubSql, aliases, skipCTE, jsonList, delayedRelationSelect) => {
|
|
6989
8161
|
if (query.inCTE && !skipCTE) {
|
|
6990
8162
|
const { select } = makeReturningSql(
|
|
6991
8163
|
ctx,
|
|
@@ -7060,14 +8232,14 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7060
8232
|
}
|
|
7061
8233
|
const value = obj[as];
|
|
7062
8234
|
if (typeof value === "object") {
|
|
7063
|
-
if (
|
|
8235
|
+
if (isExpression(value)) {
|
|
7064
8236
|
list.push(`${value.toSQL(ctx, quotedAs)} "${as}"`);
|
|
7065
8237
|
if (jsonList) {
|
|
7066
8238
|
jsonList[as] = value.result.value;
|
|
7067
8239
|
}
|
|
7068
8240
|
aliases?.push(as);
|
|
7069
|
-
} else if (delayedRelationSelect &&
|
|
7070
|
-
|
|
8241
|
+
} else if (delayedRelationSelect && isRelationQuery(value)) {
|
|
8242
|
+
setDelayedRelation(delayedRelationSelect, as, value);
|
|
7071
8243
|
} else {
|
|
7072
8244
|
pushSubQuerySql(ctx, query, value, as, list, quotedAs, aliases);
|
|
7073
8245
|
if (jsonList) {
|
|
@@ -7132,7 +8304,7 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7132
8304
|
hookSelect.delete(column);
|
|
7133
8305
|
continue;
|
|
7134
8306
|
}
|
|
7135
|
-
name =
|
|
8307
|
+
name = getFreeAlias(selected, column);
|
|
7136
8308
|
item.as = name;
|
|
7137
8309
|
item.temp = name;
|
|
7138
8310
|
sql += ` "${name}"`;
|
|
@@ -7150,6 +8322,10 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
|
|
|
7150
8322
|
list.push(sql);
|
|
7151
8323
|
}
|
|
7152
8324
|
}
|
|
8325
|
+
if (!isSubSql && ctx.cteHooks?.hasSelect) {
|
|
8326
|
+
const count = ctx.selectedCount = list.length || query.selectAllColumns?.length || 0;
|
|
8327
|
+
return count ? (list.length ? list.join(", ") : selectAllSql(query, quotedAs, jsonList)) + ", NULL" : "";
|
|
8328
|
+
}
|
|
7153
8329
|
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs, jsonList);
|
|
7154
8330
|
};
|
|
7155
8331
|
const selectAllSql = (query, quotedAs, jsonList) => {
|
|
@@ -7181,7 +8357,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7181
8357
|
sql = `'[]'::json`;
|
|
7182
8358
|
break;
|
|
7183
8359
|
default:
|
|
7184
|
-
throw new
|
|
8360
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7185
8361
|
}
|
|
7186
8362
|
list.push(`${sql} "${as}"`);
|
|
7187
8363
|
aliases?.push(as);
|
|
@@ -7210,7 +8386,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7210
8386
|
case "void":
|
|
7211
8387
|
return;
|
|
7212
8388
|
default:
|
|
7213
|
-
throw new
|
|
8389
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7214
8390
|
}
|
|
7215
8391
|
if (sql) {
|
|
7216
8392
|
list.push(`${coalesce(ctx, query, sql, quotedAs)} "${as}"`);
|
|
@@ -7230,7 +8406,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7230
8406
|
if (!first && query.q.runtimeComputeds?.[as]) {
|
|
7231
8407
|
query = queryJson(query);
|
|
7232
8408
|
} else if (!first) {
|
|
7233
|
-
throw new
|
|
8409
|
+
throw new OrchidOrmInternalError(
|
|
7234
8410
|
query,
|
|
7235
8411
|
`Nothing was selected for pluck`
|
|
7236
8412
|
);
|
|
@@ -7252,7 +8428,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7252
8428
|
case "void":
|
|
7253
8429
|
break;
|
|
7254
8430
|
default:
|
|
7255
|
-
throw new
|
|
8431
|
+
throw new UnhandledTypeError(query, returnType);
|
|
7256
8432
|
}
|
|
7257
8433
|
list.push(
|
|
7258
8434
|
`${coalesce(
|
|
@@ -7266,7 +8442,7 @@ const pushSubQuerySql = (ctx, mainQuery, query, as, list, quotedAs, aliases) =>
|
|
|
7266
8442
|
const coalesce = (ctx, query, sql, quotedAs) => {
|
|
7267
8443
|
const { coalesceValue } = query.q;
|
|
7268
8444
|
if (coalesceValue !== void 0) {
|
|
7269
|
-
const value =
|
|
8445
|
+
const value = isExpression(coalesceValue) ? coalesceValue.toSQL(ctx, quotedAs) : addValue(ctx.values, coalesceValue);
|
|
7270
8446
|
return `COALESCE(${sql}, ${value})`;
|
|
7271
8447
|
}
|
|
7272
8448
|
return sql;
|
|
@@ -7281,7 +8457,7 @@ const orderByToSql = (ctx, data, order, quotedAs) => {
|
|
|
7281
8457
|
if (typeof order === "string") {
|
|
7282
8458
|
return addOrder(ctx, data, order, quotedAs);
|
|
7283
8459
|
}
|
|
7284
|
-
if (
|
|
8460
|
+
if (isExpression(order)) {
|
|
7285
8461
|
return order.toSQL(ctx, quotedAs);
|
|
7286
8462
|
}
|
|
7287
8463
|
const sql = [];
|
|
@@ -7294,15 +8470,15 @@ const orderByToSql = (ctx, data, order, quotedAs) => {
|
|
|
7294
8470
|
const addOrder = (ctx, data, column, quotedAs, dir) => {
|
|
7295
8471
|
if (data.sources?.[column]) {
|
|
7296
8472
|
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 ? `${
|
|
8473
|
+
const order = dir || (!search.order || search.order === true ? emptyObject : search.order);
|
|
8474
|
+
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
8475
|
}
|
|
7300
8476
|
return `${maybeSelectedColumnToSql(ctx, data, column, quotedAs)} ${dir || "ASC"}`;
|
|
7301
8477
|
};
|
|
7302
8478
|
|
|
7303
8479
|
const windowToSql = (ctx, data, window, quotedAs) => {
|
|
7304
8480
|
if (typeof window === "string") return `"${window}"`;
|
|
7305
|
-
if (
|
|
8481
|
+
if (isExpression(window)) return `(${window.toSQL(ctx, quotedAs)})`;
|
|
7306
8482
|
const sql = [];
|
|
7307
8483
|
if (window.partitionBy) {
|
|
7308
8484
|
sql.push(
|
|
@@ -7412,7 +8588,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
7412
8588
|
let only;
|
|
7413
8589
|
let sql;
|
|
7414
8590
|
if (typeof from === "object") {
|
|
7415
|
-
if (
|
|
8591
|
+
if (isExpression(from)) {
|
|
7416
8592
|
sql = from.toSQL(ctx, quotedAs) + " " + quotedAs;
|
|
7417
8593
|
} else {
|
|
7418
8594
|
only = from.q.only;
|
|
@@ -7439,7 +8615,7 @@ const fromToSql = (ctx, data, from, quotedAs) => {
|
|
|
7439
8615
|
return (only === void 0 ? data.only : only) ? `ONLY ${sql}` : sql;
|
|
7440
8616
|
};
|
|
7441
8617
|
const getSearchLang = (ctx, data, source, quotedAs) => {
|
|
7442
|
-
return source.langSQL ?? (source.langSQL = "languageColumn" in source ? columnToSql(ctx, data, data.shape, source.languageColumn, quotedAs) :
|
|
8618
|
+
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
8619
|
};
|
|
7444
8620
|
const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
7445
8621
|
let sql = source.textSQL;
|
|
@@ -7464,7 +8640,7 @@ const getSearchText = (ctx, data, source, quotedAs, forHeadline) => {
|
|
|
7464
8640
|
sql = columnToSql(ctx, data, data.shape, source.vector, quotedAs);
|
|
7465
8641
|
} else {
|
|
7466
8642
|
if (typeof source.text === "string") {
|
|
7467
|
-
sql =
|
|
8643
|
+
sql = addValue(ctx.values, source.text);
|
|
7468
8644
|
} else {
|
|
7469
8645
|
sql = source.text.toSQL(ctx, quotedAs);
|
|
7470
8646
|
}
|
|
@@ -7480,7 +8656,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
7480
8656
|
let tsVector = "";
|
|
7481
8657
|
let i = 0;
|
|
7482
8658
|
for (const key in source.in) {
|
|
7483
|
-
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${
|
|
8659
|
+
tsVector = (tsVector ? `${tsVector} || ` : "") + `setweight(to_tsvector(${lang}, ${text[i++]}), ${addValue(
|
|
7484
8660
|
ctx.values,
|
|
7485
8661
|
source.in[key]
|
|
7486
8662
|
)})`;
|
|
@@ -7494,7 +8670,7 @@ const getTsVector = (ctx, data, lang, source, quotedAs) => {
|
|
|
7494
8670
|
}
|
|
7495
8671
|
};
|
|
7496
8672
|
|
|
7497
|
-
const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
8673
|
+
const pushUpdateSql = (ctx, table, query, quotedAs, isSubSql) => {
|
|
7498
8674
|
const quotedTable = quoteSchemaAndTable(
|
|
7499
8675
|
query.schema,
|
|
7500
8676
|
table.table || query.from
|
|
@@ -7506,26 +8682,27 @@ const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
|
7506
8682
|
Object.assign(hookSet, item);
|
|
7507
8683
|
}
|
|
7508
8684
|
} else {
|
|
7509
|
-
hookSet =
|
|
8685
|
+
hookSet = emptyObject;
|
|
7510
8686
|
}
|
|
7511
8687
|
const set = [];
|
|
7512
8688
|
processData(ctx, table, set, query.updateData, hookSet, quotedAs);
|
|
7513
8689
|
if (query.hookUpdateSet) {
|
|
7514
|
-
applySet(ctx, table, set, hookSet,
|
|
8690
|
+
applySet(ctx, table, set, hookSet, emptyObject, quotedAs);
|
|
7515
8691
|
}
|
|
7516
|
-
let
|
|
7517
|
-
const delayedRelationSelect = query.selectRelation ?
|
|
8692
|
+
let tableHook;
|
|
8693
|
+
const delayedRelationSelect = query.selectRelation ? newDelayedRelationSelect(table) : void 0;
|
|
7518
8694
|
if (!set.length) {
|
|
7519
8695
|
if (!query.select) {
|
|
7520
8696
|
query.select = countSelect;
|
|
7521
8697
|
}
|
|
7522
|
-
|
|
8698
|
+
tableHook = pushUpdateReturning(
|
|
7523
8699
|
ctx,
|
|
7524
8700
|
table,
|
|
7525
8701
|
query,
|
|
7526
8702
|
quotedAs,
|
|
7527
8703
|
"SELECT",
|
|
7528
|
-
delayedRelationSelect
|
|
8704
|
+
delayedRelationSelect,
|
|
8705
|
+
isSubSql
|
|
7529
8706
|
);
|
|
7530
8707
|
ctx.sql.push(`FROM ${quotedTable}`);
|
|
7531
8708
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
@@ -7576,43 +8753,45 @@ const pushUpdateSql = (ctx, table, query, quotedAs) => {
|
|
|
7576
8753
|
if (whereSql) {
|
|
7577
8754
|
ctx.sql.push("WHERE", whereSql);
|
|
7578
8755
|
}
|
|
7579
|
-
|
|
8756
|
+
tableHook = pushUpdateReturning(
|
|
7580
8757
|
ctx,
|
|
7581
8758
|
table,
|
|
7582
8759
|
query,
|
|
7583
8760
|
quotedAs,
|
|
7584
8761
|
"RETURNING",
|
|
7585
|
-
delayedRelationSelect
|
|
8762
|
+
delayedRelationSelect,
|
|
8763
|
+
isSubSql
|
|
7586
8764
|
);
|
|
7587
8765
|
}
|
|
7588
8766
|
return {
|
|
7589
|
-
|
|
8767
|
+
tableHook,
|
|
7590
8768
|
delayedRelationSelect,
|
|
7591
8769
|
text: ctx.sql.join(" "),
|
|
7592
8770
|
values: ctx.values
|
|
7593
8771
|
};
|
|
7594
8772
|
};
|
|
7595
|
-
const pushUpdateReturning = (ctx, table, query, quotedAs, keyword, delayedRelationSelect) => {
|
|
8773
|
+
const pushUpdateReturning = (ctx, table, query, quotedAs, keyword, delayedRelationSelect, isSubSql) => {
|
|
7596
8774
|
const { inCTE } = query;
|
|
7597
|
-
const { select,
|
|
8775
|
+
const { select, tableHook } = makeReturningSql(
|
|
7598
8776
|
ctx,
|
|
7599
8777
|
table,
|
|
7600
8778
|
query,
|
|
7601
8779
|
quotedAs,
|
|
7602
8780
|
delayedRelationSelect,
|
|
7603
|
-
|
|
7604
|
-
inCTE &&
|
|
8781
|
+
"Update",
|
|
8782
|
+
inCTE && "Create",
|
|
8783
|
+
isSubSql
|
|
7605
8784
|
);
|
|
7606
8785
|
const s = inCTE && (inCTE.selectNum || !select) ? select ? "0, " + select : "0" : select;
|
|
7607
8786
|
if (s) ctx.sql.push(keyword, s);
|
|
7608
|
-
return
|
|
8787
|
+
return tableHook;
|
|
7609
8788
|
};
|
|
7610
8789
|
const processData = (ctx, table, set, data, hookSet, quotedAs) => {
|
|
7611
8790
|
let append;
|
|
7612
8791
|
for (const item of data) {
|
|
7613
8792
|
if (typeof item === "function") {
|
|
7614
8793
|
const result = item(data);
|
|
7615
|
-
if (result) append =
|
|
8794
|
+
if (result) append = pushOrNewArray(append, result);
|
|
7616
8795
|
} else {
|
|
7617
8796
|
applySet(ctx, table, set, item, hookSet, quotedAs);
|
|
7618
8797
|
}
|
|
@@ -7639,7 +8818,7 @@ const applySet = (ctx, table, set, item, hookSet, quotedAs) => {
|
|
|
7639
8818
|
};
|
|
7640
8819
|
const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
7641
8820
|
if (value && typeof value === "object") {
|
|
7642
|
-
if (
|
|
8821
|
+
if (isExpression(value)) {
|
|
7643
8822
|
return value.toSQL(ctx, quotedAs);
|
|
7644
8823
|
} else if (value instanceof QueryClass) {
|
|
7645
8824
|
if (value.q.subQuery === 1) {
|
|
@@ -7649,13 +8828,13 @@ const processValue = (ctx, table, QueryClass, key, value, quotedAs) => {
|
|
|
7649
8828
|
joinSubQuery(table, value).toSQL(ctx)
|
|
7650
8829
|
)})`;
|
|
7651
8830
|
} else if ("op" in value && "arg" in value) {
|
|
7652
|
-
return `"${table.q.shape[key].data.name || key}" ${value.op} ${
|
|
8831
|
+
return `"${table.q.shape[key].data.name || key}" ${value.op} ${addValue(ctx.values, value.arg)}`;
|
|
7653
8832
|
}
|
|
7654
8833
|
}
|
|
7655
|
-
return
|
|
8834
|
+
return addValue(ctx.values, value);
|
|
7656
8835
|
};
|
|
7657
8836
|
|
|
7658
|
-
const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
8837
|
+
const pushDeleteSql = (ctx, table, query, quotedAs, isSubSql) => {
|
|
7659
8838
|
const from = `"${table.table || query.from}"`;
|
|
7660
8839
|
ctx.sql.push(`DELETE FROM ${from}`);
|
|
7661
8840
|
if (from !== quotedAs) {
|
|
@@ -7669,10 +8848,10 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
7669
8848
|
for (const item of query.join) {
|
|
7670
8849
|
const lateral = "l" in item.args && item.args.l;
|
|
7671
8850
|
if (lateral) {
|
|
7672
|
-
if (
|
|
8851
|
+
if (isRelationQuery(lateral)) {
|
|
7673
8852
|
continue;
|
|
7674
8853
|
}
|
|
7675
|
-
throw new
|
|
8854
|
+
throw new OrchidOrmInternalError(
|
|
7676
8855
|
table,
|
|
7677
8856
|
"Join lateral is not supported in delete"
|
|
7678
8857
|
);
|
|
@@ -7699,18 +8878,20 @@ const pushDeleteSql = (ctx, table, query, quotedAs) => {
|
|
|
7699
8878
|
ctx.sql.push("WHERE", conditions);
|
|
7700
8879
|
}
|
|
7701
8880
|
}
|
|
7702
|
-
const delayedRelationSelect = query.selectRelation ?
|
|
8881
|
+
const delayedRelationSelect = query.selectRelation ? newDelayedRelationSelect(table) : void 0;
|
|
7703
8882
|
const returning = makeReturningSql(
|
|
7704
8883
|
ctx,
|
|
7705
8884
|
table,
|
|
7706
8885
|
query,
|
|
7707
8886
|
quotedAs,
|
|
7708
8887
|
delayedRelationSelect,
|
|
7709
|
-
|
|
8888
|
+
"Delete",
|
|
8889
|
+
void 0,
|
|
8890
|
+
isSubSql
|
|
7710
8891
|
);
|
|
7711
8892
|
if (returning.select) ctx.sql.push("RETURNING", returning.select);
|
|
7712
8893
|
return {
|
|
7713
|
-
|
|
8894
|
+
tableHook: returning.tableHook,
|
|
7714
8895
|
delayedRelationSelect,
|
|
7715
8896
|
text: ctx.sql.join(" "),
|
|
7716
8897
|
values: ctx.values
|
|
@@ -7725,14 +8906,14 @@ const pushTruncateSql = (ctx, table, query) => {
|
|
|
7725
8906
|
|
|
7726
8907
|
const pushColumnInfoSql = (ctx, table, query) => {
|
|
7727
8908
|
ctx.sql.push(
|
|
7728
|
-
`SELECT * FROM information_schema.columns WHERE table_name = ${
|
|
8909
|
+
`SELECT * FROM information_schema.columns WHERE table_name = ${addValue(
|
|
7729
8910
|
ctx.values,
|
|
7730
8911
|
table.table
|
|
7731
8912
|
)} AND table_catalog = current_database() AND table_schema = ${query.schema || "current_schema()"}`
|
|
7732
8913
|
);
|
|
7733
8914
|
if (query.column) {
|
|
7734
8915
|
ctx.sql.push(
|
|
7735
|
-
`AND column_name = ${
|
|
8916
|
+
`AND column_name = ${addValue(
|
|
7736
8917
|
ctx.values,
|
|
7737
8918
|
table.q.shape[query.column]?.data.name || query.column
|
|
7738
8919
|
)}`
|
|
@@ -7776,7 +8957,34 @@ const pushCopySql = (ctx, table, query, quotedAs) => {
|
|
|
7776
8957
|
pushWhereStatementSql(ctx, table, query, quotedAs);
|
|
7777
8958
|
};
|
|
7778
8959
|
|
|
7779
|
-
const
|
|
8960
|
+
const toSubSqlText = (q, cteName, options) => getSqlText(subToSql(q, cteName, options));
|
|
8961
|
+
const subToSql = (q, cteName, options) => {
|
|
8962
|
+
var _a;
|
|
8963
|
+
const sql = toSQL(q, options, true);
|
|
8964
|
+
if (sql.tableHook && (sql.tableHook.after || sql.tableHook.afterCommit) && !q.q.inCTE) {
|
|
8965
|
+
const shape = {};
|
|
8966
|
+
if (sql.tableHook.select) {
|
|
8967
|
+
for (const key of sql.tableHook.select.keys()) {
|
|
8968
|
+
shape[key] = q.shape[key];
|
|
8969
|
+
}
|
|
8970
|
+
}
|
|
8971
|
+
const item = {
|
|
8972
|
+
shape,
|
|
8973
|
+
tableHook: sql.tableHook
|
|
8974
|
+
};
|
|
8975
|
+
if (options.cteHooks) {
|
|
8976
|
+
if (sql.tableHook.select) options.cteHooks.hasSelect = true;
|
|
8977
|
+
(_a = options.cteHooks.tableHooks)[cteName] ?? (_a[cteName] = item);
|
|
8978
|
+
} else {
|
|
8979
|
+
options.cteHooks = {
|
|
8980
|
+
hasSelect: !!sql.tableHook.select,
|
|
8981
|
+
tableHooks: { [cteName]: item }
|
|
8982
|
+
};
|
|
8983
|
+
}
|
|
8984
|
+
}
|
|
8985
|
+
return sql;
|
|
8986
|
+
};
|
|
8987
|
+
const toSQL = (table, options, isSubSql) => {
|
|
7780
8988
|
const query = table.q;
|
|
7781
8989
|
const sql = [];
|
|
7782
8990
|
const values = options?.values || [];
|
|
@@ -7787,7 +8995,8 @@ const toSQL = (table, options) => {
|
|
|
7787
8995
|
values,
|
|
7788
8996
|
aliasValue: options?.aliasValue,
|
|
7789
8997
|
skipBatchCheck: options?.skipBatchCheck,
|
|
7790
|
-
hasNonSelect: options?.hasNonSelect
|
|
8998
|
+
hasNonSelect: options?.hasNonSelect,
|
|
8999
|
+
cteHooks: options?.cteHooks
|
|
7791
9000
|
};
|
|
7792
9001
|
if (query.with) {
|
|
7793
9002
|
pushWithSql(ctx, query.with);
|
|
@@ -7806,11 +9015,11 @@ const toSQL = (table, options) => {
|
|
|
7806
9015
|
} else {
|
|
7807
9016
|
const quotedAs = `"${query.as || tableName}"`;
|
|
7808
9017
|
if (query.type === "insert") {
|
|
7809
|
-
result = makeInsertSql(ctx, table, query, `"${tableName}"
|
|
9018
|
+
result = makeInsertSql(ctx, table, query, `"${tableName}"`, isSubSql);
|
|
7810
9019
|
} else if (query.type === "update") {
|
|
7811
|
-
result = pushUpdateSql(ctx, table, query, quotedAs);
|
|
9020
|
+
result = pushUpdateSql(ctx, table, query, quotedAs, isSubSql);
|
|
7812
9021
|
} else if (query.type === "delete") {
|
|
7813
|
-
result = pushDeleteSql(ctx, table, query, quotedAs);
|
|
9022
|
+
result = pushDeleteSql(ctx, table, query, quotedAs, isSubSql);
|
|
7814
9023
|
} else if (query.type === "copy") {
|
|
7815
9024
|
pushCopySql(ctx, table, query, quotedAs);
|
|
7816
9025
|
result = { text: sql.join(" "), values };
|
|
@@ -7826,7 +9035,7 @@ const toSQL = (table, options) => {
|
|
|
7826
9035
|
const s = getSqlText(firstSql);
|
|
7827
9036
|
sql.push(query.union.p ? s : `(${s})`);
|
|
7828
9037
|
for (const u of query.union.u) {
|
|
7829
|
-
const s2 =
|
|
9038
|
+
const s2 = isExpression(u.a) ? u.a.toSQL(ctx, quotedAs) : getSqlText(toSQL(u.a, ctx));
|
|
7830
9039
|
sql.push(`${u.k} ${u.p ? s2 : "(" + s2 + ")"}`);
|
|
7831
9040
|
}
|
|
7832
9041
|
} else {
|
|
@@ -7835,7 +9044,7 @@ const toSQL = (table, options) => {
|
|
|
7835
9044
|
pushDistinctSql(ctx, table, query.distinct, quotedAs);
|
|
7836
9045
|
}
|
|
7837
9046
|
const aliases = query.group ? [] : void 0;
|
|
7838
|
-
pushSelectSql(ctx, table, query, quotedAs, aliases);
|
|
9047
|
+
pushSelectSql(ctx, table, query, quotedAs, isSubSql, aliases);
|
|
7839
9048
|
fromQuery = (table.table || query.from) && pushFromAndAs(ctx, table, query, quotedAs) || void 0;
|
|
7840
9049
|
if (query.join) {
|
|
7841
9050
|
pushJoinSql(
|
|
@@ -7850,7 +9059,7 @@ const toSQL = (table, options) => {
|
|
|
7850
9059
|
}
|
|
7851
9060
|
if (query.group) {
|
|
7852
9061
|
const group = query.group.map((item) => {
|
|
7853
|
-
if (
|
|
9062
|
+
if (isExpression(item)) {
|
|
7854
9063
|
return item.toSQL(ctx, quotedAs);
|
|
7855
9064
|
} else {
|
|
7856
9065
|
const i = aliases.indexOf(item);
|
|
@@ -7884,15 +9093,15 @@ const toSQL = (table, options) => {
|
|
|
7884
9093
|
if (query.useFromLimitOffset) {
|
|
7885
9094
|
const q = fromQuery?.q;
|
|
7886
9095
|
if (q.limit) {
|
|
7887
|
-
sql.push(`LIMIT ${
|
|
9096
|
+
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
7888
9097
|
}
|
|
7889
9098
|
if (q.offset) {
|
|
7890
|
-
sql.push(`OFFSET ${
|
|
9099
|
+
sql.push(`OFFSET ${addValue(values, q.offset)}`);
|
|
7891
9100
|
}
|
|
7892
9101
|
} else {
|
|
7893
9102
|
pushLimitSQL(sql, values, query);
|
|
7894
9103
|
if (query.offset && !query.returnsOne) {
|
|
7895
|
-
sql.push(`OFFSET ${
|
|
9104
|
+
sql.push(`OFFSET ${addValue(values, query.offset)}`);
|
|
7896
9105
|
}
|
|
7897
9106
|
}
|
|
7898
9107
|
if (query.for) {
|
|
@@ -7901,7 +9110,7 @@ const toSQL = (table, options) => {
|
|
|
7901
9110
|
if (tableNames) {
|
|
7902
9111
|
sql.push(
|
|
7903
9112
|
"OF",
|
|
7904
|
-
|
|
9113
|
+
isExpression(tableNames) ? tableNames.toSQL(ctx, quotedAs) : tableNames.map((x) => `"${x}"`).join(", ")
|
|
7905
9114
|
);
|
|
7906
9115
|
}
|
|
7907
9116
|
if (query.for.mode) sql.push(query.for.mode);
|
|
@@ -7909,13 +9118,29 @@ const toSQL = (table, options) => {
|
|
|
7909
9118
|
result = {
|
|
7910
9119
|
text: sql.join(" "),
|
|
7911
9120
|
values,
|
|
7912
|
-
|
|
9121
|
+
tableHook: query.hookSelect && {
|
|
9122
|
+
select: query.hookSelect
|
|
9123
|
+
},
|
|
7913
9124
|
delayedRelationSelect: ctx.delayedRelationSelect
|
|
7914
9125
|
};
|
|
7915
9126
|
}
|
|
7916
9127
|
if (options && (query.type || ctx.hasNonSelect)) {
|
|
7917
9128
|
options.hasNonSelect = true;
|
|
7918
9129
|
}
|
|
9130
|
+
if (!isSubSql && ctx.cteHooks && "text" in result) {
|
|
9131
|
+
result.cteHooks = ctx.cteHooks;
|
|
9132
|
+
if (ctx.cteHooks.hasSelect) {
|
|
9133
|
+
result.text += ` UNION ALL SELECT ${"NULL, ".repeat(
|
|
9134
|
+
ctx.selectedCount || 0
|
|
9135
|
+
)}json_build_object(${Object.entries(ctx.cteHooks.tableHooks).map(
|
|
9136
|
+
([cteName, data]) => `'${cteName}', (SELECT json_agg(${makeRowToJson(
|
|
9137
|
+
cteName,
|
|
9138
|
+
data.shape,
|
|
9139
|
+
false
|
|
9140
|
+
)}) FROM "${cteName}")`
|
|
9141
|
+
).join(", ")})`;
|
|
9142
|
+
}
|
|
9143
|
+
}
|
|
7919
9144
|
return result;
|
|
7920
9145
|
};
|
|
7921
9146
|
function pushLimitSQL(sql, values, q) {
|
|
@@ -7923,12 +9148,12 @@ function pushLimitSQL(sql, values, q) {
|
|
|
7923
9148
|
if (queryTypeWithLimitOne[q.returnType] && !q.returning) {
|
|
7924
9149
|
sql.push(`LIMIT 1`);
|
|
7925
9150
|
} else if (q.limit) {
|
|
7926
|
-
sql.push(`LIMIT ${
|
|
9151
|
+
sql.push(`LIMIT ${addValue(values, q.limit)}`);
|
|
7927
9152
|
}
|
|
7928
9153
|
}
|
|
7929
9154
|
}
|
|
7930
9155
|
|
|
7931
|
-
class FnExpression extends
|
|
9156
|
+
class FnExpression extends Expression {
|
|
7932
9157
|
/**
|
|
7933
9158
|
* @param query - query object.
|
|
7934
9159
|
* @param fn - SQL function name.
|
|
@@ -7936,7 +9161,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7936
9161
|
* @param options - aggregate options.
|
|
7937
9162
|
* @param value - column type of the function result.
|
|
7938
9163
|
*/
|
|
7939
|
-
constructor(query, fn, args, options =
|
|
9164
|
+
constructor(query, fn, args, options = emptyObject, value) {
|
|
7940
9165
|
super();
|
|
7941
9166
|
this.query = query;
|
|
7942
9167
|
this.fn = fn;
|
|
@@ -7949,7 +9174,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7949
9174
|
query.q.returnsOne = true;
|
|
7950
9175
|
query.q.getColumn = value;
|
|
7951
9176
|
query.q.select = [this];
|
|
7952
|
-
addColumnParserToQuery(query.q,
|
|
9177
|
+
addColumnParserToQuery(query.q, getValueKey, value);
|
|
7953
9178
|
}
|
|
7954
9179
|
// Builds function SQL.
|
|
7955
9180
|
makeSQL(ctx, quotedAs) {
|
|
@@ -7961,7 +9186,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7961
9186
|
this.args.map((arg) => {
|
|
7962
9187
|
if (typeof arg === "string") {
|
|
7963
9188
|
return arg === "*" ? "*" : columnToSql(ctx, this.q, this.q.shape, arg, quotedAs);
|
|
7964
|
-
} else if (arg instanceof
|
|
9189
|
+
} else if (arg instanceof Expression) {
|
|
7965
9190
|
return arg.toSQL(ctx, quotedAs);
|
|
7966
9191
|
} else if ("pairs" in arg) {
|
|
7967
9192
|
const args = [];
|
|
@@ -7969,7 +9194,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7969
9194
|
for (const key in pairs) {
|
|
7970
9195
|
args.push(
|
|
7971
9196
|
// ::text is needed to bypass "could not determine data type of parameter" postgres error
|
|
7972
|
-
`${
|
|
9197
|
+
`${addValue(values, key)}::text, ${rawOrColumnToSql(
|
|
7973
9198
|
ctx,
|
|
7974
9199
|
this.q,
|
|
7975
9200
|
pairs[key],
|
|
@@ -7979,7 +9204,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7979
9204
|
}
|
|
7980
9205
|
return args.join(", ");
|
|
7981
9206
|
} else {
|
|
7982
|
-
return
|
|
9207
|
+
return addValue(values, arg.value);
|
|
7983
9208
|
}
|
|
7984
9209
|
}).join(", ")
|
|
7985
9210
|
);
|
|
@@ -7990,7 +9215,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
7990
9215
|
{ ...ctx, sql },
|
|
7991
9216
|
this.q,
|
|
7992
9217
|
quotedAs,
|
|
7993
|
-
|
|
9218
|
+
toArray(options.order)
|
|
7994
9219
|
);
|
|
7995
9220
|
}
|
|
7996
9221
|
sql.push(")");
|
|
@@ -8020,7 +9245,7 @@ class FnExpression extends orchidCore.Expression {
|
|
|
8020
9245
|
}
|
|
8021
9246
|
function makeFnExpression(self, type, fn, args, options) {
|
|
8022
9247
|
const q = extendQuery(self, type.operators);
|
|
8023
|
-
q.baseQuery.type =
|
|
9248
|
+
q.baseQuery.type = ExpressionTypeMethod.prototype.type;
|
|
8024
9249
|
new FnExpression(
|
|
8025
9250
|
q,
|
|
8026
9251
|
fn,
|
|
@@ -8512,7 +9737,7 @@ class AggregateMethods {
|
|
|
8512
9737
|
* @param over - OVER clause config
|
|
8513
9738
|
*/
|
|
8514
9739
|
rowNumber(over) {
|
|
8515
|
-
return makeFnExpression(this, intNullable, "row_number",
|
|
9740
|
+
return makeFnExpression(this, intNullable, "row_number", emptyArray, {
|
|
8516
9741
|
over
|
|
8517
9742
|
});
|
|
8518
9743
|
}
|
|
@@ -8535,7 +9760,7 @@ class AggregateMethods {
|
|
|
8535
9760
|
* @param over - OVER clause config
|
|
8536
9761
|
*/
|
|
8537
9762
|
rank(over) {
|
|
8538
|
-
return makeFnExpression(this, intNullable, "rank",
|
|
9763
|
+
return makeFnExpression(this, intNullable, "rank", emptyArray, {
|
|
8539
9764
|
over
|
|
8540
9765
|
});
|
|
8541
9766
|
}
|
|
@@ -8558,7 +9783,7 @@ class AggregateMethods {
|
|
|
8558
9783
|
* @param over - OVER clause config
|
|
8559
9784
|
*/
|
|
8560
9785
|
denseRank(over) {
|
|
8561
|
-
return makeFnExpression(this, intNullable, "dense_rank",
|
|
9786
|
+
return makeFnExpression(this, intNullable, "dense_rank", emptyArray, {
|
|
8562
9787
|
over
|
|
8563
9788
|
});
|
|
8564
9789
|
}
|
|
@@ -8581,7 +9806,7 @@ class AggregateMethods {
|
|
|
8581
9806
|
* @param over - OVER clause config
|
|
8582
9807
|
*/
|
|
8583
9808
|
percentRank(over) {
|
|
8584
|
-
return makeFnExpression(this, intNullable, "percent_rank",
|
|
9809
|
+
return makeFnExpression(this, intNullable, "percent_rank", emptyArray, {
|
|
8585
9810
|
over
|
|
8586
9811
|
});
|
|
8587
9812
|
}
|
|
@@ -8604,7 +9829,7 @@ class AggregateMethods {
|
|
|
8604
9829
|
* @param over - OVER clause config
|
|
8605
9830
|
*/
|
|
8606
9831
|
cumeDist(over) {
|
|
8607
|
-
return makeFnExpression(this, floatNullable, "cume_dist",
|
|
9832
|
+
return makeFnExpression(this, floatNullable, "cume_dist", emptyArray, {
|
|
8608
9833
|
over
|
|
8609
9834
|
});
|
|
8610
9835
|
}
|
|
@@ -8624,7 +9849,7 @@ class QueryAsMethods {
|
|
|
8624
9849
|
* @param as - alias for the table of this query
|
|
8625
9850
|
*/
|
|
8626
9851
|
as(as) {
|
|
8627
|
-
return
|
|
9852
|
+
return _setQueryAs(_clone(this), as);
|
|
8628
9853
|
}
|
|
8629
9854
|
}
|
|
8630
9855
|
|
|
@@ -8638,7 +9863,7 @@ class Clear {
|
|
|
8638
9863
|
} else if (clear === "counters") {
|
|
8639
9864
|
if ("type" in q.q && q.q.type === "update") {
|
|
8640
9865
|
q.q.updateData = q.q.updateData.filter((item) => {
|
|
8641
|
-
if (!
|
|
9866
|
+
if (!isExpression(item) && typeof item !== "function") {
|
|
8642
9867
|
let removed = false;
|
|
8643
9868
|
for (const key in item) {
|
|
8644
9869
|
const value = item[key];
|
|
@@ -8813,7 +10038,7 @@ const _addWith = (query, withStore, item, key = "with") => {
|
|
|
8813
10038
|
if (item.q) {
|
|
8814
10039
|
item.q.q.with?.forEach((item2, i, arr) => {
|
|
8815
10040
|
if (item2?.q?.q.type) {
|
|
8816
|
-
|
|
10041
|
+
pushOrNewArrayToObjectImmutable(withStore, key, item2);
|
|
8817
10042
|
arr[i] = void 0;
|
|
8818
10043
|
}
|
|
8819
10044
|
});
|
|
@@ -8824,7 +10049,7 @@ const _addWith = (query, withStore, item, key = "with") => {
|
|
|
8824
10049
|
q.with = q.with ? [...q.with, ...values] : values;
|
|
8825
10050
|
}
|
|
8826
10051
|
}
|
|
8827
|
-
|
|
10052
|
+
pushOrNewArrayToObjectImmutable(withStore, key, item);
|
|
8828
10053
|
};
|
|
8829
10054
|
const moveQueryValueToWith = (q, withStore, value, withKey, set, key) => {
|
|
8830
10055
|
if (value.q.type) {
|
|
@@ -8839,7 +10064,10 @@ const moveQueryValueToWith = (q, withStore, value, withKey, set, key) => {
|
|
|
8839
10064
|
withKey
|
|
8840
10065
|
);
|
|
8841
10066
|
if (set) {
|
|
8842
|
-
|
|
10067
|
+
const sub = _clone(value.baseQuery);
|
|
10068
|
+
sub.q.select = value.q.select;
|
|
10069
|
+
sub.q.as = sub.q.from = as;
|
|
10070
|
+
set[key] = sub;
|
|
8843
10071
|
}
|
|
8844
10072
|
return as;
|
|
8845
10073
|
}
|
|
@@ -8897,7 +10125,7 @@ class WithMethods {
|
|
|
8897
10125
|
const q = _clone(this);
|
|
8898
10126
|
const [options, shapeFn, sql] = args.length === 2 ? [void 0, args[0], args[1]] : args;
|
|
8899
10127
|
const shape = shapeFn(this.columnTypes);
|
|
8900
|
-
|
|
10128
|
+
pushQueryValueImmutable(q, "with", {
|
|
8901
10129
|
n: name,
|
|
8902
10130
|
o: { ...options, columns: Object.keys(shape) },
|
|
8903
10131
|
s: sql(q)
|
|
@@ -9199,7 +10427,7 @@ const processCreateItem = (q, item, rowIndex, ctx, encoders) => {
|
|
|
9199
10427
|
};
|
|
9200
10428
|
const throwOnReadOnly$1 = (q, column, key) => {
|
|
9201
10429
|
if (column.data.appReadOnly || column.data.readOnly) {
|
|
9202
|
-
throw new
|
|
10430
|
+
throw new OrchidOrmInternalError(
|
|
9203
10431
|
q,
|
|
9204
10432
|
"Trying to insert a readonly column",
|
|
9205
10433
|
{ column: key }
|
|
@@ -9222,7 +10450,7 @@ const handleOneData = (q, data, ctx) => {
|
|
|
9222
10450
|
columns.map(
|
|
9223
10451
|
(key) => (
|
|
9224
10452
|
// undefined values were stripped and no need to check for them
|
|
9225
|
-
encoders[key] && !
|
|
10453
|
+
encoders[key] && !isExpression(data[key]) && data[key] !== null ? encoders[key](data[key]) : data[key]
|
|
9226
10454
|
)
|
|
9227
10455
|
)
|
|
9228
10456
|
];
|
|
@@ -9241,7 +10469,7 @@ const handleManyData = (q, data, ctx) => {
|
|
|
9241
10469
|
const columns = Array.from(ctx.columns.keys());
|
|
9242
10470
|
data.forEach((item, i) => {
|
|
9243
10471
|
values[i] = columns.map(
|
|
9244
|
-
(key) => encoders[key] && item[key] !== void 0 && !
|
|
10472
|
+
(key) => encoders[key] && item[key] !== void 0 && !isExpression(item[key]) ? encoders[key](item[key]) : item[key]
|
|
9245
10473
|
);
|
|
9246
10474
|
});
|
|
9247
10475
|
return { columns, values };
|
|
@@ -9276,8 +10504,8 @@ const insert = (self, {
|
|
|
9276
10504
|
q.queryColumnsCount = obj.queryColumnsCount;
|
|
9277
10505
|
}
|
|
9278
10506
|
if (values.length > 1) {
|
|
9279
|
-
const insertValuesAs =
|
|
9280
|
-
|
|
10507
|
+
const insertValuesAs = _getQueryFreeAlias(q, "v");
|
|
10508
|
+
_setQueryAlias(self, "v", insertValuesAs);
|
|
9281
10509
|
q.insertValuesAs = insertValuesAs;
|
|
9282
10510
|
}
|
|
9283
10511
|
}
|
|
@@ -9921,7 +11149,7 @@ class Having {
|
|
|
9921
11149
|
*/
|
|
9922
11150
|
having(...args) {
|
|
9923
11151
|
const q = _clone(this);
|
|
9924
|
-
return
|
|
11152
|
+
return pushQueryValueImmutable(
|
|
9925
11153
|
q,
|
|
9926
11154
|
"having",
|
|
9927
11155
|
args.map((arg) => arg(q).q.expr)
|
|
@@ -9937,14 +11165,14 @@ class Having {
|
|
|
9937
11165
|
* @param args - SQL expression
|
|
9938
11166
|
*/
|
|
9939
11167
|
havingSql(...args) {
|
|
9940
|
-
return
|
|
11168
|
+
return pushQueryValueImmutable(_clone(this), "having", args);
|
|
9941
11169
|
}
|
|
9942
11170
|
}
|
|
9943
11171
|
|
|
9944
|
-
const before = (q, key, cb) =>
|
|
11172
|
+
const before = (q, key, cb) => pushQueryValueImmutable(q, `before${key}`, cb);
|
|
9945
11173
|
const after = (query, key, select, cb, commit) => {
|
|
9946
11174
|
const q = query;
|
|
9947
|
-
|
|
11175
|
+
pushQueryValueImmutable(
|
|
9948
11176
|
q,
|
|
9949
11177
|
`after${key}${commit ? "Commit" : ""}`,
|
|
9950
11178
|
cb
|
|
@@ -9957,16 +11185,16 @@ const after = (query, key, select, cb, commit) => {
|
|
|
9957
11185
|
return query;
|
|
9958
11186
|
};
|
|
9959
11187
|
const _queryHookBeforeQuery = (q, cb) => {
|
|
9960
|
-
return
|
|
11188
|
+
return pushQueryValueImmutable(q, "before", cb);
|
|
9961
11189
|
};
|
|
9962
11190
|
const _queryHookAfterQuery = (q, cb) => {
|
|
9963
|
-
return
|
|
11191
|
+
return pushQueryValueImmutable(q, "after", cb);
|
|
9964
11192
|
};
|
|
9965
11193
|
const _queryHookBeforeCreate = (q, cb) => {
|
|
9966
11194
|
return before(
|
|
9967
11195
|
q,
|
|
9968
11196
|
"Create",
|
|
9969
|
-
(q2) => cb(new
|
|
11197
|
+
(q2) => cb(new QueryHookUtils(q2, q2.q.columns, "hookCreateSet"))
|
|
9970
11198
|
);
|
|
9971
11199
|
};
|
|
9972
11200
|
const _queryHookAfterCreate = (q, select, cb) => {
|
|
@@ -9983,7 +11211,7 @@ const _queryHookBeforeUpdate = (q, cb) => {
|
|
|
9983
11211
|
columns.push(...Object.keys(item));
|
|
9984
11212
|
}
|
|
9985
11213
|
}
|
|
9986
|
-
return cb(new
|
|
11214
|
+
return cb(new QueryHookUtils(q2, columns, "hookUpdateSet"));
|
|
9987
11215
|
});
|
|
9988
11216
|
};
|
|
9989
11217
|
const _queryHookAfterUpdate = (q, select, cb) => {
|
|
@@ -10198,7 +11426,7 @@ class QueryHooks {
|
|
|
10198
11426
|
*/
|
|
10199
11427
|
catchAfterCommitError(fn) {
|
|
10200
11428
|
const q = _clone(this);
|
|
10201
|
-
|
|
11429
|
+
pushQueryValueImmutable(q, "catchAfterCommitErrors", fn);
|
|
10202
11430
|
return q;
|
|
10203
11431
|
}
|
|
10204
11432
|
}
|
|
@@ -10863,9 +12091,9 @@ class Join {
|
|
|
10863
12091
|
Object.entries(shape).map(([key, column]) => [key, column._parse])
|
|
10864
12092
|
);
|
|
10865
12093
|
const { q } = query;
|
|
10866
|
-
|
|
10867
|
-
|
|
10868
|
-
|
|
12094
|
+
setObjectValueImmutable(q, "joinedShapes", as, shape);
|
|
12095
|
+
setObjectValueImmutable(q, "joinedParsers", as, parsers);
|
|
12096
|
+
pushOrNewArrayToObjectImmutable(q, "join", {
|
|
10869
12097
|
type: "JOIN",
|
|
10870
12098
|
args: { a: as, c: shape, d: data }
|
|
10871
12099
|
});
|
|
@@ -10882,7 +12110,7 @@ const makeOnItem = (joinTo, joinFrom, args) => ({
|
|
|
10882
12110
|
}
|
|
10883
12111
|
});
|
|
10884
12112
|
const pushQueryOnForOuter = (q, joinFrom, joinTo, leftColumn, rightColumn) => {
|
|
10885
|
-
return
|
|
12113
|
+
return pushQueryValueImmutable(q, "and", {
|
|
10886
12114
|
ON: {
|
|
10887
12115
|
joinFrom: joinTo,
|
|
10888
12116
|
from: leftColumn,
|
|
@@ -10893,21 +12121,21 @@ const pushQueryOnForOuter = (q, joinFrom, joinTo, leftColumn, rightColumn) => {
|
|
|
10893
12121
|
});
|
|
10894
12122
|
};
|
|
10895
12123
|
const pushQueryOn = (q, joinFrom, joinTo, ...on) => {
|
|
10896
|
-
return
|
|
12124
|
+
return pushQueryValueImmutable(
|
|
10897
12125
|
q,
|
|
10898
12126
|
"and",
|
|
10899
12127
|
makeOnItem(joinFrom, joinTo, on)
|
|
10900
12128
|
);
|
|
10901
12129
|
};
|
|
10902
12130
|
const pushQueryOrOn = (q, joinFrom, joinTo, ...on) => {
|
|
10903
|
-
return
|
|
12131
|
+
return pushQueryValueImmutable(q, "or", [
|
|
10904
12132
|
makeOnItem(joinFrom, joinTo, on)
|
|
10905
12133
|
]);
|
|
10906
12134
|
};
|
|
10907
12135
|
const addQueryOn = (query, joinFrom, joinTo, ...args) => {
|
|
10908
12136
|
const cloned = _clone(query);
|
|
10909
12137
|
const { q } = cloned;
|
|
10910
|
-
|
|
12138
|
+
setObjectValueImmutable(
|
|
10911
12139
|
q,
|
|
10912
12140
|
"joinedShapes",
|
|
10913
12141
|
joinFrom.q.as || joinFrom.table,
|
|
@@ -10932,7 +12160,7 @@ const _queryJoinOrOn = (q, args) => {
|
|
|
10932
12160
|
);
|
|
10933
12161
|
};
|
|
10934
12162
|
const _queryJoinOnJsonPathEquals = (q, args) => {
|
|
10935
|
-
return
|
|
12163
|
+
return pushQueryValueImmutable(q, "and", {
|
|
10936
12164
|
ON: args
|
|
10937
12165
|
});
|
|
10938
12166
|
};
|
|
@@ -11051,7 +12279,7 @@ class MergeQueryMethods {
|
|
|
11051
12279
|
|
|
11052
12280
|
const throwOnReadOnly = (q, column, key) => {
|
|
11053
12281
|
if (column.data.appReadOnly || column.data.readOnly) {
|
|
11054
|
-
throw new
|
|
12282
|
+
throw new OrchidOrmInternalError(
|
|
11055
12283
|
q,
|
|
11056
12284
|
"Trying to update a readonly column",
|
|
11057
12285
|
{ column: key }
|
|
@@ -11086,7 +12314,7 @@ const _queryChangeCounter = (self, op, data) => {
|
|
|
11086
12314
|
throwOnReadOnly(self, column, data);
|
|
11087
12315
|
}
|
|
11088
12316
|
}
|
|
11089
|
-
|
|
12317
|
+
pushQueryValueImmutable(self, "updateData", map);
|
|
11090
12318
|
return self;
|
|
11091
12319
|
};
|
|
11092
12320
|
const _queryUpdate = (updateSelf, arg) => {
|
|
@@ -11095,7 +12323,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11095
12323
|
q.type = "update";
|
|
11096
12324
|
const returnCount = !q.select;
|
|
11097
12325
|
const set = { ...arg };
|
|
11098
|
-
|
|
12326
|
+
pushQueryValueImmutable(query, "updateData", set);
|
|
11099
12327
|
const { shape } = q;
|
|
11100
12328
|
const ctx = {};
|
|
11101
12329
|
let selectQuery;
|
|
@@ -11119,14 +12347,14 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11119
12347
|
value
|
|
11120
12348
|
);
|
|
11121
12349
|
if (value instanceof Db && value.q.type && value.q.subQuery) {
|
|
11122
|
-
throw new
|
|
12350
|
+
throw new OrchidOrmInternalError(
|
|
11123
12351
|
value,
|
|
11124
12352
|
`Only selecting queries are allowed inside a callback of update, ${value.q.type} is given instead.`
|
|
11125
12353
|
);
|
|
11126
12354
|
}
|
|
11127
12355
|
set[key] = value;
|
|
11128
12356
|
}
|
|
11129
|
-
if (value !== null && value !== void 0 && !
|
|
12357
|
+
if (value !== null && value !== void 0 && !isExpression(value)) {
|
|
11130
12358
|
if (value instanceof Db) {
|
|
11131
12359
|
moveQueryValueToWith(query, q, value, "with", set, key);
|
|
11132
12360
|
} else {
|
|
@@ -11138,7 +12366,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11138
12366
|
}
|
|
11139
12367
|
const { queries } = ctx;
|
|
11140
12368
|
if (queries) {
|
|
11141
|
-
const primaryKeys =
|
|
12369
|
+
const primaryKeys = requirePrimaryKeys(
|
|
11142
12370
|
query,
|
|
11143
12371
|
"Cannot perform complex update on a table without primary keys"
|
|
11144
12372
|
);
|
|
@@ -11147,7 +12375,7 @@ const _queryUpdate = (updateSelf, arg) => {
|
|
|
11147
12375
|
hookSelect.set(column, { select: column });
|
|
11148
12376
|
}
|
|
11149
12377
|
q.patchResult = async (_, _h, queryResult) => {
|
|
11150
|
-
await Promise.all(queries.map(
|
|
12378
|
+
await Promise.all(queries.map(callWithThis, queryResult));
|
|
11151
12379
|
if (ctx.collect) {
|
|
11152
12380
|
const t = query.baseQuery.clone();
|
|
11153
12381
|
_queryWhereIn(
|
|
@@ -11563,20 +12791,20 @@ class Update {
|
|
|
11563
12791
|
}
|
|
11564
12792
|
}
|
|
11565
12793
|
|
|
11566
|
-
class Headline extends
|
|
12794
|
+
class Headline extends Expression {
|
|
11567
12795
|
constructor(q, source, params) {
|
|
11568
12796
|
super();
|
|
11569
12797
|
this.q = q;
|
|
11570
12798
|
this.source = source;
|
|
11571
12799
|
this.params = params;
|
|
11572
|
-
this.result =
|
|
12800
|
+
this.result = emptyObject;
|
|
11573
12801
|
q.expr = this;
|
|
11574
12802
|
}
|
|
11575
12803
|
makeSQL(ctx, quotedAs) {
|
|
11576
12804
|
const { q, source, params } = this;
|
|
11577
12805
|
const lang = getSearchLang(ctx, q, source, quotedAs);
|
|
11578
|
-
const text = params?.text ? params.text instanceof
|
|
11579
|
-
const options = params?.options ? `, ${params.options instanceof
|
|
12806
|
+
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);
|
|
12807
|
+
const options = params?.options ? `, ${params.options instanceof Expression ? params.options.toSQL(ctx, quotedAs) : addValue(ctx.values, params.options)}` : "";
|
|
11580
12808
|
return `ts_headline(${lang}, ${text}, "${source.as}"${options})`;
|
|
11581
12809
|
}
|
|
11582
12810
|
}
|
|
@@ -11585,7 +12813,7 @@ AggregateMethods.prototype.headline = function(search, params) {
|
|
|
11585
12813
|
const q = this;
|
|
11586
12814
|
const source = q.q.sources?.[search];
|
|
11587
12815
|
if (!source)
|
|
11588
|
-
throw new
|
|
12816
|
+
throw new OrchidOrmInternalError(q, `Search \`${search}\` is not defined`);
|
|
11589
12817
|
return new Headline(
|
|
11590
12818
|
q.q,
|
|
11591
12819
|
source,
|
|
@@ -11792,9 +13020,9 @@ class SearchMethods {
|
|
|
11792
13020
|
}
|
|
11793
13021
|
setQueryObjectValueImmutable(q, "sources", arg.as, arg);
|
|
11794
13022
|
if (arg.order) {
|
|
11795
|
-
|
|
13023
|
+
pushQueryValueImmutable(q, "order", arg.as);
|
|
11796
13024
|
}
|
|
11797
|
-
return
|
|
13025
|
+
return pushQueryValueImmutable(q, "and", { SEARCH: arg });
|
|
11798
13026
|
}
|
|
11799
13027
|
}
|
|
11800
13028
|
|
|
@@ -11827,7 +13055,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11827
13055
|
const inCTE = {
|
|
11828
13056
|
selectNum: !!(hasAfterCallback || hasAfterCommitCallback),
|
|
11829
13057
|
targetHookSelect: hookSelect,
|
|
11830
|
-
delayedRelationSelect:
|
|
13058
|
+
delayedRelationSelect: newDelayedRelationSelect(
|
|
11831
13059
|
query
|
|
11832
13060
|
)
|
|
11833
13061
|
};
|
|
@@ -11879,7 +13107,7 @@ function orCreate(query, data, updateData, mergeData) {
|
|
|
11879
13107
|
await q22;
|
|
11880
13108
|
created = true;
|
|
11881
13109
|
} else if (queryResult.rowCount > 1) {
|
|
11882
|
-
throw new
|
|
13110
|
+
throw new MoreThanOneRowError(
|
|
11883
13111
|
q2,
|
|
11884
13112
|
`Only one row was expected to find, found ${queryResult.rowCount} rows.`
|
|
11885
13113
|
);
|
|
@@ -11903,7 +13131,7 @@ const QueryUpsert = {
|
|
|
11903
13131
|
} else {
|
|
11904
13132
|
updateData = data.update;
|
|
11905
13133
|
}
|
|
11906
|
-
if (!
|
|
13134
|
+
if (!isObjectEmpty(updateData)) {
|
|
11907
13135
|
_queryUpdate(q, updateData);
|
|
11908
13136
|
}
|
|
11909
13137
|
return orCreate(q, data.create, updateData, mergeData);
|
|
@@ -11973,7 +13201,7 @@ class TransformMethods {
|
|
|
11973
13201
|
* @param fn - function to transform query result with
|
|
11974
13202
|
*/
|
|
11975
13203
|
transform(fn) {
|
|
11976
|
-
return
|
|
13204
|
+
return pushQueryValueImmutable(_clone(this), "transform", fn);
|
|
11977
13205
|
}
|
|
11978
13206
|
}
|
|
11979
13207
|
|
|
@@ -12020,7 +13248,7 @@ class QueryMap {
|
|
|
12020
13248
|
* @param thisArg - same as in the native array map
|
|
12021
13249
|
*/
|
|
12022
13250
|
map(fn, thisArg) {
|
|
12023
|
-
return
|
|
13251
|
+
return pushQueryValueImmutable(_clone(this), "transform", {
|
|
12024
13252
|
map: fn,
|
|
12025
13253
|
thisArg
|
|
12026
13254
|
});
|
|
@@ -12045,7 +13273,7 @@ class ScopeMethods {
|
|
|
12045
13273
|
if (!q.q.scopes?.[scope]) {
|
|
12046
13274
|
const s = q.internal.scopes[scope];
|
|
12047
13275
|
if (!s) throw new Error(`Scope ${scope} is not defined`);
|
|
12048
|
-
|
|
13276
|
+
setObjectValueImmutable(q.q, "scopes", scope, s);
|
|
12049
13277
|
}
|
|
12050
13278
|
return q;
|
|
12051
13279
|
}
|
|
@@ -12087,7 +13315,7 @@ function enableSoftDelete(query, table, shape, softDelete, scopes) {
|
|
|
12087
13315
|
};
|
|
12088
13316
|
scopes.deleted = scope;
|
|
12089
13317
|
const { q } = query;
|
|
12090
|
-
|
|
13318
|
+
setObjectValueImmutable(q, "scopes", "nonDeleted", scope);
|
|
12091
13319
|
const _del = _softDelete(
|
|
12092
13320
|
column,
|
|
12093
13321
|
query.internal.nowSQL
|
|
@@ -12126,7 +13354,7 @@ class SoftDeleteMethods {
|
|
|
12126
13354
|
}
|
|
12127
13355
|
}
|
|
12128
13356
|
|
|
12129
|
-
class ColumnRefExpression extends
|
|
13357
|
+
class ColumnRefExpression extends Expression {
|
|
12130
13358
|
constructor(value, name) {
|
|
12131
13359
|
super();
|
|
12132
13360
|
this.name = name;
|
|
@@ -12143,7 +13371,7 @@ class ColumnRefExpression extends orchidCore.Expression {
|
|
|
12143
13371
|
);
|
|
12144
13372
|
}
|
|
12145
13373
|
}
|
|
12146
|
-
class RefExpression extends
|
|
13374
|
+
class RefExpression extends Expression {
|
|
12147
13375
|
constructor(value, query, ref) {
|
|
12148
13376
|
super();
|
|
12149
13377
|
this.ref = ref;
|
|
@@ -12157,7 +13385,7 @@ class RefExpression extends orchidCore.Expression {
|
|
|
12157
13385
|
return columnToSql(ctx, this.q, this.q.shape, this.ref, as && `"${as}"`);
|
|
12158
13386
|
}
|
|
12159
13387
|
}
|
|
12160
|
-
class OrExpression extends
|
|
13388
|
+
class OrExpression extends Expression {
|
|
12161
13389
|
constructor(args) {
|
|
12162
13390
|
super();
|
|
12163
13391
|
this.args = args;
|
|
@@ -12167,7 +13395,7 @@ class OrExpression extends orchidCore.Expression {
|
|
|
12167
13395
|
const res = [];
|
|
12168
13396
|
for (const arg of this.args) {
|
|
12169
13397
|
if (arg) {
|
|
12170
|
-
if (
|
|
13398
|
+
if (isExpression(arg)) {
|
|
12171
13399
|
const sql = arg.toSQL(ctx, quotedAs);
|
|
12172
13400
|
if (sql) res.push(sql);
|
|
12173
13401
|
} else {
|
|
@@ -12253,7 +13481,7 @@ class ExpressionMethods {
|
|
|
12253
13481
|
return new RefExpression(column || UnknownColumn.instance, q, arg);
|
|
12254
13482
|
}
|
|
12255
13483
|
val(value) {
|
|
12256
|
-
return new
|
|
13484
|
+
return new ValExpression(value);
|
|
12257
13485
|
}
|
|
12258
13486
|
/**
|
|
12259
13487
|
* `fn` allows to call an arbitrary SQL function.
|
|
@@ -12292,7 +13520,7 @@ class ExpressionMethods {
|
|
|
12292
13520
|
* @param options
|
|
12293
13521
|
*/
|
|
12294
13522
|
fn(fn, args, options) {
|
|
12295
|
-
return makeFnExpression(this,
|
|
13523
|
+
return makeFnExpression(this, emptyObject, fn, args, options);
|
|
12296
13524
|
}
|
|
12297
13525
|
or(...args) {
|
|
12298
13526
|
return new OrExpression(args);
|
|
@@ -12467,7 +13695,7 @@ class QueryMethods {
|
|
|
12467
13695
|
find(value) {
|
|
12468
13696
|
const q = _clone(this);
|
|
12469
13697
|
if (value === null || value === void 0) {
|
|
12470
|
-
throw new
|
|
13698
|
+
throw new OrchidOrmInternalError(
|
|
12471
13699
|
q,
|
|
12472
13700
|
`${value} is not allowed in the find method`
|
|
12473
13701
|
);
|
|
@@ -12651,7 +13879,7 @@ class QueryMethods {
|
|
|
12651
13879
|
* @param arg - window config
|
|
12652
13880
|
*/
|
|
12653
13881
|
window(arg) {
|
|
12654
|
-
return
|
|
13882
|
+
return pushQueryValueImmutable(_clone(this), "window", arg);
|
|
12655
13883
|
}
|
|
12656
13884
|
wrap(query, as) {
|
|
12657
13885
|
return queryWrap(this, _clone(query), as);
|
|
@@ -12706,7 +13934,7 @@ class QueryMethods {
|
|
|
12706
13934
|
* @param args - SQL expression
|
|
12707
13935
|
*/
|
|
12708
13936
|
orderSql(...args) {
|
|
12709
|
-
return
|
|
13937
|
+
return pushQueryValueImmutable(
|
|
12710
13938
|
_clone(this),
|
|
12711
13939
|
"order",
|
|
12712
13940
|
sqlQueryArgsToExpression(args)
|
|
@@ -12893,9 +14121,9 @@ class QueryMethods {
|
|
|
12893
14121
|
const helperAs = this.q.as || this.table;
|
|
12894
14122
|
return (query, ...args) => {
|
|
12895
14123
|
const q = _clone(query);
|
|
12896
|
-
const as =
|
|
14124
|
+
const as = _getQueryAs(q);
|
|
12897
14125
|
if (as) {
|
|
12898
|
-
|
|
14126
|
+
_setQueryAlias(q, as, helperAs);
|
|
12899
14127
|
}
|
|
12900
14128
|
return fn(q, ...args);
|
|
12901
14129
|
};
|
|
@@ -13024,7 +14252,7 @@ class QueryMethods {
|
|
|
13024
14252
|
}
|
|
13025
14253
|
Object.assign(QueryMethods.prototype, QueryUpsert);
|
|
13026
14254
|
Object.assign(QueryMethods.prototype, QueryOrCreate);
|
|
13027
|
-
|
|
14255
|
+
applyMixins(QueryMethods, [
|
|
13028
14256
|
QueryAsMethods,
|
|
13029
14257
|
AggregateMethods,
|
|
13030
14258
|
Select,
|
|
@@ -13162,7 +14390,7 @@ const parseIndexOrExclude = (item) => {
|
|
|
13162
14390
|
const performQuery = async (q, args, method) => {
|
|
13163
14391
|
const trx = q.internal.transactionStorage.getStore();
|
|
13164
14392
|
let sql;
|
|
13165
|
-
if (
|
|
14393
|
+
if (isRawSQL(args[0])) {
|
|
13166
14394
|
const values = [];
|
|
13167
14395
|
sql = {
|
|
13168
14396
|
text: args[0].toSQL({ values }),
|
|
@@ -13208,7 +14436,7 @@ class Db extends QueryMethods {
|
|
|
13208
14436
|
this.columnTypes = columnTypes;
|
|
13209
14437
|
const self = this;
|
|
13210
14438
|
const { softDelete } = options;
|
|
13211
|
-
const scopes = options.scopes || softDelete ? {} :
|
|
14439
|
+
const scopes = options.scopes || softDelete ? {} : emptyObject;
|
|
13212
14440
|
this.baseQuery = this;
|
|
13213
14441
|
this.relations = {};
|
|
13214
14442
|
this.relationQueries = {};
|
|
@@ -13230,7 +14458,7 @@ class Db extends QueryMethods {
|
|
|
13230
14458
|
if (column.data.name) {
|
|
13231
14459
|
prepareSelectAll = true;
|
|
13232
14460
|
} else if (snakeCase) {
|
|
13233
|
-
const snakeName =
|
|
14461
|
+
const snakeName = toSnakeCase(key);
|
|
13234
14462
|
if (snakeName !== key) {
|
|
13235
14463
|
prepareSelectAll = true;
|
|
13236
14464
|
column.data.name = snakeName;
|
|
@@ -13241,7 +14469,7 @@ class Db extends QueryMethods {
|
|
|
13241
14469
|
}
|
|
13242
14470
|
const { modifyQuery: mq } = column.data;
|
|
13243
14471
|
if (mq) {
|
|
13244
|
-
modifyQuery =
|
|
14472
|
+
modifyQuery = pushOrNewArray(modifyQuery, (q) => mq(q, column));
|
|
13245
14473
|
}
|
|
13246
14474
|
if (typeof column.data.default === "function") {
|
|
13247
14475
|
if (!runtimeDefaultColumns) runtimeDefaultColumns = [key];
|
|
@@ -13320,7 +14548,7 @@ class Db extends QueryMethods {
|
|
|
13320
14548
|
cb(this);
|
|
13321
14549
|
}
|
|
13322
14550
|
}
|
|
13323
|
-
this.error = class extends
|
|
14551
|
+
this.error = class extends QueryError {
|
|
13324
14552
|
constructor(message) {
|
|
13325
14553
|
super(self, message);
|
|
13326
14554
|
}
|
|
@@ -13420,7 +14648,7 @@ class Db extends QueryMethods {
|
|
|
13420
14648
|
const {
|
|
13421
14649
|
rows: [row]
|
|
13422
14650
|
} = await performQuery(q, args, "query");
|
|
13423
|
-
if (!row) throw new
|
|
14651
|
+
if (!row) throw new NotFoundError(q);
|
|
13424
14652
|
return row;
|
|
13425
14653
|
},
|
|
13426
14654
|
async takeOptional(...args) {
|
|
@@ -13439,7 +14667,7 @@ class Db extends QueryMethods {
|
|
|
13439
14667
|
const {
|
|
13440
14668
|
rows: [row]
|
|
13441
14669
|
} = await performQuery(q, args, "arrays");
|
|
13442
|
-
if (!row) throw new
|
|
14670
|
+
if (!row) throw new NotFoundError(q);
|
|
13443
14671
|
return row[0];
|
|
13444
14672
|
},
|
|
13445
14673
|
async getOptional(...args) {
|
|
@@ -13471,7 +14699,7 @@ class Db extends QueryMethods {
|
|
|
13471
14699
|
return performQuery(this, args, "arrays");
|
|
13472
14700
|
}
|
|
13473
14701
|
}
|
|
13474
|
-
|
|
14702
|
+
applyMixins(Db, [QueryMethods]);
|
|
13475
14703
|
Db.prototype.constructor = Db;
|
|
13476
14704
|
const createDbWithAdapter = ({
|
|
13477
14705
|
log,
|
|
@@ -13492,7 +14720,7 @@ const createDbWithAdapter = ({
|
|
|
13492
14720
|
};
|
|
13493
14721
|
const ct = typeof ctOrFn === "function" ? ctOrFn(makeColumnTypes(schemaConfig)) : ctOrFn;
|
|
13494
14722
|
if (snakeCase) {
|
|
13495
|
-
ct[
|
|
14723
|
+
ct[snakeCaseKey] = true;
|
|
13496
14724
|
}
|
|
13497
14725
|
const transactionStorage = new node_async_hooks.AsyncLocalStorage();
|
|
13498
14726
|
const qb = _initQueryBuilder(
|
|
@@ -13694,6 +14922,7 @@ exports.CitextColumn = CitextColumn;
|
|
|
13694
14922
|
exports.Clear = Clear;
|
|
13695
14923
|
exports.ColumnRefExpression = ColumnRefExpression;
|
|
13696
14924
|
exports.ColumnType = ColumnType;
|
|
14925
|
+
exports.ColumnTypeBase = ColumnTypeBase;
|
|
13697
14926
|
exports.ComputedColumn = ComputedColumn;
|
|
13698
14927
|
exports.CustomTypeColumn = CustomTypeColumn;
|
|
13699
14928
|
exports.DateBaseColumn = DateBaseColumn;
|
|
@@ -13707,7 +14936,9 @@ exports.DomainColumn = DomainColumn;
|
|
|
13707
14936
|
exports.DoublePrecisionColumn = DoublePrecisionColumn;
|
|
13708
14937
|
exports.DynamicRawSQL = DynamicRawSQL;
|
|
13709
14938
|
exports.EnumColumn = EnumColumn;
|
|
14939
|
+
exports.Expression = Expression;
|
|
13710
14940
|
exports.ExpressionMethods = ExpressionMethods;
|
|
14941
|
+
exports.ExpressionTypeMethod = ExpressionTypeMethod;
|
|
13711
14942
|
exports.FnExpression = FnExpression;
|
|
13712
14943
|
exports.For = For;
|
|
13713
14944
|
exports.FromMethods = FromMethods;
|
|
@@ -13727,22 +14958,29 @@ exports.MacAddr8Column = MacAddr8Column;
|
|
|
13727
14958
|
exports.MacAddrColumn = MacAddrColumn;
|
|
13728
14959
|
exports.MergeQueryMethods = MergeQueryMethods;
|
|
13729
14960
|
exports.MoneyColumn = MoneyColumn;
|
|
14961
|
+
exports.MoreThanOneRowError = MoreThanOneRowError;
|
|
14962
|
+
exports.NotFoundError = NotFoundError;
|
|
13730
14963
|
exports.NumberAsStringBaseColumn = NumberAsStringBaseColumn;
|
|
13731
14964
|
exports.NumberBaseColumn = NumberBaseColumn;
|
|
13732
14965
|
exports.OnMethods = OnMethods;
|
|
13733
14966
|
exports.Operators = Operators;
|
|
13734
14967
|
exports.OrExpression = OrExpression;
|
|
14968
|
+
exports.OrchidOrmError = OrchidOrmError;
|
|
14969
|
+
exports.OrchidOrmInternalError = OrchidOrmInternalError;
|
|
13735
14970
|
exports.PathColumn = PathColumn;
|
|
13736
14971
|
exports.PointColumn = PointColumn;
|
|
13737
14972
|
exports.PolygonColumn = PolygonColumn;
|
|
13738
14973
|
exports.PostgisGeographyPointColumn = PostgisGeographyPointColumn;
|
|
13739
14974
|
exports.QueryAsMethods = QueryAsMethods;
|
|
14975
|
+
exports.QueryError = QueryError;
|
|
13740
14976
|
exports.QueryGet = QueryGet;
|
|
14977
|
+
exports.QueryHookUtils = QueryHookUtils;
|
|
13741
14978
|
exports.QueryHooks = QueryHooks;
|
|
13742
14979
|
exports.QueryLog = QueryLog;
|
|
13743
14980
|
exports.QueryMethods = QueryMethods;
|
|
13744
14981
|
exports.QueryUpsert = QueryUpsert;
|
|
13745
14982
|
exports.RawSQL = RawSQL;
|
|
14983
|
+
exports.RawSQLBase = RawSQLBase;
|
|
13746
14984
|
exports.RealColumn = RealColumn;
|
|
13747
14985
|
exports.RefExpression = RefExpression;
|
|
13748
14986
|
exports.SearchMethods = SearchMethods;
|
|
@@ -13763,16 +15001,27 @@ exports.TransformMethods = TransformMethods;
|
|
|
13763
15001
|
exports.TsQueryColumn = TsQueryColumn;
|
|
13764
15002
|
exports.TsVectorColumn = TsVectorColumn;
|
|
13765
15003
|
exports.UUIDColumn = UUIDColumn;
|
|
15004
|
+
exports.UnhandledTypeError = UnhandledTypeError;
|
|
13766
15005
|
exports.Union = Union;
|
|
13767
15006
|
exports.UnknownColumn = UnknownColumn;
|
|
13768
15007
|
exports.Update = Update;
|
|
15008
|
+
exports.ValExpression = ValExpression;
|
|
13769
15009
|
exports.VarCharColumn = VarCharColumn;
|
|
13770
15010
|
exports.VirtualColumn = VirtualColumn;
|
|
13771
15011
|
exports.Where = Where;
|
|
13772
15012
|
exports.WithMethods = WithMethods;
|
|
13773
15013
|
exports.XMLColumn = XMLColumn;
|
|
15014
|
+
exports._addToHookSelect = _addToHookSelect;
|
|
15015
|
+
exports._addToHookSelectWithTable = _addToHookSelectWithTable;
|
|
13774
15016
|
exports._addWith = _addWith;
|
|
15017
|
+
exports._applyRelationAliases = _applyRelationAliases;
|
|
15018
|
+
exports._checkIfAliased = _checkIfAliased;
|
|
13775
15019
|
exports._clone = _clone;
|
|
15020
|
+
exports._copyQueryAliasToQuery = _copyQueryAliasToQuery;
|
|
15021
|
+
exports._getQueryAliasOrName = _getQueryAliasOrName;
|
|
15022
|
+
exports._getQueryAs = _getQueryAs;
|
|
15023
|
+
exports._getQueryFreeAlias = _getQueryFreeAlias;
|
|
15024
|
+
exports._getQueryOuterAliases = _getQueryOuterAliases;
|
|
13776
15025
|
exports._getSelectableColumn = _getSelectableColumn;
|
|
13777
15026
|
exports._initQueryBuilder = _initQueryBuilder;
|
|
13778
15027
|
exports._queryAfterSaveCommit = _queryAfterSaveCommit;
|
|
@@ -13830,17 +15079,31 @@ exports._queryWhereNotSql = _queryWhereNotSql;
|
|
|
13830
15079
|
exports._queryWhereOneOf = _queryWhereOneOf;
|
|
13831
15080
|
exports._queryWhereSql = _queryWhereSql;
|
|
13832
15081
|
exports._runAfterCommitHooks = _runAfterCommitHooks;
|
|
15082
|
+
exports._setQueryAlias = _setQueryAlias;
|
|
15083
|
+
exports._setQueryAs = _setQueryAs;
|
|
15084
|
+
exports._setSubQueryAliases = _setSubQueryAliases;
|
|
15085
|
+
exports.addCode = addCode;
|
|
13833
15086
|
exports.addColumnParserToQuery = addColumnParserToQuery;
|
|
13834
15087
|
exports.addParserForRawExpression = addParserForRawExpression;
|
|
13835
15088
|
exports.addParserForSelectItem = addParserForSelectItem;
|
|
13836
15089
|
exports.addQueryOn = addQueryOn;
|
|
15090
|
+
exports.addValue = addValue;
|
|
13837
15091
|
exports.anyShape = anyShape;
|
|
13838
15092
|
exports.applyComputedColumns = applyComputedColumns;
|
|
15093
|
+
exports.applyMixins = applyMixins;
|
|
15094
|
+
exports.applyTransforms = applyTransforms;
|
|
15095
|
+
exports.arrayDataToCode = arrayDataToCode;
|
|
13839
15096
|
exports.assignDbDataToColumn = assignDbDataToColumn;
|
|
15097
|
+
exports.backtickQuote = backtickQuote;
|
|
15098
|
+
exports.callWithThis = callWithThis;
|
|
13840
15099
|
exports.checkIfASimpleQuery = checkIfASimpleQuery;
|
|
13841
15100
|
exports.cloneQueryBaseUnscoped = cloneQueryBaseUnscoped;
|
|
15101
|
+
exports.codeToString = codeToString;
|
|
15102
|
+
exports.colors = colors;
|
|
13842
15103
|
exports.columnCheckToCode = columnCheckToCode;
|
|
13843
15104
|
exports.columnCode = columnCode;
|
|
15105
|
+
exports.columnDefaultArgumentToCode = columnDefaultArgumentToCode;
|
|
15106
|
+
exports.columnErrorMessagesToCode = columnErrorMessagesToCode;
|
|
13844
15107
|
exports.columnExcludesToCode = columnExcludesToCode;
|
|
13845
15108
|
exports.columnForeignKeysToCode = columnForeignKeysToCode;
|
|
13846
15109
|
exports.columnIndexesToCode = columnIndexesToCode;
|
|
@@ -13848,79 +15111,141 @@ exports.columnsShapeToCode = columnsShapeToCode;
|
|
|
13848
15111
|
exports.commitSql = commitSql;
|
|
13849
15112
|
exports.constraintInnerToCode = constraintInnerToCode;
|
|
13850
15113
|
exports.constraintToCode = constraintToCode;
|
|
15114
|
+
exports.consumeColumnName = consumeColumnName;
|
|
13851
15115
|
exports.copyTableData = copyTableData;
|
|
13852
15116
|
exports.countSelect = countSelect;
|
|
13853
15117
|
exports.createDbWithAdapter = createDbWithAdapter;
|
|
15118
|
+
exports.dateDataToCode = dateDataToCode;
|
|
15119
|
+
exports.deepCompare = deepCompare;
|
|
13854
15120
|
exports.defaultSchemaConfig = defaultSchemaConfig;
|
|
15121
|
+
exports.emptyArray = emptyArray;
|
|
15122
|
+
exports.emptyObject = emptyObject;
|
|
13855
15123
|
exports.escapeForLog = escapeForLog;
|
|
13856
15124
|
exports.escapeForMigration = escapeForMigration;
|
|
13857
15125
|
exports.escapeString = escapeString;
|
|
13858
15126
|
exports.excludeInnerToCode = excludeInnerToCode;
|
|
13859
15127
|
exports.excludeToCode = excludeToCode;
|
|
15128
|
+
exports.exhaustive = exhaustive;
|
|
13860
15129
|
exports.extendQuery = extendQuery;
|
|
13861
15130
|
exports.filterResult = filterResult;
|
|
13862
15131
|
exports.foreignKeyArgumentToCode = foreignKeyArgumentToCode;
|
|
15132
|
+
exports.getCallerFilePath = getCallerFilePath;
|
|
13863
15133
|
exports.getClonedQueryData = getClonedQueryData;
|
|
13864
15134
|
exports.getColumnBaseType = getColumnBaseType;
|
|
13865
15135
|
exports.getColumnInfo = getColumnInfo;
|
|
13866
15136
|
exports.getColumnTypes = getColumnTypes;
|
|
15137
|
+
exports.getDefaultLanguage = getDefaultLanguage;
|
|
15138
|
+
exports.getFreeAlias = getFreeAlias;
|
|
15139
|
+
exports.getFreeSetAlias = getFreeSetAlias;
|
|
13867
15140
|
exports.getFullColumnTable = getFullColumnTable;
|
|
15141
|
+
exports.getImportPath = getImportPath;
|
|
15142
|
+
exports.getPrimaryKeys = getPrimaryKeys;
|
|
13868
15143
|
exports.getQueryAs = getQueryAs;
|
|
15144
|
+
exports.getQueryParsers = getQueryParsers;
|
|
13869
15145
|
exports.getShapeFromSelect = getShapeFromSelect;
|
|
13870
15146
|
exports.getSqlText = getSqlText;
|
|
15147
|
+
exports.getStackTrace = getStackTrace;
|
|
15148
|
+
exports.getValueKey = getValueKey;
|
|
13871
15149
|
exports.handleResult = handleResult;
|
|
13872
15150
|
exports.identityToCode = identityToCode;
|
|
13873
15151
|
exports.indexInnerToCode = indexInnerToCode;
|
|
13874
15152
|
exports.indexToCode = indexToCode;
|
|
13875
15153
|
exports.isDefaultTimeStamp = isDefaultTimeStamp;
|
|
15154
|
+
exports.isExpression = isExpression;
|
|
13876
15155
|
exports.isInUserTransaction = isInUserTransaction;
|
|
15156
|
+
exports.isIterable = isIterable;
|
|
15157
|
+
exports.isObjectEmpty = isObjectEmpty;
|
|
13877
15158
|
exports.isQueryReturnsAll = isQueryReturnsAll;
|
|
15159
|
+
exports.isRawSQL = isRawSQL;
|
|
15160
|
+
exports.isRelationQuery = isRelationQuery;
|
|
13878
15161
|
exports.isSelectingCount = isSelectingCount;
|
|
15162
|
+
exports.isTemplateLiteralArgs = isTemplateLiteralArgs;
|
|
13879
15163
|
exports.joinSubQuery = joinSubQuery;
|
|
15164
|
+
exports.joinTruthy = joinTruthy;
|
|
15165
|
+
exports.logColors = logColors;
|
|
13880
15166
|
exports.logParamToLogObject = logParamToLogObject;
|
|
15167
|
+
exports.makeColumnNullable = makeColumnNullable;
|
|
13881
15168
|
exports.makeColumnTypes = makeColumnTypes;
|
|
13882
15169
|
exports.makeColumnsByType = makeColumnsByType;
|
|
13883
15170
|
exports.makeFnExpression = makeFnExpression;
|
|
13884
15171
|
exports.moveQueryValueToWith = moveQueryValueToWith;
|
|
15172
|
+
exports.newDelayedRelationSelect = newDelayedRelationSelect;
|
|
15173
|
+
exports.noop = noop;
|
|
15174
|
+
exports.numberDataToCode = numberDataToCode;
|
|
15175
|
+
exports.objectHasValues = objectHasValues;
|
|
15176
|
+
exports.omit = omit;
|
|
13885
15177
|
exports.parseRecord = parseRecord;
|
|
13886
15178
|
exports.parseTableData = parseTableData;
|
|
13887
15179
|
exports.parseTableDataInput = parseTableDataInput;
|
|
15180
|
+
exports.pathToLog = pathToLog;
|
|
15181
|
+
exports.pick = pick;
|
|
15182
|
+
exports.pluralize = pluralize;
|
|
13888
15183
|
exports.postgisTypmodToSql = postgisTypmodToSql;
|
|
13889
15184
|
exports.primaryKeyInnerToCode = primaryKeyInnerToCode;
|
|
13890
15185
|
exports.processComputedBatches = processComputedBatches;
|
|
13891
15186
|
exports.processComputedResult = processComputedResult;
|
|
13892
15187
|
exports.processSelectArg = processSelectArg;
|
|
15188
|
+
exports.pushColumnData = pushColumnData;
|
|
13893
15189
|
exports.pushLimitSQL = pushLimitSQL;
|
|
15190
|
+
exports.pushOrNewArray = pushOrNewArray;
|
|
15191
|
+
exports.pushOrNewArrayToObjectImmutable = pushOrNewArrayToObjectImmutable;
|
|
13894
15192
|
exports.pushQueryArrayImmutable = pushQueryArrayImmutable;
|
|
13895
15193
|
exports.pushQueryOn = pushQueryOn;
|
|
13896
15194
|
exports.pushQueryOnForOuter = pushQueryOnForOuter;
|
|
13897
15195
|
exports.pushQueryOrOn = pushQueryOrOn;
|
|
15196
|
+
exports.pushQueryValueImmutable = pushQueryValueImmutable;
|
|
13898
15197
|
exports.pushTableDataCode = pushTableDataCode;
|
|
15198
|
+
exports.queryColumnNameToKey = queryColumnNameToKey;
|
|
13899
15199
|
exports.queryFrom = queryFrom;
|
|
13900
15200
|
exports.queryFromSql = queryFromSql;
|
|
13901
15201
|
exports.queryJson = queryJson;
|
|
13902
15202
|
exports.queryMethodByReturnType = queryMethodByReturnType;
|
|
13903
15203
|
exports.queryTypeWithLimitOne = queryTypeWithLimitOne;
|
|
13904
15204
|
exports.queryWrap = queryWrap;
|
|
15205
|
+
exports.quoteObjectKey = quoteObjectKey;
|
|
13905
15206
|
exports.raw = raw;
|
|
13906
15207
|
exports.referencesArgsToCode = referencesArgsToCode;
|
|
15208
|
+
exports.requirePrimaryKeys = requirePrimaryKeys;
|
|
13907
15209
|
exports.resolveSubQueryCallbackV2 = resolveSubQueryCallbackV2;
|
|
15210
|
+
exports.returnArg = returnArg;
|
|
13908
15211
|
exports.rollbackSql = rollbackSql;
|
|
13909
15212
|
exports.saveAliasedShape = saveAliasedShape;
|
|
15213
|
+
exports.setColumnData = setColumnData;
|
|
13910
15214
|
exports.setColumnDefaultParse = setColumnDefaultParse;
|
|
13911
15215
|
exports.setColumnEncode = setColumnEncode;
|
|
13912
15216
|
exports.setColumnParse = setColumnParse;
|
|
13913
15217
|
exports.setColumnParseNull = setColumnParseNull;
|
|
15218
|
+
exports.setConnectRetryConfig = setConnectRetryConfig;
|
|
15219
|
+
exports.setCurrentColumnName = setCurrentColumnName;
|
|
15220
|
+
exports.setDataValue = setDataValue;
|
|
15221
|
+
exports.setDefaultLanguage = setDefaultLanguage;
|
|
15222
|
+
exports.setDefaultNowFn = setDefaultNowFn;
|
|
15223
|
+
exports.setDelayedRelation = setDelayedRelation;
|
|
15224
|
+
exports.setObjectValueImmutable = setObjectValueImmutable;
|
|
13914
15225
|
exports.setParserForSelectedString = setParserForSelectedString;
|
|
15226
|
+
exports.setParserToQuery = setParserToQuery;
|
|
13915
15227
|
exports.setQueryObjectValueImmutable = setQueryObjectValueImmutable;
|
|
13916
15228
|
exports.setQueryOperators = setQueryOperators;
|
|
13917
15229
|
exports.simplifyColumnDefault = simplifyColumnDefault;
|
|
15230
|
+
exports.singleQuote = singleQuote;
|
|
15231
|
+
exports.singleQuoteArray = singleQuoteArray;
|
|
15232
|
+
exports.snakeCaseKey = snakeCaseKey;
|
|
15233
|
+
exports.spreadObjectValues = spreadObjectValues;
|
|
13918
15234
|
exports.sqlFn = sqlFn;
|
|
13919
15235
|
exports.sqlQueryArgsToExpression = sqlQueryArgsToExpression;
|
|
15236
|
+
exports.stringDataToCode = stringDataToCode;
|
|
13920
15237
|
exports.tableDataMethods = tableDataMethods;
|
|
15238
|
+
exports.templateLiteralSQLToCode = templateLiteralSQLToCode;
|
|
13921
15239
|
exports.templateLiteralToSQL = templateLiteralToSQL;
|
|
13922
15240
|
exports.testTransaction = testTransaction;
|
|
13923
15241
|
exports.throwIfJoinLateral = throwIfJoinLateral;
|
|
13924
15242
|
exports.throwIfNoWhere = throwIfNoWhere;
|
|
15243
|
+
exports.timestampHelpers = timestampHelpers;
|
|
15244
|
+
exports.toArray = toArray;
|
|
15245
|
+
exports.toCamelCase = toCamelCase;
|
|
15246
|
+
exports.toPascalCase = toPascalCase;
|
|
13925
15247
|
exports.toSQL = toSQL;
|
|
15248
|
+
exports.toSnakeCase = toSnakeCase;
|
|
15249
|
+
exports.toSubSqlText = toSubSqlText;
|
|
15250
|
+
exports.wrapAdapterFnWithConnectRetry = wrapAdapterFnWithConnectRetry;
|
|
13926
15251
|
//# sourceMappingURL=index.js.map
|