orchid-orm 1.17.38 → 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.mjs CHANGED
@@ -181,31 +181,45 @@ const relationWhere = (len, keys, valueKeys) => (params) => {
181
181
  }
182
182
  return obj;
183
183
  };
184
- function joinHasThrough(q, fromQuery, toQuery, throughRelation, sourceRelation) {
184
+ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
185
185
  return q.whereExists(
186
- throughRelation.joinQuery(fromQuery, throughRelation.query),
186
+ throughRelation.joinQuery(throughRelation.query, baseQuery),
187
187
  () => {
188
- const as = getQueryAs(toQuery);
188
+ const as = getQueryAs(joiningQuery);
189
189
  return sourceRelation.joinQuery(
190
- throughRelation.query,
191
- sourceRelation.query.as(as)
190
+ sourceRelation.query.as(as),
191
+ throughRelation.query
192
192
  );
193
193
  }
194
194
  );
195
195
  }
196
- function joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len) {
197
- const q = toQuery.clone();
196
+ function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
197
+ const q = joiningQuery.clone();
198
198
  setQueryObjectValue(
199
199
  q,
200
200
  "joinedShapes",
201
- fromQuery.q.as || fromQuery.table,
202
- fromQuery.q.shape
201
+ baseQuery.q.as || baseQuery.table,
202
+ baseQuery.q.shape
203
203
  );
204
204
  for (let i = 0; i < len; i++) {
205
- pushQueryOn(q, fromQuery, toQuery, foreignKeys[i], primaryKeys[i]);
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 = (fromQuery, toQuery, primaryKeys2, foreignKeys2) => {
269
- const q = toQuery.clone();
282
+ const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
283
+ const q = joiningQuery.clone();
270
284
  setQueryObjectValue(
271
285
  q,
272
286
  "joinedShapes",
273
- fromQuery.q.as || fromQuery.table,
274
- fromQuery.q.shape
287
+ baseQuery.q.as || baseQuery.table,
288
+ baseQuery.q.shape
275
289
  );
276
290
  for (let i = 0; i < len; i++) {
277
- pushQueryOn(q, fromQuery, toQuery, primaryKeys2[i], foreignKeys2[i]);
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(fromQuery, toQuery) {
288
- return join(fromQuery, toQuery, primaryKeys, foreignKeys);
289
- },
290
- reverseJoin(fromQuery, toQuery) {
291
- return join(toQuery, fromQuery, foreignKeys, primaryKeys);
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(throughRelation.query, sourceRelation.query).as(relationName);
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(fromQuery, toQuery) {
538
- return joinHasThrough(
539
- toQuery,
540
- fromQuery,
541
- toQuery,
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(fromQuery, toQuery) {
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(fromQuery, toQuery) {
577
- return joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len);
578
- },
579
- reverseJoin(fromQuery, toQuery) {
580
- return joinHasRelation(toQuery, fromQuery, foreignKeys, primaryKeys, len);
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 fromQuery = query2.clone();
585
- fromQuery.q.select = fromQuerySelect;
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: fromQuery };
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
- throughRelation.query,
763
- sourceRelationQuery
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(fromQuery, toQuery) {
776
- return joinHasThrough(
777
- toQuery,
778
- fromQuery,
779
- toQuery,
780
- throughRelation,
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(fromQuery, toQuery) {
815
- return joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len);
816
- },
817
- reverseJoin(fromQuery, toQuery) {
818
- return joinHasRelation(toQuery, fromQuery, foreignKeys, primaryKeys, len);
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 fromQuery = query2.clone();
823
- fromQuery.q.select = fromQuerySelect;
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: fromQuery };
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 = (toQuery, tableAs, foreignAs) => {
1099
- return toQuery.whereExists(subQuery, (q) => {
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
- // joinQuery can be a property of RelationQuery and be used by whereExists and other stuff which needs it
1134
- // and the chained query itself may be a query around this joinQuery
1135
- joinQuery(fromQuery, toQuery) {
1136
- const join = joinQuery(
1137
- toQuery,
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
- join.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, join.q.joinedShapes), {
1142
- [fromQuery.q.as || fromQuery.table]: fromQuery.q.shape
1182
+ joined.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, joined.q.joinedShapes), {
1183
+ [baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
1143
1184
  });
1144
- return join;
1145
- },
1146
- reverseJoin(fromQuery, toQuery) {
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 fromQuery = ref.q.clone();
1159
- fromQuery.q.select = selectPrimaryKeysAsForeignKeys;
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(fromQuery, data);
1203
+ const createdCount = await subQuery.count()._createFrom(baseQuery2, data);
1165
1204
  if (createdCount === 0) {
1166
- throw new NotFoundError(fromQuery);
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
- const query = this.q.isSubQuery ? toTable : toTable._all()._whereExists(
1589
- this.baseQuery,
1590
- (q2) => data.reverseJoin(this, toTable)
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);