orchid-orm 1.19.0 → 1.20.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 +10 -9
- package/dist/index.js +210 -117
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +215 -122
- package/dist/index.mjs.map +1 -1
- package/package.json +7 -6
package/dist/index.mjs
CHANGED
|
@@ -1,35 +1,43 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { makeColumnTypes, defaultSchemaConfig, QueryHooks, getColumnTypes, _queryHookAfterCreate, _queryHookAfterUpdate, getQueryAs, setQueryObjectValue, pushQueryOn, VirtualColumn, pushQueryValue, _queryCreateMany, isQueryReturnsAll, _queryHookBeforeUpdate, _queryFindBy, _queryCreate, _queryRows, _queryUpdate, _queryDelete, _queryDefaults, _queryUpdateOrThrow, _queryWhere, toSQLCacheKey, _queryJoinOn, OrchidOrmInternalError, _queryCreateFrom, NotFoundError, _queryFindByOptional, _querySelect, _queryTake, _queryTakeOptional, _queryAll, Adapter, Db, anyShape, addComputedColumns, getClonedQueryData } from 'pqb';
|
|
2
2
|
export * from 'pqb';
|
|
3
3
|
import { getStackTrace, applyMixins, getCallerFilePath, snakeCaseKey, toSnakeCase, emptyArray, toArray } from 'orchid-core';
|
|
4
4
|
import { AsyncLocalStorage } from 'node:async_hooks';
|
|
5
5
|
|
|
6
6
|
function createBaseTable({
|
|
7
|
+
schemaConfig = defaultSchemaConfig,
|
|
7
8
|
columnTypes: columnTypesArg,
|
|
8
9
|
snakeCase,
|
|
9
10
|
filePath: filePathArg,
|
|
10
11
|
nowSQL,
|
|
11
12
|
exportAs = "BaseTable",
|
|
12
|
-
language
|
|
13
|
-
schemaProvider: schemaProviderArg
|
|
13
|
+
language
|
|
14
14
|
} = {}) {
|
|
15
15
|
var _a;
|
|
16
|
-
const columnTypes
|
|
17
|
-
columnTypes
|
|
18
|
-
) : columnTypesArg || columnTypes;
|
|
16
|
+
const columnTypes = typeof columnTypesArg === "function" ? columnTypesArg(makeColumnTypes(schemaConfig)) : columnTypesArg || makeColumnTypes(defaultSchemaConfig);
|
|
19
17
|
const filePathOrStack = filePathArg || getStackTrace();
|
|
20
18
|
let filePath;
|
|
21
|
-
function schemaProvider() {
|
|
22
|
-
const schema = schemaProviderArg.call(this);
|
|
23
|
-
this.schema = () => schema;
|
|
24
|
-
return schema;
|
|
25
|
-
}
|
|
26
19
|
const base = (_a = class {
|
|
27
20
|
constructor() {
|
|
28
21
|
this.snakeCase = snakeCase;
|
|
29
|
-
this.types = columnTypes
|
|
22
|
+
this.types = columnTypes;
|
|
30
23
|
this.q = {};
|
|
31
24
|
this.language = language;
|
|
32
25
|
}
|
|
26
|
+
static inputSchema() {
|
|
27
|
+
return this._inputSchema === void 0 ? this._inputSchema = schemaConfig.inputSchema.call(this) : this._inputSchema;
|
|
28
|
+
}
|
|
29
|
+
static outputSchema() {
|
|
30
|
+
return this._outputSchema === void 0 ? this._outputSchema = schemaConfig.outputSchema.call(this) : this._outputSchema;
|
|
31
|
+
}
|
|
32
|
+
static querySchema() {
|
|
33
|
+
return this._querySchema === void 0 ? this._querySchema = schemaConfig.querySchema.call(this) : this._querySchema;
|
|
34
|
+
}
|
|
35
|
+
static updateSchema() {
|
|
36
|
+
return this._updateSchema === void 0 ? this._updateSchema = schemaConfig.updateSchema.call(this) : this._updateSchema;
|
|
37
|
+
}
|
|
38
|
+
static pkeySchema() {
|
|
39
|
+
return this._pkeySchema === void 0 ? this._pkeySchema = schemaConfig.pkeySchema.call(this) : this._pkeySchema;
|
|
40
|
+
}
|
|
33
41
|
static getFilePath() {
|
|
34
42
|
if (filePath)
|
|
35
43
|
return filePath;
|
|
@@ -63,8 +71,8 @@ function createBaseTable({
|
|
|
63
71
|
);
|
|
64
72
|
}
|
|
65
73
|
setColumns(fn) {
|
|
66
|
-
columnTypes
|
|
67
|
-
const shape = getColumnTypes(columnTypes
|
|
74
|
+
columnTypes[snakeCaseKey] = this.snakeCase;
|
|
75
|
+
const shape = getColumnTypes(columnTypes, fn, nowSQL, this.language);
|
|
68
76
|
if (this.snakeCase) {
|
|
69
77
|
for (const key in shape) {
|
|
70
78
|
const column = shape[key];
|
|
@@ -76,7 +84,7 @@ function createBaseTable({
|
|
|
76
84
|
}
|
|
77
85
|
}
|
|
78
86
|
}
|
|
79
|
-
return shape;
|
|
87
|
+
return this.constructor.prototype.columns = shape;
|
|
80
88
|
}
|
|
81
89
|
setComputed(computed) {
|
|
82
90
|
return computed;
|
|
@@ -112,9 +120,9 @@ function createBaseTable({
|
|
|
112
120
|
options
|
|
113
121
|
};
|
|
114
122
|
}
|
|
115
|
-
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a
|
|
123
|
+
}, _a.nowSQL = nowSQL, _a.exportAs = exportAs, _a);
|
|
116
124
|
applyMixins(base, [QueryHooks]);
|
|
117
|
-
base.prototype.types = columnTypes
|
|
125
|
+
base.prototype.types = columnTypes;
|
|
118
126
|
return base;
|
|
119
127
|
}
|
|
120
128
|
|
|
@@ -141,7 +149,8 @@ const hasRelationHandleCreate = (q, ctx, item, rowIndex, key, primaryKeys, neste
|
|
|
141
149
|
q.q.wrapInTransaction = true;
|
|
142
150
|
const relationData = [values];
|
|
143
151
|
store.hasRelation[key] = relationData;
|
|
144
|
-
|
|
152
|
+
_queryHookAfterCreate(
|
|
153
|
+
q,
|
|
145
154
|
primaryKeys,
|
|
146
155
|
(rows, q2) => nestedInsert(
|
|
147
156
|
q2,
|
|
@@ -158,7 +167,7 @@ const hasRelationHandleUpdate = (q, set, key, primaryKeys, nestedUpdate) => {
|
|
|
158
167
|
return;
|
|
159
168
|
selectIfNotSelected(q, primaryKeys);
|
|
160
169
|
q.q.wrapInTransaction = true;
|
|
161
|
-
|
|
170
|
+
_queryHookAfterUpdate(q, q.primaryKeys, (rows, q2) => {
|
|
162
171
|
return nestedUpdate(
|
|
163
172
|
q2,
|
|
164
173
|
rows,
|
|
@@ -225,8 +234,8 @@ const joinQueryChainingHOF = (reverseJoin, joinQuery) => (joiningQuery, baseQuer
|
|
|
225
234
|
};
|
|
226
235
|
|
|
227
236
|
class BelongsToVirtualColumn extends VirtualColumn {
|
|
228
|
-
constructor(key, state) {
|
|
229
|
-
super();
|
|
237
|
+
constructor(schema, key, state) {
|
|
238
|
+
super(schema);
|
|
230
239
|
this.key = key;
|
|
231
240
|
this.state = state;
|
|
232
241
|
this.nestedInsert = nestedInsert$3(this.state);
|
|
@@ -303,7 +312,11 @@ const makeBelongsToMethod = (relation, relationName, query) => {
|
|
|
303
312
|
method(params) {
|
|
304
313
|
return query.where(makeWhere(params));
|
|
305
314
|
},
|
|
306
|
-
virtualColumn: new BelongsToVirtualColumn(
|
|
315
|
+
virtualColumn: new BelongsToVirtualColumn(
|
|
316
|
+
defaultSchemaConfig,
|
|
317
|
+
relationName,
|
|
318
|
+
state
|
|
319
|
+
),
|
|
307
320
|
joinQuery: joinQueryChainingHOF(
|
|
308
321
|
reverseJoin,
|
|
309
322
|
(joiningQuery, baseQuery) => join(baseQuery, joiningQuery, primaryKeys, foreignKeys)
|
|
@@ -346,7 +359,7 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
|
|
|
346
359
|
for (let i = 0, len = items.length; i < len; i++) {
|
|
347
360
|
items[i] = "create" in items[i] ? items[i].create : items[i].connectOrCreate.create;
|
|
348
361
|
}
|
|
349
|
-
created = await t.select(...primaryKeys)
|
|
362
|
+
created = await _queryCreateMany(t.select(...primaryKeys), items);
|
|
350
363
|
} else {
|
|
351
364
|
created = emptyArray;
|
|
352
365
|
}
|
|
@@ -380,7 +393,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
380
393
|
throw new Error("`upsert` option is not allowed in a batch update");
|
|
381
394
|
}
|
|
382
395
|
let idsForDelete;
|
|
383
|
-
q
|
|
396
|
+
_queryHookBeforeUpdate(q, async (q2) => {
|
|
384
397
|
if (params.disconnect) {
|
|
385
398
|
for (const key of foreignKeys) {
|
|
386
399
|
update[key] = null;
|
|
@@ -398,7 +411,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
398
411
|
}
|
|
399
412
|
}
|
|
400
413
|
if (loadPrimaryKeys) {
|
|
401
|
-
const record = await query.select(...loadPrimaryKeys)
|
|
414
|
+
const record = await _queryFindBy(query.select(...loadPrimaryKeys), [
|
|
415
|
+
params.set
|
|
416
|
+
]);
|
|
402
417
|
for (let i = 0, len2 = loadPrimaryKeys.length; i < len2; i++) {
|
|
403
418
|
update[loadForeignKeys[i]] = record[loadPrimaryKeys[i]];
|
|
404
419
|
}
|
|
@@ -406,7 +421,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
406
421
|
} else if (params.create) {
|
|
407
422
|
const q3 = query.clone();
|
|
408
423
|
q3.q.select = primaryKeys;
|
|
409
|
-
const record = await q3
|
|
424
|
+
const record = await _queryCreate(q3, params.create);
|
|
410
425
|
for (let i = 0; i < len; i++) {
|
|
411
426
|
update[foreignKeys[i]] = record[primaryKeys[i]];
|
|
412
427
|
}
|
|
@@ -414,7 +429,7 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
414
429
|
const selectQuery = q2.clone();
|
|
415
430
|
selectQuery.q.type = void 0;
|
|
416
431
|
selectQuery.q.distinct = emptyArray;
|
|
417
|
-
idsForDelete = await selectQuery
|
|
432
|
+
idsForDelete = await _queryRows(selectQuery);
|
|
418
433
|
for (const foreignKey of foreignKeys) {
|
|
419
434
|
update[foreignKey] = null;
|
|
420
435
|
}
|
|
@@ -438,17 +453,20 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
438
453
|
obj[primaryKeys[i]] = id;
|
|
439
454
|
}
|
|
440
455
|
if (obj) {
|
|
441
|
-
await
|
|
456
|
+
await _queryUpdate(
|
|
457
|
+
query.findBy(obj),
|
|
458
|
+
upsert.update
|
|
459
|
+
);
|
|
442
460
|
} else {
|
|
443
461
|
const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
|
|
444
|
-
const result = await query.select(...primaryKeys)
|
|
462
|
+
const result = await _queryCreate(query.select(...primaryKeys), data);
|
|
445
463
|
for (let i = 0; i < len; i++) {
|
|
446
464
|
((_a2 = state.updateData) != null ? _a2 : state.updateData = {})[foreignKeys[i]] = result[primaryKeys[i]];
|
|
447
465
|
}
|
|
448
466
|
}
|
|
449
467
|
});
|
|
450
468
|
} else if (params.delete || params.update) {
|
|
451
|
-
q
|
|
469
|
+
_queryHookAfterUpdate(q, [], async (data) => {
|
|
452
470
|
let ids;
|
|
453
471
|
if (params.delete) {
|
|
454
472
|
ids = idsForDelete;
|
|
@@ -476,11 +494,9 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
|
|
|
476
494
|
ids
|
|
477
495
|
);
|
|
478
496
|
if (params.delete) {
|
|
479
|
-
await t
|
|
497
|
+
await _queryDelete(t);
|
|
480
498
|
} else {
|
|
481
|
-
await t.
|
|
482
|
-
params.update
|
|
483
|
-
);
|
|
499
|
+
await _queryUpdate(t, params.update);
|
|
484
500
|
}
|
|
485
501
|
});
|
|
486
502
|
}
|
|
@@ -504,8 +520,8 @@ var __spreadValues$5 = (a, b) => {
|
|
|
504
520
|
return a;
|
|
505
521
|
};
|
|
506
522
|
class HasOneVirtualColumn extends VirtualColumn {
|
|
507
|
-
constructor(key, state) {
|
|
508
|
-
super();
|
|
523
|
+
constructor(schema, key, state) {
|
|
524
|
+
super(schema);
|
|
509
525
|
this.key = key;
|
|
510
526
|
this.state = state;
|
|
511
527
|
this.nestedInsert = nestedInsert$2(state);
|
|
@@ -600,9 +616,13 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
600
616
|
for (let i = 0; i < len; i++) {
|
|
601
617
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
602
618
|
}
|
|
603
|
-
return query.where(values)
|
|
619
|
+
return _queryDefaults(query.where(values), values);
|
|
604
620
|
},
|
|
605
|
-
virtualColumn: new HasOneVirtualColumn(
|
|
621
|
+
virtualColumn: new HasOneVirtualColumn(
|
|
622
|
+
defaultSchemaConfig,
|
|
623
|
+
relationName,
|
|
624
|
+
state
|
|
625
|
+
),
|
|
606
626
|
joinQuery: joinQueryChainingHOF(
|
|
607
627
|
reverseJoin,
|
|
608
628
|
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
@@ -636,9 +656,15 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
636
656
|
for (let i2 = 0; i2 < len; i2++) {
|
|
637
657
|
data2[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
638
658
|
}
|
|
639
|
-
items[i] = "connect" in item ?
|
|
640
|
-
item.
|
|
641
|
-
|
|
659
|
+
items[i] = "connect" in item ? _queryUpdateOrThrow(
|
|
660
|
+
t.where(item.connect),
|
|
661
|
+
data2
|
|
662
|
+
) : _queryUpdate(
|
|
663
|
+
t.where(
|
|
664
|
+
item.connectOrCreate.where
|
|
665
|
+
),
|
|
666
|
+
data2
|
|
667
|
+
);
|
|
642
668
|
}
|
|
643
669
|
connected = await Promise.all(items);
|
|
644
670
|
} else {
|
|
@@ -684,7 +710,8 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
684
710
|
ids
|
|
685
711
|
);
|
|
686
712
|
if (params.create || params.disconnect || params.set) {
|
|
687
|
-
await
|
|
713
|
+
await _queryUpdate(
|
|
714
|
+
currentRelationsQuery,
|
|
688
715
|
setNulls
|
|
689
716
|
);
|
|
690
717
|
const record = data[0];
|
|
@@ -700,16 +727,25 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
700
727
|
for (let i = 0; i < len; i++) {
|
|
701
728
|
obj[foreignKeys[i]] = record[primaryKeys[i]];
|
|
702
729
|
}
|
|
703
|
-
await
|
|
730
|
+
await _queryUpdate(
|
|
731
|
+
_queryWhere(t, [params.set]),
|
|
732
|
+
obj
|
|
733
|
+
);
|
|
704
734
|
}
|
|
705
735
|
} else if (params.update) {
|
|
706
|
-
await
|
|
736
|
+
await _queryUpdate(
|
|
737
|
+
currentRelationsQuery,
|
|
738
|
+
params.update
|
|
739
|
+
);
|
|
707
740
|
} else if (params.delete) {
|
|
708
|
-
await currentRelationsQuery
|
|
741
|
+
await _queryDelete(currentRelationsQuery);
|
|
709
742
|
} else if (params.upsert) {
|
|
710
743
|
const { update, create } = params.upsert;
|
|
711
744
|
currentRelationsQuery.q.select = foreignKeys;
|
|
712
|
-
const updatedIds = await
|
|
745
|
+
const updatedIds = await _queryUpdate(
|
|
746
|
+
_queryRows(currentRelationsQuery),
|
|
747
|
+
update
|
|
748
|
+
);
|
|
713
749
|
if (updatedIds.length < ids.length) {
|
|
714
750
|
const data2 = typeof create === "function" ? create() : create;
|
|
715
751
|
await t.createMany(
|
|
@@ -748,8 +784,8 @@ var __spreadValues$4 = (a, b) => {
|
|
|
748
784
|
return a;
|
|
749
785
|
};
|
|
750
786
|
class HasManyVirtualColumn extends VirtualColumn {
|
|
751
|
-
constructor(key, state) {
|
|
752
|
-
super();
|
|
787
|
+
constructor(schema, key, state) {
|
|
788
|
+
super(schema);
|
|
753
789
|
this.key = key;
|
|
754
790
|
this.state = state;
|
|
755
791
|
this.nestedInsert = nestedInsert$1(state);
|
|
@@ -848,9 +884,13 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
848
884
|
for (let i = 0; i < len; i++) {
|
|
849
885
|
values[foreignKeys[i]] = params[primaryKeys[i]];
|
|
850
886
|
}
|
|
851
|
-
return query.where(values)
|
|
887
|
+
return _queryDefaults(query.where(values), values);
|
|
852
888
|
},
|
|
853
|
-
virtualColumn: new HasManyVirtualColumn(
|
|
889
|
+
virtualColumn: new HasManyVirtualColumn(
|
|
890
|
+
defaultSchemaConfig,
|
|
891
|
+
relationName,
|
|
892
|
+
state
|
|
893
|
+
),
|
|
854
894
|
joinQuery: joinQueryChainingHOF(
|
|
855
895
|
reverseJoin,
|
|
856
896
|
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
@@ -893,7 +933,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
893
933
|
for (let i2 = 0; i2 < len2; i2++) {
|
|
894
934
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
895
935
|
}
|
|
896
|
-
items[i] =
|
|
936
|
+
items[i] = _queryUpdateOrThrow(
|
|
937
|
+
t.orWhere(...connect),
|
|
938
|
+
obj
|
|
939
|
+
);
|
|
897
940
|
}
|
|
898
941
|
await Promise.all(items);
|
|
899
942
|
}
|
|
@@ -914,7 +957,10 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
914
957
|
obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
|
|
915
958
|
}
|
|
916
959
|
queries.push(
|
|
917
|
-
|
|
960
|
+
_queryUpdate(
|
|
961
|
+
t.where(item.where),
|
|
962
|
+
obj
|
|
963
|
+
)
|
|
918
964
|
);
|
|
919
965
|
}
|
|
920
966
|
}
|
|
@@ -959,7 +1005,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
959
1005
|
}
|
|
960
1006
|
}
|
|
961
1007
|
}
|
|
962
|
-
await t
|
|
1008
|
+
await _queryCreateMany(t, records);
|
|
963
1009
|
}
|
|
964
1010
|
};
|
|
965
1011
|
};
|
|
@@ -983,24 +1029,30 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
983
1029
|
for (const foreignKey of foreignKeys) {
|
|
984
1030
|
obj[foreignKey] = null;
|
|
985
1031
|
}
|
|
986
|
-
await
|
|
987
|
-
|
|
988
|
-
|
|
989
|
-
|
|
990
|
-
|
|
991
|
-
|
|
992
|
-
|
|
1032
|
+
await _queryUpdate(
|
|
1033
|
+
getWhereForNestedUpdate(
|
|
1034
|
+
t,
|
|
1035
|
+
data,
|
|
1036
|
+
params.disconnect,
|
|
1037
|
+
primaryKeys,
|
|
1038
|
+
foreignKeys
|
|
1039
|
+
),
|
|
1040
|
+
obj
|
|
1041
|
+
);
|
|
993
1042
|
if (params.set) {
|
|
994
1043
|
delete t.q[toSQLCacheKey];
|
|
995
1044
|
const obj2 = {};
|
|
996
1045
|
for (let i = 0; i < len; i++) {
|
|
997
1046
|
obj2[foreignKeys[i]] = data[0][primaryKeys[i]];
|
|
998
1047
|
}
|
|
999
|
-
await
|
|
1000
|
-
|
|
1001
|
-
|
|
1002
|
-
|
|
1003
|
-
|
|
1048
|
+
await _queryUpdate(
|
|
1049
|
+
t.where(
|
|
1050
|
+
Array.isArray(params.set) ? {
|
|
1051
|
+
OR: params.set
|
|
1052
|
+
} : params.set
|
|
1053
|
+
),
|
|
1054
|
+
obj2
|
|
1055
|
+
);
|
|
1004
1056
|
}
|
|
1005
1057
|
}
|
|
1006
1058
|
if (params.delete || params.update) {
|
|
@@ -1013,9 +1065,9 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
|
|
|
1013
1065
|
foreignKeys
|
|
1014
1066
|
);
|
|
1015
1067
|
if (params.delete) {
|
|
1016
|
-
await q
|
|
1068
|
+
await _queryDelete(q);
|
|
1017
1069
|
} else if (params.update) {
|
|
1018
|
-
await q
|
|
1070
|
+
await _queryUpdate(q, params.update.data);
|
|
1019
1071
|
}
|
|
1020
1072
|
}
|
|
1021
1073
|
};
|
|
@@ -1041,8 +1093,8 @@ var __spreadValues$3 = (a, b) => {
|
|
|
1041
1093
|
};
|
|
1042
1094
|
var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
|
|
1043
1095
|
class HasAndBelongsToManyVirtualColumn extends VirtualColumn {
|
|
1044
|
-
constructor(key, state) {
|
|
1045
|
-
super();
|
|
1096
|
+
constructor(schema, key, state) {
|
|
1097
|
+
super(schema);
|
|
1046
1098
|
this.key = key;
|
|
1047
1099
|
this.state = state;
|
|
1048
1100
|
this.nestedInsert = nestedInsert(state);
|
|
@@ -1111,7 +1163,9 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1111
1163
|
baseQuery.table = joinTable;
|
|
1112
1164
|
const shape = {};
|
|
1113
1165
|
for (let i = 0; i < len; i++) {
|
|
1114
|
-
shape[foreignKeys[i]] = removeColumnName(
|
|
1166
|
+
shape[foreignKeys[i]] = removeColumnName(
|
|
1167
|
+
table.shape[primaryKeys[i]]
|
|
1168
|
+
);
|
|
1115
1169
|
}
|
|
1116
1170
|
for (let i = 0; i < throughLen; i++) {
|
|
1117
1171
|
shape[throughForeignKeys[i]] = removeColumnName(
|
|
@@ -1137,13 +1191,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1137
1191
|
const joinQuery = (joiningQuery, tableAs, foreignAs) => {
|
|
1138
1192
|
return joiningQuery.whereExists(subQuery, (q) => {
|
|
1139
1193
|
for (let i = 0; i < throughLen; i++) {
|
|
1140
|
-
q
|
|
1194
|
+
_queryJoinOn(q, [
|
|
1141
1195
|
throughForeignKeysFull[i],
|
|
1142
1196
|
`${foreignAs}.${throughPrimaryKeys[i]}`
|
|
1143
|
-
);
|
|
1197
|
+
]);
|
|
1144
1198
|
}
|
|
1145
1199
|
for (let i = 0; i < len; i++) {
|
|
1146
|
-
q
|
|
1200
|
+
_queryJoinOn(q, [foreignKeysFull[i], `${tableAs}.${primaryKeys[i]}`]);
|
|
1147
1201
|
}
|
|
1148
1202
|
return q;
|
|
1149
1203
|
});
|
|
@@ -1170,12 +1224,19 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1170
1224
|
where[foreignKeysFull[i]] = params[primaryKeys[i]];
|
|
1171
1225
|
}
|
|
1172
1226
|
for (let i = 0; i < throughLen; i++) {
|
|
1173
|
-
q
|
|
1227
|
+
_queryJoinOn(q, [
|
|
1228
|
+
throughForeignKeysFull[i],
|
|
1229
|
+
throughPrimaryKeysFull[i]
|
|
1230
|
+
]);
|
|
1174
1231
|
}
|
|
1175
|
-
return q
|
|
1232
|
+
return _queryWhere(q, [where]);
|
|
1176
1233
|
});
|
|
1177
1234
|
},
|
|
1178
|
-
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
1235
|
+
virtualColumn: new HasAndBelongsToManyVirtualColumn(
|
|
1236
|
+
defaultSchemaConfig,
|
|
1237
|
+
relationName,
|
|
1238
|
+
state
|
|
1239
|
+
),
|
|
1179
1240
|
joinQuery: joinQueryChainingHOF(reverseJoin, (joiningQuery, baseQuery2) => {
|
|
1180
1241
|
const joined = joinQuery(
|
|
1181
1242
|
joiningQuery,
|
|
@@ -1190,7 +1251,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1190
1251
|
reverseJoin,
|
|
1191
1252
|
modifyRelatedQuery(relationQuery) {
|
|
1192
1253
|
const ref = {};
|
|
1193
|
-
relationQuery
|
|
1254
|
+
_queryHookAfterCreate(relationQuery, [], async (result) => {
|
|
1194
1255
|
if (result.length > 1) {
|
|
1195
1256
|
throw new OrchidOrmInternalError(
|
|
1196
1257
|
relationQuery,
|
|
@@ -1203,7 +1264,11 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1203
1264
|
for (let i = 0; i < throughLen; i++) {
|
|
1204
1265
|
data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
|
|
1205
1266
|
}
|
|
1206
|
-
const createdCount = await
|
|
1267
|
+
const createdCount = await _queryCreateFrom(
|
|
1268
|
+
subQuery.count(),
|
|
1269
|
+
baseQuery2,
|
|
1270
|
+
data
|
|
1271
|
+
);
|
|
1207
1272
|
if (createdCount === 0) {
|
|
1208
1273
|
throw new NotFoundError(baseQuery2);
|
|
1209
1274
|
}
|
|
@@ -1222,12 +1287,17 @@ const queryJoinTable = (state, data, conditions) => {
|
|
|
1222
1287
|
}
|
|
1223
1288
|
});
|
|
1224
1289
|
if (conditions) {
|
|
1225
|
-
t
|
|
1226
|
-
|
|
1227
|
-
|
|
1228
|
-
|
|
1290
|
+
_queryWhere(t, [
|
|
1291
|
+
{
|
|
1292
|
+
IN: {
|
|
1293
|
+
columns: state.throughForeignKeys,
|
|
1294
|
+
values: _querySelect(
|
|
1295
|
+
state.relatedTableQuery.where(conditionsToWhereArg(conditions)),
|
|
1296
|
+
state.throughPrimaryKeys
|
|
1297
|
+
)
|
|
1298
|
+
}
|
|
1229
1299
|
}
|
|
1230
|
-
|
|
1300
|
+
]);
|
|
1231
1301
|
}
|
|
1232
1302
|
return t;
|
|
1233
1303
|
};
|
|
@@ -1274,9 +1344,7 @@ const nestedInsert = ({
|
|
|
1274
1344
|
const queries = [];
|
|
1275
1345
|
for (const [, { connect }] of items) {
|
|
1276
1346
|
for (const item of connect) {
|
|
1277
|
-
queries.push(
|
|
1278
|
-
t.select(...throughPrimaryKeys)._findBy(item)._take()
|
|
1279
|
-
);
|
|
1347
|
+
queries.push(_queryFindBy(t.select(...throughPrimaryKeys), [item]));
|
|
1280
1348
|
}
|
|
1281
1349
|
}
|
|
1282
1350
|
connected = await Promise.all(queries);
|
|
@@ -1295,7 +1363,7 @@ const nestedInsert = ({
|
|
|
1295
1363
|
for (const [, { connectOrCreate }] of items) {
|
|
1296
1364
|
for (const item of connectOrCreate) {
|
|
1297
1365
|
queries.push(
|
|
1298
|
-
t.select(...throughPrimaryKeys)
|
|
1366
|
+
_queryFindByOptional(t.select(...throughPrimaryKeys), [item.where])
|
|
1299
1367
|
);
|
|
1300
1368
|
}
|
|
1301
1369
|
}
|
|
@@ -1335,7 +1403,10 @@ const nestedInsert = ({
|
|
|
1335
1403
|
}
|
|
1336
1404
|
}
|
|
1337
1405
|
}
|
|
1338
|
-
created = await
|
|
1406
|
+
created = await _queryCreateMany(
|
|
1407
|
+
t.select(...throughPrimaryKeys),
|
|
1408
|
+
records2
|
|
1409
|
+
);
|
|
1339
1410
|
} else {
|
|
1340
1411
|
created = [];
|
|
1341
1412
|
}
|
|
@@ -1393,7 +1464,10 @@ const nestedUpdate = (state) => {
|
|
|
1393
1464
|
const throughLen = state.throughPrimaryKeys.length;
|
|
1394
1465
|
return async (_, data, params) => {
|
|
1395
1466
|
if (params.create) {
|
|
1396
|
-
const idsRows = await
|
|
1467
|
+
const idsRows = await _queryCreateMany(
|
|
1468
|
+
_queryRows(state.relatedTableQuery.select(...state.throughPrimaryKeys)),
|
|
1469
|
+
params.create
|
|
1470
|
+
);
|
|
1397
1471
|
const records = [];
|
|
1398
1472
|
for (const item of data) {
|
|
1399
1473
|
const obj = {};
|
|
@@ -1411,41 +1485,58 @@ const nestedUpdate = (state) => {
|
|
|
1411
1485
|
await state.joinTableQuery.createMany(records);
|
|
1412
1486
|
}
|
|
1413
1487
|
if (params.update) {
|
|
1414
|
-
await
|
|
1415
|
-
|
|
1416
|
-
|
|
1417
|
-
|
|
1418
|
-
|
|
1419
|
-
|
|
1420
|
-
|
|
1421
|
-
|
|
1422
|
-
|
|
1423
|
-
|
|
1424
|
-
|
|
1425
|
-
|
|
1426
|
-
|
|
1427
|
-
|
|
1428
|
-
|
|
1429
|
-
|
|
1488
|
+
await _queryUpdate(
|
|
1489
|
+
_queryWhere(
|
|
1490
|
+
state.relatedTableQuery.whereExists(state.joinTableQuery, (q) => {
|
|
1491
|
+
for (let i = 0; i < throughLen; i++) {
|
|
1492
|
+
_queryJoinOn(q, [
|
|
1493
|
+
state.throughForeignKeysFull[i],
|
|
1494
|
+
state.throughPrimaryKeysFull[i]
|
|
1495
|
+
]);
|
|
1496
|
+
}
|
|
1497
|
+
return _queryWhere(q, [
|
|
1498
|
+
{
|
|
1499
|
+
IN: {
|
|
1500
|
+
columns: state.foreignKeysFull,
|
|
1501
|
+
values: data.map(
|
|
1502
|
+
(item) => state.primaryKeys.map((key) => item[key])
|
|
1503
|
+
)
|
|
1504
|
+
}
|
|
1505
|
+
}
|
|
1506
|
+
]);
|
|
1507
|
+
}),
|
|
1508
|
+
[conditionsToWhereArg(params.update.where)]
|
|
1509
|
+
),
|
|
1510
|
+
params.update.data
|
|
1511
|
+
);
|
|
1430
1512
|
}
|
|
1431
1513
|
if (params.disconnect) {
|
|
1432
|
-
await queryJoinTable(state, data, params.disconnect)
|
|
1514
|
+
await _queryDelete(queryJoinTable(state, data, params.disconnect));
|
|
1433
1515
|
}
|
|
1434
1516
|
if (params.delete) {
|
|
1435
1517
|
const j = queryJoinTable(state, data, params.delete);
|
|
1436
|
-
const idsRows = await
|
|
1437
|
-
|
|
1438
|
-
|
|
1439
|
-
|
|
1440
|
-
|
|
1441
|
-
|
|
1442
|
-
|
|
1518
|
+
const idsRows = await _queryDelete(
|
|
1519
|
+
_queryRows(_querySelect(j, state.throughForeignKeys))
|
|
1520
|
+
);
|
|
1521
|
+
await _queryDelete(
|
|
1522
|
+
state.relatedTableQuery.where({
|
|
1523
|
+
IN: {
|
|
1524
|
+
columns: state.throughPrimaryKeys,
|
|
1525
|
+
values: idsRows
|
|
1526
|
+
}
|
|
1527
|
+
})
|
|
1528
|
+
);
|
|
1443
1529
|
}
|
|
1444
1530
|
if (params.set) {
|
|
1445
1531
|
const j = queryJoinTable(state, data);
|
|
1446
|
-
await j
|
|
1532
|
+
await _queryDelete(j);
|
|
1447
1533
|
delete j.q[toSQLCacheKey];
|
|
1448
|
-
const idsRows = await
|
|
1534
|
+
const idsRows = await _queryRows(
|
|
1535
|
+
_querySelect(
|
|
1536
|
+
state.relatedTableQuery.where(conditionsToWhereArg(params.set)),
|
|
1537
|
+
state.throughPrimaryKeys
|
|
1538
|
+
)
|
|
1539
|
+
);
|
|
1449
1540
|
await insertToJoinTable(state, j, data, idsRows);
|
|
1450
1541
|
}
|
|
1451
1542
|
};
|
|
@@ -1587,9 +1678,9 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
|
|
|
1587
1678
|
}
|
|
1588
1679
|
if (data.returns === "one") {
|
|
1589
1680
|
if (relation.options.required) {
|
|
1590
|
-
query
|
|
1681
|
+
_queryTake(query);
|
|
1591
1682
|
} else {
|
|
1592
|
-
query
|
|
1683
|
+
_queryTakeOptional(query);
|
|
1593
1684
|
}
|
|
1594
1685
|
query.q.returnsOne = true;
|
|
1595
1686
|
}
|
|
@@ -1632,11 +1723,13 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1632
1723
|
query = toTable;
|
|
1633
1724
|
query.q.isSubQuery = true;
|
|
1634
1725
|
} else {
|
|
1635
|
-
query = toTable
|
|
1636
|
-
|
|
1637
|
-
|
|
1726
|
+
query = _queryWhere(_queryAll(toTable), [
|
|
1727
|
+
{
|
|
1728
|
+
EXISTS: {
|
|
1729
|
+
args: [data.reverseJoin(this, toTable)]
|
|
1730
|
+
}
|
|
1638
1731
|
}
|
|
1639
|
-
|
|
1732
|
+
]);
|
|
1640
1733
|
}
|
|
1641
1734
|
if (this.q.relChain) {
|
|
1642
1735
|
query.q.relChain = [...this.q.relChain, this];
|
|
@@ -1734,7 +1827,7 @@ const orchidORM = (_a, tables) => {
|
|
|
1734
1827
|
void 0,
|
|
1735
1828
|
void 0,
|
|
1736
1829
|
anyShape,
|
|
1737
|
-
|
|
1830
|
+
makeColumnTypes(defaultSchemaConfig),
|
|
1738
1831
|
transactionStorage,
|
|
1739
1832
|
commonOptions
|
|
1740
1833
|
);
|