orchid-orm 1.64.10 → 1.65.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.ts +5 -4
- package/dist/index.js +183 -182
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +4 -3
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/index.d.ts +1 -1
- package/dist/migrations/index.js +99 -98
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +5 -4
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/migrations/node-postgres.d.ts +1 -1
- package/dist/migrations/node-postgres.js +99 -98
- package/dist/migrations/node-postgres.js.map +1 -1
- package/dist/migrations/node-postgres.mjs +5 -4
- package/dist/migrations/node-postgres.mjs.map +1 -1
- package/dist/migrations/postgres-js.d.ts +1 -1
- package/dist/migrations/postgres-js.js +99 -98
- package/dist/migrations/postgres-js.js.map +1 -1
- package/dist/migrations/postgres-js.mjs +5 -4
- package/dist/migrations/postgres-js.mjs.map +1 -1
- package/dist/node-postgres.d.ts +3 -3
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +6 -5
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +25 -21
package/dist/index.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
'use strict';
|
|
2
2
|
|
|
3
|
+
var internal = require('pqb/internal');
|
|
3
4
|
var pqb = require('pqb');
|
|
4
5
|
var node_async_hooks = require('node:async_hooks');
|
|
5
6
|
|
|
6
7
|
function createBaseTable({
|
|
7
|
-
schemaConfig =
|
|
8
|
+
schemaConfig = internal.defaultSchemaConfig,
|
|
8
9
|
columnTypes: columnTypesArg,
|
|
9
10
|
snakeCase,
|
|
10
11
|
filePath: filePathArg,
|
|
@@ -14,12 +15,12 @@ function createBaseTable({
|
|
|
14
15
|
autoForeignKeys
|
|
15
16
|
} = {}) {
|
|
16
17
|
var _a;
|
|
17
|
-
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(
|
|
18
|
-
const filePathOrStack = filePathArg ||
|
|
18
|
+
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(internal.makeColumnTypes(schemaConfig)) : columnTypesArg || internal.makeColumnTypes(schemaConfig);
|
|
19
|
+
const filePathOrStack = filePathArg || internal.getStackTrace();
|
|
19
20
|
let filePath;
|
|
20
21
|
const defaultColumns = {
|
|
21
|
-
shape:
|
|
22
|
-
data:
|
|
22
|
+
shape: internal.emptyObject,
|
|
23
|
+
data: internal.emptyArray
|
|
23
24
|
};
|
|
24
25
|
const instances = /* @__PURE__ */ new WeakMap();
|
|
25
26
|
const base = (_a = class {
|
|
@@ -59,7 +60,7 @@ function createBaseTable({
|
|
|
59
60
|
if (typeof filePathOrStack === "string") {
|
|
60
61
|
return filePath = filePathOrStack;
|
|
61
62
|
}
|
|
62
|
-
filePath =
|
|
63
|
+
filePath = internal.getCallerFilePath(filePathOrStack);
|
|
63
64
|
if (filePath) return filePath;
|
|
64
65
|
throw new Error(
|
|
65
66
|
`Failed to determine file path of a base table. Please set the \`filePath\` option of \`createBaseTable\` manually.`
|
|
@@ -80,21 +81,21 @@ function createBaseTable({
|
|
|
80
81
|
if (this.filePath) return this.filePath;
|
|
81
82
|
if (typeof filePathOrStack === "string")
|
|
82
83
|
return this.filePath = filePathOrStack;
|
|
83
|
-
const filePath2 =
|
|
84
|
+
const filePath2 = internal.getCallerFilePath(filePathOrStack);
|
|
84
85
|
if (filePath2) return this.filePath = filePath2;
|
|
85
86
|
throw new Error(
|
|
86
87
|
`Failed to determine file path for table ${this.constructor.name}. Please set \`filePath\` property manually`
|
|
87
88
|
);
|
|
88
89
|
}
|
|
89
90
|
setColumns(fn, dataFn) {
|
|
90
|
-
columnTypes[
|
|
91
|
-
const shape =
|
|
92
|
-
const tableData =
|
|
91
|
+
columnTypes[internal.snakeCaseKey] = this.snakeCase;
|
|
92
|
+
const shape = internal.getColumnTypes(columnTypes, fn, nowSQL, this.language);
|
|
93
|
+
const tableData = internal.parseTableData(dataFn);
|
|
93
94
|
if (this.snakeCase) {
|
|
94
95
|
for (const key in shape) {
|
|
95
96
|
const column = shape[key];
|
|
96
97
|
if (column.data.name) continue;
|
|
97
|
-
const snakeName =
|
|
98
|
+
const snakeName = internal.toSnakeCase(key);
|
|
98
99
|
if (snakeName !== key) {
|
|
99
100
|
column.data.name = snakeName;
|
|
100
101
|
}
|
|
@@ -139,8 +140,8 @@ function createBaseTable({
|
|
|
139
140
|
options
|
|
140
141
|
};
|
|
141
142
|
}
|
|
142
|
-
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a.columnTypes = columnTypes, _a.sql =
|
|
143
|
-
|
|
143
|
+
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a.columnTypes = columnTypes, _a.sql = internal._createDbSqlMethod(columnTypes), _a);
|
|
144
|
+
internal.applyMixins(base, [internal.QueryHooks]);
|
|
144
145
|
base.prototype.types = columnTypes;
|
|
145
146
|
base.prototype.snakeCase = snakeCase;
|
|
146
147
|
base.prototype.autoForeignKeys = autoForeignKeys === true ? {} : autoForeignKeys || void 0;
|
|
@@ -168,7 +169,7 @@ const hasRelationHandleCreate = (q, ctx, items, rowIndexes, key, primaryKeys, ne
|
|
|
168
169
|
}
|
|
169
170
|
const relationData = [values];
|
|
170
171
|
store.hasRelation[key] = relationData;
|
|
171
|
-
|
|
172
|
+
internal._queryHookAfterCreate(
|
|
172
173
|
q,
|
|
173
174
|
primaryKeys,
|
|
174
175
|
(rows, q2) => nestedInsert(
|
|
@@ -186,7 +187,7 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
186
187
|
if (!value.set && !("upsert" in value) && (!value.add || Array.isArray(value.add) && value.add.length === 0) && (!value.disconnect || Array.isArray(value.disconnect) && value.disconnect.length === 0) && (!value.delete || Array.isArray(value.delete) && value.delete.length === 0) && (!value.update || Array.isArray(value.update.where) && value.update.where.length === 0) && (!value.create || Array.isArray(value.create) && value.create.length === 0))
|
|
187
188
|
return;
|
|
188
189
|
q.q.wrapInTransaction = true;
|
|
189
|
-
|
|
190
|
+
internal._queryHookAfterUpdate(q, primaryKeys, (rows, q2) => {
|
|
190
191
|
return nestedUpdate(
|
|
191
192
|
q2,
|
|
192
193
|
rows,
|
|
@@ -201,7 +202,7 @@ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelat
|
|
|
201
202
|
baseQuery
|
|
202
203
|
),
|
|
203
204
|
() => {
|
|
204
|
-
const as =
|
|
205
|
+
const as = internal.getQueryAs(joiningQuery);
|
|
205
206
|
return sourceRelation.joinQuery(
|
|
206
207
|
sourceRelation.query.as(as),
|
|
207
208
|
throughRelation.query
|
|
@@ -210,11 +211,11 @@ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelat
|
|
|
210
211
|
);
|
|
211
212
|
}
|
|
212
213
|
function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
|
|
213
|
-
const baseAs =
|
|
214
|
+
const baseAs = internal.getQueryAs(baseQuery);
|
|
214
215
|
const q = joiningQuery.clone();
|
|
215
|
-
|
|
216
|
+
internal.setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
|
|
216
217
|
for (let i = 0; i < len; i++) {
|
|
217
|
-
|
|
218
|
+
internal.pushQueryOnForOuter(
|
|
218
219
|
q,
|
|
219
220
|
baseQuery,
|
|
220
221
|
joiningQuery,
|
|
@@ -230,7 +231,7 @@ const addAutoForeignKey = (tableConfig, from, to, primaryKeys, foreignKeys, opti
|
|
|
230
231
|
let fkeyOptions = options.foreignKey !== void 0 ? options.foreignKey : tableConfig.autoForeignKeys;
|
|
231
232
|
if (!fkeyOptions) return;
|
|
232
233
|
if (fkeyOptions === true) {
|
|
233
|
-
fkeyOptions = tableConfig.autoForeignKeys ||
|
|
234
|
+
fkeyOptions = tableConfig.autoForeignKeys || internal.emptyObject;
|
|
234
235
|
}
|
|
235
236
|
if (foreignKeys.length === 1) {
|
|
236
237
|
const column = from.shape[foreignKeys[0]];
|
|
@@ -303,11 +304,11 @@ const joinQueryChainHOF = (relPKeys, reverseJoin, joinQuery) => (joiningQuery, b
|
|
|
303
304
|
if (jq.q.returnType !== "value" && jq.q.returnType !== "valueOrThrow") {
|
|
304
305
|
let tablePrefix;
|
|
305
306
|
if (jq.q.order) {
|
|
306
|
-
const prefix = tablePrefix =
|
|
307
|
+
const prefix = tablePrefix = internal.getQueryAs(jq) + ".";
|
|
307
308
|
useWhereExist = jq.q.order.every((o) => {
|
|
308
309
|
if (typeof o === "string") {
|
|
309
310
|
return isOwnColumn(prefix, o);
|
|
310
|
-
} else if (
|
|
311
|
+
} else if (internal.isExpression(o)) {
|
|
311
312
|
return false;
|
|
312
313
|
} else {
|
|
313
314
|
for (const key in o) {
|
|
@@ -320,11 +321,11 @@ const joinQueryChainHOF = (relPKeys, reverseJoin, joinQuery) => (joiningQuery, b
|
|
|
320
321
|
});
|
|
321
322
|
}
|
|
322
323
|
if (useWhereExist && jq.q.select) {
|
|
323
|
-
const prefix = tablePrefix ||
|
|
324
|
+
const prefix = tablePrefix || internal.getQueryAs(jq) + ".";
|
|
324
325
|
useWhereExist = jq.q.select.every((s) => {
|
|
325
326
|
if (typeof s === "string") {
|
|
326
327
|
return isOwnColumn(prefix, s);
|
|
327
|
-
} else if (
|
|
328
|
+
} else if (internal.isExpression(s)) {
|
|
328
329
|
return false;
|
|
329
330
|
} else if (!s) {
|
|
330
331
|
return false;
|
|
@@ -362,7 +363,7 @@ const selectRowNumber = (result, relPKeys) => {
|
|
|
362
363
|
const hookSelect = result.q.hookSelect = new Map(
|
|
363
364
|
result.q.hookSelect && [...result.q.hookSelect]
|
|
364
365
|
);
|
|
365
|
-
const as = `"${
|
|
366
|
+
const as = `"${internal.getQueryAs(result)}"`;
|
|
366
367
|
const partitionColumns = relPKeys.map(
|
|
367
368
|
(key) => `${as}."${result.shape[key]?.data.name || key}"`
|
|
368
369
|
);
|
|
@@ -377,9 +378,9 @@ const selectRowNumber = (result, relPKeys) => {
|
|
|
377
378
|
const combineOrdering = (result, joined) => {
|
|
378
379
|
const { order } = joined.q;
|
|
379
380
|
if (order) {
|
|
380
|
-
const as =
|
|
381
|
+
const as = internal.getQueryAs(joined);
|
|
381
382
|
const add = order.map(
|
|
382
|
-
(o) => typeof o === "string" ? `${as}.${o}` :
|
|
383
|
+
(o) => typeof o === "string" ? `${as}.${o}` : internal.isExpression(o) ? o : Object.fromEntries(
|
|
383
384
|
Object.entries(o).map(([key, value]) => [`${as}.${key}`, value])
|
|
384
385
|
)
|
|
385
386
|
);
|
|
@@ -388,18 +389,18 @@ const combineOrdering = (result, joined) => {
|
|
|
388
389
|
}
|
|
389
390
|
};
|
|
390
391
|
const wrapQuery = (joiningQuery, result, item) => {
|
|
391
|
-
const baseOuterQuery =
|
|
392
|
+
const baseOuterQuery = internal.cloneQueryBaseUnscoped(joiningQuery);
|
|
392
393
|
const outer = baseOuterQuery.clone();
|
|
393
|
-
outer.q.and = [new
|
|
394
|
+
outer.q.and = [new internal.DynamicRawSQL(() => new internal.RawSql(`${item.as || "r"} = 1`))];
|
|
394
395
|
outer.q.useFromLimitOffset = true;
|
|
395
|
-
outer.shape =
|
|
396
|
+
outer.shape = internal.getShapeFromSelect(result, true);
|
|
396
397
|
outer.q.select = Object.keys(outer.shape);
|
|
397
398
|
outer.q.returnType = result.q.returnType;
|
|
398
399
|
result.q.returnsOne = true;
|
|
399
400
|
return result.wrap(outer);
|
|
400
401
|
};
|
|
401
402
|
|
|
402
|
-
class BelongsToVirtualColumn extends
|
|
403
|
+
class BelongsToVirtualColumn extends internal.VirtualColumn {
|
|
403
404
|
constructor(schema, key, state) {
|
|
404
405
|
super(schema);
|
|
405
406
|
this.key = key;
|
|
@@ -416,7 +417,7 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
416
417
|
var _a;
|
|
417
418
|
const value = item[key];
|
|
418
419
|
const kind = value.create ? "create" : value.connect ? "connect" : "connectOrCreate";
|
|
419
|
-
|
|
420
|
+
{
|
|
420
421
|
const nestedCreateItem = (_a = nestedCreateItems ?? (nestedCreateItems = {}))[kind] ?? (_a[kind] = {
|
|
421
422
|
items: [],
|
|
422
423
|
values: []
|
|
@@ -425,7 +426,7 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
425
426
|
nestedCreateItem.values.push(value[kind]);
|
|
426
427
|
if (kind !== "connect") {
|
|
427
428
|
for (const key2 of foreignKeys) {
|
|
428
|
-
item[key2] = new
|
|
429
|
+
item[key2] = new internal.RawSql("");
|
|
429
430
|
}
|
|
430
431
|
}
|
|
431
432
|
}
|
|
@@ -441,7 +442,7 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
441
442
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
442
443
|
if (create) {
|
|
443
444
|
const selectPKeys = query.select(...primaryKeys);
|
|
444
|
-
|
|
445
|
+
internal._prependWith(
|
|
445
446
|
q,
|
|
446
447
|
(as) => {
|
|
447
448
|
const count = create.items.length;
|
|
@@ -457,15 +458,15 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
457
458
|
});
|
|
458
459
|
});
|
|
459
460
|
},
|
|
460
|
-
|
|
461
|
+
internal._queryInsertMany(selectPKeys, create.values)
|
|
461
462
|
);
|
|
462
463
|
}
|
|
463
464
|
if (connect) {
|
|
464
465
|
connect.values.forEach((value, itemI) => {
|
|
465
|
-
const as =
|
|
466
|
-
|
|
466
|
+
const as = internal.getFreeAlias(q.q.withShapes, "q");
|
|
467
|
+
internal._prependWith(q, as, query.select(...primaryKeys).findBy(value));
|
|
467
468
|
foreignKeys.map((foreignKey, i) => {
|
|
468
|
-
connect.items[itemI][foreignKey] = new
|
|
469
|
+
connect.items[itemI][foreignKey] = new internal.RawSql(
|
|
469
470
|
selectCteColumnMustExistSql(i, as, primaryKeys[i])
|
|
470
471
|
);
|
|
471
472
|
});
|
|
@@ -479,11 +480,11 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
|
|
|
479
480
|
foreignKeys
|
|
480
481
|
);
|
|
481
482
|
const selectPKeys = query.select(...primaryKeys);
|
|
482
|
-
|
|
483
|
+
internal._prependWith(
|
|
483
484
|
q,
|
|
484
485
|
asFn,
|
|
485
|
-
|
|
486
|
-
|
|
486
|
+
internal._orCreate(
|
|
487
|
+
internal._queryWhere(selectPKeys, [value.where]),
|
|
487
488
|
value.create
|
|
488
489
|
)
|
|
489
490
|
);
|
|
@@ -501,8 +502,8 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
501
502
|
const foreignKeys = relation.options.columns;
|
|
502
503
|
const { on } = relation.options;
|
|
503
504
|
if (on) {
|
|
504
|
-
|
|
505
|
-
|
|
505
|
+
internal._queryWhere(query, [on]);
|
|
506
|
+
internal._queryDefaults(query, on);
|
|
506
507
|
}
|
|
507
508
|
const len = primaryKeys.length;
|
|
508
509
|
const state = { query, primaryKeys, foreignKeys, len, on };
|
|
@@ -515,11 +516,11 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
515
516
|
relation.options
|
|
516
517
|
);
|
|
517
518
|
const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
|
|
518
|
-
const baseAs =
|
|
519
|
+
const baseAs = internal.getQueryAs(baseQuery);
|
|
519
520
|
const q = joiningQuery.clone();
|
|
520
|
-
|
|
521
|
+
internal.setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
|
|
521
522
|
for (let i = 0; i < len; i++) {
|
|
522
|
-
|
|
523
|
+
internal.pushQueryOnForOuter(
|
|
523
524
|
q,
|
|
524
525
|
baseQuery,
|
|
525
526
|
joiningQuery,
|
|
@@ -547,12 +548,12 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
547
548
|
return query.where(obj);
|
|
548
549
|
},
|
|
549
550
|
virtualColumn: new BelongsToVirtualColumn(
|
|
550
|
-
|
|
551
|
+
internal.defaultSchemaConfig,
|
|
551
552
|
relationName,
|
|
552
553
|
state
|
|
553
554
|
),
|
|
554
555
|
joinQuery: joinQueryChainHOF(
|
|
555
|
-
|
|
556
|
+
internal.getPrimaryKeys(query),
|
|
556
557
|
reverseJoin,
|
|
557
558
|
(joiningQuery, baseQuery) => join(
|
|
558
559
|
baseQuery,
|
|
@@ -567,29 +568,29 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
|
|
|
567
568
|
const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
568
569
|
return (self, update, params) => {
|
|
569
570
|
if (params.create) {
|
|
570
|
-
const createQuery =
|
|
571
|
-
|
|
571
|
+
const createQuery = internal._querySelect(
|
|
572
|
+
internal._queryCreate(query.clone(), params.create),
|
|
572
573
|
primaryKeys
|
|
573
574
|
);
|
|
574
575
|
const asFn = setForeignKeysFromCte(update, primaryKeys, foreignKeys);
|
|
575
|
-
|
|
576
|
+
internal._prependWith(self, asFn, createQuery);
|
|
576
577
|
} else if (params.update) {
|
|
577
578
|
let appendedAs;
|
|
578
|
-
|
|
579
|
+
internal._hookSelectColumns(self, foreignKeys, (aliasedForeignKeys) => {
|
|
579
580
|
selectIdsSql._sql = selectCteColumnsSql(
|
|
580
581
|
appendedAs,
|
|
581
582
|
aliasedForeignKeys
|
|
582
583
|
);
|
|
583
584
|
});
|
|
584
|
-
const selectIdsSql = new
|
|
585
|
-
const updateQuery =
|
|
586
|
-
|
|
585
|
+
const selectIdsSql = new internal.RawSql("");
|
|
586
|
+
const updateQuery = internal._queryUpdate(
|
|
587
|
+
internal._queryWhereIn(query.clone(), true, primaryKeys, selectIdsSql),
|
|
587
588
|
params.update
|
|
588
589
|
);
|
|
589
590
|
updateQuery.q.returnType = "value";
|
|
590
|
-
|
|
591
|
+
internal._appendQuery(self, updateQuery, (as) => appendedAs = as);
|
|
591
592
|
} else if (params.upsert) {
|
|
592
|
-
if (
|
|
593
|
+
if (internal.isQueryReturnsAll(self)) {
|
|
593
594
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
594
595
|
}
|
|
595
596
|
const { relQuery } = relWithSelectIds(
|
|
@@ -598,17 +599,17 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
598
599
|
primaryKeys,
|
|
599
600
|
foreignKeys
|
|
600
601
|
);
|
|
601
|
-
const upsertQuery =
|
|
602
|
-
|
|
602
|
+
const upsertQuery = internal._querySelect(
|
|
603
|
+
internal._queryUpsert(
|
|
603
604
|
relQuery,
|
|
604
605
|
params.upsert
|
|
605
606
|
),
|
|
606
607
|
primaryKeys
|
|
607
608
|
);
|
|
608
609
|
const asFn = setForeignKeysFromCte(update, primaryKeys, foreignKeys);
|
|
609
|
-
|
|
610
|
+
internal._prependWith(self, asFn, upsertQuery);
|
|
610
611
|
} else if (params.delete) {
|
|
611
|
-
|
|
612
|
+
internal._hookSelectColumns(self, foreignKeys, internal.noop);
|
|
612
613
|
disconnect(update, foreignKeys);
|
|
613
614
|
const { selectIdsSql, relQuery } = relWithSelectIds(
|
|
614
615
|
self,
|
|
@@ -617,10 +618,10 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
617
618
|
foreignKeys
|
|
618
619
|
);
|
|
619
620
|
self.q.and = self.q.or = void 0;
|
|
620
|
-
|
|
621
|
-
const deleteQuery =
|
|
621
|
+
internal._queryWhereIn(self, true, foreignKeys, selectIdsSql);
|
|
622
|
+
const deleteQuery = internal._queryDelete(relQuery);
|
|
622
623
|
deleteQuery.q.returnType = "value";
|
|
623
|
-
|
|
624
|
+
internal._appendQuery(self, deleteQuery, internal.noop);
|
|
624
625
|
} else if (params.disconnect) {
|
|
625
626
|
disconnect(update, foreignKeys);
|
|
626
627
|
} else if (params.set) {
|
|
@@ -642,10 +643,10 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
642
643
|
loadForeignKeys,
|
|
643
644
|
true
|
|
644
645
|
);
|
|
645
|
-
|
|
646
|
+
internal._prependWith(
|
|
646
647
|
self,
|
|
647
648
|
asFn,
|
|
648
|
-
|
|
649
|
+
internal._queryFindBy(query.select(...loadPrimaryKeys), params.set)
|
|
649
650
|
);
|
|
650
651
|
}
|
|
651
652
|
}
|
|
@@ -658,8 +659,8 @@ const disconnect = (update, foreignKeys) => {
|
|
|
658
659
|
};
|
|
659
660
|
const relWithSelectIds = (self, rel, primaryKeys, foreignKeys) => {
|
|
660
661
|
const selectIdsQuery = makeSelectIdsQuery(self, foreignKeys);
|
|
661
|
-
const selectIdsSql = new
|
|
662
|
-
|
|
662
|
+
const selectIdsSql = new internal.RawSql("");
|
|
663
|
+
internal._prependWith(
|
|
663
664
|
self,
|
|
664
665
|
(as) => {
|
|
665
666
|
selectIdsSql._sql = selectCteColumnsSql(as, foreignKeys);
|
|
@@ -668,12 +669,12 @@ const relWithSelectIds = (self, rel, primaryKeys, foreignKeys) => {
|
|
|
668
669
|
);
|
|
669
670
|
return {
|
|
670
671
|
selectIdsSql,
|
|
671
|
-
relQuery:
|
|
672
|
+
relQuery: internal._queryWhereIn(rel.clone(), true, primaryKeys, selectIdsSql)
|
|
672
673
|
};
|
|
673
674
|
};
|
|
674
675
|
const makeSelectIdsQuery = (self, foreignKeys) => {
|
|
675
676
|
const selectIdsQuery = self.baseQuery.clone();
|
|
676
|
-
selectIdsQuery.q.distinct =
|
|
677
|
+
selectIdsQuery.q.distinct = internal.emptyArray;
|
|
677
678
|
selectIdsQuery.q.select = foreignKeys;
|
|
678
679
|
selectIdsQuery.q.and = self.q.and;
|
|
679
680
|
selectIdsQuery.q.or = self.q.or;
|
|
@@ -681,7 +682,7 @@ const makeSelectIdsQuery = (self, foreignKeys) => {
|
|
|
681
682
|
};
|
|
682
683
|
const setForeignKeysFromCte = (record, primaryKeys, foreignKeys, mustExist) => {
|
|
683
684
|
for (const key of foreignKeys) {
|
|
684
|
-
record[key] = new
|
|
685
|
+
record[key] = new internal.RawSql("");
|
|
685
686
|
}
|
|
686
687
|
return (as) => {
|
|
687
688
|
foreignKeys.forEach(
|
|
@@ -705,7 +706,7 @@ const selectCteColumnMustExistSql = (i, cteAs, column) => {
|
|
|
705
706
|
return i === 0 ? `CASE WHEN (SELECT count(*) FROM "${cteAs}") = 0 AND (SELECT 'not-found')::int = 0 THEN NULL ELSE ${selectColumn} END` : selectColumn;
|
|
706
707
|
};
|
|
707
708
|
|
|
708
|
-
class HasOneVirtualColumn extends
|
|
709
|
+
class HasOneVirtualColumn extends internal.VirtualColumn {
|
|
709
710
|
constructor(schema, key, state) {
|
|
710
711
|
super(schema);
|
|
711
712
|
this.key = key;
|
|
@@ -724,7 +725,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
724
725
|
var _a;
|
|
725
726
|
const value = item[this.key];
|
|
726
727
|
const kind = value.create ? "create" : value.connect ? "connect" : "connectOrCreate";
|
|
727
|
-
|
|
728
|
+
{
|
|
728
729
|
const nestedCreateItem = (_a = nestedCreateItems ?? (nestedCreateItems = {}))[kind] ?? (_a[kind] = {
|
|
729
730
|
indexes: [],
|
|
730
731
|
items: [],
|
|
@@ -734,7 +735,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
734
735
|
nestedCreateItem.values.push(value[kind]);
|
|
735
736
|
const data = value.create ? { ...value.create } : {};
|
|
736
737
|
for (const key of foreignKeys) {
|
|
737
|
-
data[key] = new
|
|
738
|
+
data[key] = new internal.RawSql("");
|
|
738
739
|
}
|
|
739
740
|
nestedCreateItem.items.push(data);
|
|
740
741
|
}
|
|
@@ -745,7 +746,7 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
745
746
|
let createAs;
|
|
746
747
|
let connectAs;
|
|
747
748
|
let connectOrCreateAs;
|
|
748
|
-
|
|
749
|
+
internal._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
749
750
|
foreignKeys.forEach((key, keyI) => {
|
|
750
751
|
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
751
752
|
if (create && createAs) {
|
|
@@ -782,29 +783,29 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
782
783
|
});
|
|
783
784
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
784
785
|
if (create) {
|
|
785
|
-
const query =
|
|
786
|
-
|
|
786
|
+
const query = internal._queryInsertMany(internal._clone(rel), create.items);
|
|
787
|
+
internal._appendQuery(self, query, (as) => createAs = as);
|
|
787
788
|
}
|
|
788
789
|
if (connect) {
|
|
789
790
|
connect.values.forEach((value, i) => {
|
|
790
|
-
const query =
|
|
791
|
+
const query = internal._queryUpdateOrThrow(
|
|
791
792
|
rel.where(value),
|
|
792
793
|
connect.items[i]
|
|
793
794
|
);
|
|
794
795
|
query.q.ensureCount = 1;
|
|
795
|
-
|
|
796
|
+
internal._appendQuery(self, query, (as) => connectAs = as);
|
|
796
797
|
});
|
|
797
798
|
}
|
|
798
799
|
if (connectOrCreate) {
|
|
799
800
|
connectOrCreate.values.forEach((value, i) => {
|
|
800
|
-
const query =
|
|
801
|
+
const query = internal._queryUpsert(rel.where(value.where), {
|
|
801
802
|
update: connectOrCreate.items[i],
|
|
802
803
|
create: {
|
|
803
804
|
...value.create,
|
|
804
805
|
...connectOrCreate.items[i]
|
|
805
806
|
}
|
|
806
807
|
});
|
|
807
|
-
|
|
808
|
+
internal._appendQuery(self, query, (as) => connectOrCreateAs = as);
|
|
808
809
|
});
|
|
809
810
|
}
|
|
810
811
|
} else {
|
|
@@ -821,14 +822,14 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
821
822
|
}
|
|
822
823
|
update(self, set) {
|
|
823
824
|
const params = set[this.key];
|
|
824
|
-
if ((params.set || params.create || params.upsert) &&
|
|
825
|
+
if ((params.set || params.create || params.upsert) && internal.isQueryReturnsAll(self)) {
|
|
825
826
|
const key = params.set ? "set" : params.create ? "create" : "upsert";
|
|
826
827
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
827
828
|
}
|
|
828
829
|
const { primaryKeys, foreignKeys, query: relQuery } = this.state;
|
|
829
830
|
if (params.create || params.update || params.upsert || params.disconnect || params.set || params.delete) {
|
|
830
831
|
let appendedAs;
|
|
831
|
-
|
|
832
|
+
internal._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
832
833
|
selectIdsSql._sql = selectCteColumnsSql(
|
|
833
834
|
appendedAs,
|
|
834
835
|
aliasedPrimaryKeys
|
|
@@ -842,9 +843,9 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
842
843
|
});
|
|
843
844
|
}
|
|
844
845
|
});
|
|
845
|
-
const selectIdsSql = new
|
|
846
|
-
const existingRelQuery =
|
|
847
|
-
|
|
846
|
+
const selectIdsSql = new internal.RawSql("");
|
|
847
|
+
const existingRelQuery = internal._queryWhereIn(
|
|
848
|
+
internal._clone(relQuery),
|
|
848
849
|
true,
|
|
849
850
|
foreignKeys,
|
|
850
851
|
selectIdsSql
|
|
@@ -853,37 +854,37 @@ class HasOneVirtualColumn extends pqb.VirtualColumn {
|
|
|
853
854
|
if (params.create || params.set || params.upsert) {
|
|
854
855
|
setIds = {};
|
|
855
856
|
foreignKeys.forEach((foreignKey) => {
|
|
856
|
-
setIds[foreignKey] = new
|
|
857
|
+
setIds[foreignKey] = new internal.RawSql("");
|
|
857
858
|
});
|
|
858
859
|
}
|
|
859
|
-
const nullifyOrDeleteQuery = params.update ?
|
|
860
|
+
const nullifyOrDeleteQuery = params.update ? internal._queryUpdate(existingRelQuery, params.update) : params.upsert ? internal._queryUpsert(existingRelQuery, {
|
|
860
861
|
update: params.upsert.update,
|
|
861
862
|
create: {
|
|
862
863
|
...typeof params.upsert.create === "function" ? params.upsert.create() : params.upsert.create,
|
|
863
864
|
...setIds
|
|
864
865
|
}
|
|
865
|
-
}) : params.delete ?
|
|
866
|
+
}) : params.delete ? internal._queryDelete(existingRelQuery) : internal._queryUpdate(existingRelQuery, this.setNulls);
|
|
866
867
|
nullifyOrDeleteQuery.q.returnType = "void";
|
|
867
|
-
|
|
868
|
+
internal._appendQuery(self, nullifyOrDeleteQuery, (as) => appendedAs = as);
|
|
868
869
|
if (params.create) {
|
|
869
|
-
const createQuery =
|
|
870
|
+
const createQuery = internal._queryInsert(internal._clone(relQuery), {
|
|
870
871
|
...params.create,
|
|
871
872
|
...setIds
|
|
872
873
|
});
|
|
873
|
-
|
|
874
|
+
internal._appendQuery(self, createQuery, internal.noop);
|
|
874
875
|
} else if (params.set) {
|
|
875
|
-
const setQuery =
|
|
876
|
-
|
|
876
|
+
const setQuery = internal._queryUpdate(
|
|
877
|
+
internal._queryWhere(internal._clone(relQuery), [params.set]),
|
|
877
878
|
setIds
|
|
878
879
|
);
|
|
879
880
|
setQuery.q.returnType = "void";
|
|
880
|
-
|
|
881
|
+
internal._appendQuery(self, setQuery, internal.noop);
|
|
881
882
|
}
|
|
882
883
|
}
|
|
883
884
|
}
|
|
884
885
|
}
|
|
885
886
|
const makeHasOneMethod = (tableConfig, table, relation, relationName, query) => {
|
|
886
|
-
const relPKeys =
|
|
887
|
+
const relPKeys = internal.getPrimaryKeys(query);
|
|
887
888
|
if ("through" in relation.options) {
|
|
888
889
|
const { through, source } = relation.options;
|
|
889
890
|
const throughRelation = getThroughRelation(table, through);
|
|
@@ -929,8 +930,8 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
929
930
|
const foreignKeys = relation.options.references;
|
|
930
931
|
const { on } = relation.options;
|
|
931
932
|
if (on) {
|
|
932
|
-
|
|
933
|
-
|
|
933
|
+
internal._queryWhere(query, [on]);
|
|
934
|
+
internal._queryDefaults(query, on);
|
|
934
935
|
}
|
|
935
936
|
addAutoForeignKey(
|
|
936
937
|
tableConfig,
|
|
@@ -963,10 +964,10 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
963
964
|
for (let i = 0; i < len; i++) {
|
|
964
965
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
965
966
|
}
|
|
966
|
-
return
|
|
967
|
+
return internal._queryDefaults(query.where(values), { ...on, ...values });
|
|
967
968
|
},
|
|
968
969
|
virtualColumn: new HasOneVirtualColumn(
|
|
969
|
-
|
|
970
|
+
internal.defaultSchemaConfig,
|
|
970
971
|
relationName,
|
|
971
972
|
state
|
|
972
973
|
),
|
|
@@ -987,7 +988,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
987
988
|
const baseQuery = query2.clone();
|
|
988
989
|
baseQuery.q.select = fromQuerySelect;
|
|
989
990
|
const q = relationQuery.q;
|
|
990
|
-
q.insertFrom =
|
|
991
|
+
q.insertFrom = internal.prepareSubQueryForSql(q, baseQuery);
|
|
991
992
|
q.values = [];
|
|
992
993
|
};
|
|
993
994
|
}
|
|
@@ -1010,10 +1011,10 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1010
1011
|
primaryKeys.forEach((primaryKey, i2) => {
|
|
1011
1012
|
data2[foreignKeys[i2]] = selfData[primaryKey];
|
|
1012
1013
|
});
|
|
1013
|
-
items[i] = "connect" in item ?
|
|
1014
|
+
items[i] = "connect" in item ? internal._queryUpdateOrThrow(
|
|
1014
1015
|
t.where(item.connect),
|
|
1015
1016
|
data2
|
|
1016
|
-
) :
|
|
1017
|
+
) : internal._queryUpdate(
|
|
1017
1018
|
t.where(
|
|
1018
1019
|
item.connectOrCreate.where
|
|
1019
1020
|
),
|
|
@@ -1051,7 +1052,7 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1051
1052
|
};
|
|
1052
1053
|
};
|
|
1053
1054
|
|
|
1054
|
-
class HasManyVirtualColumn extends
|
|
1055
|
+
class HasManyVirtualColumn extends internal.VirtualColumn {
|
|
1055
1056
|
constructor(schema, key, state) {
|
|
1056
1057
|
super(schema);
|
|
1057
1058
|
this.key = key;
|
|
@@ -1075,7 +1076,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1075
1076
|
const data = value.create.map((obj) => {
|
|
1076
1077
|
const data2 = { ...obj };
|
|
1077
1078
|
for (const key of foreignKeys) {
|
|
1078
|
-
data2[key] = new
|
|
1079
|
+
data2[key] = new internal.RawSql("");
|
|
1079
1080
|
}
|
|
1080
1081
|
return data2;
|
|
1081
1082
|
});
|
|
@@ -1092,7 +1093,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1092
1093
|
nestedCreateItem.values.push(value[kind]);
|
|
1093
1094
|
const data = {};
|
|
1094
1095
|
for (const key of foreignKeys) {
|
|
1095
|
-
data[key] = new
|
|
1096
|
+
data[key] = new internal.RawSql("");
|
|
1096
1097
|
}
|
|
1097
1098
|
nestedCreateItem.items.push(data);
|
|
1098
1099
|
}
|
|
@@ -1104,7 +1105,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1104
1105
|
let createAs;
|
|
1105
1106
|
let connectAs;
|
|
1106
1107
|
let connectOrCreateAs;
|
|
1107
|
-
|
|
1108
|
+
internal._hookSelectColumns(self, primaryKeys, (aliasedPrimaryKeys) => {
|
|
1108
1109
|
foreignKeys.forEach((key, keyI) => {
|
|
1109
1110
|
const primaryKey = aliasedPrimaryKeys[keyI];
|
|
1110
1111
|
if (create && createAs) {
|
|
@@ -1144,34 +1145,34 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1144
1145
|
});
|
|
1145
1146
|
const { create, connect, connectOrCreate } = nestedCreateItems;
|
|
1146
1147
|
if (create) {
|
|
1147
|
-
const query =
|
|
1148
|
-
|
|
1148
|
+
const query = internal._queryInsertMany(
|
|
1149
|
+
internal._clone(rel),
|
|
1149
1150
|
create.items.flat()
|
|
1150
1151
|
);
|
|
1151
|
-
|
|
1152
|
+
internal._appendQuery(self, query, (as) => createAs = as);
|
|
1152
1153
|
}
|
|
1153
1154
|
if (connect) {
|
|
1154
1155
|
connect.values.forEach((value, i) => {
|
|
1155
|
-
const query =
|
|
1156
|
+
const query = internal._queryUpdateOrThrow(
|
|
1156
1157
|
rel.whereOneOf(...value),
|
|
1157
1158
|
connect.items[i]
|
|
1158
1159
|
);
|
|
1159
1160
|
query.q.ensureCount = value.length;
|
|
1160
|
-
|
|
1161
|
+
internal._appendQuery(self, query, (as) => connectAs = as);
|
|
1161
1162
|
});
|
|
1162
1163
|
}
|
|
1163
1164
|
if (connectOrCreate) {
|
|
1164
1165
|
connectOrCreate.values.forEach((array, i) => {
|
|
1165
1166
|
const foreignKeyValues = connectOrCreate.items[i];
|
|
1166
1167
|
for (const value of array) {
|
|
1167
|
-
const query =
|
|
1168
|
+
const query = internal._queryUpsert(rel.where(value.where), {
|
|
1168
1169
|
update: foreignKeyValues,
|
|
1169
1170
|
create: {
|
|
1170
1171
|
...value.create,
|
|
1171
1172
|
...foreignKeyValues
|
|
1172
1173
|
}
|
|
1173
1174
|
});
|
|
1174
|
-
|
|
1175
|
+
internal._appendQuery(self, query, (as) => connectOrCreateAs = as);
|
|
1175
1176
|
}
|
|
1176
1177
|
});
|
|
1177
1178
|
}
|
|
@@ -1189,7 +1190,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1189
1190
|
}
|
|
1190
1191
|
update(q, set) {
|
|
1191
1192
|
const params = set[this.key];
|
|
1192
|
-
if ((params.set || params.create) &&
|
|
1193
|
+
if ((params.set || params.create) && internal.isQueryReturnsAll(q)) {
|
|
1193
1194
|
const key = params.set ? "set" : "create";
|
|
1194
1195
|
throw new Error(`\`${key}\` option is not allowed in a batch update`);
|
|
1195
1196
|
}
|
|
@@ -1203,7 +1204,7 @@ class HasManyVirtualColumn extends pqb.VirtualColumn {
|
|
|
1203
1204
|
}
|
|
1204
1205
|
}
|
|
1205
1206
|
const makeHasManyMethod = (tableConfig, table, relation, relationName, query) => {
|
|
1206
|
-
const relPKeys =
|
|
1207
|
+
const relPKeys = internal.getPrimaryKeys(query);
|
|
1207
1208
|
if ("through" in relation.options) {
|
|
1208
1209
|
const { through, source } = relation.options;
|
|
1209
1210
|
const throughRelation = getThroughRelation(table, through);
|
|
@@ -1252,8 +1253,8 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
1252
1253
|
const foreignKeys = relation.options.references;
|
|
1253
1254
|
const { on } = relation.options;
|
|
1254
1255
|
if (on) {
|
|
1255
|
-
|
|
1256
|
-
|
|
1256
|
+
internal._queryWhere(query, [on]);
|
|
1257
|
+
internal._queryDefaults(query, on);
|
|
1257
1258
|
}
|
|
1258
1259
|
addAutoForeignKey(
|
|
1259
1260
|
tableConfig,
|
|
@@ -1286,10 +1287,10 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
1286
1287
|
for (let i = 0; i < len; i++) {
|
|
1287
1288
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
1288
1289
|
}
|
|
1289
|
-
return
|
|
1290
|
+
return internal._queryDefaults(query.where(values), { ...on, ...values });
|
|
1290
1291
|
},
|
|
1291
1292
|
virtualColumn: new HasManyVirtualColumn(
|
|
1292
|
-
|
|
1293
|
+
internal.defaultSchemaConfig,
|
|
1293
1294
|
relationName,
|
|
1294
1295
|
state
|
|
1295
1296
|
),
|
|
@@ -1310,7 +1311,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
|
|
|
1310
1311
|
const baseQuery = query2.clone();
|
|
1311
1312
|
baseQuery.q.select = fromQuerySelect;
|
|
1312
1313
|
const q = relationQuery.q;
|
|
1313
|
-
q.insertFrom =
|
|
1314
|
+
q.insertFrom = internal.prepareSubQueryForSql(q, baseQuery);
|
|
1314
1315
|
q.values = [];
|
|
1315
1316
|
};
|
|
1316
1317
|
}
|
|
@@ -1322,7 +1323,7 @@ const getWhereForNestedUpdate = (t, data, params, primaryKeys, foreignKeys) => {
|
|
|
1322
1323
|
columns: foreignKeys,
|
|
1323
1324
|
values: data.map((item) => primaryKeys.map((key) => item[key]))
|
|
1324
1325
|
},
|
|
1325
|
-
OR: params ?
|
|
1326
|
+
OR: params ? internal.toArray(params) : void 0
|
|
1326
1327
|
});
|
|
1327
1328
|
};
|
|
1328
1329
|
const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
@@ -1342,7 +1343,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1342
1343
|
for (let i2 = 0; i2 < len2; i2++) {
|
|
1343
1344
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
1344
1345
|
}
|
|
1345
|
-
items[i] =
|
|
1346
|
+
items[i] = internal._queryUpdateOrThrow(
|
|
1346
1347
|
t.where({ OR: connect }),
|
|
1347
1348
|
obj
|
|
1348
1349
|
);
|
|
@@ -1366,7 +1367,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1366
1367
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
1367
1368
|
}
|
|
1368
1369
|
queries.push(
|
|
1369
|
-
|
|
1370
|
+
internal._queryUpdate(
|
|
1370
1371
|
t.where(item.where),
|
|
1371
1372
|
obj
|
|
1372
1373
|
)
|
|
@@ -1420,7 +1421,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1420
1421
|
}
|
|
1421
1422
|
}
|
|
1422
1423
|
}
|
|
1423
|
-
await
|
|
1424
|
+
await internal._queryCreateMany(t, records);
|
|
1424
1425
|
}
|
|
1425
1426
|
};
|
|
1426
1427
|
};
|
|
@@ -1451,8 +1452,8 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1451
1452
|
for (let i = 0; i < len; i++) {
|
|
1452
1453
|
obj[foreignKeys[i]] = data[0][primaryKeys[i]];
|
|
1453
1454
|
}
|
|
1454
|
-
const relatedWheres =
|
|
1455
|
-
const count = await
|
|
1455
|
+
const relatedWheres = internal.toArray(params.add);
|
|
1456
|
+
const count = await internal._queryUpdate(
|
|
1456
1457
|
t.where({ OR: relatedWheres }),
|
|
1457
1458
|
obj
|
|
1458
1459
|
);
|
|
@@ -1468,7 +1469,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1468
1469
|
for (const foreignKey of foreignKeys) {
|
|
1469
1470
|
obj[foreignKey] = null;
|
|
1470
1471
|
}
|
|
1471
|
-
const setConditions = params.set && (Array.isArray(params.set) ? params.set.length :
|
|
1472
|
+
const setConditions = params.set && (Array.isArray(params.set) ? params.set.length : internal.objectHasValues(params.set)) && (Array.isArray(params.set) ? {
|
|
1472
1473
|
OR: params.set
|
|
1473
1474
|
} : params.set);
|
|
1474
1475
|
let queryToDisconnect = getWhereForNestedUpdate(
|
|
@@ -1481,13 +1482,13 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1481
1482
|
if (setConditions) {
|
|
1482
1483
|
queryToDisconnect = queryToDisconnect.whereNot(setConditions);
|
|
1483
1484
|
}
|
|
1484
|
-
await
|
|
1485
|
+
await internal._queryUpdate(queryToDisconnect, obj);
|
|
1485
1486
|
if (setConditions) {
|
|
1486
1487
|
const obj2 = {};
|
|
1487
1488
|
for (let i = 0; i < len; i++) {
|
|
1488
1489
|
obj2[foreignKeys[i]] = data[0][primaryKeys[i]];
|
|
1489
1490
|
}
|
|
1490
|
-
await
|
|
1491
|
+
await internal._queryUpdate(
|
|
1491
1492
|
t.where(setConditions),
|
|
1492
1493
|
obj2
|
|
1493
1494
|
);
|
|
@@ -1502,15 +1503,15 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1502
1503
|
foreignKeys
|
|
1503
1504
|
);
|
|
1504
1505
|
if (params.delete) {
|
|
1505
|
-
await
|
|
1506
|
+
await internal._queryDelete(q);
|
|
1506
1507
|
} else if (params.update) {
|
|
1507
|
-
await
|
|
1508
|
+
await internal._queryUpdate(q, params.update.data);
|
|
1508
1509
|
}
|
|
1509
1510
|
}
|
|
1510
1511
|
};
|
|
1511
1512
|
};
|
|
1512
1513
|
|
|
1513
|
-
class HasAndBelongsToManyVirtualColumn extends
|
|
1514
|
+
class HasAndBelongsToManyVirtualColumn extends internal.VirtualColumn {
|
|
1514
1515
|
constructor(joinTable, schema, key, state) {
|
|
1515
1516
|
super(schema);
|
|
1516
1517
|
this.joinTable = joinTable;
|
|
@@ -1558,18 +1559,18 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1558
1559
|
const throughPrimaryKeys = options.through.references;
|
|
1559
1560
|
const { on } = options;
|
|
1560
1561
|
if (on) {
|
|
1561
|
-
|
|
1562
|
-
|
|
1562
|
+
internal._queryWhere(query, [on]);
|
|
1563
|
+
internal._queryDefaults(query, on);
|
|
1563
1564
|
}
|
|
1564
1565
|
const foreignKeysFull = foreignKeys.map((key, i) => {
|
|
1565
|
-
if (snakeCase) key = foreignKeys[i] =
|
|
1566
|
+
if (snakeCase) key = foreignKeys[i] = internal.toSnakeCase(key);
|
|
1566
1567
|
return `${joinTable}.${key}`;
|
|
1567
1568
|
});
|
|
1568
1569
|
const throughForeignKeysFull = throughForeignKeys.map((key, i) => {
|
|
1569
|
-
if (snakeCase) key = throughForeignKeys[i] =
|
|
1570
|
+
if (snakeCase) key = throughForeignKeys[i] = internal.toSnakeCase(key);
|
|
1570
1571
|
return `${joinTable}.${key}`;
|
|
1571
1572
|
});
|
|
1572
|
-
const foreignTable =
|
|
1573
|
+
const foreignTable = internal.getQueryAs(query);
|
|
1573
1574
|
const throughPrimaryKeysFull = throughPrimaryKeys.map(
|
|
1574
1575
|
(key) => `${foreignTable}.${key}`
|
|
1575
1576
|
);
|
|
@@ -1633,16 +1634,16 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1633
1634
|
const joinQuery = (joiningQuery, tableAs, foreignAs, joinedShapes) => {
|
|
1634
1635
|
const cloned = joiningQuery.clone();
|
|
1635
1636
|
cloned.q.joinedShapes = joinedShapes;
|
|
1636
|
-
return
|
|
1637
|
+
return internal._queryWhereExists(cloned, subQuery, [
|
|
1637
1638
|
(q) => {
|
|
1638
1639
|
for (let i = 0; i < throughLen; i++) {
|
|
1639
|
-
|
|
1640
|
+
internal._queryJoinOn(q, [
|
|
1640
1641
|
throughForeignKeysFull[i],
|
|
1641
1642
|
`${foreignAs}.${throughPrimaryKeys[i]}`
|
|
1642
1643
|
]);
|
|
1643
1644
|
}
|
|
1644
1645
|
for (let i = 0; i < len; i++) {
|
|
1645
|
-
|
|
1646
|
+
internal._queryJoinOn(q, [foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`]);
|
|
1646
1647
|
}
|
|
1647
1648
|
return q;
|
|
1648
1649
|
}
|
|
@@ -1654,10 +1655,10 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1654
1655
|
}
|
|
1655
1656
|
const selectPrimaryKeysAsForeignKeys = [{ selectAs: obj }];
|
|
1656
1657
|
const reverseJoin = (baseQuery2, joiningQuery) => {
|
|
1657
|
-
const foreignAs =
|
|
1658
|
+
const foreignAs = internal.getQueryAs(joiningQuery);
|
|
1658
1659
|
return joinQuery(
|
|
1659
1660
|
baseQuery2,
|
|
1660
|
-
|
|
1661
|
+
internal.getQueryAs(baseQuery2),
|
|
1661
1662
|
foreignAs,
|
|
1662
1663
|
{
|
|
1663
1664
|
...baseQuery2.q.joinedShapes,
|
|
@@ -1675,28 +1676,28 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1675
1676
|
where[foreignKeysFull[i]] = params[primaryKeys[i]];
|
|
1676
1677
|
}
|
|
1677
1678
|
for (let i = 0; i < throughLen; i++) {
|
|
1678
|
-
|
|
1679
|
+
internal._queryJoinOn(q2, [
|
|
1679
1680
|
throughForeignKeysFull[i],
|
|
1680
1681
|
throughPrimaryKeysFull[i]
|
|
1681
1682
|
]);
|
|
1682
1683
|
}
|
|
1683
|
-
return
|
|
1684
|
+
return internal._queryWhere(q2, [where]);
|
|
1684
1685
|
});
|
|
1685
|
-
return on ?
|
|
1686
|
+
return on ? internal._queryDefaults(q, on) : q;
|
|
1686
1687
|
},
|
|
1687
1688
|
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
1688
1689
|
subQuery,
|
|
1689
|
-
|
|
1690
|
+
internal.defaultSchemaConfig,
|
|
1690
1691
|
relationName,
|
|
1691
1692
|
state
|
|
1692
1693
|
),
|
|
1693
1694
|
joinQuery: joinQueryChainHOF(
|
|
1694
|
-
|
|
1695
|
+
internal.getPrimaryKeys(query),
|
|
1695
1696
|
reverseJoin,
|
|
1696
1697
|
(joiningQuery, baseQuery2) => joinQuery(
|
|
1697
1698
|
joiningQuery,
|
|
1698
|
-
|
|
1699
|
-
|
|
1699
|
+
internal.getQueryAs(baseQuery2),
|
|
1700
|
+
internal.getQueryAs(joiningQuery),
|
|
1700
1701
|
{
|
|
1701
1702
|
...joiningQuery.q.joinedShapes,
|
|
1702
1703
|
[baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
|
|
@@ -1706,7 +1707,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1706
1707
|
reverseJoin,
|
|
1707
1708
|
modifyRelatedQuery(relationQuery) {
|
|
1708
1709
|
const ref = {};
|
|
1709
|
-
|
|
1710
|
+
internal._queryHookAfterCreate(
|
|
1710
1711
|
relationQuery,
|
|
1711
1712
|
[],
|
|
1712
1713
|
async (result) => {
|
|
@@ -1719,7 +1720,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
|
|
|
1719
1720
|
}
|
|
1720
1721
|
return dataRow;
|
|
1721
1722
|
});
|
|
1722
|
-
const createdCount = await
|
|
1723
|
+
const createdCount = await internal._queryCreateManyFrom(
|
|
1723
1724
|
subQuery.count(),
|
|
1724
1725
|
baseQuery2,
|
|
1725
1726
|
data
|
|
@@ -1743,11 +1744,11 @@ const queryJoinTable = (state, data, conditions) => {
|
|
|
1743
1744
|
}
|
|
1744
1745
|
});
|
|
1745
1746
|
if (conditions) {
|
|
1746
|
-
|
|
1747
|
+
internal._queryWhere(t, [
|
|
1747
1748
|
{
|
|
1748
1749
|
IN: {
|
|
1749
1750
|
columns: state.throughForeignKeys,
|
|
1750
|
-
values:
|
|
1751
|
+
values: internal._querySelect(
|
|
1751
1752
|
state.relatedTableQuery.where(conditionsToWhereArg(conditions)),
|
|
1752
1753
|
state.throughPrimaryKeys
|
|
1753
1754
|
)
|
|
@@ -1756,10 +1757,10 @@ const queryJoinTable = (state, data, conditions) => {
|
|
|
1756
1757
|
]);
|
|
1757
1758
|
}
|
|
1758
1759
|
if (state.on) {
|
|
1759
|
-
|
|
1760
|
+
internal._queryWhereExists(t, state.relatedTableQuery, [
|
|
1760
1761
|
(q) => {
|
|
1761
1762
|
for (let i = 0; i < state.throughPrimaryKeys.length; i++) {
|
|
1762
|
-
|
|
1763
|
+
internal._queryJoinOn(q, [
|
|
1763
1764
|
state.throughPrimaryKeysFull[i],
|
|
1764
1765
|
state.throughForeignKeysFull[i]
|
|
1765
1766
|
]);
|
|
@@ -1814,7 +1815,7 @@ const nestedInsert = ({
|
|
|
1814
1815
|
for (const [, { connect }] of items) {
|
|
1815
1816
|
for (const item of connect) {
|
|
1816
1817
|
queries.push(
|
|
1817
|
-
|
|
1818
|
+
internal._queryFindBy(
|
|
1818
1819
|
t.select(...throughPrimaryKeys),
|
|
1819
1820
|
item
|
|
1820
1821
|
)
|
|
@@ -1837,7 +1838,7 @@ const nestedInsert = ({
|
|
|
1837
1838
|
for (const [, { connectOrCreate }] of items) {
|
|
1838
1839
|
for (const item of connectOrCreate) {
|
|
1839
1840
|
queries.push(
|
|
1840
|
-
|
|
1841
|
+
internal._queryFindByOptional(
|
|
1841
1842
|
t.select(...throughPrimaryKeys),
|
|
1842
1843
|
item.where
|
|
1843
1844
|
)
|
|
@@ -1880,7 +1881,7 @@ const nestedInsert = ({
|
|
|
1880
1881
|
}
|
|
1881
1882
|
}
|
|
1882
1883
|
}
|
|
1883
|
-
created = await
|
|
1884
|
+
created = await internal._queryCreateMany(
|
|
1884
1885
|
t.select(...throughPrimaryKeys),
|
|
1885
1886
|
records2
|
|
1886
1887
|
);
|
|
@@ -1941,8 +1942,8 @@ const nestedUpdate = (state) => {
|
|
|
1941
1942
|
const throughLen = state.throughPrimaryKeys.length;
|
|
1942
1943
|
return async (query, data, params) => {
|
|
1943
1944
|
if (params.create) {
|
|
1944
|
-
const idsRows = await
|
|
1945
|
-
|
|
1945
|
+
const idsRows = await internal._queryCreateMany(
|
|
1946
|
+
internal._queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
|
|
1946
1947
|
params.create
|
|
1947
1948
|
);
|
|
1948
1949
|
const records = [];
|
|
@@ -1962,16 +1963,16 @@ const nestedUpdate = (state) => {
|
|
|
1962
1963
|
await state.joinTableQuery.createMany(records);
|
|
1963
1964
|
}
|
|
1964
1965
|
if (params.update) {
|
|
1965
|
-
await
|
|
1966
|
-
|
|
1966
|
+
await internal._queryUpdate(
|
|
1967
|
+
internal._queryWhere(
|
|
1967
1968
|
state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
|
|
1968
1969
|
for (let i = 0; i < throughLen; i++) {
|
|
1969
|
-
|
|
1970
|
+
internal._queryJoinOn(q, [
|
|
1970
1971
|
state.throughForeignKeysFull[i],
|
|
1971
1972
|
state.throughPrimaryKeysFull[i]
|
|
1972
1973
|
]);
|
|
1973
1974
|
}
|
|
1974
|
-
return
|
|
1975
|
+
return internal._queryWhere(q, [
|
|
1975
1976
|
{
|
|
1976
1977
|
IN: {
|
|
1977
1978
|
columns: state.foreignKeysFull,
|
|
@@ -1989,14 +1990,14 @@ const nestedUpdate = (state) => {
|
|
|
1989
1990
|
}
|
|
1990
1991
|
if (params.add) {
|
|
1991
1992
|
const as = query.table;
|
|
1992
|
-
const relatedWheres =
|
|
1993
|
+
const relatedWheres = internal.toArray(params.add);
|
|
1993
1994
|
const joinTableColumns = [
|
|
1994
1995
|
...state.foreignKeys,
|
|
1995
1996
|
...state.throughForeignKeys
|
|
1996
1997
|
];
|
|
1997
1998
|
try {
|
|
1998
1999
|
const count = await state.joinTableQuery.insertForEachFrom(
|
|
1999
|
-
|
|
2000
|
+
internal._querySelect(
|
|
2000
2001
|
state.relatedTableQuery.whereOneOf(...relatedWheres),
|
|
2001
2002
|
[
|
|
2002
2003
|
Object.fromEntries([
|
|
@@ -2018,7 +2019,7 @@ const nestedUpdate = (state) => {
|
|
|
2018
2019
|
state.primaryKeysShape[key]
|
|
2019
2020
|
])
|
|
2020
2021
|
),
|
|
2021
|
-
data.map((x) =>
|
|
2022
|
+
data.map((x) => internal.pick(x, state.primaryKeys))
|
|
2022
2023
|
)
|
|
2023
2024
|
).onConflict(joinTableColumns).merge([state.foreignKeys[0]]);
|
|
2024
2025
|
if (count < data.length * relatedWheres.length) {
|
|
@@ -2040,16 +2041,16 @@ const nestedUpdate = (state) => {
|
|
|
2040
2041
|
}
|
|
2041
2042
|
}
|
|
2042
2043
|
if (params.disconnect) {
|
|
2043
|
-
await
|
|
2044
|
+
await internal._queryDelete(
|
|
2044
2045
|
queryJoinTable(state, data, params.disconnect)
|
|
2045
2046
|
);
|
|
2046
2047
|
}
|
|
2047
2048
|
if (params.delete) {
|
|
2048
2049
|
const j = queryJoinTable(state, data, params.delete);
|
|
2049
|
-
const idsRows = await
|
|
2050
|
-
|
|
2050
|
+
const idsRows = await internal._queryDelete(
|
|
2051
|
+
internal._queryRows(internal._querySelect(j, state.throughForeignKeys))
|
|
2051
2052
|
);
|
|
2052
|
-
await
|
|
2053
|
+
await internal._queryDelete(
|
|
2053
2054
|
state.relatedTableQuery.where({
|
|
2054
2055
|
IN: {
|
|
2055
2056
|
columns: state.throughPrimaryKeys,
|
|
@@ -2060,10 +2061,10 @@ const nestedUpdate = (state) => {
|
|
|
2060
2061
|
}
|
|
2061
2062
|
if (params.set) {
|
|
2062
2063
|
const j = queryJoinTable(state, data);
|
|
2063
|
-
await
|
|
2064
|
-
if (Array.isArray(params.set) ? params.set.length :
|
|
2065
|
-
const idsRows = await
|
|
2066
|
-
|
|
2064
|
+
await internal._queryDelete(j);
|
|
2065
|
+
if (Array.isArray(params.set) ? params.set.length : internal.objectHasValues(params.set)) {
|
|
2066
|
+
const idsRows = await internal._queryRows(
|
|
2067
|
+
internal._querySelect(
|
|
2067
2068
|
state.relatedTableQuery.where(
|
|
2068
2069
|
conditionsToWhereArg(params.set)
|
|
2069
2070
|
),
|
|
@@ -2195,9 +2196,9 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
|
|
|
2195
2196
|
}
|
|
2196
2197
|
if (data.returns === "one") {
|
|
2197
2198
|
if (relation.options.required) {
|
|
2198
|
-
|
|
2199
|
+
internal._queryTake(query);
|
|
2199
2200
|
} else {
|
|
2200
|
-
|
|
2201
|
+
internal._queryTakeOptional(query);
|
|
2201
2202
|
}
|
|
2202
2203
|
query.q.returnsOne = true;
|
|
2203
2204
|
}
|
|
@@ -2271,9 +2272,9 @@ const orchidORMWithAdapter = ({
|
|
|
2271
2272
|
} else {
|
|
2272
2273
|
adapter = options.adapter;
|
|
2273
2274
|
asyncStorage = new node_async_hooks.AsyncLocalStorage();
|
|
2274
|
-
qb =
|
|
2275
|
+
qb = internal._initQueryBuilder(
|
|
2275
2276
|
adapter,
|
|
2276
|
-
|
|
2277
|
+
internal.makeColumnTypes(internal.defaultSchemaConfig),
|
|
2277
2278
|
asyncStorage,
|
|
2278
2279
|
commonOptions,
|
|
2279
2280
|
options
|
|
@@ -2318,7 +2319,7 @@ const orchidORMWithAdapter = ({
|
|
|
2318
2319
|
computed: table.computed,
|
|
2319
2320
|
nowSQL: tableClass.nowSQL
|
|
2320
2321
|
};
|
|
2321
|
-
const dbTable = new
|
|
2322
|
+
const dbTable = new internal.Db(
|
|
2322
2323
|
adapter,
|
|
2323
2324
|
// eslint-disable-next-line @typescript-eslint/no-explicit-any
|
|
2324
2325
|
qb,
|
|
@@ -2361,7 +2362,7 @@ const createRepo = (table, methods) => {
|
|
|
2361
2362
|
const proto = Object.create(q2.baseQuery);
|
|
2362
2363
|
proto.baseQuery = proto;
|
|
2363
2364
|
const result = Object.create(proto);
|
|
2364
|
-
result.q =
|
|
2365
|
+
result.q = internal.getClonedQueryData(q2.q);
|
|
2365
2366
|
if (plainMethods) {
|
|
2366
2367
|
Object.assign(proto.baseQuery, plainMethods);
|
|
2367
2368
|
}
|