orchid-orm 1.17.37 → 1.18.0
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 +7 -7
- package/dist/index.js +156 -118
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +151 -99
- package/dist/index.mjs.map +1 -1
- package/package.json +5 -5
package/dist/index.mjs
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import { columnTypes, QueryHooks, getColumnTypes, getQueryAs, setQueryObjectValue, pushQueryOn, VirtualColumn, pushQueryValue, isQueryReturnsAll, toSQLCacheKey, OrchidOrmInternalError, NotFoundError, Adapter, Db, anyShape, addComputedColumns, getClonedQueryData } from 'pqb';
|
|
2
|
-
export
|
|
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
|
|
|
@@ -181,31 +181,45 @@ const relationWhere = (len, keys, valueKeys) => (params) => {
|
|
|
181
181
|
}
|
|
182
182
|
return obj;
|
|
183
183
|
};
|
|
184
|
-
function joinHasThrough(q,
|
|
184
|
+
function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
|
|
185
185
|
return q.whereExists(
|
|
186
|
-
throughRelation.joinQuery(
|
|
186
|
+
throughRelation.joinQuery(throughRelation.query, baseQuery),
|
|
187
187
|
() => {
|
|
188
|
-
const as = getQueryAs(
|
|
188
|
+
const as = getQueryAs(joiningQuery);
|
|
189
189
|
return sourceRelation.joinQuery(
|
|
190
|
-
|
|
191
|
-
|
|
190
|
+
sourceRelation.query.as(as),
|
|
191
|
+
throughRelation.query
|
|
192
192
|
);
|
|
193
193
|
}
|
|
194
194
|
);
|
|
195
195
|
}
|
|
196
|
-
function joinHasRelation(
|
|
197
|
-
const q =
|
|
196
|
+
function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
|
|
197
|
+
const q = joiningQuery.clone();
|
|
198
198
|
setQueryObjectValue(
|
|
199
199
|
q,
|
|
200
200
|
"joinedShapes",
|
|
201
|
-
|
|
202
|
-
|
|
201
|
+
baseQuery.q.as || baseQuery.table,
|
|
202
|
+
baseQuery.q.shape
|
|
203
203
|
);
|
|
204
204
|
for (let i = 0; i < len; i++) {
|
|
205
|
-
pushQueryOn(q,
|
|
205
|
+
pushQueryOn(q, baseQuery, joiningQuery, foreignKeys[i], primaryKeys[i]);
|
|
206
206
|
}
|
|
207
207
|
return q;
|
|
208
208
|
}
|
|
209
|
+
const joinQueryChainingHOF = (reverseJoin, joinQuery) => (joiningQuery, baseQuery) => {
|
|
210
|
+
const chain = joiningQuery.q.relChain;
|
|
211
|
+
if (!chain || chain.length === 1) {
|
|
212
|
+
return joinQuery(joiningQuery, baseQuery);
|
|
213
|
+
}
|
|
214
|
+
const last = chain[chain.length - 1];
|
|
215
|
+
const query = "relationConfig" in last ? last.relationConfig.joinQuery(last, baseQuery) : last;
|
|
216
|
+
const inner = reverseJoin(query, joiningQuery);
|
|
217
|
+
return joiningQuery.where({
|
|
218
|
+
EXISTS: {
|
|
219
|
+
args: [inner]
|
|
220
|
+
}
|
|
221
|
+
});
|
|
222
|
+
};
|
|
209
223
|
|
|
210
224
|
class BelongsToVirtualColumn extends VirtualColumn {
|
|
211
225
|
constructor(key, state) {
|
|
@@ -265,31 +279,33 @@ const makeBelongsToMethod = (relation, relationName, query) => {
|
|
|
265
279
|
const len = primaryKeys.length;
|
|
266
280
|
const state = { query, primaryKeys, foreignKeys, len };
|
|
267
281
|
const makeWhere = relationWhere(len, primaryKeys, foreignKeys);
|
|
268
|
-
const join = (
|
|
269
|
-
const q =
|
|
282
|
+
const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
|
|
283
|
+
const q = joiningQuery.clone();
|
|
270
284
|
setQueryObjectValue(
|
|
271
285
|
q,
|
|
272
286
|
"joinedShapes",
|
|
273
|
-
|
|
274
|
-
|
|
287
|
+
baseQuery.q.as || baseQuery.table,
|
|
288
|
+
baseQuery.q.shape
|
|
275
289
|
);
|
|
276
290
|
for (let i = 0; i < len; i++) {
|
|
277
|
-
pushQueryOn(q,
|
|
291
|
+
pushQueryOn(q, baseQuery, joiningQuery, primaryKeys2[i], foreignKeys2[i]);
|
|
278
292
|
}
|
|
279
293
|
return q;
|
|
280
294
|
};
|
|
295
|
+
const reverseJoin = (baseQuery, joiningQuery) => {
|
|
296
|
+
return join(joiningQuery, baseQuery, foreignKeys, primaryKeys);
|
|
297
|
+
};
|
|
281
298
|
return {
|
|
282
299
|
returns: "one",
|
|
283
300
|
method(params) {
|
|
284
301
|
return query.where(makeWhere(params));
|
|
285
302
|
},
|
|
286
303
|
virtualColumn: new BelongsToVirtualColumn(relationName, state),
|
|
287
|
-
joinQuery(
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
}
|
|
304
|
+
joinQuery: joinQueryChainingHOF(
|
|
305
|
+
reverseJoin,
|
|
306
|
+
(joiningQuery, baseQuery) => join(baseQuery, joiningQuery, primaryKeys, foreignKeys)
|
|
307
|
+
),
|
|
308
|
+
reverseJoin
|
|
293
309
|
};
|
|
294
310
|
};
|
|
295
311
|
const nestedInsert$3 = ({ query, primaryKeys }) => {
|
|
@@ -523,8 +539,17 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
523
539
|
const { through, source } = relation.options;
|
|
524
540
|
const throughRelation = getThroughRelation(table, through);
|
|
525
541
|
const sourceRelation = getSourceRelation(throughRelation, source);
|
|
526
|
-
const sourceQuery = sourceRelation.joinQuery(
|
|
542
|
+
const sourceQuery = sourceRelation.joinQuery(sourceRelation.query, throughRelation.query).as(relationName);
|
|
527
543
|
const whereExistsCallback = () => sourceQuery;
|
|
544
|
+
const reverseJoin2 = (baseQuery, joiningQuery) => {
|
|
545
|
+
return joinHasThrough(
|
|
546
|
+
baseQuery,
|
|
547
|
+
baseQuery,
|
|
548
|
+
joiningQuery,
|
|
549
|
+
throughRelation,
|
|
550
|
+
sourceRelation
|
|
551
|
+
);
|
|
552
|
+
};
|
|
528
553
|
return {
|
|
529
554
|
returns: "one",
|
|
530
555
|
method: (params) => {
|
|
@@ -534,24 +559,17 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
534
559
|
whereExistsCallback
|
|
535
560
|
);
|
|
536
561
|
},
|
|
537
|
-
joinQuery(
|
|
538
|
-
|
|
539
|
-
|
|
540
|
-
|
|
541
|
-
|
|
562
|
+
joinQuery: joinQueryChainingHOF(
|
|
563
|
+
reverseJoin2,
|
|
564
|
+
(joiningQuery, baseQuery) => joinHasThrough(
|
|
565
|
+
joiningQuery,
|
|
566
|
+
baseQuery,
|
|
567
|
+
joiningQuery,
|
|
542
568
|
throughRelation,
|
|
543
569
|
sourceRelation
|
|
544
|
-
)
|
|
545
|
-
|
|
546
|
-
reverseJoin
|
|
547
|
-
return joinHasThrough(
|
|
548
|
-
fromQuery,
|
|
549
|
-
fromQuery,
|
|
550
|
-
toQuery,
|
|
551
|
-
throughRelation,
|
|
552
|
-
sourceRelation
|
|
553
|
-
);
|
|
554
|
-
}
|
|
570
|
+
)
|
|
571
|
+
),
|
|
572
|
+
reverseJoin: reverseJoin2
|
|
555
573
|
};
|
|
556
574
|
}
|
|
557
575
|
const primaryKeys = "columns" in relation.options ? relation.options.columns : [relation.options.primaryKey];
|
|
@@ -563,6 +581,15 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
563
581
|
reversedOn[foreignKeys[i]] = primaryKeys[i];
|
|
564
582
|
}
|
|
565
583
|
const fromQuerySelect = [{ selectAs: reversedOn }];
|
|
584
|
+
const reverseJoin = (baseQuery, joiningQuery) => {
|
|
585
|
+
return joinHasRelation(
|
|
586
|
+
joiningQuery,
|
|
587
|
+
baseQuery,
|
|
588
|
+
foreignKeys,
|
|
589
|
+
primaryKeys,
|
|
590
|
+
len
|
|
591
|
+
);
|
|
592
|
+
};
|
|
566
593
|
return {
|
|
567
594
|
returns: "one",
|
|
568
595
|
method: (params) => {
|
|
@@ -573,19 +600,18 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
|
|
|
573
600
|
return query.where(values)._defaults(values);
|
|
574
601
|
},
|
|
575
602
|
virtualColumn: new HasOneVirtualColumn(relationName, state),
|
|
576
|
-
joinQuery(
|
|
577
|
-
|
|
578
|
-
|
|
579
|
-
|
|
580
|
-
|
|
581
|
-
},
|
|
603
|
+
joinQuery: joinQueryChainingHOF(
|
|
604
|
+
reverseJoin,
|
|
605
|
+
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
606
|
+
),
|
|
607
|
+
reverseJoin,
|
|
582
608
|
modifyRelatedQuery(relationQuery) {
|
|
583
609
|
return (query2) => {
|
|
584
|
-
const
|
|
585
|
-
|
|
610
|
+
const baseQuery = query2.clone();
|
|
611
|
+
baseQuery.q.select = fromQuerySelect;
|
|
586
612
|
const q = relationQuery.q;
|
|
587
613
|
q.kind = "from";
|
|
588
|
-
q.values = { from:
|
|
614
|
+
q.values = { from: baseQuery };
|
|
589
615
|
};
|
|
590
616
|
}
|
|
591
617
|
};
|
|
@@ -759,10 +785,19 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
759
785
|
const sourceRelation = getSourceRelation(throughRelation, source);
|
|
760
786
|
const sourceRelationQuery = sourceRelation.query.as(relationName);
|
|
761
787
|
const sourceQuery = sourceRelation.joinQuery(
|
|
762
|
-
|
|
763
|
-
|
|
788
|
+
sourceRelationQuery,
|
|
789
|
+
throughRelation.query
|
|
764
790
|
);
|
|
765
791
|
const whereExistsCallback = () => sourceQuery;
|
|
792
|
+
const reverseJoin2 = (baseQuery, joiningQuery) => {
|
|
793
|
+
return joinHasThrough(
|
|
794
|
+
baseQuery,
|
|
795
|
+
baseQuery,
|
|
796
|
+
joiningQuery,
|
|
797
|
+
throughRelation,
|
|
798
|
+
sourceRelation
|
|
799
|
+
);
|
|
800
|
+
};
|
|
766
801
|
return {
|
|
767
802
|
returns: "many",
|
|
768
803
|
method: (params) => {
|
|
@@ -772,24 +807,17 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
772
807
|
whereExistsCallback
|
|
773
808
|
);
|
|
774
809
|
},
|
|
775
|
-
joinQuery(
|
|
776
|
-
|
|
777
|
-
|
|
778
|
-
|
|
779
|
-
|
|
780
|
-
|
|
781
|
-
sourceRelation
|
|
782
|
-
);
|
|
783
|
-
},
|
|
784
|
-
reverseJoin(fromQuery, toQuery) {
|
|
785
|
-
return joinHasThrough(
|
|
786
|
-
fromQuery,
|
|
787
|
-
fromQuery,
|
|
788
|
-
toQuery,
|
|
810
|
+
joinQuery: joinQueryChainingHOF(
|
|
811
|
+
reverseJoin2,
|
|
812
|
+
(joiningQuery, baseQuery) => joinHasThrough(
|
|
813
|
+
joiningQuery,
|
|
814
|
+
baseQuery,
|
|
815
|
+
joiningQuery,
|
|
789
816
|
throughRelation,
|
|
790
817
|
sourceRelation
|
|
791
|
-
)
|
|
792
|
-
|
|
818
|
+
)
|
|
819
|
+
),
|
|
820
|
+
reverseJoin: reverseJoin2
|
|
793
821
|
};
|
|
794
822
|
}
|
|
795
823
|
const primaryKeys = "columns" in relation.options ? relation.options.columns : [relation.options.primaryKey];
|
|
@@ -801,6 +829,15 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
801
829
|
reversedOn[foreignKeys[i]] = primaryKeys[i];
|
|
802
830
|
}
|
|
803
831
|
const fromQuerySelect = [{ selectAs: reversedOn }];
|
|
832
|
+
const reverseJoin = (baseQuery, joiningQuery) => {
|
|
833
|
+
return joinHasRelation(
|
|
834
|
+
joiningQuery,
|
|
835
|
+
baseQuery,
|
|
836
|
+
foreignKeys,
|
|
837
|
+
primaryKeys,
|
|
838
|
+
len
|
|
839
|
+
);
|
|
840
|
+
};
|
|
804
841
|
return {
|
|
805
842
|
returns: "many",
|
|
806
843
|
method: (params) => {
|
|
@@ -811,19 +848,18 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
|
|
|
811
848
|
return query.where(values)._defaults(values);
|
|
812
849
|
},
|
|
813
850
|
virtualColumn: new HasManyVirtualColumn(relationName, state),
|
|
814
|
-
joinQuery(
|
|
815
|
-
|
|
816
|
-
|
|
817
|
-
|
|
818
|
-
|
|
819
|
-
},
|
|
851
|
+
joinQuery: joinQueryChainingHOF(
|
|
852
|
+
reverseJoin,
|
|
853
|
+
(joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
|
|
854
|
+
),
|
|
855
|
+
reverseJoin,
|
|
820
856
|
modifyRelatedQuery(relationQuery) {
|
|
821
857
|
return (query2) => {
|
|
822
|
-
const
|
|
823
|
-
|
|
858
|
+
const baseQuery = query2.clone();
|
|
859
|
+
baseQuery.q.select = fromQuerySelect;
|
|
824
860
|
const q = relationQuery.q;
|
|
825
861
|
q.kind = "from";
|
|
826
|
-
q.values = { from:
|
|
862
|
+
q.values = { from: baseQuery };
|
|
827
863
|
};
|
|
828
864
|
}
|
|
829
865
|
};
|
|
@@ -1095,8 +1131,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1095
1131
|
throughForeignKeysFull,
|
|
1096
1132
|
throughPrimaryKeysFull
|
|
1097
1133
|
};
|
|
1098
|
-
const joinQuery = (
|
|
1099
|
-
return
|
|
1134
|
+
const joinQuery = (joiningQuery, tableAs, foreignAs) => {
|
|
1135
|
+
return joiningQuery.whereExists(subQuery, (q) => {
|
|
1100
1136
|
for (let i = 0; i < throughLen; i++) {
|
|
1101
1137
|
q._on(
|
|
1102
1138
|
throughForeignKeysFull[i],
|
|
@@ -1114,6 +1150,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1114
1150
|
obj[foreignKeys[i]] = primaryKeys[i];
|
|
1115
1151
|
}
|
|
1116
1152
|
const selectPrimaryKeysAsForeignKeys = [{ selectAs: obj }];
|
|
1153
|
+
const reverseJoin = (baseQuery2, joiningQuery) => {
|
|
1154
|
+
return joinQuery(
|
|
1155
|
+
baseQuery2,
|
|
1156
|
+
getQueryAs(baseQuery2),
|
|
1157
|
+
getQueryAs(joiningQuery)
|
|
1158
|
+
);
|
|
1159
|
+
};
|
|
1117
1160
|
return {
|
|
1118
1161
|
returns: "many",
|
|
1119
1162
|
method(params) {
|
|
@@ -1130,22 +1173,18 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1130
1173
|
});
|
|
1131
1174
|
},
|
|
1132
1175
|
virtualColumn: new HasAndBelongsToManyVirtualColumn(relationName, state),
|
|
1133
|
-
|
|
1134
|
-
|
|
1135
|
-
|
|
1136
|
-
|
|
1137
|
-
|
|
1138
|
-
getQueryAs(fromQuery),
|
|
1139
|
-
getQueryAs(toQuery)
|
|
1176
|
+
joinQuery: joinQueryChainingHOF(reverseJoin, (joiningQuery, baseQuery2) => {
|
|
1177
|
+
const joined = joinQuery(
|
|
1178
|
+
joiningQuery,
|
|
1179
|
+
getQueryAs(baseQuery2),
|
|
1180
|
+
getQueryAs(joiningQuery)
|
|
1140
1181
|
);
|
|
1141
|
-
|
|
1142
|
-
[
|
|
1182
|
+
joined.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, joined.q.joinedShapes), {
|
|
1183
|
+
[baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
|
|
1143
1184
|
});
|
|
1144
|
-
return
|
|
1145
|
-
},
|
|
1146
|
-
reverseJoin
|
|
1147
|
-
return joinQuery(fromQuery, getQueryAs(fromQuery), getQueryAs(toQuery));
|
|
1148
|
-
},
|
|
1185
|
+
return joined;
|
|
1186
|
+
}),
|
|
1187
|
+
reverseJoin,
|
|
1149
1188
|
modifyRelatedQuery(relationQuery) {
|
|
1150
1189
|
const ref = {};
|
|
1151
1190
|
relationQuery._afterCreate([], async (result) => {
|
|
@@ -1155,15 +1194,15 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
|
|
|
1155
1194
|
"Creating multiple `hasAndBelongsToMany` records is not yet supported"
|
|
1156
1195
|
);
|
|
1157
1196
|
}
|
|
1158
|
-
const
|
|
1159
|
-
|
|
1197
|
+
const baseQuery2 = ref.q.clone();
|
|
1198
|
+
baseQuery2.q.select = selectPrimaryKeysAsForeignKeys;
|
|
1160
1199
|
const data = {};
|
|
1161
1200
|
for (let i = 0; i < throughLen; i++) {
|
|
1162
1201
|
data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
|
|
1163
1202
|
}
|
|
1164
|
-
const createdCount = await subQuery.count()._createFrom(
|
|
1203
|
+
const createdCount = await subQuery.count()._createFrom(baseQuery2, data);
|
|
1165
1204
|
if (createdCount === 0) {
|
|
1166
|
-
throw new NotFoundError(
|
|
1205
|
+
throw new NotFoundError(baseQuery2);
|
|
1167
1206
|
}
|
|
1168
1207
|
});
|
|
1169
1208
|
return (q) => {
|
|
@@ -1585,10 +1624,23 @@ const makeRelationQuery = (table, relationName, data, q) => {
|
|
|
1585
1624
|
get() {
|
|
1586
1625
|
var _a;
|
|
1587
1626
|
const toTable = q.clone();
|
|
1588
|
-
|
|
1589
|
-
|
|
1590
|
-
|
|
1591
|
-
|
|
1627
|
+
let query;
|
|
1628
|
+
if (this.q.isSubQuery) {
|
|
1629
|
+
query = toTable;
|
|
1630
|
+
query.q.isSubQuery = true;
|
|
1631
|
+
} else {
|
|
1632
|
+
query = toTable._all()._where({
|
|
1633
|
+
EXISTS: {
|
|
1634
|
+
args: [data.reverseJoin(this, toTable)]
|
|
1635
|
+
}
|
|
1636
|
+
});
|
|
1637
|
+
}
|
|
1638
|
+
if (this.q.relChain) {
|
|
1639
|
+
query.q.relChain = [...this.q.relChain, this];
|
|
1640
|
+
query.q.returnType = "all";
|
|
1641
|
+
} else {
|
|
1642
|
+
query.q.relChain = [this];
|
|
1643
|
+
}
|
|
1592
1644
|
query.q.joinedShapes = __spreadValues$2({
|
|
1593
1645
|
[getQueryAs(this)]: this.q.shape
|
|
1594
1646
|
}, this.q.joinedShapes);
|