orchid-orm 1.36.4 → 1.37.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,6 +1,6 @@
1
- import { Query, SelectableFromShape, CreateMethodsNames, CreateData, AddQueryDefaults, RelationConfigBase, UpdateData, WhereArg, JoinQueryMethod, DeleteMethodsNames, Db, IsolationLevel, TransactionOptions, Adapter, FromArg, FromResult, AdapterOptions, DbSharedOptions, RelationJoinQuery, RelationQuery, RelationsBase, ShapeColumnPrimaryKeys, ShapeUniqueColumns, TableDataItemsUniqueColumns, TableDataItemsUniqueColumnTuples, UniqueConstraints, TableDataItemsUniqueConstraints, ComputedColumnsFromOptions, MapTableScopesOption, TableDataItem, ComputedOptionsFactory, QueryData, QueryBase, TableDataFn, DbTableOptionScopes, RawSQL, DynamicRawSQL, DefaultSchemaConfig, DefaultColumnTypes, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery } from 'pqb';
1
+ import { 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, Simplify, ColumnShapeOutput, DefaultSelectColumns, ColumnShapeInput, ColumnShapeInputPartial, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, QueryColumn, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
3
+ import { ColumnsShapeBase, EmptyObject, MaybeArray, RecordUnknown, Simplify, ColumnShapeOutput, DefaultSelectColumns, ColumnShapeInput, ColumnShapeInputPartial, IsQuery, CoreQueryScopes, ColumnSchemaConfig, StaticSQLArgs, QueryColumn, DynamicSQLArg, QueryColumns, QueryReturnType } from 'orchid-core';
4
4
  export * from 'orchid-core';
5
5
 
6
6
  interface RelationCommonOptions<Related extends TableClass = TableClass, Scope extends Query = Query> {
@@ -40,8 +40,10 @@ type HasOneQuery<T extends RelationConfigSelf, Name extends string, TableQuery e
40
40
  selectable: SelectableFromShape<TableQuery['shape'], Name>;
41
41
  } : K extends 'join' ? RelJoin : TableQuery[K];
42
42
  };
43
- interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Q extends Query, CD = CreateData<T['relations'][Name]['options'] extends RelationThroughOptions ? Q : AddQueryDefaults<Q, HasOnePopulate<T, Name>>>> extends RelationConfigBase {
43
+ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasOne, Q extends Query, CD = T['relations'][Name]['options'] extends RelationThroughOptions ? CreateData<Q, CreateBelongsToData<Q>> : CreateData<AddQueryDefaults<Q, HasOnePopulate<T, Name>>, CreateBelongsToData<Q>>> extends RelationConfigBase {
44
44
  query: Q;
45
+ params: HasOneParams<T, Rel>;
46
+ maybeSingle: T['relations'][Name]['options']['required'] extends true ? SetQueryReturnsOne<Q> : SetQueryReturnsOneOptional<Q>;
45
47
  omitForeignKeyInCreate: never;
46
48
  optionalDataForCreate: T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : {
47
49
  [P in Name]?: RelationToOneDataForCreate<{
@@ -59,12 +61,12 @@ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Q extend
59
61
  };
60
62
  dataForUpdateOne: {
61
63
  disconnect: boolean;
64
+ } | {
65
+ set: WhereArg<Q>;
62
66
  } | {
63
67
  delete: boolean;
64
68
  } | {
65
69
  update: UpdateData<Q>;
66
- } | {
67
- set: WhereArg<Q>;
68
70
  } | {
69
71
  upsert: {
70
72
  update: UpdateData<Q>;
@@ -79,8 +81,10 @@ interface HasMany extends RelationThunkBase {
79
81
  type: 'hasMany';
80
82
  options: HasOneOptions;
81
83
  }
82
- interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Q extends Query> extends RelationConfigBase {
84
+ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasMany, Q extends Query> extends RelationConfigBase {
83
85
  query: Q;
86
+ params: HasOneParams<T, Rel>;
87
+ maybeSingle: Q;
84
88
  omitForeignKeyInCreate: never;
85
89
  optionalDataForCreate: {
86
90
  [P in Name]?: T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : {
@@ -135,8 +139,10 @@ type BelongsToQuery<T extends Query, Name extends string> = {
135
139
  selectable: SelectableFromShape<T['shape'], Name>;
136
140
  } : P extends 'join' ? RelJoin : P extends CreateMethodsNames | DeleteMethodsNames ? never : T[P];
137
141
  };
138
- interface BelongsToInfo<Name extends string, FK extends string, Required, Q extends Query> extends RelationConfigBase {
142
+ interface BelongsToInfo<T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo, FK extends string, Required, Q extends Query> extends RelationConfigBase {
139
143
  query: Q;
144
+ params: BelongsToParams<T, Rel>;
145
+ maybeSingle: Required extends true ? SetQueryReturnsOne<Q> : SetQueryReturnsOneOptional<Q>;
140
146
  omitForeignKeyInCreate: FK;
141
147
  dataForCreate: {
142
148
  columns: FK;
@@ -156,7 +162,7 @@ interface BelongsToInfo<Name extends string, FK extends string, Required, Q exte
156
162
  } | {
157
163
  update: UpdateData<Q>;
158
164
  } | {
159
- create: CreateData<Q>;
165
+ create: CreateData<Q, CreateBelongsToData<Q>>;
160
166
  };
161
167
  dataForUpdateOne: {
162
168
  disconnect: boolean;
@@ -167,11 +173,11 @@ interface BelongsToInfo<Name extends string, FK extends string, Required, Q exte
167
173
  } | {
168
174
  update: UpdateData<Q>;
169
175
  } | {
170
- create: CreateData<Q>;
176
+ create: CreateData<Q, CreateBelongsToData<Q>>;
171
177
  } | {
172
178
  upsert: {
173
179
  update: UpdateData<Q>;
174
- create: CreateData<Q> | (() => CreateData<Q>);
180
+ create: CreateData<Q, CreateBelongsToData<Q>> | (() => CreateData<Q, CreateBelongsToData<Q>>);
175
181
  };
176
182
  };
177
183
  }
@@ -297,9 +303,10 @@ type HasAndBelongsToManyQuery<Name extends string, TableQuery extends Query> = {
297
303
  selectable: SelectableFromShape<TableQuery['shape'], Name>;
298
304
  } : K extends 'join' ? RelJoin : TableQuery[K];
299
305
  };
300
- interface HasAndBelongsToManyInfo<Name extends string, Q extends Query> extends RelationConfigBase {
306
+ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasAndBelongsToMany, Q extends Query> extends RelationConfigBase {
301
307
  query: Q;
302
- joinQuery: RelationJoinQuery;
308
+ params: HasAndBelongsToManyParams<T, Rel>;
309
+ maybeSingle: Q;
303
310
  omitForeignKeyInCreate: never;
304
311
  optionalDataForCreate: {
305
312
  [P in Name]?: {
@@ -386,9 +393,17 @@ interface RelationConfigSelf {
386
393
  relations: RelationThunks;
387
394
  }
388
395
  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;
389
- type MapRelation<T extends RelationConfigSelf, K extends keyof T['relations'] & string, R extends Query> = T['relations'][K] extends BelongsTo ? RelationQuery<BelongsToInfo<K, T['relations'][K]['options']['columns'][number] & string, T['relations'][K]['options']['required'], BelongsToQuery<R, K>>, BelongsToParams<T, T['relations'][K]>, T['relations'][K]['options']['required'], true> : T['relations'][K] extends HasOne ? RelationQuery<HasOneInfo<T, K, HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>, HasOneParams<T, T['relations'][K]>, T['relations'][K]['options']['required'], true> : T['relations'][K] extends HasMany ? RelationQuery<HasManyInfo<T, K, HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>, HasOneParams<T, T['relations'][K]>, true, false> : T['relations'][K] extends HasAndBelongsToMany ? RelationQuery<HasAndBelongsToManyInfo<K, HasAndBelongsToManyQuery<K, RelationScopeOrTable<T['relations'][K]>>>, HasAndBelongsToManyParams<T, T['relations'][K]>, true, false> : never;
396
+ type MapRelation<T extends RelationConfigSelf, K extends keyof T['relations'] & string> = T['relations'][K] extends BelongsTo ? {
397
+ 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>>;
398
+ } : T['relations'][K] extends HasOne ? {
399
+ relationConfig: HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>;
400
+ } : T['relations'][K] extends HasMany ? {
401
+ relationConfig: HasManyInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationScopeOrTable<T['relations'][K]>>>;
402
+ } : T['relations'][K] extends HasAndBelongsToMany ? {
403
+ relationConfig: HasAndBelongsToManyInfo<T, K, T['relations'][K], HasAndBelongsToManyQuery<K, RelationScopeOrTable<T['relations'][K]>>>;
404
+ } : never;
390
405
  type MapRelations<T> = T extends RelationConfigSelf ? {
391
- [K in keyof T['relations'] & string]: MapRelation<T, K, RelationScopeOrTable<T['relations'][K]>>;
406
+ [K in keyof T['relations'] & string]: MapRelation<T, K>;
392
407
  } : EmptyObject;
393
408
 
394
409
  interface TableClass<T extends ORMTableInput = ORMTableInput> {
@@ -407,9 +422,11 @@ interface TableInfo {
407
422
  interface Table extends Query, TableInfo {
408
423
  }
409
424
  interface TableToDb<T extends ORMTableInput, Relations extends RelationsBase> extends TableInfo, Db<T['table'], T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>> {
410
- relations: Relations;
425
+ relations: {
426
+ [K in keyof Relations]: Relations[K]['relationConfig']['query'] & Relations[K];
427
+ };
411
428
  }
412
- type ORMTableInputToQueryBuilder<T extends ORMTableInput> = T extends RelationConfigSelf ? TableToDb<T, MapRelations<T>> & MapRelations<T> : TableToDb<T, EmptyObject>;
429
+ type ORMTableInputToQueryBuilder<T extends ORMTableInput> = T extends RelationConfigSelf ? TableToDb<T, MapRelations<T>> : TableToDb<T, EmptyObject>;
413
430
  type ScopeFn<Related extends TableClass, Scope extends Query> = (q: ORMTableInputToQueryBuilder<InstanceType<Related>>) => Scope;
414
431
  interface ORMTableInput {
415
432
  table: string;
@@ -461,7 +478,7 @@ interface BaseTableInstance<ColumnTypes> {
461
478
  language?: string;
462
479
  filePath: string;
463
480
  result: ColumnsShapeBase;
464
- clone<T extends QueryBase>(this: T): T;
481
+ clone<T extends IsQuery>(this: T): T;
465
482
  getFilePath(): string;
466
483
  setColumns<Shape extends ColumnsShapeBase, Data extends MaybeArray<TableDataItem>>(fn: (t: ColumnTypes) => Shape, tableData?: TableDataFn<Shape, Data>): SetColumnsResult<Shape, Data>;
467
484
  /**
package/dist/index.js CHANGED
@@ -341,11 +341,16 @@ const makeBelongsToMethod = (relation, relationName, query) => {
341
341
  return q;
342
342
  };
343
343
  const reverseJoin = (baseQuery, joiningQuery) => {
344
- return join(joiningQuery, baseQuery, foreignKeys, primaryKeys);
344
+ return join(
345
+ joiningQuery,
346
+ baseQuery,
347
+ foreignKeys,
348
+ primaryKeys
349
+ );
345
350
  };
346
351
  return {
347
352
  returns: "one",
348
- method(params) {
353
+ queryRelated(params) {
349
354
  return query.where(makeWhere(params));
350
355
  },
351
356
  virtualColumn: new BelongsToVirtualColumn(
@@ -552,19 +557,19 @@ const nestedUpdate$3 = ({ query, primaryKeys, foreignKeys, len }) => {
552
557
  };
553
558
  };
554
559
 
555
- var __defProp$5 = Object.defineProperty;
556
- var __getOwnPropSymbols$5 = Object.getOwnPropertySymbols;
557
- var __hasOwnProp$5 = Object.prototype.hasOwnProperty;
558
- var __propIsEnum$5 = Object.prototype.propertyIsEnumerable;
559
- var __defNormalProp$5 = (obj, key, value) => key in obj ? __defProp$5(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
560
- var __spreadValues$5 = (a, b) => {
560
+ var __defProp$4 = Object.defineProperty;
561
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
562
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
563
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
564
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
565
+ var __spreadValues$4 = (a, b) => {
561
566
  for (var prop in b || (b = {}))
562
- if (__hasOwnProp$5.call(b, prop))
563
- __defNormalProp$5(a, prop, b[prop]);
564
- if (__getOwnPropSymbols$5)
565
- for (var prop of __getOwnPropSymbols$5(b)) {
566
- if (__propIsEnum$5.call(b, prop))
567
- __defNormalProp$5(a, prop, b[prop]);
567
+ if (__hasOwnProp$4.call(b, prop))
568
+ __defNormalProp$4(a, prop, b[prop]);
569
+ if (__getOwnPropSymbols$4)
570
+ for (var prop of __getOwnPropSymbols$4(b)) {
571
+ if (__propIsEnum$4.call(b, prop))
572
+ __defNormalProp$4(a, prop, b[prop]);
568
573
  }
569
574
  return a;
570
575
  };
@@ -607,7 +612,10 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
607
612
  const { through, source } = relation.options;
608
613
  const throughRelation = getThroughRelation(table, through);
609
614
  const sourceRelation = getSourceRelation(throughRelation, source);
610
- const sourceQuery = sourceRelation.joinQuery(sourceRelation.query, throughRelation.query).as(relationName);
615
+ const sourceQuery = sourceRelation.joinQuery(
616
+ sourceRelation.query,
617
+ throughRelation.query
618
+ ).as(relationName);
611
619
  const whereExistsCallback = () => sourceQuery;
612
620
  const reverseJoin2 = (baseQuery, joiningQuery) => {
613
621
  return joinHasThrough(
@@ -620,8 +628,8 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
620
628
  };
621
629
  return {
622
630
  returns: "one",
623
- method: (params) => {
624
- const throughQuery = table[through](params);
631
+ queryRelated: (params) => {
632
+ const throughQuery = table.queryRelated(through, params);
625
633
  return query.whereExists(throughQuery, whereExistsCallback);
626
634
  },
627
635
  joinQuery: joinQueryChainingHOF(
@@ -657,7 +665,7 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
657
665
  };
658
666
  return {
659
667
  returns: "one",
660
- method: (params) => {
668
+ queryRelated: (params) => {
661
669
  const values = {};
662
670
  for (let i = 0; i < len; i++) {
663
671
  values[foreignKeys[i]] = params[primaryKeys[i]];
@@ -671,7 +679,13 @@ const makeHasOneMethod = (table, relation, relationName, query) => {
671
679
  ),
672
680
  joinQuery: joinQueryChainingHOF(
673
681
  reverseJoin,
674
- (joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
682
+ (joiningQuery, baseQuery) => joinHasRelation(
683
+ baseQuery,
684
+ joiningQuery,
685
+ primaryKeys,
686
+ foreignKeys,
687
+ len
688
+ )
675
689
  ),
676
690
  reverseJoin,
677
691
  modifyRelatedQuery(relationQuery) {
@@ -730,7 +744,7 @@ const nestedInsert$2 = ({ query, primaryKeys, foreignKeys }) => {
730
744
  if (items.length) {
731
745
  for (let i = 0, len = items.length; i < len; i++) {
732
746
  const [selfData, item] = items[i];
733
- const data2 = __spreadValues$5({}, "create" in item ? item.create : item.connectOrCreate.create);
747
+ const data2 = __spreadValues$4({}, "create" in item ? item.create : item.connectOrCreate.create);
734
748
  for (let i2 = 0; i2 < primaryKeys.length; i2++) {
735
749
  data2[foreignKeys[i2]] = selfData[primaryKeys[i2]];
736
750
  }
@@ -759,7 +773,7 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
759
773
  await pqb._queryUpdate(currentRelationsQuery, setNulls);
760
774
  const record = data[0];
761
775
  if (params.create) {
762
- const obj = __spreadValues$5({}, params.create);
776
+ const obj = __spreadValues$4({}, params.create);
763
777
  for (let i = 0; i < len; i++) {
764
778
  obj[foreignKeys[i]] = record[primaryKeys[i]];
765
779
  }
@@ -793,7 +807,7 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
793
807
  if (!updatedIds.some(
794
808
  (updated) => updated.every((value, i) => value === ids2[i])
795
809
  )) {
796
- const obj = __spreadValues$5({}, data2);
810
+ const obj = __spreadValues$4({}, data2);
797
811
  for (let i = 0; i < len; i++) {
798
812
  obj[foreignKeys[i]] = ids2[i];
799
813
  }
@@ -807,19 +821,19 @@ const nestedUpdate$2 = ({ query, primaryKeys, foreignKeys }) => {
807
821
  };
808
822
  };
809
823
 
810
- var __defProp$4 = Object.defineProperty;
811
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
812
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
813
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
814
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
815
- var __spreadValues$4 = (a, b) => {
824
+ var __defProp$3 = Object.defineProperty;
825
+ var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
826
+ var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
827
+ var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
828
+ var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
829
+ var __spreadValues$3 = (a, b) => {
816
830
  for (var prop in b || (b = {}))
817
- if (__hasOwnProp$4.call(b, prop))
818
- __defNormalProp$4(a, prop, b[prop]);
819
- if (__getOwnPropSymbols$4)
820
- for (var prop of __getOwnPropSymbols$4(b)) {
821
- if (__propIsEnum$4.call(b, prop))
822
- __defNormalProp$4(a, prop, b[prop]);
831
+ if (__hasOwnProp$3.call(b, prop))
832
+ __defNormalProp$3(a, prop, b[prop]);
833
+ if (__getOwnPropSymbols$3)
834
+ for (var prop of __getOwnPropSymbols$3(b)) {
835
+ if (__propIsEnum$3.call(b, prop))
836
+ __defNormalProp$3(a, prop, b[prop]);
823
837
  }
824
838
  return a;
825
839
  };
@@ -862,7 +876,9 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
862
876
  const { through, source } = relation.options;
863
877
  const throughRelation = getThroughRelation(table, through);
864
878
  const sourceRelation = getSourceRelation(throughRelation, source);
865
- const sourceRelationQuery = sourceRelation.query.as(relationName);
879
+ const sourceRelationQuery = sourceRelation.query.as(
880
+ relationName
881
+ );
866
882
  const sourceQuery = sourceRelation.joinQuery(
867
883
  sourceRelationQuery,
868
884
  throughRelation.query
@@ -879,9 +895,12 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
879
895
  };
880
896
  return {
881
897
  returns: "many",
882
- method: (params) => {
883
- const throughQuery = table[through](params);
884
- return query.whereExists(throughQuery, whereExistsCallback);
898
+ queryRelated: (params) => {
899
+ const throughQuery = table.queryRelated(through, params);
900
+ return query.whereExists(
901
+ throughQuery,
902
+ whereExistsCallback
903
+ );
885
904
  },
886
905
  joinQuery: joinQueryChainingHOF(
887
906
  reverseJoin2,
@@ -916,7 +935,7 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
916
935
  };
917
936
  return {
918
937
  returns: "many",
919
- method: (params) => {
938
+ queryRelated: (params) => {
920
939
  const values = {};
921
940
  for (let i = 0; i < len; i++) {
922
941
  values[foreignKeys[i]] = params[primaryKeys[i]];
@@ -930,7 +949,13 @@ const makeHasManyMethod = (table, relation, relationName, query) => {
930
949
  ),
931
950
  joinQuery: joinQueryChainingHOF(
932
951
  reverseJoin,
933
- (joiningQuery, baseQuery) => joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len)
952
+ (joiningQuery, baseQuery) => joinHasRelation(
953
+ baseQuery,
954
+ joiningQuery,
955
+ primaryKeys,
956
+ foreignKeys,
957
+ len
958
+ )
934
959
  ),
935
960
  reverseJoin,
936
961
  modifyRelatedQuery(relationQuery) {
@@ -1031,13 +1056,13 @@ const nestedInsert$1 = ({ query, primaryKeys, foreignKeys }) => {
1031
1056
  }
1032
1057
  if (create) {
1033
1058
  for (const item of create) {
1034
- records.push(__spreadValues$4(__spreadValues$4({}, item), obj));
1059
+ records.push(__spreadValues$3(__spreadValues$3({}, item), obj));
1035
1060
  }
1036
1061
  }
1037
1062
  if (connectOrCreate) {
1038
1063
  for (const item of connectOrCreate) {
1039
1064
  if (connected[connectedI++] === 0) {
1040
- records.push(__spreadValues$4(__spreadValues$4({}, item.create), obj));
1065
+ records.push(__spreadValues$3(__spreadValues$3({}, item.create), obj));
1041
1066
  }
1042
1067
  }
1043
1068
  }
@@ -1057,7 +1082,7 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1057
1082
  obj[foreignKeys[i]] = data[0][primaryKeys[i]];
1058
1083
  }
1059
1084
  await t.insertMany(
1060
- params.create.map((create) => __spreadValues$4(__spreadValues$4({}, create), obj))
1085
+ params.create.map((create) => __spreadValues$3(__spreadValues$3({}, create), obj))
1061
1086
  );
1062
1087
  delete t.q[pqb.toSQLCacheKey];
1063
1088
  }
@@ -1110,25 +1135,25 @@ const nestedUpdate$1 = ({ query, primaryKeys, foreignKeys }) => {
1110
1135
  };
1111
1136
  };
1112
1137
 
1113
- var __defProp$3 = Object.defineProperty;
1114
- var __defProps$2 = Object.defineProperties;
1115
- var __getOwnPropDescs$2 = Object.getOwnPropertyDescriptors;
1116
- var __getOwnPropSymbols$3 = Object.getOwnPropertySymbols;
1117
- var __hasOwnProp$3 = Object.prototype.hasOwnProperty;
1118
- var __propIsEnum$3 = Object.prototype.propertyIsEnumerable;
1119
- var __defNormalProp$3 = (obj, key, value) => key in obj ? __defProp$3(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1120
- var __spreadValues$3 = (a, b) => {
1138
+ var __defProp$2 = Object.defineProperty;
1139
+ var __defProps$1 = Object.defineProperties;
1140
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
1141
+ var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
1142
+ var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
1143
+ var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
1144
+ var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1145
+ var __spreadValues$2 = (a, b) => {
1121
1146
  for (var prop in b || (b = {}))
1122
- if (__hasOwnProp$3.call(b, prop))
1123
- __defNormalProp$3(a, prop, b[prop]);
1124
- if (__getOwnPropSymbols$3)
1125
- for (var prop of __getOwnPropSymbols$3(b)) {
1126
- if (__propIsEnum$3.call(b, prop))
1127
- __defNormalProp$3(a, prop, b[prop]);
1147
+ if (__hasOwnProp$2.call(b, prop))
1148
+ __defNormalProp$2(a, prop, b[prop]);
1149
+ if (__getOwnPropSymbols$2)
1150
+ for (var prop of __getOwnPropSymbols$2(b)) {
1151
+ if (__propIsEnum$2.call(b, prop))
1152
+ __defNormalProp$2(a, prop, b[prop]);
1128
1153
  }
1129
1154
  return a;
1130
1155
  };
1131
- var __spreadProps$2 = (a, b) => __defProps$2(a, __getOwnPropDescs$2(b));
1156
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1132
1157
  class HasAndBelongsToManyVirtualColumn extends pqb.VirtualColumn {
1133
1158
  constructor(joinTable, schema, key, state) {
1134
1159
  super(schema);
@@ -1163,7 +1188,7 @@ const removeColumnName = (column) => {
1163
1188
  if (!column.data.name)
1164
1189
  return column;
1165
1190
  const cloned = Object.create(column);
1166
- cloned.data = __spreadProps$2(__spreadValues$3({}, column.data), { name: void 0 });
1191
+ cloned.data = __spreadProps$1(__spreadValues$2({}, column.data), { name: void 0 });
1167
1192
  return cloned;
1168
1193
  };
1169
1194
  const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query) => {
@@ -1205,7 +1230,7 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1205
1230
  );
1206
1231
  }
1207
1232
  baseQuery.shape = shape;
1208
- baseQuery.q = __spreadProps$2(__spreadValues$3({}, baseQuery.q), {
1233
+ baseQuery.q = __spreadProps$1(__spreadValues$2({}, baseQuery.q), {
1209
1234
  shape: baseQuery.shape
1210
1235
  });
1211
1236
  const subQuery = Object.create(baseQuery);
@@ -1245,13 +1270,18 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1245
1270
  const selectPrimaryKeysAsForeignKeys = [{ selectAs: obj }];
1246
1271
  const reverseJoin = (baseQuery2, joiningQuery) => {
1247
1272
  const foreignAs = pqb.getQueryAs(joiningQuery);
1248
- return joinQuery(baseQuery2, pqb.getQueryAs(baseQuery2), foreignAs, __spreadProps$2(__spreadValues$3({}, baseQuery2.q.joinedShapes), {
1249
- [foreignAs]: joiningQuery.q.shape
1250
- }));
1273
+ return joinQuery(
1274
+ baseQuery2,
1275
+ pqb.getQueryAs(baseQuery2),
1276
+ foreignAs,
1277
+ __spreadProps$1(__spreadValues$2({}, baseQuery2.q.joinedShapes), {
1278
+ [foreignAs]: joiningQuery.q.shape
1279
+ })
1280
+ );
1251
1281
  };
1252
1282
  return {
1253
1283
  returns: "many",
1254
- method(params) {
1284
+ queryRelated(params) {
1255
1285
  return query.whereExists(subQuery, (q) => {
1256
1286
  q = q.clone();
1257
1287
  const where = {};
@@ -1275,35 +1305,44 @@ const makeHasAndBelongsToManyMethod = (table, qb, relation, relationName, query)
1275
1305
  ),
1276
1306
  joinQuery: joinQueryChainingHOF(
1277
1307
  reverseJoin,
1278
- (joiningQuery, baseQuery2) => joinQuery(joiningQuery, pqb.getQueryAs(baseQuery2), pqb.getQueryAs(joiningQuery), __spreadProps$2(__spreadValues$3({}, joiningQuery.q.joinedShapes), {
1279
- [baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
1280
- }))
1308
+ (joiningQuery, baseQuery2) => joinQuery(
1309
+ joiningQuery,
1310
+ pqb.getQueryAs(baseQuery2),
1311
+ pqb.getQueryAs(joiningQuery),
1312
+ __spreadProps$1(__spreadValues$2({}, joiningQuery.q.joinedShapes), {
1313
+ [baseQuery2.q.as || baseQuery2.table]: baseQuery2.q.shape
1314
+ })
1315
+ )
1281
1316
  ),
1282
1317
  reverseJoin,
1283
1318
  modifyRelatedQuery(relationQuery) {
1284
1319
  const ref = {};
1285
- pqb._queryHookAfterCreate(relationQuery, [], async (result) => {
1286
- if (result.length > 1) {
1287
- throw new pqb.OrchidOrmInternalError(
1288
- relationQuery,
1289
- "Creating multiple `hasAndBelongsToMany` records is not yet supported"
1320
+ pqb._queryHookAfterCreate(
1321
+ relationQuery,
1322
+ [],
1323
+ async (result) => {
1324
+ if (result.length > 1) {
1325
+ throw new pqb.OrchidOrmInternalError(
1326
+ relationQuery,
1327
+ "Creating multiple `hasAndBelongsToMany` records is not yet supported"
1328
+ );
1329
+ }
1330
+ const baseQuery2 = ref.q.clone();
1331
+ baseQuery2.q.select = selectPrimaryKeysAsForeignKeys;
1332
+ const data = {};
1333
+ for (let i = 0; i < throughLen; i++) {
1334
+ data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
1335
+ }
1336
+ const createdCount = await pqb._queryCreateFrom(
1337
+ subQuery.count(),
1338
+ baseQuery2,
1339
+ data
1290
1340
  );
1341
+ if (createdCount === 0) {
1342
+ throw new pqb.NotFoundError(baseQuery2);
1343
+ }
1291
1344
  }
1292
- const baseQuery2 = ref.q.clone();
1293
- baseQuery2.q.select = selectPrimaryKeysAsForeignKeys;
1294
- const data = {};
1295
- for (let i = 0; i < throughLen; i++) {
1296
- data[throughForeignKeys[i]] = result[0][throughPrimaryKeys[i]];
1297
- }
1298
- const createdCount = await pqb._queryCreateFrom(
1299
- subQuery.count(),
1300
- baseQuery2,
1301
- data
1302
- );
1303
- if (createdCount === 0) {
1304
- throw new pqb.NotFoundError(baseQuery2);
1305
- }
1306
- });
1345
+ );
1307
1346
  return (q) => {
1308
1347
  ref.q = q;
1309
1348
  };
@@ -1343,7 +1382,7 @@ const insertToJoinTable = (state, joinTableTransaction, data, idsRows) => {
1343
1382
  obj[state.foreignKeys[i]] = item[state.primaryKeys[i]];
1344
1383
  }
1345
1384
  for (const ids of idsRows) {
1346
- const record = __spreadValues$3({}, obj);
1385
+ const record = __spreadValues$2({}, obj);
1347
1386
  for (let i = 0; i < throughLen; i++) {
1348
1387
  record[state.throughForeignKeys[i]] = ids[i];
1349
1388
  }
@@ -1486,7 +1525,7 @@ const nestedInsert = ({
1486
1525
  obj[foreignKeys[i]] = selfData[primaryKeys[i]];
1487
1526
  }
1488
1527
  for (const relationData of relationKeys) {
1489
- const record = __spreadValues$3({}, obj);
1528
+ const record = __spreadValues$2({}, obj);
1490
1529
  for (let i = 0; i < throughLen; i++) {
1491
1530
  record[throughForeignKeys[i]] = relationData[throughPrimaryKeys[i]];
1492
1531
  }
@@ -1512,7 +1551,7 @@ const nestedUpdate = (state) => {
1512
1551
  obj[state.foreignKeys[i]] = item[state.primaryKeys[i]];
1513
1552
  }
1514
1553
  for (const ids of idsRows) {
1515
- const record = __spreadValues$3({}, obj);
1554
+ const record = __spreadValues$2({}, obj);
1516
1555
  for (let i = 0; i < throughLen; i++) {
1517
1556
  record[state.throughForeignKeys[i]] = ids[i];
1518
1557
  }
@@ -1585,25 +1624,6 @@ const nestedUpdate = (state) => {
1585
1624
  };
1586
1625
  };
1587
1626
 
1588
- var __defProp$2 = Object.defineProperty;
1589
- var __defProps$1 = Object.defineProperties;
1590
- var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
1591
- var __getOwnPropSymbols$2 = Object.getOwnPropertySymbols;
1592
- var __hasOwnProp$2 = Object.prototype.hasOwnProperty;
1593
- var __propIsEnum$2 = Object.prototype.propertyIsEnumerable;
1594
- var __defNormalProp$2 = (obj, key, value) => key in obj ? __defProp$2(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
1595
- var __spreadValues$2 = (a, b) => {
1596
- for (var prop in b || (b = {}))
1597
- if (__hasOwnProp$2.call(b, prop))
1598
- __defNormalProp$2(a, prop, b[prop]);
1599
- if (__getOwnPropSymbols$2)
1600
- for (var prop of __getOwnPropSymbols$2(b)) {
1601
- if (__propIsEnum$2.call(b, prop))
1602
- __defNormalProp$2(a, prop, b[prop]);
1603
- }
1604
- return a;
1605
- };
1606
- var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
1607
1627
  const applyRelations = (qb, tables, result) => {
1608
1628
  var _a;
1609
1629
  const tableEntries = Object.entries(tables);
@@ -1733,7 +1753,6 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1733
1753
  if (data.virtualColumn) {
1734
1754
  dbTable.shape[relationName] = dbTable.q.shape[relationName] = data.virtualColumn;
1735
1755
  }
1736
- makeRelationQuery(dbTable, relationName, data, query);
1737
1756
  baseQuery.joinQuery = data.joinQuery;
1738
1757
  const { join: originalJoin } = baseQuery;
1739
1758
  baseQuery.join = function(...args) {
@@ -1748,7 +1767,10 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1748
1767
  baseQuery.relationConfig = {
1749
1768
  table: otherDbTable,
1750
1769
  query,
1751
- joinQuery: data.joinQuery
1770
+ queryRelated: data.queryRelated,
1771
+ joinQuery: data.joinQuery,
1772
+ reverseJoin: data.reverseJoin,
1773
+ modifyRelatedQuery: data.modifyRelatedQuery
1752
1774
  };
1753
1775
  dbTable.relations[relationName] = query;
1754
1776
  const tableRelations = delayedRelations.get(dbTable);
@@ -1758,54 +1780,6 @@ const applyRelation = (qb, { relationName, relation, dbTable, otherDbTable }, de
1758
1780
  applyRelation(qb, data2, delayedRelations);
1759
1781
  });
1760
1782
  };
1761
- const makeRelationQuery = (table, relationName, data, q) => {
1762
- Object.defineProperty(table, relationName, {
1763
- configurable: true,
1764
- get() {
1765
- var _a;
1766
- const toTable = q.clone();
1767
- let query;
1768
- if (this.q.subQuery) {
1769
- query = toTable;
1770
- query.q.subQuery = 2;
1771
- } else {
1772
- query = pqb._queryWhere(pqb._queryAll(toTable), [
1773
- {
1774
- EXISTS: { q: data.reverseJoin(this, toTable) }
1775
- }
1776
- ]);
1777
- }
1778
- if (this.q.relChain) {
1779
- query.q.relChain = [...this.q.relChain, this];
1780
- query.q.returnType = "all";
1781
- } else {
1782
- query.q.relChain = [this];
1783
- }
1784
- const aliases = this.q.as ? __spreadValues$2({}, this.q.aliases) : __spreadProps$1(__spreadValues$2({}, this.q.aliases), { [this.table]: this.table });
1785
- const relAliases = query.q.aliases;
1786
- for (const as in relAliases) {
1787
- aliases[as] = pqb._queryResolveAlias(aliases, as);
1788
- }
1789
- query.q.as = aliases[query.q.as];
1790
- query.q.aliases = aliases;
1791
- query.q.joinedShapes = __spreadValues$2({
1792
- [pqb.getQueryAs(this)]: this.q.shape
1793
- }, this.q.joinedShapes);
1794
- const setQuery = (_a = data.modifyRelatedQuery) == null ? void 0 : _a.call(data, query);
1795
- setQuery == null ? void 0 : setQuery(this);
1796
- return new Proxy(data.method, {
1797
- get(_, prop) {
1798
- return query[prop];
1799
- }
1800
- });
1801
- },
1802
- set(value) {
1803
- Object.defineProperty(this, relationName, {
1804
- value
1805
- });
1806
- }
1807
- });
1808
- };
1809
1783
 
1810
1784
  function transaction(fnOrOptions, fn) {
1811
1785
  return this.$queryBuilder.transaction(