orchid-orm 1.40.2 → 1.41.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,16 +1,14 @@
1
- import { Query, TableData, SelectableFromShape, CreateMethodsNames, CreateData, CreateBelongsToData, AddQueryDefaults, RelationConfigBase, SetQueryReturnsOne, SetQueryReturnsOneOptional, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, RelationsBase, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, QueryData, TableDataFn, DbTableOptionScopes, RawSQL, DynamicRawSQL, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
1
+ import { TableData, Query, SelectableFromShape, CreateMethodsNames, CreateData, CreateBelongsToData, AddQueryDefaults, RelationConfigBase, SetQueryReturnsOne, SetQueryReturnsOneOptional, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, RelationsBase, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, QueryData, TableDataFn, DbTableOptionScopes, RawSQL, DynamicRawSQL, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
2
2
  export * from 'pqb';
3
- import { ColumnsShapeBase, EmptyObject, MaybeArray, RecordUnknown, ShallowSimplify, ColumnShapeOutput, DefaultSelectColumns, ColumnShapeInput, ColumnShapeInputPartial, IsQuery, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, QueryColumn, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
3
+ import { ColumnsShapeBase, ColumnShapeInputPartial, EmptyObject, MaybeArray, RecordUnknown, ShallowSimplify, ColumnShapeOutput, DefaultSelectColumns, ColumnShapeInput, IsQuery, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, QueryColumn, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
4
4
  export * from 'orchid-core';
5
5
 
6
- interface RelationCommonOptions<Related extends TableClass = TableClass, Scope extends Query = Query> {
7
- scope?: ScopeFn<Related, Scope>;
6
+ interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends ColumnsShapeBase = ColumnsShapeBase> {
8
7
  required?: boolean;
9
- }
10
- interface RelationRefsOptions<Column extends PropertyKey = string, Ref extends PropertyKey = string> {
11
8
  columns: Column[];
12
- references: Ref[];
9
+ references: (keyof Shape)[];
13
10
  foreignKey?: boolean | TableData.References.Options;
11
+ on?: ColumnShapeInputPartial<Shape>;
14
12
  }
15
13
  interface RelationThroughOptions<Through extends PropertyKey = string, Source extends PropertyKey = string> {
16
14
  through: Through;
@@ -21,7 +19,10 @@ interface HasOne extends RelationThunkBase {
21
19
  type: 'hasOne';
22
20
  options: HasOneOptions;
23
21
  }
24
- type HasOneOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass, Scope extends Query = Query, Through extends string = string, Source extends string = string> = RelationCommonOptions<Related, Scope> & (RelationRefsOptions<keyof Columns, keyof InstanceType<Related>['columns']['shape']> | RelationThroughOptions<Through, Source>);
22
+ interface RelationHasOneThroughOptions<Through extends string, Source extends string> extends RelationThroughOptions<Through, Source> {
23
+ required?: boolean;
24
+ }
25
+ type HasOneOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass, Through extends string = string, Source extends string = string> = RelationRefsOptions<keyof Columns, InstanceType<Related>['columns']['shape']> | RelationHasOneThroughOptions<Through, Source>;
25
26
  type HasOneParams<T extends RelationConfigSelf, Relation extends RelationThunkBase> = Relation['options'] extends RelationRefsOptions ? {
26
27
  [Name in Relation['options']['columns'][number]]: T['columns']['shape'][Name]['type'];
27
28
  } : Relation['options'] extends RelationThroughOptions ? RelationConfigParams<T, T['relations'][Relation['options']['through']]> : never;
@@ -127,8 +128,7 @@ interface BelongsTo extends RelationThunkBase {
127
128
  type: 'belongsTo';
128
129
  options: BelongsToOptions;
129
130
  }
130
- interface BelongsToOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass, Scope extends Query = Query> extends RelationCommonOptions<Related, Scope>, RelationRefsOptions<keyof Columns, keyof InstanceType<Related>['columns']['shape']> {
131
- }
131
+ type BelongsToOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass> = RelationRefsOptions<keyof Columns, InstanceType<Related>['columns']['shape']>;
132
132
  type BelongsToFKey<Relation extends RelationThunkBase> = Relation['options'] extends RelationRefsOptions ? Relation['options']['columns'][number] : never;
133
133
  type BelongsToParams<T extends RelationConfigSelf, Relation extends BelongsTo> = {
134
134
  [Name in BelongsToFKey<Relation>]: T['columns']['shape'][Name]['type'];
@@ -291,8 +291,7 @@ interface HasAndBelongsToMany extends RelationThunkBase {
291
291
  type: 'hasAndBelongsToMany';
292
292
  options: HasAndBelongsToManyOptions;
293
293
  }
294
- interface HasAndBelongsToManyOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass, Scope extends Query = Query> {
295
- scope?: ScopeFn<Related, Scope>;
294
+ interface HasAndBelongsToManyOptions<Columns extends ColumnsShapeBase = ColumnsShapeBase, Related extends TableClass = TableClass> {
296
295
  required?: boolean;
297
296
  columns: (keyof Columns)[];
298
297
  references: string[];
@@ -303,6 +302,7 @@ interface HasAndBelongsToManyOptions<Columns extends ColumnsShapeBase = ColumnsS
303
302
  references: (keyof InstanceType<Related>['columns']['shape'])[];
304
303
  foreignKey?: boolean | TableData.References.Options;
305
304
  };
305
+ on?: ColumnShapeInputPartial<InstanceType<Related>['columns']['shape']>;
306
306
  }
307
307
  type HasAndBelongsToManyParams<T extends RelationConfigSelf, Relation extends HasAndBelongsToMany> = {
308
308
  [Name in Relation['options']['columns'][number]]: T['columns']['shape'][Name]['type'];
@@ -392,13 +392,13 @@ type RelationToOneDataForCreateSameQuery<Q extends Query> = {
392
392
  interface RelationThunkBase {
393
393
  type: string;
394
394
  fn(): TableClass;
395
- options: RelationCommonOptions;
395
+ options: unknown;
396
396
  }
397
397
  type RelationThunk = BelongsTo | HasOne | HasMany | HasAndBelongsToMany;
398
398
  interface RelationThunks {
399
399
  [K: string]: RelationThunk;
400
400
  }
401
- type RelationScopeOrTable<Relation extends RelationThunkBase> = Relation['options']['scope'] extends (q: Query) => Query ? ReturnType<Relation['options']['scope']> : ORMTableInputToQueryBuilder<InstanceType<ReturnType<Relation['fn']>>>;
401
+ type RelationTableToQuery<Relation extends RelationThunkBase> = ORMTableInputToQueryBuilder<InstanceType<ReturnType<Relation['fn']>>>;
402
402
  interface RelationConfigSelf {
403
403
  columns: {
404
404
  shape: ColumnsShapeBase;
@@ -407,13 +407,13 @@ interface RelationConfigSelf {
407
407
  }
408
408
  type RelationConfigParams<T extends RelationConfigSelf, Relation extends RelationThunk> = Relation extends BelongsTo ? BelongsToParams<T, Relation> : Relation extends HasOne | HasMany ? HasOneParams<T, Relation> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyParams<T, Relation> : never;
409
409
  type MapRelation<T extends RelationConfigSelf, K extends keyof T['relations'] & string> = T['relations'][K] extends BelongsTo ? {
410
- relationConfig: BelongsToInfo<T, K, T['relations'][K], T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<RelationScopeOrTable<T['relations'][K]>, K>>;
410
+ relationConfig: BelongsToInfo<T, K, T['relations'][K], T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<RelationTableToQuery<T['relations'][K]>, K>>;
411
411
  } : T['relations'][K] extends HasOne ? {
412
- relationConfig: HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>;
412
+ relationConfig: HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>>;
413
413
  } : T['relations'][K] extends HasMany ? {
414
- relationConfig: HasManyInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>;
414
+ relationConfig: HasManyInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>>;
415
415
  } : T['relations'][K] extends HasAndBelongsToMany ? {
416
- relationConfig: HasAndBelongsToManyInfo<T, K, T['relations'][K], HasAndBelongsToManyQuery<K, RelationScopeOrTable<T['relations'][K]>>>;
416
+ relationConfig: HasAndBelongsToManyInfo<T, K, T['relations'][K], HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>>;
417
417
  } : never;
418
418
  type MapRelations<T> = T extends RelationConfigSelf ? {
419
419
  [K in keyof T['relations'] & string]: MapRelation<T, K>;
@@ -440,7 +440,6 @@ interface TableToDb<T extends ORMTableInput, Relations extends RelationsBase> ex
440
440
  };
441
441
  }
442
442
  type ORMTableInputToQueryBuilder<T extends ORMTableInput> = T extends RelationConfigSelf ? TableToDb<T, MapRelations<T>> : TableToDb<T, EmptyObject>;
443
- type ScopeFn<Related extends TableClass, Scope extends Query> = (q: ORMTableInputToQueryBuilder<InstanceType<Related>>) => Scope;
444
443
  interface ORMTableInput {
445
444
  table: string;
446
445
  columns: {
@@ -599,7 +598,7 @@ interface BaseTableInstance<ColumnTypes> {
599
598
  shape: Shape;
600
599
  };
601
600
  }, scopes: DbTableOptionScopes<Table, Shape, Keys>): CoreQueryScopes<Keys>;
602
- belongsTo<Columns extends ColumnsShapeBase, Related extends TableClass, Scope extends Query, Options extends BelongsToOptions<Columns, Related, Scope>>(this: {
601
+ belongsTo<Columns extends ColumnsShapeBase, Related extends TableClass, Options extends BelongsToOptions<Columns, Related>>(this: {
603
602
  columns: {
604
603
  shape: Columns;
605
604
  };
@@ -608,7 +607,7 @@ interface BaseTableInstance<ColumnTypes> {
608
607
  fn: () => Related;
609
608
  options: Options;
610
609
  };
611
- hasOne<Columns extends ColumnsShapeBase, Related extends TableClass, Scope extends Query, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Scope, Through, Source>>(this: {
610
+ hasOne<Columns extends ColumnsShapeBase, Related extends TableClass, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Through, Source>>(this: {
612
611
  columns: {
613
612
  shape: Columns;
614
613
  };
@@ -617,7 +616,7 @@ interface BaseTableInstance<ColumnTypes> {
617
616
  fn: () => Related;
618
617
  options: Options;
619
618
  };
620
- hasMany<Columns extends ColumnsShapeBase, Related extends TableClass, Scope extends Query, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Scope, Through, Source>>(this: {
619
+ hasMany<Columns extends ColumnsShapeBase, Related extends TableClass, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Through, Source>>(this: {
621
620
  columns: {
622
621
  shape: Columns;
623
622
  };
@@ -626,7 +625,7 @@ interface BaseTableInstance<ColumnTypes> {
626
625
  fn: () => Related;
627
626
  options: Options;
628
627
  };
629
- hasAndBelongsToMany<Columns extends ColumnsShapeBase, Related extends TableClass, Scope extends Query, Options extends HasAndBelongsToManyOptions<Columns, Related, Scope>>(this: {
628
+ hasAndBelongsToMany<Columns extends ColumnsShapeBase, Related extends TableClass, Options extends HasAndBelongsToManyOptions<Columns, Related>>(this: {
630
629
  columns: {
631
630
  shape: Columns;
632
631
  };
@@ -718,4 +717,4 @@ declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(tabl
718
717
  shape: T['shape'];
719
718
  }>(q: Q) => Query & Q & MapMethods<T, Methods>) & T, Methods>;
720
719
 
721
- export { type BaseTableClass, type BaseTableInstance, type Insertable, type MapMethods, type MapQueryMethods, type MethodsBase, type ORMTableInput, type ORMTableInputToQueryBuilder, type OrchidORM, type Queryable, type Repo, type ScopeFn, type Selectable, type SetColumnsResult, type Table, type TableClass, type TableClasses, type TableInfo, type TableToDb, type Updatable, createBaseTable, createRepo, orchidORM };
720
+ export { type BaseTableClass, type BaseTableInstance, type Insertable, type MapMethods, type MapQueryMethods, type MethodsBase, type ORMTableInput, type ORMTableInputToQueryBuilder, type OrchidORM, type Queryable, type Repo, type Selectable, type SetColumnsResult, type Table, type TableClass, type TableClasses, type TableInfo, type TableToDb, type Updatable, createBaseTable, createRepo, orchidORM };
package/dist/index.js CHANGED
@@ -204,13 +204,6 @@ const selectIfNotSelected = (q, columns) => {
204
204
  q.q.select = select;
205
205
  }
206
206
  };
207
- const relationWhere = (len, keys, valueKeys) => (params) => {
208
- const obj = {};
209
- for (let i = 0; i < len; i++) {
210
- obj[keys[i]] = params[valueKeys[i]];
211
- }
212
- return obj;
213
- };
214
207
  function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelation) {
215
208
  return q.whereExists(
216
209
  throughRelation.joinQuery(throughRelation.query, baseQuery),
@@ -224,14 +217,9 @@ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelat
224
217
  );
225
218
  }
226
219
  function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
227
- const q = joiningQuery.clone();
228
- pqb.setQueryObjectValue(
229
- q,
230
- "joinedShapes",
231
- baseQuery.q.as || baseQuery.table,
232
- baseQuery.q.shape
233
- );
234
220
  const baseAs = pqb.getQueryAs(baseQuery);
221
+ const q = joiningQuery.clone();
222
+ pqb.setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
235
223
  for (let i = 0; i < len; i++) {
236
224
  pqb.pushQueryOnForOuter(
237
225
  q,
@@ -340,7 +328,7 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
340
328
  const relationData = [values];
341
329
  store.belongsTo[key] = relationData;
342
330
  q.q.wrapInTransaction = true;
343
- pqb.pushQueryValue(q, "beforeCreate", async (q2) => {
331
+ pqb.pushQueryValueImmutable(q, "beforeCreate", async (q2) => {
344
332
  const inserted = await this.nestedInsert(
345
333
  q2,
346
334
  relationData.map(([, , data]) => data)
@@ -364,9 +352,13 @@ class BelongsToVirtualColumn extends pqb.VirtualColumn {
364
352
  const makeBelongsToMethod = (tableConfig, table, relation, relationName, query) => {
365
353
  const primaryKeys = relation.options.references;
366
354
  const foreignKeys = relation.options.columns;
355
+ const { on } = relation.options;
356
+ if (on) {
357
+ pqb._queryWhere(query, [on]);
358
+ pqb._queryDefaults(query, on);
359
+ }
367
360
  const len = primaryKeys.length;
368
- const state = { query, primaryKeys, foreignKeys, len };
369
- const makeWhere = relationWhere(len, primaryKeys, foreignKeys);
361
+ const state = { query, primaryKeys, foreignKeys, len, on };
370
362
  addAutoForeignKey(
371
363
  tableConfig,
372
364
  table,
@@ -376,14 +368,9 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
376
368
  relation.options
377
369
  );
378
370
  const join = (baseQuery, joiningQuery, primaryKeys2, foreignKeys2) => {
379
- const q = joiningQuery.clone();
380
- pqb.setQueryObjectValue(
381
- q,
382
- "joinedShapes",
383
- baseQuery.q.as || baseQuery.table,
384
- baseQuery.q.shape
385
- );
386
371
  const baseAs = pqb.getQueryAs(baseQuery);
372
+ const q = joiningQuery.clone();
373
+ pqb.setQueryObjectValueImmutable(q, "joinedShapes", baseAs, baseQuery.q.shape);
387
374
  for (let i = 0; i < len; i++) {
388
375
  pqb.pushQueryOnForOuter(
389
376
  q,
@@ -406,7 +393,11 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
406
393
  return {
407
394
  returns: "one",
408
395
  queryRelated(params) {
409
- return query.where(makeWhere(params));
396
+ const obj = {};
397
+ for (let i = 0; i < len; i++) {
398
+ obj[primaryKeys[i]] = params[foreignKeys[i]];
399
+ }
400
+ return query.where(obj);
410
401
  },
411
402
  virtualColumn: new BelongsToVirtualColumn(
412
403
  pqb.defaultSchemaConfig,
@@ -420,13 +411,21 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
420
411
  reverseJoin
421
412
  };
422
413
  };
423
- const nestedInsert$3 = ({ query, primaryKeys }) => {
414
+ const nestedInsert$3 = ({ query, primaryKeys, on }) => {
424
415
  return async (_, data) => {
425
416
  const t = query.clone();
426
417
  const items = [];
427
418
  for (const item of data) {
428
419
  if (item.connectOrCreate) {
429
- items.push(item);
420
+ items.push(
421
+ on ? {
422
+ ...item,
423
+ connectOrCreate: {
424
+ ...item.connectOrCreate,
425
+ where: { ...item.connectOrCreate.where, ...on }
426
+ }
427
+ } : item
428
+ );
430
429
  }
431
430
  }
432
431
  let connectOrCreated;
@@ -464,7 +463,9 @@ const nestedInsert$3 = ({ query, primaryKeys }) => {
464
463
  items.length = 0;
465
464
  for (const item of data) {
466
465
  if (item.connect) {
467
- items.push(item);
466
+ items.push(
467
+ on ? { ...item, connect: { ...item.connect, ...on } } : item
468
+ );
468
469
  }
469
470
  }
470
471
  let connected;
@@ -551,12 +552,11 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
551
552
  }
552
553
  obj[primaryKeys[i]] = id;
553
554
  }
554
- if (obj) {
555
- await pqb._queryUpdate(
556
- query.findBy(obj),
557
- upsert.update
558
- );
559
- } else {
555
+ const count = obj ? await pqb._queryUpdate(
556
+ query.findBy(obj),
557
+ upsert.update
558
+ ) : 0;
559
+ if (!count) {
560
560
  const data = typeof upsert.create === "function" ? upsert.create() : upsert.create;
561
561
  const result = await pqb._queryCreate(
562
562
  query.select(...primaryKeys),
@@ -647,10 +647,13 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
647
647
  const { through, source } = relation.options;
648
648
  const throughRelation = getThroughRelation(table, through);
649
649
  const sourceRelation = getSourceRelation(throughRelation, source);
650
+ const sourceRelationQuery = sourceRelation.query.as(
651
+ relationName
652
+ );
650
653
  const sourceQuery = sourceRelation.joinQuery(
651
- sourceRelation.query,
654
+ sourceRelationQuery,
652
655
  throughRelation.query
653
- ).as(relationName);
656
+ );
654
657
  const whereExistsCallback = () => sourceQuery;
655
658
  const reverseJoin2 = (baseQuery, joiningQuery) => {
656
659
  return joinHasThrough(
@@ -682,6 +685,11 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
682
685
  }
683
686
  const primaryKeys = relation.options.columns;
684
687
  const foreignKeys = relation.options.references;
688
+ const { on } = relation.options;
689
+ if (on) {
690
+ pqb._queryWhere(query, [on]);
691
+ pqb._queryDefaults(query, on);
692
+ }
685
693
  addAutoForeignKey(
686
694
  tableConfig,
687
695
  query,
@@ -690,7 +698,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
690
698
  foreignKeys,
691
699
  relation.options
692
700
  );
693
- const state = { query, primaryKeys, foreignKeys };
701
+ const state = { query, primaryKeys, foreignKeys, on };
694
702
  const len = primaryKeys.length;
695
703
  const reversedOn = {};
696
704
  for (let i = 0; i < len; i++) {
@@ -713,7 +721,7 @@ const makeHasOneMethod = (tableConfig, table, relation, relationName, query) =>
713
721
  for (let i = 0; i < len; i++) {
714
722
  values[foreignKeys[i]] = params[primaryKeys[i]];
715
723
  }
716
- return pqb._queryDefaults(query.where(values), values);
724
+ return pqb._queryDefaults(query.where(values), { ...on, ...values });
717
725
  },
718
726
  virtualColumn: new HasOneVirtualColumn(
719
727
  pqb.defaultSchemaConfig,
@@ -837,7 +845,9 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
837
845
  } else if (params.update) {
838
846
  await pqb._queryUpdate(currentRelationsQuery, params.update);
839
847
  } else if (params.delete) {
840
- await pqb._queryDelete(currentRelationsQuery);
848
+ const q = pqb._queryDelete(currentRelationsQuery);
849
+ q.q.returnType = "value";
850
+ await q;
841
851
  } else if (params.upsert) {
842
852
  const { update, create } = params.upsert;
843
853
  currentRelationsQuery.q.select = foreignKeys;
@@ -946,6 +956,11 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
946
956
  }
947
957
  const primaryKeys = relation.options.columns;
948
958
  const foreignKeys = relation.options.references;
959
+ const { on } = relation.options;
960
+ if (on) {
961
+ pqb._queryWhere(query, [on]);
962
+ pqb._queryDefaults(query, on);
963
+ }
949
964
  addAutoForeignKey(
950
965
  tableConfig,
951
966
  query,
@@ -954,7 +969,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
954
969
  foreignKeys,
955
970
  relation.options
956
971
  );
957
- const state = { query, primaryKeys, foreignKeys };
972
+ const state = { query, primaryKeys, foreignKeys, on };
958
973
  const len = primaryKeys.length;
959
974
  const reversedOn = {};
960
975
  for (let i = 0; i < len; i++) {
@@ -977,7 +992,7 @@ const makeHasManyMethod = (tableConfig, table, relation, relationName, query) =>
977
992
  for (let i = 0; i < len; i++) {
978
993
  values[foreignKeys[i]] = params[primaryKeys[i]];
979
994
  }
980
- return pqb._queryDefaults(query.where(values), values);
995
+ return pqb._queryDefaults(query.where(values), { ...on, ...values });
981
996
  },
982
997
  virtualColumn: new HasManyVirtualColumn(
983
998
  pqb.defaultSchemaConfig,
@@ -1033,7 +1048,7 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1033
1048
  obj[foreignKeys[i2]] = selfData[primaryKeys[i2]];
1034
1049
  }
1035
1050
  items[i] = pqb._queryUpdateOrThrow(
1036
- t.orWhere(...connect),
1051
+ t.where({ OR: connect }),
1037
1052
  obj
1038
1053
  );
1039
1054
  }
@@ -1144,7 +1159,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1144
1159
  }
1145
1160
  const relatedWheres = orchidCore.toArray(params.add);
1146
1161
  const count = await pqb._queryUpdate(
1147
- t.orWhere(...relatedWheres),
1162
+ t.where({ OR: relatedWheres }),
1148
1163
  obj
1149
1164
  );
1150
1165
  if (count < relatedWheres.length) {
@@ -1246,6 +1261,11 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1246
1261
  const joinTable = options.through.table;
1247
1262
  const throughForeignKeys = options.through.columns;
1248
1263
  const throughPrimaryKeys = options.through.references;
1264
+ const { on } = options;
1265
+ if (on) {
1266
+ pqb._queryWhere(query, [on]);
1267
+ pqb._queryDefaults(query, on);
1268
+ }
1249
1269
  const { snakeCase } = table.internal;
1250
1270
  const foreignKeysFull = foreignKeys.map((key, i) => {
1251
1271
  if (snakeCase) key = foreignKeys[i] = orchidCore.toSnakeCase(key);
@@ -1308,7 +1328,8 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1308
1328
  foreignKeysFull,
1309
1329
  throughForeignKeysFull,
1310
1330
  throughPrimaryKeysFull,
1311
- primaryKeysShape
1331
+ primaryKeysShape,
1332
+ on
1312
1333
  };
1313
1334
  const joinQuery = (joiningQuery, tableAs, foreignAs, joinedShapes) => {
1314
1335
  const cloned = joiningQuery.clone();
@@ -1348,20 +1369,21 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1348
1369
  return {
1349
1370
  returns: "many",
1350
1371
  queryRelated(params) {
1351
- return query.whereExists(subQuery, (q) => {
1352
- q = q.clone();
1372
+ const q = query.whereExists(subQuery, (q2) => {
1373
+ q2 = q2.clone();
1353
1374
  const where = {};
1354
1375
  for (let i = 0; i < len; i++) {
1355
1376
  where[foreignKeysFull[i]] = params[primaryKeys[i]];
1356
1377
  }
1357
1378
  for (let i = 0; i < throughLen; i++) {
1358
- pqb._queryJoinOn(q, [
1379
+ pqb._queryJoinOn(q2, [
1359
1380
  throughForeignKeysFull[i],
1360
1381
  throughPrimaryKeysFull[i]
1361
1382
  ]);
1362
1383
  }
1363
- return pqb._queryWhere(q, [where]);
1384
+ return pqb._queryWhere(q2, [where]);
1364
1385
  });
1386
+ return on ? pqb._queryDefaults(q, on) : q;
1365
1387
  },
1366
1388
  virtualColumn: new HasAndBelongsToManyVirtualColumn(
1367
1389
  subQuery,
@@ -1436,6 +1458,19 @@ const queryJoinTable = (state, data, conditions) => {
1436
1458
  }
1437
1459
  ]);
1438
1460
  }
1461
+ if (state.on) {
1462
+ pqb._queryWhereExists(t, state.relatedTableQuery, [
1463
+ (q) => {
1464
+ for (let i = 0; i < state.throughPrimaryKeys.length; i++) {
1465
+ pqb._queryJoinOn(q, [
1466
+ state.throughPrimaryKeysFull[i],
1467
+ state.throughForeignKeysFull[i]
1468
+ ]);
1469
+ }
1470
+ return q;
1471
+ }
1472
+ ]);
1473
+ }
1439
1474
  return t;
1440
1475
  };
1441
1476
  const conditionsToWhereArg = (conditions) => Array.isArray(conditions) ? { OR: conditions } : conditions;
@@ -1663,7 +1698,7 @@ const nestedUpdate = (state) => {
1663
1698
  try {
1664
1699
  const count = await state.joinTableQuery.insertManyFrom(
1665
1700
  pqb._querySelect(
1666
- state.relatedTableQuery.orWhere(...relatedWheres),
1701
+ state.relatedTableQuery.whereOneOf(...relatedWheres),
1667
1702
  [
1668
1703
  Object.fromEntries([
1669
1704
  ...state.primaryKeys.map((key, i) => [
@@ -1690,7 +1725,7 @@ const nestedUpdate = (state) => {
1690
1725
  if (count < data.length * relatedWheres.length) {
1691
1726
  throw new pqb.OrchidOrmInternalError(
1692
1727
  query,
1693
- `Expected to find at least ${relatedWheres.length} record(s) based on \`connect\` conditions, but found ${count / data.length}`
1728
+ `Expected to find at least ${relatedWheres.length} record(s) based on \`add\` conditions, but found ${count / data.length}`
1694
1729
  );
1695
1730
  }
1696
1731
  } catch (err) {
@@ -1832,7 +1867,7 @@ const delayRelation = (delayedRelations, table, relationName, data) => {
1832
1867
  const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations) => {
1833
1868
  const baseQuery = Object.create(otherDbTable);
1834
1869
  baseQuery.baseQuery = baseQuery;
1835
- const query = (relation.options.scope ? relation.options.scope(baseQuery) : baseQuery).as(relationName);
1870
+ const query = baseQuery.as(relationName);
1836
1871
  const definedAs = query.definedAs;
1837
1872
  if (!definedAs) {
1838
1873
  throw new Error(