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