orchid-orm 1.14.4 → 1.15.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,18 +1,34 @@
1
1
  import * as pqb from 'pqb';
2
- import { QueryWithTable, SetQueryTableAlias, Query, WhereArg, UpdateData, CreateData, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, RelationConfigBase, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, RelationQueryBase, DefaultColumnTypes, QueryData, QueryBase, ColumnsShape, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
2
+ import { Query, QueryWithTable, SetQueryTableAlias, WhereArg, UpdateData, CreateData, Db, IsolationLevel, TransactionOptions, Adapter, FromArgs, FromResult, AdapterOptions, QueryLogOptions, NoPrimaryKeyOption, RelationConfigBase, RelationQuery, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsAll, RelationQueryBase, DefaultColumnTypes, QueryData, QueryBase, ColumnsShape, QueryBeforeHook, QueryAfterHook, AfterHook, WhereResult, MergeQuery, SetQueryReturns, QueryReturnType } from 'pqb';
3
3
  export { OrchidOrmError, OrchidOrmInternalError, columnTypes, raw, testTransaction } from 'pqb';
4
4
  import * as orchid_core from 'orchid-core';
5
5
  import { EmptyObject, MaybeArray, ColumnTypesBase, ColumnsShapeBase, ColumnShapeOutput } from 'orchid-core';
6
6
 
7
- interface BelongsTo extends RelationThunkBase {
7
+ type RelationCommonOptions<Related extends TableClass = TableClass, Scope extends Query = Query> = {
8
+ scope?: ScopeFn<Related, Scope>;
9
+ required?: boolean;
10
+ };
11
+ type RelationRefsOptions<Column extends PropertyKey = string, Ref extends PropertyKey = string> = {
12
+ columns: Column[];
13
+ references: Ref[];
14
+ };
15
+ type RelationKeysOptions<PK extends PropertyKey = string, FK extends PropertyKey = string> = {
16
+ primaryKey: PK;
17
+ foreignKey: FK;
18
+ };
19
+ type RelationRefsOrKeysOptions<Column extends PropertyKey = string, Ref extends PropertyKey = string, PK extends PropertyKey = string, FK extends PropertyKey = string> = RelationRefsOptions<Column, Ref> | RelationKeysOptions<PK, FK>;
20
+ type RelationThroughOptions<Through extends PropertyKey = string, Source extends PropertyKey = string> = {
21
+ through: Through;
22
+ source: Source;
23
+ };
24
+ type RelationHasOptions<Column extends PropertyKey = string, Ref extends PropertyKey = string> = RelationRefsOrKeysOptions<Column, Ref, Column, Ref>;
25
+
26
+ type BelongsTo = RelationThunkBase & {
8
27
  type: 'belongsTo';
9
- returns: 'one';
10
- options: RelationCommonOptions & {
11
- primaryKey: string;
12
- foreignKey: string;
13
- };
14
- }
15
- type BelongsToInfo<T extends Table, Relation extends BelongsTo, K extends string, FK extends string = Relation['options']['foreignKey'], Q extends QueryWithTable = SetQueryTableAlias<DbTable<ReturnType<Relation['fn']>>, K>> = {
28
+ options: BelongsToOptions;
29
+ };
30
+ type BelongsToOptions<Self extends Table = Table, Related extends TableClass = TableClass, Scope extends Query = Query> = RelationCommonOptions<Related, Scope> & RelationRefsOrKeysOptions<keyof Self['columns']['shape'], keyof InstanceType<Related>['columns']['shape'], keyof InstanceType<Related>['columns']['shape'], keyof Self['columns']['shape']>;
31
+ type BelongsToInfo<T extends Table, Relation extends BelongsTo, K extends string, FK extends string = Relation['options'] extends RelationRefsOptions ? Relation['options']['columns'][number] : Relation['options'] extends RelationKeysOptions ? Relation['options']['foreignKey'] : never, Q extends QueryWithTable = SetQueryTableAlias<DbTable<ReturnType<Relation['fn']>>, K>> = {
16
32
  table: Q;
17
33
  query: Q;
18
34
  joinQuery(fromQuery: Query, toQuery: Query): Query;
@@ -40,26 +56,20 @@ type BelongsToInfo<T extends Table, Relation extends BelongsTo, K extends string
40
56
  create: CreateData<Q> | (() => CreateData<Q>);
41
57
  };
42
58
  };
43
- params: Record<FK, T['columns']['shape'][FK]['type']>;
59
+ params: {
60
+ [K in FK]: T['columns']['shape'][FK]['type'];
61
+ };
44
62
  populate: never;
45
63
  chainedCreate: false;
46
64
  chainedDelete: false;
47
65
  };
48
66
 
49
- interface HasMany extends RelationThunkBase {
67
+ type HasMany = RelationThunkBase & {
50
68
  type: 'hasMany';
51
- returns: 'many';
52
- options: RelationCommonOptions & ({
53
- primaryKey: string;
54
- foreignKey: string;
55
- } | {
56
- through: string;
57
- source: string;
58
- });
59
- }
60
- type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany, K extends string, Populate extends string = Relation['options'] extends {
61
- foreignKey: string;
62
- } ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = [Populate] extends [never] ? Q : Q & {
69
+ options: HasManyOptions;
70
+ };
71
+ type HasManyOptions<Self extends Table = Table, Related extends TableClass = TableClass, Scope extends Query = Query, Through extends string = string, Source extends string = string> = HasOneOptions<Self, Related, Scope, Through, Source>;
72
+ type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation extends HasMany, K extends string, Populate extends string = Relation['options'] extends RelationRefsOptions ? Relation['options']['references'][number] : Relation['options'] extends RelationKeysOptions ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = Relation['options'] extends RelationThroughOptions ? Q : Q & {
63
73
  meta: {
64
74
  defaults: Record<Populate, true>;
65
75
  };
@@ -70,9 +80,7 @@ type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation ext
70
80
  one: false;
71
81
  required: Relation['options']['required'] extends true ? true : false;
72
82
  omitForeignKeyInCreate: never;
73
- dataForCreate: Relation['options'] extends {
74
- through: string;
75
- } ? EmptyObject : RelationToManyDataForCreate<{
83
+ dataForCreate: Relation['options'] extends RelationThroughOptions ? EmptyObject : RelationToManyDataForCreate<{
76
84
  nestedCreateQuery: NestedCreateQuery;
77
85
  table: Q;
78
86
  }>;
@@ -88,32 +96,20 @@ type HasManyInfo<T extends Table, Relations extends RelationThunks, Relation ext
88
96
  set?: MaybeArray<WhereArg<Q>>;
89
97
  create?: CreateData<NestedCreateQuery>[];
90
98
  };
91
- params: Relation['options'] extends {
92
- primaryKey: string;
93
- } ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
94
- through: string;
95
- } ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
99
+ params: Relation['options'] extends RelationRefsOptions ? {
100
+ [K in Relation['options']['columns'][number]]: T['columns']['shape'][K]['type'];
101
+ } : Relation['options'] extends RelationKeysOptions ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends RelationThroughOptions ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
96
102
  populate: Populate;
97
- chainedCreate: Relation['options'] extends {
98
- primaryKey: string;
99
- } ? true : false;
103
+ chainedCreate: Relation['options'] extends RelationThroughOptions ? false : true;
100
104
  chainedDelete: true;
101
105
  };
102
106
 
103
- interface HasOne extends RelationThunkBase {
107
+ type HasOne = RelationThunkBase & {
104
108
  type: 'hasOne';
105
- returns: 'one';
106
- options: RelationCommonOptions & ({
107
- primaryKey: string;
108
- foreignKey: string;
109
- } | {
110
- through: string;
111
- source: string;
112
- });
113
- }
114
- type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne, K extends string, Populate extends string = Relation['options'] extends {
115
- foreignKey: string;
116
- } ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = [Populate] extends [never] ? Q : Q & {
109
+ options: HasOneOptions;
110
+ };
111
+ type HasOneOptions<Self extends Table = Table, Related extends TableClass = TableClass, Scope extends Query = Query, Through extends string = string, Source extends string = string> = RelationCommonOptions<Related, Scope> & (RelationHasOptions<keyof Self['columns']['shape'], keyof InstanceType<Related>['columns']['shape']> | RelationThroughOptions<Through, Source>);
112
+ type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation extends HasOne, K extends string, Populate extends string = Relation['options'] extends RelationRefsOptions ? Relation['options']['references'][number] : Relation['options'] extends RelationKeysOptions ? Relation['options']['foreignKey'] : never, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>, NestedCreateQuery extends Query = Relation['options'] extends RelationThroughOptions ? Q : Q & {
117
113
  meta: {
118
114
  defaults: Record<Populate, true>;
119
115
  };
@@ -124,9 +120,7 @@ type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation exte
124
120
  one: true;
125
121
  required: Relation['options']['required'] extends true ? true : false;
126
122
  omitForeignKeyInCreate: never;
127
- dataForCreate: Relation['options'] extends {
128
- through: string;
129
- } ? EmptyObject : RelationToOneDataForCreate<{
123
+ dataForCreate: Relation['options'] extends RelationThroughOptions ? EmptyObject : RelationToOneDataForCreate<{
130
124
  nestedCreateQuery: NestedCreateQuery;
131
125
  table: Q;
132
126
  }>;
@@ -147,15 +141,11 @@ type HasOneInfo<T extends Table, Relations extends RelationThunks, Relation exte
147
141
  } | {
148
142
  create: CreateData<NestedCreateQuery>;
149
143
  };
150
- params: Relation['options'] extends {
151
- primaryKey: string;
152
- } ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends {
153
- through: string;
154
- } ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
144
+ params: Relation['options'] extends RelationRefsOptions ? {
145
+ [K in Relation['options']['columns'][number]]: T['columns']['shape'][K]['type'];
146
+ } : Relation['options'] extends RelationKeysOptions ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : Relation['options'] extends RelationThroughOptions ? RelationConfig<T, Relations, Relations[Relation['options']['through']]>['params'] : never;
155
147
  populate: Populate;
156
- chainedCreate: Relation['options'] extends {
157
- primaryKey: string;
158
- } ? true : false;
148
+ chainedCreate: Relation['options'] extends RelationThroughOptions ? false : true;
159
149
  chainedDelete: true;
160
150
  };
161
151
 
@@ -241,17 +231,25 @@ declare const orchidORM: <T extends TableClasses>({ log, logger, autoPreparedSta
241
231
  noPrimaryKey?: NoPrimaryKeyOption | undefined;
242
232
  }, tables: T) => OrchidORM<T>;
243
233
 
244
- interface HasAndBelongsToMany extends RelationThunkBase {
234
+ type HasAndBelongsToMany = RelationThunkBase & {
245
235
  type: 'hasAndBelongsToMany';
246
- returns: 'many';
247
- options: RelationCommonOptions & {
248
- primaryKey: string;
249
- foreignKey: string;
250
- associationPrimaryKey: string;
251
- associationForeignKey: string;
252
- joinTable: string;
236
+ options: HasAndBelongsToManyOptions;
237
+ };
238
+ type HasAndBelongsToManyOptions<Self extends Table = Table, Related extends TableClass = TableClass, Scope extends Query = Query> = RelationCommonOptions<Related, Scope> & ({
239
+ columns: (keyof Self['columns']['shape'])[];
240
+ references: string[];
241
+ through: {
242
+ table: string;
243
+ columns: string[];
244
+ references: (keyof InstanceType<Related>['columns']['shape'])[];
253
245
  };
254
- }
246
+ } | {
247
+ primaryKey: keyof Self['columns']['shape'];
248
+ foreignKey: string;
249
+ joinTable: string;
250
+ associationPrimaryKey: string;
251
+ associationForeignKey: keyof InstanceType<Related>['columns']['shape'];
252
+ });
255
253
  type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMany, K extends string, TC extends TableClass = ReturnType<Relation['fn']>, Q extends QueryWithTable = SetQueryTableAlias<DbTable<TC>, K>> = {
256
254
  table: Q;
257
255
  query: Q;
@@ -274,16 +272,18 @@ type HasAndBelongsToManyInfo<T extends Table, Relation extends HasAndBelongsToMa
274
272
  create?: CreateData<Q>[];
275
273
  };
276
274
  dataForUpdateOne: EmptyObject;
277
- params: Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']>;
275
+ params: Relation['options'] extends {
276
+ columns: string[];
277
+ } ? {
278
+ [K in Relation['options']['columns'][number]]: T['columns']['shape'][K]['type'];
279
+ } : Relation['options'] extends {
280
+ primaryKey: string;
281
+ } ? Record<Relation['options']['primaryKey'], T['columns']['shape'][Relation['options']['primaryKey']]['type']> : never;
278
282
  populate: never;
279
283
  chainedCreate: true;
280
284
  chainedDelete: true;
281
285
  };
282
286
 
283
- type RelationCommonOptions = {
284
- scope?(q: QueryWithTable): QueryWithTable;
285
- required?: boolean;
286
- };
287
287
  type RelationToOneDataForCreate<Rel extends {
288
288
  nestedCreateQuery: Query;
289
289
  table: Query;
@@ -316,7 +316,6 @@ type RelationToManyDataForCreate<Rel extends {
316
316
  };
317
317
  interface RelationThunkBase {
318
318
  type: string;
319
- returns: 'one' | 'many';
320
319
  fn(): TableClass;
321
320
  options: RelationCommonOptions;
322
321
  }
@@ -647,58 +646,23 @@ declare const createBaseTable: <CT extends Record<string, orchid_core.AnyColumnT
647
646
  shape: T_13;
648
647
  type: ColumnShapeOutput<T_13>;
649
648
  };
650
- belongsTo<Self extends any, Related extends TableClass<Table>, Scope extends Query, Options extends {
651
- primaryKey: keyof InstanceType<Related>["columns"]["shape"];
652
- foreignKey: keyof Self["columns"]["shape"];
653
- scope?: ScopeFn<Related, Scope> | undefined;
654
- required?: boolean | undefined;
655
- }>(this: Self, fn: () => Related, options: Options): {
649
+ belongsTo<Self extends any, Related extends TableClass<Table>, Scope extends Query, Options extends BelongsToOptions<Self, Related, Scope>>(this: Self, fn: () => Related, options: Options): {
656
650
  type: "belongsTo";
657
- returns: "one";
658
651
  fn: () => Related;
659
652
  options: Options;
660
653
  };
661
- hasOne<Self_1 extends any, Related_1 extends TableClass<Table>, Scope_1 extends Query, Through extends string, Source extends string, Options_1 extends ({
662
- primaryKey: keyof Self_1["columns"]["shape"];
663
- foreignKey: keyof InstanceType<Related_1>["columns"]["shape"];
664
- } | {
665
- through: Through;
666
- source: Source;
667
- }) & {
668
- scope?: ScopeFn<Related_1, Scope_1> | undefined;
669
- required?: boolean | undefined;
670
- }>(this: Self_1, fn: () => Related_1, options: Options_1): {
654
+ hasOne<Self_1 extends any, Related_1 extends TableClass<Table>, Scope_1 extends Query, Through extends string, Source extends string, Options_1 extends HasOneOptions<Self_1, Related_1, Scope_1, Through, Source>>(this: Self_1, fn: () => Related_1, options: Options_1): {
671
655
  type: "hasOne";
672
- returns: "one";
673
656
  fn: () => Related_1;
674
657
  options: Options_1;
675
658
  };
676
- hasMany<Self_2 extends any, Related_2 extends TableClass<Table>, Scope_2 extends Query, Through_1 extends string, Source_1 extends string, Options_2 extends ({
677
- primaryKey: keyof Self_2["columns"]["shape"];
678
- foreignKey: keyof InstanceType<Related_2>["columns"]["shape"];
679
- } | {
680
- through: Through_1;
681
- source: Source_1;
682
- }) & {
683
- scope?: ScopeFn<Related_2, Scope_2> | undefined;
684
- required?: boolean | undefined;
685
- }>(this: Self_2, fn: () => Related_2, options: Options_2): {
659
+ hasMany<Self_2 extends any, Related_2 extends TableClass<Table>, Scope_2 extends Query, Through_1 extends string, Source_1 extends string, Options_2 extends HasManyOptions<Self_2, Related_2, Scope_2, Through_1, Source_1>>(this: Self_2, fn: () => Related_2, options: Options_2): {
686
660
  type: "hasMany";
687
- returns: "many";
688
661
  fn: () => Related_2;
689
662
  options: Options_2;
690
663
  };
691
- hasAndBelongsToMany<Self_3 extends any, Related_3 extends TableClass<Table>, Scope_3 extends Query, Options_3 extends {
692
- primaryKey: keyof Self_3["columns"]["shape"];
693
- associationPrimaryKey: keyof InstanceType<Related_3>["columns"]["shape"];
694
- foreignKey: string;
695
- associationForeignKey: string;
696
- joinTable: string;
697
- scope?: ScopeFn<Related_3, Scope_3> | undefined;
698
- required?: boolean | undefined;
699
- }>(this: Self_3, fn: () => Related_3, options: Options_3): {
664
+ hasAndBelongsToMany<Self_3 extends any, Related_3 extends TableClass<Table>, Scope_3 extends Query, Options_3 extends HasAndBelongsToManyOptions<Self_3, Related_3, Scope_3>>(this: Self_3, fn: () => Related_3, options: Options_3): {
700
665
  type: "hasAndBelongsToMany";
701
- returns: "many";
702
666
  fn: () => Related_3;
703
667
  options: Options_3;
704
668
  };
@@ -744,4 +708,4 @@ type Repo<T extends Query, Methods extends MethodsBase<T>, Mapped = MapMethods<T
744
708
  }>(q: Q) => Q & Mapped) & T & Mapped;
745
709
  declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(table: T, methods: Methods) => Repo<T, Methods, MapMethods<T, Methods>>;
746
710
 
747
- export { DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, Table, TableClass, TableClasses, TableToDb, TableType, createBaseTable, createRepo, orchidORM };
711
+ export { DbTable, MapMethods, MapQueryMethods, MethodsBase, OrchidORM, QueryMethods, Repo, ScopeFn, Table, TableClass, TableClasses, TableToDb, TableType, createBaseTable, createRepo, orchidORM };