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.d.ts CHANGED
@@ -1,4 +1,4 @@
1
- import { Query, QueryWithTable, SetQueryTableAlias, WhereArg, UpdateData, CreateData, AddQueryDefaults, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, RelationQuery, RelationConfigBase, RelationQueryBase, ComputedColumnsBase, QueryData, QueryBase, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
1
+ import { Query, QueryWithTable, SetQueryTableAlias, RelationJoinQuery, WhereArg, UpdateData, CreateData, AddQueryDefaults, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, RelationQuery, RelationConfigBase, RelationQueryBase, ComputedColumnsBase, QueryData, QueryBase, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
2
2
  export * from 'pqb';
3
3
  import { EmptyObject, MaybeArray, StringKey, ColumnsShapeBase, ColumnShapeQueryType, ColumnShapeOutput, ColumnShapeInput } from 'orchid-core';
4
4
 
@@ -32,7 +32,7 @@ type BelongsToInfo<T extends Table, Relation extends BelongsTo, K extends string
32
32
  }>, Required = Relation['options']['required'] extends true ? true : false> = {
33
33
  table: Q;
34
34
  query: Q;
35
- joinQuery(fromQuery: Query, toQuery: Query): Query;
35
+ joinQuery: RelationJoinQuery;
36
36
  one: true;
37
37
  required: Required;
38
38
  omitForeignKeyInCreate: FK;
@@ -80,7 +80,7 @@ type HasManyOptions<Self extends Table = Table, Related extends TableClass = Tab
80
80
  type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany, K extends string, Populate extends Record<string, true> = Relation['options'] extends RelationRefsOptions ? Record<Relation['options']['references'][number], true> : Relation['options'] extends RelationKeysOptions ? Record<Relation['options']['foreignKey'], true> : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = Relation['options'] extends RelationThroughOptions ? Q : AddQueryDefaults<Q, Populate>> = {
81
81
  table: Q;
82
82
  query: Q;
83
- joinQuery(fromQuery: Query, toQuery: Query): Query;
83
+ joinQuery: RelationJoinQuery;
84
84
  one: false;
85
85
  required: Relation['options']['required'] extends true ? true : false;
86
86
  omitForeignKeyInCreate: never;
@@ -118,7 +118,7 @@ type HasOneOptions<Self extends Table = Table, Related extends TableClass = Tabl
118
118
  type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne, K extends string, Populate extends Record<string, true> = Relation['options'] extends RelationRefsOptions ? Record<Relation['options']['references'][number], true> : Relation['options'] extends RelationKeysOptions ? Record<Relation['options']['foreignKey'], true> : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = Relation['options'] extends RelationThroughOptions ? Q : AddQueryDefaults<Q, Populate>> = {
119
119
  table: Q;
120
120
  query: Q;
121
- joinQuery(fromQuery: Query, toQuery: Query): Query;
121
+ joinQuery: RelationJoinQuery;
122
122
  one: true;
123
123
  required: Relation['options']['required'] extends true ? true : false;
124
124
  omitForeignKeyInCreate: never;
@@ -257,7 +257,7 @@ type HasAndBelongsToManyOptions<Self extends Table = Table, Related extends Tabl
257
257
  type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMany, K extends string, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>> = {
258
258
  table: Q;
259
259
  query: Q;
260
- joinQuery(fromQuery: Query, toQuery: Query): Query;
260
+ joinQuery: RelationJoinQuery;
261
261
  one: false;
262
262
  required: Relation['options']['required'] extends true ? true : false;
263
263
  omitForeignKeyInCreate: never;
package/dist/index.js CHANGED
@@ -182,31 +182,45 @@ const relationWhere = (len, keys, valueKeys) => (params) => {
182
182
  }
183
183
  return obj;
184
184
  };
185
- function joinHasThrough(q, fromQuery, toQuery, throughRelation, sourceRelation) {
185
+ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
186
186
  return q.whereExists(
187
- throughRelation.joinQuery(fromQuery, throughRelation.query),
187
+ throughRelation.joinQuery(throughRelation.query, baseQuery),
188
188
  () => {
189
- const as = pqb.getQueryAs(toQuery);
189
+ const as = pqb.getQueryAs(joiningQuery);
190
190
  return sourceRelation.joinQuery(
191
- throughRelation.query,
192
- sourceRelation.query.as(as)
191
+ sourceRelation.query.as(as),
192
+ throughRelation.query
193
193
  );
194
194
  }
195
195
  );
196
196
  }
197
- function joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len) {
198
- const q = toQuery.clone();
197
+ function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
198
+ const q = joiningQuery.clone();
199
199
  pqb.setQueryObjectValue(
200
200
  q,
201
201
  "joinedShapes",
202
- fromQuery.q.as || fromQuery.table,
203
- fromQuery.q.shape
202
+ baseQuery.q.as || baseQuery.table,
203
+ baseQuery.q.shape
204
204
  );
205
205
  for (let i = 0; i < len; i++) {
206
- pqb.pushQueryOn(q, fromQuery, toQuery, foreignKeys[i], primaryKeys[i]);
206
+ pqb.pushQueryOn(q, baseQuery, joiningQuery, foreignKeys[i], primaryKeys[i]);
207
207
  }
208
208
  return q;
209
209
  }
210
+ const joinQueryChainingHOF = (reverseJoin, joinQuery) => (joiningQuery, baseQuery) => {
211
+ const chain = joiningQuery.q.relChain;
212
+ if (!chain || chain.length === 1) {
213
+ return joinQuery(joiningQuery, baseQuery);
214
+ }
215
+ const last = chain[chain.length - 1];
216
+ const query = "relationConfig" in last ? last.relationConfig.joinQuery(last, baseQuery) : last;
217
+ const inner = reverseJoin(query, joiningQuery);
218
+ return joiningQuery.where({
219
+ EXISTS: {
220
+ args: [inner]
221
+ }
222
+ });
223
+ };
210
224
 
211
225
  class BelongsToVirtualColumn extends pqb.VirtualColumn {
212
226
  constructor(key, state) {
@@ -266,31 +280,33 @@ const makeBelongsToMethod = (relation, relationName, query) => {
266
280
  const len = primaryKeys.length;
267
281
  const state = { query, primaryKeys, foreignKeys, len };
268
282
  const makeWhere = relationWhere(len, primaryKeys, foreignKeys);
269
- const join = (fromQuery, toQuery, primaryKeys2, foreignKeys2) => {
270
- const q = toQuery.clone();
283
+ const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
284
+ const q = joiningQuery.clone();
271
285
  pqb.setQueryObjectValue(
272
286
  q,
273
287
  "joinedShapes",
274
- fromQuery.q.as || fromQuery.table,
275
- fromQuery.q.shape
288
+ baseQuery.q.as || baseQuery.table,
289
+ baseQuery.q.shape
276
290
  );
277
291
  for (let i = 0; i < len; i++) {
278
- pqb.pushQueryOn(q, fromQuery, toQuery, primaryKeys2[i], foreignKeys2[i]);
292
+ pqb.pushQueryOn(q, baseQuery, joiningQuery, primaryKeys2[i], foreignKeys2[i]);
279
293
  }
280
294
  return q;
281
295
  };
296
+ const reverseJoin = (baseQuery, joiningQuery) => {
297
+ return join(joiningQuery, baseQuery, foreignKeys, primaryKeys);
298
+ };
282
299
  return {
283
300
  returns: "one",
284
301
  method(params) {
285
302
  return query.where(makeWhere(params));
286
303
  },
287
304
  virtualColumn: new BelongsToVirtualColumn(relationName, state),
288
- joinQuery(fromQuery, toQuery) {
289
- return join(fromQuery, toQuery, primaryKeys, foreignKeys);
290
- },
291
- reverseJoin(fromQuery, toQuery) {
292
- return join(toQuery, fromQuery, foreignKeys, primaryKeys);
293
- }
305
+ joinQuery: joinQueryChainingHOF(
306
+ reverseJoin,
307
+ (joiningQuery, baseQuery) => join(baseQuery, joiningQuery, primaryKeys, foreignKeys)
308
+ ),
309
+ reverseJoin
294
310
  };
295
311
  };
296
312
  const nestedInsert$3 = ({ query, primaryKeys }) => {
@@ -524,8 +540,17 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
524
540
  const { through, source } = relation.options;
525
541
  const throughRelation = getThroughRelation(table, through);
526
542
  const sourceRelation = getSourceRelation(throughRelation, source);
527
- const sourceQuery = sourceRelation.joinQuery(throughRelation.query, sourceRelation.query).as(relationName);
543
+ const sourceQuery = sourceRelation.joinQuery(sourceRelation.query, throughRelation.query).as(relationName);
528
544
  const whereExistsCallback = () => sourceQuery;
545
+ const reverseJoin2 = (baseQuery, joiningQuery) => {
546
+ return joinHasThrough(
547
+ baseQuery,
548
+ baseQuery,
549
+ joiningQuery,
550
+ throughRelation,
551
+ sourceRelation
552
+ );
553
+ };
529
554
  return {
530
555
  returns: "one",
531
556
  method: (params) => {
@@ -535,24 +560,17 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
535
560
  whereExistsCallback
536
561
  );
537
562
  },
538
- joinQuery(fromQuery, toQuery) {
539
- return joinHasThrough(
540
- toQuery,
541
- fromQuery,
542
- toQuery,
563
+ joinQuery: joinQueryChainingHOF(
564
+ reverseJoin2,
565
+ (joiningQuery, baseQuery) => joinHasThrough(
566
+ joiningQuery,
567
+ baseQuery,
568
+ joiningQuery,
543
569
  throughRelation,
544
570
  sourceRelation
545
- );
546
- },
547
- reverseJoin(fromQuery, toQuery) {
548
- return joinHasThrough(
549
- fromQuery,
550
- fromQuery,
551
- toQuery,
552
- throughRelation,
553
- sourceRelation
554
- );
555
- }
571
+ )
572
+ ),
573
+ reverseJoin: reverseJoin2
556
574
  };
557
575
  }
558
576
  const primaryKeys = "columns" in relation.options ? relation.options.columns : [relation.options.primaryKey];
@@ -564,6 +582,15 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
564
582
  reversedOn[foreignKeys[i]] = primaryKeys[i];
565
583
  }
566
584
  const fromQuerySelect = [{ selectAs: reversedOn }];
585
+ const reverseJoin = (baseQuery, joiningQuery) => {
586
+ return joinHasRelation(
587
+ joiningQuery,
588
+ baseQuery,
589
+ foreignKeys,
590
+ primaryKeys,
591
+ len
592
+ );
593
+ };
567
594
  return {
568
595
  returns: "one",
569
596
  method: (params) => {
@@ -574,19 +601,18 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
574
601
  return query.where(values)._defaults(values);
575
602
  },
576
603
  virtualColumn: new HasOneVirtualColumn(relationName, state),
577
- joinQuery(fromQuery, toQuery) {
578
- return joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len);
579
- },
580
- reverseJoin(fromQuery, toQuery) {
581
- return joinHasRelation(toQuery, fromQuery, foreignKeys, primaryKeys, len);
582
- },
604
+ joinQuery: joinQueryChainingHOF(
605
+ reverseJoin,
606
+ (joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
607
+ ),
608
+ reverseJoin,
583
609
  modifyRelatedQuery(relationQuery) {
584
610
  return (query2) => {
585
- const fromQuery = query2.clone();
586
- fromQuery.q.select = fromQuerySelect;
611
+ const baseQuery = query2.clone();
612
+ baseQuery.q.select = fromQuerySelect;
587
613
  const q = relationQuery.q;
588
614
  q.kind = "from";
589
- q.values = { from: fromQuery };
615
+ q.values = { from: baseQuery };
590
616
  };
591
617
  }
592
618
  };
@@ -760,10 +786,19 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
760
786
  const sourceRelation = getSourceRelation(throughRelation, source);
761
787
  const sourceRelationQuery = sourceRelation.query.as(relationName);
762
788
  const sourceQuery = sourceRelation.joinQuery(
763
- throughRelation.query,
764
- sourceRelationQuery
789
+ sourceRelationQuery,
790
+ throughRelation.query
765
791
  );
766
792
  const whereExistsCallback = () => sourceQuery;
793
+ const reverseJoin2 = (baseQuery, joiningQuery) => {
794
+ return joinHasThrough(
795
+ baseQuery,
796
+ baseQuery,
797
+ joiningQuery,
798
+ throughRelation,
799
+ sourceRelation
800
+ );
801
+ };
767
802
  return {
768
803
  returns: "many",
769
804
  method: (params) => {
@@ -773,24 +808,17 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
773
808
  whereExistsCallback
774
809
  );
775
810
  },
776
- joinQuery(fromQuery, toQuery) {
777
- return joinHasThrough(
778
- toQuery,
779
- fromQuery,
780
- toQuery,
781
- throughRelation,
782
- sourceRelation
783
- );
784
- },
785
- reverseJoin(fromQuery, toQuery) {
786
- return joinHasThrough(
787
- fromQuery,
788
- fromQuery,
789
- toQuery,
811
+ joinQuery: joinQueryChainingHOF(
812
+ reverseJoin2,
813
+ (joiningQuery, baseQuery) => joinHasThrough(
814
+ joiningQuery,
815
+ baseQuery,
816
+ joiningQuery,
790
817
  throughRelation,
791
818
  sourceRelation
792
- );
793
- }
819
+ )
820
+ ),
821
+ reverseJoin: reverseJoin2
794
822
  };
795
823
  }
796
824
  const primaryKeys = "columns" in relation.options ? relation.options.columns : [relation.options.primaryKey];
@@ -802,6 +830,15 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
802
830
  reversedOn[foreignKeys[i]] = primaryKeys[i];
803
831
  }
804
832
  const fromQuerySelect = [{ selectAs: reversedOn }];
833
+ const reverseJoin = (baseQuery, joiningQuery) => {
834
+ return joinHasRelation(
835
+ joiningQuery,
836
+ baseQuery,
837
+ foreignKeys,
838
+ primaryKeys,
839
+ len
840
+ );
841
+ };
805
842
  return {
806
843
  returns: "many",
807
844
  method: (params) => {
@@ -812,19 +849,18 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
812
849
  return query.where(values)._defaults(values);
813
850
  },
814
851
  virtualColumn: new HasManyVirtualColumn(relationName, state),
815
- joinQuery(fromQuery, toQuery) {
816
- return joinHasRelation(fromQuery, toQuery, primaryKeys, foreignKeys, len);
817
- },
818
- reverseJoin(fromQuery, toQuery) {
819
- return joinHasRelation(toQuery, fromQuery, foreignKeys, primaryKeys, len);
820
- },
852
+ joinQuery: joinQueryChainingHOF(
853
+ reverseJoin,
854
+ (joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
855
+ ),
856
+ reverseJoin,
821
857
  modifyRelatedQuery(relationQuery) {
822
858
  return (query2) => {
823
- const fromQuery = query2.clone();
824
- fromQuery.q.select = fromQuerySelect;
859
+ const baseQuery = query2.clone();
860
+ baseQuery.q.select = fromQuerySelect;
825
861
  const q = relationQuery.q;
826
862
  q.kind = "from";
827
- q.values = { from: fromQuery };
863
+ q.values = { from: baseQuery };
828
864
  };
829
865
  }
830
866
  };
@@ -1096,8 +1132,8 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1096
1132
  throughForeignKeysFull,
1097
1133
  throughPrimaryKeysFull
1098
1134
  };
1099
- const joinQuery = (toQuery, tableAs, foreignAs) => {
1100
- return toQuery.whereExists(subQuery, (q) => {
1135
+ const joinQuery = (joiningQuery, tableAs, foreignAs) => {
1136
+ return joiningQuery.whereExists(subQuery, (q) => {
1101
1137
  for (let i = 0; i < throughLen; i++) {
1102
1138
  q._on(
1103
1139
  throughForeignKeysFull[i],
@@ -1115,6 +1151,13 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1115
1151
  obj[foreignKeys[i]] = primaryKeys[i];
1116
1152
  }
1117
1153
  const selectPrimaryKeysAsForeignKeys = [{ selectAs: obj }];
1154
+ const reverseJoin = (baseQuery2, joiningQuery) => {
1155
+ return joinQuery(
1156
+ baseQuery2,
1157
+ pqb.getQueryAs(baseQuery2),
1158
+ pqb.getQueryAs(joiningQuery)
1159
+ );
1160
+ };
1118
1161
  return {
1119
1162
  returns: "many",
1120
1163
  method(params) {
@@ -1131,22 +1174,18 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1131
1174
  });
1132
1175
  },
1133
1176
  virtualColumn: new HasAndBelongsToManyVirtualColumn(relationName, state),
1134
- // joinQuery can be a property of RelationQuery and be used by whereExists and other stuff which needs it
1135
- // and the chained query itself may be a query around this joinQuery
1136
- joinQuery(fromQuery, toQuery) {
1137
- const join = joinQuery(
1138
- toQuery,
1139
- pqb.getQueryAs(fromQuery),
1140
- pqb.getQueryAs(toQuery)
1177
+ joinQuery: joinQueryChainingHOF(reverseJoin, (joiningQuery, baseQuery2) => {
1178
+ const joined = joinQuery(
1179
+ joiningQuery,
1180
+ pqb.getQueryAs(baseQuery2),
1181
+ pqb.getQueryAs(joiningQuery)
1141
1182
  );
1142
- join.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, join.q.joinedShapes), {
1143
- [fromQuery.q.as || fromQuery.table]: fromQuery.q.shape
1183
+ joined.q.joinedShapes = __spreadProps$1(__spreadValues$3({}, joined.q.joinedShapes), {
1184
+ [baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
1144
1185
  });
1145
- return join;
1146
- },
1147
- reverseJoin(fromQuery, toQuery) {
1148
- return joinQuery(fromQuery, pqb.getQueryAs(fromQuery), pqb.getQueryAs(toQuery));
1149
- },
1186
+ return joined;
1187
+ }),
1188
+ reverseJoin,
1150
1189
  modifyRelatedQuery(relationQuery) {
1151
1190
  const ref = {};
1152
1191
  relationQuery._afterCreate([], async (result) => {
@@ -1156,15 +1195,15 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1156
1195
  "Creating multiple `hasAndBelongsToMany` records is not yet supported"
1157
1196
  );
1158
1197
  }
1159
- const fromQuery = ref.q.clone();
1160
- fromQuery.q.select = selectPrimaryKeysAsForeignKeys;
1198
+ const baseQuery2 = ref.q.clone();
1199
+ baseQuery2.q.select = selectPrimaryKeysAsForeignKeys;
1161
1200
  const data = {};
1162
1201
  for (let i = 0; i < throughLen; i++) {
1163
1202
  data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
1164
1203
  }
1165
- const createdCount = await subQuery.count()._createFrom(fromQuery, data);
1204
+ const createdCount = await subQuery.count()._createFrom(baseQuery2, data);
1166
1205
  if (createdCount === 0) {
1167
- throw new pqb.NotFoundError(fromQuery);
1206
+ throw new pqb.NotFoundError(baseQuery2);
1168
1207
  }
1169
1208
  });
1170
1209
  return (q) => {
@@ -1586,10 +1625,23 @@ const makeRelationQuery = (table, relationName, data, q) => {
1586
1625
  get() {
1587
1626
  var _a;
1588
1627
  const toTable = q.clone();
1589
- const query = this.q.isSubQuery ? toTable : toTable._all()._whereExists(
1590
- this.baseQuery,
1591
- (q2) => data.reverseJoin(this, toTable)
1592
- );
1628
+ let query;
1629
+ if (this.q.isSubQuery) {
1630
+ query = toTable;
1631
+ query.q.isSubQuery = true;
1632
+ } else {
1633
+ query = toTable._all()._where({
1634
+ EXISTS: {
1635
+ args: [data.reverseJoin(this, toTable)]
1636
+ }
1637
+ });
1638
+ }
1639
+ if (this.q.relChain) {
1640
+ query.q.relChain = [...this.q.relChain, this];
1641
+ query.q.returnType = "all";
1642
+ } else {
1643
+ query.q.relChain = [this];
1644
+ }
1593
1645
  query.q.joinedShapes = __spreadValues$2({
1594
1646
  [pqb.getQueryAs(this)]: this.q.shape
1595
1647
  }, this.q.joinedShapes);