orchid-orm 1.76.5 → 1.77.1

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,7 +1,7 @@
1
- import { Adapter, AfterCommitStandaloneHook, AfterHook, Column, ColumnSchemaConfig, ColumnsShape, ComputedOptionsConfig, ComputedOptionsFactory, CreateData, CreateManyMethodsNames, CreateMethodsNames, CreateSelf, DbSharedOptions, DbSqlMethod, DbTableOptionScopes, DefaultColumnTypes, DefaultSchemaConfig, DeleteMethodsNames, EmptyObject, FromArg, FromResult, Grant, IsQuery, IsolationLevel, MaybeArray, MergeQuery, PickQuerySelectableRelations, QueryAfterHook, QueryBeforeActionHook, QueryBeforeHook, QueryData, QueryHasWhere, QueryReturnType, QuerySchema, QueryScopes, RawSqlBase, RecordUnknown, RelationConfigBase, Rls, SelectableFromShape, ShallowSimplify, StorageOptions, TableData, TableDataFn, TableDataItem, TransactionOptions, UpdateData, WhereArg } from "pqb/internal";
1
+ import { Adapter, AfterCommitStandaloneHook, AfterHook, Column, ColumnSchemaConfig, ColumnsShape, ComputedOptionsConfig, ComputedOptionsFactory, CreateData, CreateManyMethodsNames, CreateMethodsNames, CreateSelf, DbSharedOptions, DbSqlMethod, DbTableOptionScopes, DefaultColumnTypes, DefaultSchemaConfig, DeleteMethodsNames, EmptyObject, FromArg, FromResult, Grant, IsQuery, IsolationLevel, MaybeArray, MergeQuery, PickQuerySelectableRelations, QueryAfterHook, QueryBeforeActionHook, QueryBeforeHook, QueryData, QueryHasWhere, QueryOrExpression, QueryReturnType, QuerySchema, QueryScopes, RawSqlBase, RecordUnknown, RelationConfigBase, Rls, RlsPolicy, SelectableFromShape, StorageOptions, TableData, TableDataFn, TableDataItem, TransactionOptions, UpdateData, WhereArg } from "pqb/internal";
2
2
  import { Db, Query } from "pqb";
3
3
  export * from "pqb";
4
- interface BelongsTo extends RelationThunkBase {
4
+ interface BelongsTo$1 extends RelationThunkBase {
5
5
  type: 'belongsTo';
6
6
  options: BelongsToOptions;
7
7
  }
@@ -12,20 +12,20 @@ interface BelongsToOptions<Columns extends Column.Shape.QueryInit = Column.Shape
12
12
  foreignKey?: boolean | TableData.References.Options;
13
13
  on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
14
14
  }
15
- type BelongsToParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type'] };
16
- type BelongsToDefaultRequired<T extends RelationConfigSelf, Rel extends BelongsTo, Related> = Related extends {
15
+ type BelongsToParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type']; };
16
+ type BelongsToDefaultRequired<T extends RelationConfigSelf, Rel extends BelongsTo$1, Related> = Related extends {
17
17
  softDelete: true;
18
18
  } ? false : BelongsToDefaultRequiredFromColumns<T, Rel>;
19
- type BelongsToDefaultRequiredFromColumns<T extends RelationConfigSelf, Rel extends BelongsTo> = BelongsToColumnRequired<T['columns']['shape'][Rel['options']['columns'][number] & string]> extends true ? true : false;
19
+ type BelongsToDefaultRequiredFromColumns<T extends RelationConfigSelf, Rel extends BelongsTo$1> = BelongsToColumnRequired<T['columns']['shape'][Rel['options']['columns'][number] & string]> extends true ? true : false;
20
20
  type BelongsToColumnRequired<Column> = Column extends {
21
21
  data: {
22
22
  isNullable: true;
23
23
  };
24
24
  } ? false : true;
25
- type BelongsToQuery<T extends Query, Name extends string> = { [P in keyof T]: P extends '__selectable' ? SelectableFromShape<T['shape'], Name> : P extends '__as' ? Name : P extends CreateMethodsNames | DeleteMethodsNames ? never : T[P] } & QueryHasWhere;
25
+ type BelongsToQuery<T extends Query, Name extends string> = { [P in keyof T]: P extends '__selectable' ? SelectableFromShape<T['shape'], Name> : P extends '__as' ? Name : P extends CreateMethodsNames | DeleteMethodsNames ? never : T[P]; } & QueryHasWhere;
26
26
  interface BelongsToDataForCreate<Name extends string, FK extends string, Required, Q extends Query> {
27
27
  columns: FK;
28
- nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: never };
28
+ nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q>; } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q>; } : { [Key in Name]?: never; };
29
29
  }
30
30
  interface BelongsToInfo<T extends RelationConfigSelf, FK extends string, Required, Q extends Query> extends RelationConfigBase {
31
31
  returnsOne: true;
@@ -61,35 +61,36 @@ interface BelongsToInfo<T extends RelationConfigSelf, FK extends string, Require
61
61
  };
62
62
  } : never);
63
63
  }
64
- interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
64
+ interface RelationRefsOptions$1<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
65
65
  required?: boolean;
66
66
  columns: Column[];
67
67
  references: (keyof Shape)[];
68
68
  foreignKey?: boolean | TableData.References.Options;
69
69
  on?: ColumnsShape.InputPartial<Shape>;
70
70
  }
71
- interface RelationThroughOptions<Through extends PropertyKey = string, Source extends PropertyKey = string> {
71
+ interface RelationThroughOptions$1<Through extends PropertyKey = string> {
72
72
  through: Through;
73
- source: Source;
73
+ source: string;
74
+ on?: ColumnsShape.InputPartial<Column.Shape.QueryInit>;
74
75
  }
75
- interface HasMany extends RelationThunkBase {
76
+ interface HasMany$1 extends RelationThunkBase {
76
77
  type: 'hasMany';
77
78
  options: HasOneOptions;
78
79
  }
79
- type HasManyQuery<T extends RelationConfigSelf, Name extends string, TableQuery extends Query> = T['relations'][Name]['options'] extends RelationRefsOptions ? { [K in keyof TableQuery]: K extends '__defaults' ? { [K in keyof TableQuery['__defaults'] | T['relations'][Name]['options']['references'][number]]: true } : K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : TableQuery[K] } & QueryHasWhere : HasOneQueryThrough<Name, TableQuery>;
80
- interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasMany, Q extends Query> extends RelationConfigBase {
80
+ type HasManyQuery<T extends RelationConfigSelf, Name extends string, TableQuery extends Query> = T['relations'][Name]['options'] extends RelationRefsOptions$1 ? { [K in keyof TableQuery]: K extends '__defaults' ? { [K in keyof TableQuery['__defaults'] | T['relations'][Name]['options']['references'][number]]: true; } : K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : TableQuery[K]; } & QueryHasWhere : HasOneQueryThrough<Name, TableQuery>;
81
+ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasMany$1, Q extends Query> extends RelationConfigBase {
81
82
  returnsOne: false;
82
83
  query: Q;
83
84
  params: HasOneParams<T, Rel['options']>;
84
85
  omitForeignKeyInCreate: never;
85
- dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? T['relations'][Name]['options'] extends RelationThroughOptions ? never : {
86
+ dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? Rel['options'] extends RelationThroughOptions$1 ? never : {
86
87
  create?: CreateData<Q>[];
87
88
  connect?: WhereArg<Q>[];
88
89
  connectOrCreate?: {
89
90
  where: WhereArg<Q>;
90
91
  create: CreateData<Q>;
91
92
  }[];
92
- } : never };
93
+ } : never; };
93
94
  dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
94
95
  disconnect?: MaybeArray<WhereArg<Q>>;
95
96
  delete?: MaybeArray<WhereArg<Q>>;
@@ -115,27 +116,27 @@ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel ext
115
116
  };
116
117
  } : never;
117
118
  }
118
- interface HasOne extends RelationThunkBase {
119
+ interface HasOne$1 extends RelationThunkBase {
119
120
  type: 'hasOne';
120
121
  options: HasOneOptions;
121
122
  }
122
- interface RelationHasOneThroughOptions<Through extends string, Source extends string> extends RelationThroughOptions<Through, Source> {
123
+ interface RelationHasOneThroughOptions<Through extends string> extends RelationThroughOptions$1<Through> {
123
124
  required?: boolean;
124
125
  }
125
- type HasOneOptions<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput, Through extends string = string, Source extends string = string> = RelationRefsOptions<keyof Columns, Related['columns']['shape']> | RelationHasOneThroughOptions<Through, Source>;
126
- type HasOneParams<T extends RelationConfigSelf, Options> = Options extends RelationRefsOptions ? { [Name in Options['columns'][number]]: T['columns']['shape'][Name]['__type'] } : Options extends RelationThroughOptions ? RelationConfigParams<T, T['relations'][Options['through']]> : never;
127
- type HasOneQueryThrough<Name extends string, TableQuery extends Query> = { [K in keyof TableQuery]: K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : K extends CreateMethodsNames ? never : TableQuery[K] } & QueryHasWhere;
128
- type HasOneQuery<T extends RelationConfigSelf, Name extends string, TableQuery extends Query> = T['relations'][Name]['options'] extends RelationRefsOptions ? { [K in keyof TableQuery]: K extends '__defaults' ? { [K in keyof TableQuery['__defaults'] | T['relations'][Name]['options']['references'][number]]: true } : K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : K extends CreateManyMethodsNames ? never : TableQuery[K] } & QueryHasWhere : HasOneQueryThrough<Name, TableQuery>;
129
- interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasOne, Q extends Query> extends RelationConfigBase {
126
+ type HasOneOptions<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput, Through extends string = string> = RelationRefsOptions$1<keyof Columns, Related['columns']['shape']> | RelationHasOneThroughOptions<Through>;
127
+ type HasOneParams<T extends RelationConfigSelf, Options> = Options extends RelationRefsOptions$1 ? { [Name in Options['columns'][number]]: T['columns']['shape'][Name]['__type']; } : Options extends RelationThroughOptions$1 ? RelationConfigParams<T, T['relations'][Options['through']]> : never;
128
+ type HasOneQueryThrough<Name extends string, TableQuery extends Query> = { [K in keyof TableQuery]: K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : K extends CreateMethodsNames ? never : TableQuery[K]; } & QueryHasWhere;
129
+ type HasOneQuery<T extends RelationConfigSelf, Name extends string, TableQuery extends Query> = T['relations'][Name]['options'] extends RelationRefsOptions$1 ? { [K in keyof TableQuery]: K extends '__defaults' ? { [K in keyof TableQuery['__defaults'] | T['relations'][Name]['options']['references'][number]]: true; } : K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : K extends CreateManyMethodsNames ? never : TableQuery[K]; } & QueryHasWhere : HasOneQueryThrough<Name, TableQuery>;
130
+ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasOne$1, Q extends Query> extends RelationConfigBase {
130
131
  returnsOne: true;
131
- required: T['relations'][Name]['options']['required'];
132
+ required: Rel['options']['required'];
132
133
  query: Q;
133
134
  params: HasOneParams<T, Rel['options']>;
134
135
  omitForeignKeyInCreate: never;
135
- dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : RelationToOneDataForCreate<{
136
+ dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? Rel['options'] extends RelationThroughOptions$1 ? EmptyObject : RelationToOneDataForCreate<{
136
137
  nestedCreateQuery: CreateData<Q>;
137
138
  table: Q;
138
- }> : never };
139
+ }> : never; };
139
140
  dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
140
141
  disconnect: boolean;
141
142
  } | {
@@ -160,11 +161,11 @@ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel exte
160
161
  create: CreateData<Q>;
161
162
  } : never;
162
163
  }
163
- interface HasAndBelongsToMany extends RelationThunkBase {
164
+ interface HasAndBelongsToMany$1 extends RelationThunkBase {
164
165
  type: 'hasAndBelongsToMany';
165
- options: HasAndBelongsToManyOptions;
166
+ options: HasAndBelongsToManyOptions$1;
166
167
  }
167
- interface HasAndBelongsToManyOptions<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput> {
168
+ interface HasAndBelongsToManyOptions$1<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput> {
168
169
  required?: boolean;
169
170
  columns: (keyof Columns)[];
170
171
  references: string[];
@@ -172,14 +173,15 @@ interface HasAndBelongsToManyOptions<Columns extends Column.Shape.QueryInit = Co
172
173
  through: {
173
174
  schema?: QuerySchema;
174
175
  table: string;
176
+ snakeCase?: boolean;
175
177
  columns: string[];
176
178
  references: (keyof Related['columns']['shape'])[];
177
179
  foreignKey?: boolean | TableData.References.Options;
178
180
  };
179
181
  on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
180
182
  }
181
- type HasAndBelongsToManyParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type'] };
182
- type HasAndBelongsToManyQuery<Name extends string, TableQuery extends Query> = { [K in keyof TableQuery]: K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : TableQuery[K] } & QueryHasWhere;
183
+ type HasAndBelongsToManyParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type']; };
184
+ type HasAndBelongsToManyQuery<Name extends string, TableQuery extends Query> = { [K in keyof TableQuery]: K extends '__selectable' ? SelectableFromShape<TableQuery['shape'], Name> : K extends '__as' ? Name : TableQuery[K]; } & QueryHasWhere;
183
185
  interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends string, FK extends string, Q extends Query> extends RelationConfigBase {
184
186
  returnsOne: false;
185
187
  query: Q;
@@ -194,7 +196,7 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
194
196
  }[];
195
197
  } : {
196
198
  connect?: WhereArg<Q>[];
197
- } };
199
+ }; };
198
200
  dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
199
201
  disconnect?: MaybeArray<WhereArg<Q>>;
200
202
  set?: MaybeArray<WhereArg<Q>>;
@@ -231,72 +233,678 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
231
233
  add?: MaybeArray<WhereArg<Q>>;
232
234
  };
233
235
  }
234
- type RelationToOneDataForCreate<Rel extends {
235
- nestedCreateQuery: unknown;
236
- table: Query;
237
- }> = {
238
- create: Rel['nestedCreateQuery'];
239
- connect?: never;
240
- connectOrCreate?: never;
241
- } | {
242
- create?: never;
243
- connect: WhereArg<Rel['table']>;
244
- connectOrCreate?: never;
245
- } | {
246
- create?: never;
247
- connect?: never;
248
- connectOrCreate: {
249
- where: WhereArg<Rel['table']>;
250
- create: Rel['nestedCreateQuery'];
236
+ interface TableFactoryOptions<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> {
237
+ schemaConfig?: () => SchemaConfig;
238
+ columnTypes?: ColumnTypes | ((t: DefaultColumnTypes<SchemaConfig>) => ColumnTypes);
239
+ snakeCase?: boolean;
240
+ filePath?: string;
241
+ nowSQL?: string;
242
+ exportAs?: string;
243
+ defineTableExportAs?: string;
244
+ language?: string;
245
+ autoForeignKeys?: boolean | TableData.References.BaseOptions;
246
+ }
247
+ interface OrmTable<Id extends string, Table extends string, Columns extends ColumnsShape, Relations, ColumnTypes = unknown, Computed = undefined, Scopes extends RecordUnknown | undefined = undefined, SoftDelete extends true | string | undefined = undefined, SchemaConfig extends ColumnSchemaConfig = DefaultSchemaConfig, Data extends MaybeArray<TableDataItem> = never, ReadOnly extends boolean | undefined = boolean | undefined> {
248
+ data: {
249
+ id: Id;
250
+ name: undefined;
251
+ table: Table;
252
+ relations: Relations;
253
+ columns: Columns;
254
+ types: ColumnTypes;
255
+ computed: Computed;
256
+ scopes: Scopes;
257
+ softDelete: SoftDelete;
258
+ readOnly: ReadOnly;
259
+ materialized: undefined;
260
+ tableData: Data extends unknown[] ? Data : [Data];
251
261
  };
252
- };
253
- interface RelationToOneDataForCreateSameQuerySelf extends CreateSelf, PickQuerySelectableRelations {}
254
- type RelationToOneDataForCreateSameQuery<Q extends RelationToOneDataForCreateSameQuerySelf> = {
255
- create: CreateData<Q>;
256
- connect?: never;
257
- connectOrCreate?: never;
258
- } | {
259
- create?: never;
260
- connect: WhereArg<Q>;
261
- connectOrCreate?: never;
262
- } | {
263
- create?: never;
264
- connect?: never;
265
- connectOrCreate: {
266
- where: WhereArg<Q>;
267
- create: CreateData<Q>;
262
+ prototype: {
263
+ columns: {
264
+ shape: Columns;
265
+ };
268
266
  };
269
- };
270
- interface RelationThunkBase {
271
- type: string;
272
- id: string;
273
- options: unknown;
274
- }
275
- type RelationThunk = BelongsTo | HasOne | HasMany | HasAndBelongsToMany;
276
- interface RelationThunks {
277
- [K: string]: RelationThunk;
267
+ inputSchema: SchemaConfig['inputSchema'];
268
+ outputSchema: SchemaConfig['outputSchema'];
269
+ querySchema: SchemaConfig['querySchema'];
270
+ pkeySchema: SchemaConfig['pkeySchema'];
271
+ createSchema: SchemaConfig['createSchema'];
272
+ updateSchema: SchemaConfig['updateSchema'];
273
+ instance(): ORMTableInput;
274
+ exportAs: string;
275
+ nowSQL?: string;
276
+ <C extends (keyof Columns & string)[]>(...columns: C): OrmTable.Relations.Endpoint<Id, C, ColumnsShape.InputPartial<Columns>>;
277
+ where<T>(this: T, conditions: ColumnsShape.InputPartial<Columns>): T;
278
+ through<Through extends string>(through: Through, source: string): OrmTable.Relations.Through<Id, Through, ColumnsShape.InputPartial<Columns>>;
279
+ primaryKey<Column extends keyof Columns & string, PrimaryKeyColumns extends [Column, ...Column[]], Name extends string>(columns: PrimaryKeyColumns, name?: Name): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Computed, Scopes, SoftDelete, SchemaConfig, Data | OrmTable.DataItemWithColumns<PrimaryKeyColumns, Name>, ReadOnly>;
280
+ index(columns: OrmTable.IndexColumn<Columns>[], options?: TableData.Index.OptionsArg): this;
281
+ searchIndex(columns: OrmTable.IndexColumn<Columns>[], options?: TableData.Index.TsVectorArg): this;
282
+ unique<Items extends [OrmTable.IndexColumn<Columns>, ...OrmTable.IndexColumn<Columns>[]], Name extends string>(columns: Items, options?: TableData.Index.UniqueOptionsArg<Name>): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Computed, Scopes, SoftDelete, SchemaConfig, Data | OrmTable.DataItemWithColumns<Items, Name>, ReadOnly>;
283
+ exclude(columns: OrmTable.ExcludeColumn<Columns>[], options?: TableData.Exclude.Options): this;
284
+ check(check: RawSqlBase, name?: string): this;
285
+ softDelete(): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Computed, Scopes, true, SchemaConfig, Data, ReadOnly>;
286
+ softDelete<Column extends keyof Columns & string>(column: Column): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Computed, Scopes, Column, SchemaConfig, Data, ReadOnly>;
287
+ computed<Fn extends ComputedOptionsFactory<ColumnTypes, Columns>>(computed: Fn): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Fn, Scopes, SoftDelete, SchemaConfig, Data, ReadOnly>;
288
+ scopes<Keys extends string>(scopes: DbTableOptionScopes<Table, Columns, Keys>): OrmTable<Id, Table, Columns, Relations, ColumnTypes, Computed, DbTableOptionScopes<Table, Columns, Keys>, SoftDelete, SchemaConfig, Data, ReadOnly>;
289
+ grants(grants: readonly Grant.TableClassGrant[]): this;
290
+ rls(rls: OrmTable.RlsConfig): this;
291
+ init(fn: OrmTable.Init<Columns>): this;
292
+ foreignKey<Column extends keyof Columns, ForeignTable extends {
293
+ data: {
294
+ columns: ColumnsShape;
295
+ };
296
+ }, ForeignColumn extends keyof ForeignTable['data']['columns']>(columns: [Column, ...Column[]], fnOrTable: () => ForeignTable, foreignColumns: [ForeignColumn, ...ForeignColumn[]], options?: TableData.References.Options): this;
297
+ foreignKey<Column extends keyof Columns>(columns: [Column, ...Column[]], fnOrTable: string, foreignColumns: [string, ...string[]], options?: TableData.References.Options): this;
298
+ relations<Fn extends (self: OrmTable.Relations.Arg<keyof Columns>) => any>(fn: Fn): OrmTable<Id, Table, Columns, Fn, ColumnTypes, Computed, Scopes, SoftDelete, SchemaConfig, Data, ReadOnly>;
278
299
  }
279
- type TableClassInstances<T extends TableClasses> = InstanceType<T[keyof T]>;
280
- type RelationToTableInputById<Tables, Id extends string> = Extract<Tables, {
281
- id: Id;
282
- }>;
283
- type RelationToTableInputByKey<T extends TableClasses, Id extends string> = Id extends keyof T ? InstanceType<T[Id]> : never;
284
- type RelationToTableInputByName<Tables, Id extends string> = Extract<Tables, {
285
- table: Id;
286
- } | {
287
- name: Id;
288
- }>;
289
- type RelationToTableInputByKeyOrName<T extends TableClasses, Id extends string> = Id extends keyof T ? RelationToTableInputByKey<T, Id> : RelationToTableInputByName<TableClassInstances<T>, Id>;
290
- type RelationToTableInputFromClasses<T extends TableClasses, Id extends string> = RelationToTableInputById<TableClassInstances<T>, Id> extends infer Result ? [Result] extends [never] ? RelationToTableInputByKeyOrName<T, Id> : Result : never;
291
- type RelationToTableInput<TC extends TableClasses, VC extends TableClasses, Rel extends RelationThunkBase> = RelationToTableInputFromClasses<TC, Rel['id']> extends infer Result ? [Result] extends [never] ? RelationToTableInputFromClasses<VC, Rel['id']> extends infer Result ? [Result] extends [never] ? false : Result : never : Result : never;
292
- type RelationTableToQuery<TC extends TableClasses, VC extends TableClasses, Rel extends RelationThunkBase> = RelationToTableInput<TC, VC, Rel> extends infer Result extends ORMTableInput ? TableQueryBuilder<TC, VC, Result> : never;
293
- interface RelationConfigSelf {
294
- columns: {
295
- shape: Column.Shape.QueryInit;
296
- };
297
- relations: RelationThunks;
300
+ declare namespace OrmTable {
301
+ export interface RlsConfig extends Rls.TableConfigBase {
302
+ permit: RlsPolicy.Policy[];
303
+ restrict?: RlsPolicy.Policy[];
304
+ }
305
+ type DataKey<Columns extends ColumnsShape> = keyof Columns & string;
306
+ export type IndexColumn<Columns extends ColumnsShape> = DataKey<Columns> | TableData.Index.ColumnOrExpressionOptions<DataKey<Columns>>;
307
+ export type ExcludeColumn<Columns extends ColumnsShape> = TableData.Exclude.ColumnOrExpressionOptions<DataKey<Columns>>;
308
+ export interface DataItemWithColumns<Columns extends unknown[], Name extends string> {
309
+ tableDataItem: true;
310
+ columns: Columns;
311
+ name: string extends Name ? never : Name;
312
+ }
313
+ export interface RelationEndpointData {
314
+ id: string;
315
+ data?: {
316
+ id: string;
317
+ };
318
+ columns: string[];
319
+ on?: RecordUnknown;
320
+ through?: string;
321
+ source?: string;
322
+ relationThrough?: string;
323
+ relationSource?: string;
324
+ }
325
+ export interface Input<Id extends string, Table extends string | undefined, Name extends string | undefined, Columns extends ColumnsShape, ColumnTypes, Relations, Computed, Scopes extends RecordUnknown | undefined, SoftDelete extends true | string | undefined, ReadOnly extends boolean | undefined, Materialized extends true | undefined = true | undefined, TableDataItems extends MaybeArray<TableDataItem> = MaybeArray<TableDataItem>> extends ORMTableInput {
326
+ id: Id;
327
+ table: Table;
328
+ name: Name;
329
+ types: ColumnTypes;
330
+ readonly readOnly: ReadOnly;
331
+ readonly materialized: Materialized;
332
+ readonly softDelete: SoftDelete;
333
+ columns: {
334
+ shape: Columns;
335
+ data: TableDataItems extends unknown[] ? TableDataItems : [TableDataItems];
336
+ };
337
+ relations: Relations;
338
+ computed: Computed extends ComputedOptionsFactory<never, never> ? Computed : undefined;
339
+ scopes: Scopes extends undefined ? undefined : Scopes;
340
+ }
341
+ export interface TableFactoryKit<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> {
342
+ defineTable: DefineTable<SchemaConfig, ColumnTypes>;
343
+ defineView: DefineView<SchemaConfig, ColumnTypes>;
344
+ sql: DbSqlMethod<ColumnTypes>;
345
+ exportAs: string;
346
+ }
347
+ export interface BeforeActionHookUtils<Columns extends ColumnsShape> {
348
+ /**
349
+ * Sets column values before a create, update, or save action.
350
+ */
351
+ set(data: { [K in keyof ColumnsShape.InputPartial<Columns>]?: ColumnsShape.InputPartial<Columns>[K] | (() => QueryOrExpression<ColumnsShape.InputPartial<Columns>[K]>); }): void;
352
+ }
353
+ export interface InitHooks<Columns extends ColumnsShape> {
354
+ /**
355
+ * Registers a hook to run before every query.
356
+ */
357
+ beforeQuery(cb: QueryBeforeHook): void;
358
+ /**
359
+ * Registers a hook to run after every query.
360
+ */
361
+ afterQuery(cb: QueryAfterHook): void;
362
+ /**
363
+ * Registers a hook to run before create actions.
364
+ */
365
+ beforeCreate(cb: (utils: BeforeActionHookUtils<Columns>) => void | Promise<void>): void;
366
+ /**
367
+ * Registers a hook to run after create actions.
368
+ */
369
+ afterCreate<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
370
+ /**
371
+ * Registers a hook to run after create commits.
372
+ */
373
+ afterCreateCommit<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
374
+ /**
375
+ * Registers a hook to run before update actions.
376
+ */
377
+ beforeUpdate(cb: (utils: BeforeActionHookUtils<Columns>) => void | Promise<void>): void;
378
+ /**
379
+ * Registers a hook to run after update actions.
380
+ */
381
+ afterUpdate<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
382
+ /**
383
+ * Registers a hook to run after update commits.
384
+ */
385
+ afterUpdateCommit<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
386
+ /**
387
+ * Registers a hook to run before create or update actions.
388
+ */
389
+ beforeSave(cb: (utils: BeforeActionHookUtils<Columns>) => void | Promise<void>): void;
390
+ /**
391
+ * Registers a hook to run after create or update actions.
392
+ */
393
+ afterSave<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
394
+ /**
395
+ * Registers a hook to run after create or update commits.
396
+ */
397
+ afterSaveCommit<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
398
+ /**
399
+ * Registers a hook to run before delete actions.
400
+ */
401
+ beforeDelete(cb: QueryBeforeHook): void;
402
+ /**
403
+ * Registers a hook to run after delete actions.
404
+ */
405
+ afterDelete<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
406
+ /**
407
+ * Registers a hook to run after delete commits.
408
+ */
409
+ afterDeleteCommit<S extends (keyof Columns & string)[]>(select: S, cb: AfterHook<S, Columns>): void;
410
+ }
411
+ export type Init<Columns extends ColumnsShape> = (orm: unknown, hooks: InitHooks<Columns>) => void;
412
+ export interface DefineTableOptions {
413
+ /**
414
+ * Stable table identity used when resolving relation targets.
415
+ */
416
+ id?: string;
417
+ /**
418
+ * Database table name used when it differs from the query-facing table name.
419
+ */
420
+ nameInDb?: string;
421
+ /**
422
+ * Database schema containing this table.
423
+ */
424
+ schema?: QuerySchema;
425
+ /**
426
+ * Suppress the no-primary-key error for this table.
427
+ */
428
+ noPrimaryKey?: boolean;
429
+ /**
430
+ * Override the table factory snake-case setting for this table.
431
+ */
432
+ snakeCase?: boolean;
433
+ /**
434
+ * Table comment.
435
+ */
436
+ comment?: string;
437
+ /**
438
+ * Exclude this table from generated migration DDL reconciliation.
439
+ */
440
+ generatorIgnore?: true;
441
+ /**
442
+ * Auto-create foreign keys for relations of this table.
443
+ */
444
+ autoForeignKeys?: TableData.References.BaseOptions | boolean;
445
+ /**
446
+ * Mark this table as read-only to prevent mutations.
447
+ */
448
+ readOnly?: boolean;
449
+ }
450
+ type DefineTableId<Table extends string, Options extends DefineTableOptions> = Options extends {
451
+ id: infer Id extends string;
452
+ } ? Id : Table;
453
+ type DefineTableReadOnly<Options extends DefineTableOptions> = Options extends {
454
+ readOnly: true;
455
+ } ? true : undefined;
456
+ export interface DefineTable<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> {
457
+ <Table extends string, Columns extends ColumnsShape>(table: Table, t: (columnTypes: ColumnTypes) => Columns): OrmTable<Table, Table, Columns, undefined, ColumnTypes, undefined, undefined, undefined, SchemaConfig, never, undefined>;
458
+ <Table extends string, const Options extends DefineTableOptions, Columns extends ColumnsShape>(table: Table, options: Options, t: (columnTypes: ColumnTypes) => Columns): OrmTable<DefineTableId<Table, Options>, Table, Columns, undefined, ColumnTypes, undefined, undefined, undefined, SchemaConfig, never, DefineTableReadOnly<Options>>;
459
+ /**
460
+ * Returns the file path where the table factory was defined.
461
+ * Uses stack trace analysis to determine the caller's file path.
462
+ * @throws Error if file path cannot be determined and `filePath` option was not provided to `createTableFactory`
463
+ */
464
+ getFilePath(): string;
465
+ /**
466
+ * Column types configured for this table factory.
467
+ */
468
+ types: ColumnTypes;
469
+ /**
470
+ * Export name configured for this table factory.
471
+ */
472
+ exportAs: string;
473
+ /**
474
+ * Custom timestamp SQL configured for this table factory.
475
+ */
476
+ nowSQL?: string;
477
+ /**
478
+ * Whether this table factory maps columns and table names to snake case.
479
+ */
480
+ snakeCase?: boolean;
481
+ /**
482
+ * Default full text search language configured for this table factory.
483
+ */
484
+ language?: string;
485
+ }
486
+ export interface DefineViewOptions {
487
+ /**
488
+ * Stable view identity used when resolving relation targets.
489
+ * Defaults to the view name when not provided.
490
+ */
491
+ id?: string;
492
+ /**
493
+ * Database schema containing this view.
494
+ */
495
+ schema?: QuerySchema;
496
+ /**
497
+ * Database view name used when it differs from the query-facing view name.
498
+ */
499
+ nameInDb?: string;
500
+ /**
501
+ * Override the table factory snake-case setting for this view.
502
+ */
503
+ snakeCase?: boolean;
504
+ /**
505
+ * Default language for full text search columns in this view.
506
+ */
507
+ language?: string;
508
+ /**
509
+ * Exclude this view from generated migration DDL reconciliation.
510
+ */
511
+ generatorIgnore?: true;
512
+ /**
513
+ * SQL expression that defines this view.
514
+ */
515
+ sql?: string | RawSqlBase;
516
+ /**
517
+ * Set to false to expose mutation methods on a regular view.
518
+ */
519
+ readOnly?: boolean;
520
+ /**
521
+ * Mark this definition as a materialized view.
522
+ */
523
+ materialized?: true;
524
+ /**
525
+ * CREATE MATERIALIZED VIEW WITH DATA / WITH NO DATA option.
526
+ */
527
+ withData?: boolean;
528
+ /**
529
+ * CREATE VIEW RECURSIVE option used by migration generation.
530
+ */
531
+ recursive?: boolean;
532
+ /**
533
+ * CREATE VIEW CHECK OPTION used by migration generation.
534
+ */
535
+ checkOption?: 'LOCAL' | 'CASCADED';
536
+ /**
537
+ * CREATE VIEW security_barrier option used by migration generation.
538
+ */
539
+ securityBarrier?: boolean;
540
+ /**
541
+ * CREATE VIEW security_invoker option used by migration generation.
542
+ */
543
+ securityInvoker?: boolean;
544
+ }
545
+ type DefineViewId<Options, Name extends string> = Options extends {
546
+ id: infer Id extends string;
547
+ } ? Id : Name;
548
+ type DefineViewReadOnly<Options> = Options extends {
549
+ readOnly: false;
550
+ } ? Options extends {
551
+ materialized: true;
552
+ } ? true : false : true;
553
+ type DefineViewMaterialized<Options> = Options extends {
554
+ materialized: true;
555
+ } ? true : undefined;
556
+ export interface View<Id extends string, Name extends string, Columns extends ColumnsShape, ColumnTypes, Relations, Computed = undefined, Scopes extends RecordUnknown | undefined = undefined, SoftDelete extends true | string | undefined = undefined, ReadOnly extends boolean | undefined = true, Materialized extends true | undefined = undefined, SchemaConfig extends ColumnSchemaConfig = DefaultSchemaConfig> {
557
+ data: {
558
+ id: Id;
559
+ table: undefined;
560
+ name: Name;
561
+ relations: Relations;
562
+ columns: Columns;
563
+ types: ColumnTypes;
564
+ computed: Computed;
565
+ scopes: Scopes;
566
+ softDelete: SoftDelete;
567
+ readOnly: ReadOnly;
568
+ materialized: Materialized;
569
+ tableData: TableDataItem[];
570
+ };
571
+ prototype: {
572
+ columns: {
573
+ shape: Columns;
574
+ };
575
+ };
576
+ inputSchema: SchemaConfig['inputSchema'];
577
+ outputSchema: SchemaConfig['outputSchema'];
578
+ querySchema: SchemaConfig['querySchema'];
579
+ pkeySchema: SchemaConfig['pkeySchema'];
580
+ createSchema: SchemaConfig['createSchema'];
581
+ updateSchema: SchemaConfig['updateSchema'];
582
+ <C extends (keyof Columns & string)[]>(...columns: C): OrmTable.Relations.Endpoint<Id, C, ColumnsShape.InputPartial<Columns>>;
583
+ instance(): ORMTableInput;
584
+ nowSQL?: string;
585
+ query(fn: (orm: OrchidORM) => Query): this;
586
+ through<Through extends string>(through: Through, source: string): OrmTable.Relations.Through<Id, Through, ColumnsShape.InputPartial<Columns>>;
587
+ softDelete(): View<Id, Name, Columns, ColumnTypes, Relations, Computed, Scopes, true, ReadOnly, Materialized, SchemaConfig>;
588
+ softDelete<Column extends keyof Columns & string>(column: Column): View<Id, Name, Columns, ColumnTypes, Relations, Computed, Scopes, Column, ReadOnly, Materialized, SchemaConfig>;
589
+ computed<Fn extends ComputedOptionsFactory<unknown, Columns>>(computed: Fn): View<Id, Name, Columns, ColumnTypes, Relations, Fn, Scopes, SoftDelete, ReadOnly, Materialized, SchemaConfig>;
590
+ scopes<Keys extends string>(scopes: DbTableOptionScopes<Name, Columns, Keys>): View<Id, Name, Columns, ColumnTypes, Relations, Computed, DbTableOptionScopes<Name, Columns, Keys>, SoftDelete, ReadOnly, Materialized, SchemaConfig>;
591
+ grants(grants: readonly Grant.TableClassGrant[]): this;
592
+ init(fn: OrmTable.Init<Columns>): this;
593
+ relations<Fn extends (self: OrmTable.Relations.Arg<keyof Columns>) => any>(fn: Fn): View<Id, Name, Columns, ColumnTypes, Fn, Computed, Scopes, SoftDelete, ReadOnly, Materialized, SchemaConfig>;
594
+ }
595
+ export interface DefineView<SchemaConfig extends ColumnSchemaConfig, ColumnTypes> {
596
+ <Name extends string, Columns extends ColumnsShape>(name: Name, t: (columnTypes: ColumnTypes) => Columns): View<Name, Name, Columns, ColumnTypes, undefined, undefined, undefined, undefined, true, undefined, SchemaConfig>;
597
+ <Name extends string, const Options extends DefineViewOptions, Columns extends ColumnsShape>(name: Name, options: Options, t: (columnTypes: ColumnTypes) => Columns): View<DefineViewId<Options, Name>, Name, Columns, ColumnTypes, undefined, undefined, undefined, undefined, DefineViewReadOnly<Options>, DefineViewMaterialized<Options>, SchemaConfig>;
598
+ }
599
+ export namespace Relations {
600
+ type Resolve<Relations> = Relations extends ((...args: never[]) => infer Result) ? ResolveMany<Result> : Relations;
601
+ type ResolveMany<Relations> = { [K in keyof Relations]: ResolveOne<Relations[K]>; };
602
+ type ResolveOne<Relation> = Relation extends BelongsTo<infer C, infer Fn, infer Required> ? ResolvedBelongsTo<TargetId<Fn>, C, TargetReferences<Fn>, Required> : Relation extends HasOne<infer C, infer Fn, infer Required> ? ResolvedHasOne<TargetId<Fn>, C, TargetReferences<Fn>, Required> : Relation extends HasMany<infer C, infer Fn> ? ResolvedHasMany<TargetId<Fn>, C, TargetReferences<Fn>> : Relation extends HasAndBelongsToMany<infer C, infer Fn, infer Options> ? ResolveHasAndBelongsToMany<C, Fn, Options> : Relation extends HasOneThrough<infer Fn, infer Required> ? ResolvedHasOneThrough<TargetId<Fn>, ResolveHasOneThroughOptions<RelationThroughOptions<TargetThrough<Fn>>, Required>> : Relation extends HasManyThrough<infer Fn> ? ResolvedHasManyThrough<TargetId<Fn>, RelationThroughOptions<TargetThrough<Fn>>> : Relation;
603
+ type ResolveHasAndBelongsToMany<C extends string[], Fn extends Function, Options, References extends string[] = TargetReferences<Fn>> = ResolvedHasAndBelongsToMany<TargetId<Fn>, C, References, ResolveHasAndBelongsToManyOptions<C, References, Options>>;
604
+ type TargetResult<Fn extends Function> = Fn extends (() => infer Result) ? Result : never;
605
+ type TargetId<Fn extends Function> = TargetResult<Fn> extends {
606
+ id: infer Id extends string;
607
+ } ? Id : TargetResult<Fn> extends {
608
+ data: {
609
+ id: infer Id extends string;
610
+ };
611
+ } ? Id : string;
612
+ type TargetReferences<Fn extends Function> = TargetResult<Fn> extends {
613
+ columns: infer References extends string[];
614
+ } ? References : string[];
615
+ type TargetThrough<Fn extends Function> = TargetResult<Fn> extends {
616
+ through: infer Through extends string;
617
+ } ? Through : string;
618
+ interface Arg<ColumnKeys> {
619
+ <C extends (ColumnKeys & string)[]>(...columns: C): Fns<C>;
620
+ hasOne<Fn extends Function>(fn: Fn): HasOneThrough<Fn>;
621
+ hasMany<Fn extends Function>(fn: Fn): HasManyThrough<Fn>;
622
+ }
623
+ interface Through<Id extends string, Through extends string, Conditions> {
624
+ id: Id;
625
+ through: Through;
626
+ source: string;
627
+ where<T>(this: T, conditions: Conditions): T;
628
+ }
629
+ interface Endpoint<Id extends string, C, Conditions> {
630
+ id: Id;
631
+ columns: C;
632
+ where<T>(this: T, conditions: Conditions): T;
633
+ through<Through extends string>(through: Through, source: string): OrmTable.Relations.Through<Id, Through, Conditions>;
634
+ }
635
+ interface Fns<C extends string[]> {
636
+ belongsTo<Fn extends Function>(fn: Fn): BelongsTo<C, Fn>;
637
+ hasOne<Fn extends Function>(fn: Fn): HasOne<C, Fn>;
638
+ hasMany<Fn extends Function>(fn: Fn): HasMany<C, Fn>;
639
+ hasAndBelongsToMany<Fn extends Function>(fn: Fn): HasAndBelongsToMany<C, Fn>;
640
+ }
641
+ interface LazyRelationRefsOptions<C extends string[]> {
642
+ columns: C;
643
+ }
644
+ interface LazyRelationRefsOptionsWithRequired<C extends string[], Required extends boolean> extends LazyRelationRefsOptions<C> {
645
+ required: Required;
646
+ }
647
+ type LazyRelationRefsOptionsForRequired<C extends string[], Required extends boolean | undefined> = Required extends boolean ? LazyRelationRefsOptionsWithRequired<C, Required> : LazyRelationRefsOptions<C>;
648
+ interface RelationRefsOptions<C extends string[], References extends string[]> extends LazyRelationRefsOptions<C> {
649
+ references: References;
650
+ }
651
+ interface RelationRefsOptionsWithRequired<C extends string[], References extends string[], Required extends boolean> extends RelationRefsOptions<C, References> {
652
+ required: Required;
653
+ }
654
+ type RelationRefsOptionsForRequired<C extends string[], References extends string[], Required extends boolean | undefined> = Required extends boolean ? RelationRefsOptionsWithRequired<C, References, Required> : RelationRefsOptions<C, References>;
655
+ interface BelongsTo<C extends string[], Fn extends Function, Required extends boolean | undefined = undefined> {
656
+ type: 'belongsTo';
657
+ fn: Fn;
658
+ options: LazyRelationRefsOptionsForRequired<C, Required>;
659
+ __required?: Required;
660
+ required(): BelongsTo<C, Fn, true>;
661
+ required(value: false): BelongsTo<C, Fn, false>;
662
+ /**
663
+ * Controls whether a foreign key constraint is auto-created on the
664
+ * **owning table** (the table this relation is defined on).
665
+ *
666
+ * - `true` (default when autoForeignKeys is enabled) — create FK with DB defaults.
667
+ * - `false` — skip FK creation entirely.
668
+ * - `{ onUpdate: 'CASCADE', onDelete: 'SET NULL' }` — create FK with these options.
669
+ *
670
+ * Only needed when `autoForeignKeys` is enabled on the table config;
671
+ * otherwise no FK is created by default.
672
+ *
673
+ * @example
674
+ * // disable FK for this relation
675
+ * user('Id').belongsTo(() => ProfileTable('UserId')).foreignKey(false)
676
+ *
677
+ * @example
678
+ * // custom FK options
679
+ * user('Id').belongsTo(() => ProfileTable('UserId')).foreignKey({ onDelete: 'CASCADE' })
680
+ */
681
+ foreignKey(value?: boolean | TableData.References.BaseOptions): BelongsTo<C, Fn, Required>;
682
+ }
683
+ interface HasOne<C extends string[], Fn extends Function, Required extends boolean | undefined = undefined> {
684
+ type: 'hasOne';
685
+ fn: Fn;
686
+ options: LazyRelationRefsOptionsForRequired<C, Required>;
687
+ __required?: Required;
688
+ required(): HasOne<C, Fn, true>;
689
+ required(value: false): HasOne<C, Fn, false>;
690
+ /**
691
+ * Controls whether a foreign key constraint is auto-created on the
692
+ * **related table** (the table pointed to by this relation).
693
+ *
694
+ * - `true` (default when autoForeignKeys is enabled) — create FK with DB defaults.
695
+ * - `false` — skip FK creation entirely.
696
+ * - `{ onUpdate: 'CASCADE', onDelete: 'SET NULL' }` — create FK with these options.
697
+ *
698
+ * Only needed when `autoForeignKeys` is enabled on the table config;
699
+ * otherwise no FK is created by default.
700
+ *
701
+ * @example
702
+ * // disable FK for this relation
703
+ * user('Id').hasOne(() => ProfileTable('UserId')).foreignKey(false)
704
+ *
705
+ * @example
706
+ * // custom FK options
707
+ * user('Id').hasOne(() => ProfileTable('UserId')).foreignKey({ onDelete: 'CASCADE' })
708
+ */
709
+ foreignKey(value?: boolean | TableData.References.BaseOptions): HasOne<C, Fn, Required>;
710
+ }
711
+ interface HasMany<C extends string[], Fn extends Function> {
712
+ type: 'hasMany';
713
+ fn: Fn;
714
+ options: LazyRelationRefsOptions<C>;
715
+ /**
716
+ * Controls whether a foreign key constraint is auto-created on the
717
+ * **related table** (the table pointed to by this relation).
718
+ *
719
+ * - `true` (default when autoForeignKeys is enabled) — create FK with DB defaults.
720
+ * - `false` — skip FK creation entirely.
721
+ * - `{ onUpdate: 'CASCADE', onDelete: 'SET NULL' }` — create FK with these options.
722
+ *
723
+ * Only needed when `autoForeignKeys` is enabled on the table config;
724
+ * otherwise no FK is created by default.
725
+ *
726
+ * @example
727
+ * // disable FK for this relation
728
+ * user('Id').hasMany(() => PostTable('UserId')).foreignKey(false)
729
+ *
730
+ * @example
731
+ * // custom FK options
732
+ * user('Id').hasMany(() => PostTable('UserId')).foreignKey({ onDelete: 'CASCADE' })
733
+ */
734
+ foreignKey(value?: boolean | TableData.References.BaseOptions): HasMany<C, Fn>;
735
+ }
736
+ interface HasAndBelongsToManyOptions<C extends string[]> {
737
+ columns: C;
738
+ }
739
+ interface HasAndBelongsToManyForeignKeyOptions {
740
+ forThisTable?: boolean | TableData.References.BaseOptions;
741
+ forRelatedTable?: boolean | TableData.References.BaseOptions;
742
+ forBothTables?: boolean | TableData.References.BaseOptions;
743
+ }
744
+ interface LazyHasAndBelongsToManyThroughOptions<C extends string[]> extends HasAndBelongsToManyOptions<C> {
745
+ references: string[];
746
+ through: {
747
+ schema?: QuerySchema;
748
+ table: string;
749
+ snakeCase?: boolean;
750
+ columns: string[];
751
+ references: string[];
752
+ };
753
+ foreignKeys?: HasAndBelongsToManyForeignKeyOptions;
754
+ }
755
+ interface HasAndBelongsToManyThroughOptions<C extends string[], References extends string[]> extends HasAndBelongsToManyOptions<C> {
756
+ references: string[];
757
+ through: {
758
+ schema?: QuerySchema;
759
+ table: string;
760
+ snakeCase?: boolean;
761
+ columns: string[];
762
+ references: References;
763
+ };
764
+ foreignKeys?: HasAndBelongsToManyForeignKeyOptions;
765
+ }
766
+ interface HasAndBelongsToManyThroughConfig {
767
+ /**
768
+ * Database schema containing this join table.
769
+ */
770
+ schema?: QuerySchema;
771
+ /**
772
+ * Set to false to preserve the join table name when the owning table
773
+ * uses snake-case mode.
774
+ */
775
+ joinTableSnakeCase?: boolean;
776
+ }
777
+ type ResolveHasAndBelongsToManyOptions<C extends string[], References extends string[], Options = HasAndBelongsToManyOptions<C>> = Options extends LazyHasAndBelongsToManyThroughOptions<C> ? HasAndBelongsToManyThroughOptions<C, References> : Options;
778
+ type ResolveHasOneThroughOptions<Options, Required extends boolean | undefined> = Required extends boolean ? Options & {
779
+ required: Required;
780
+ } : Options;
781
+ interface HasAndBelongsToMany<C extends string[], Fn extends Function, Options = HasAndBelongsToManyOptions<C>> {
782
+ type: 'hasAndBelongsToMany';
783
+ fn: Fn;
784
+ options: Options;
785
+ through(joinTable: string, selfColumns: string | (string[] & {
786
+ length: C['length'];
787
+ }), relColumns: string | string[], options?: HasAndBelongsToManyThroughConfig): HasAndBelongsToMany<C, Fn, LazyHasAndBelongsToManyThroughOptions<C>>;
788
+ /**
789
+ * Controls foreign key constraints on the **join table**. Unlike other
790
+ * relation kinds, hasAndBelongsToMany creates FKs on both sides of the
791
+ * join, so this method accepts a richer options object.
792
+ *
793
+ * - `true` (default) — create all join-table FKs with DB defaults.
794
+ * - `false` — skip all join-table FK creation.
795
+ * - `{ forThisTable, forRelatedTable, forBothTables }` — fine-grained control:
796
+ * - `forThisTable` — FK from join table → **owning table** (this table).
797
+ * - `forRelatedTable` — FK from join table → **related table**.
798
+ * - `forBothTables` — shorthand: applies to both FKs at once.
799
+ *
800
+ * Each sub-value can be `false` (skip), `true` (default), or
801
+ * `{ onUpdate: 'CASCADE', onDelete: 'SET NULL' }` (custom options).
802
+ *
803
+ * Must be called **after** `.through()`.
804
+ *
805
+ * @example
806
+ * // disable all join-table FKs
807
+ * post('Id')
808
+ * .hasAndBelongsToMany(() => TagTable('Id'))
809
+ * .through('postTag', ['postId'], ['tagId'])
810
+ * .foreignKey(false)
811
+ *
812
+ * @example
813
+ * // custom FK on both sides
814
+ * post('Id')
815
+ * .hasAndBelongsToMany(() => TagTable('Id'))
816
+ * .through('postTag', ['postId'], ['tagId'])
817
+ * .foreignKey({
818
+ * forThisTable: { onDelete: 'CASCADE' },
819
+ * forRelatedTable: { onDelete: 'CASCADE' },
820
+ * })
821
+ *
822
+ * @example
823
+ * // same custom FK via shorthand
824
+ * post('Id')
825
+ * .hasAndBelongsToMany(() => TagTable('Id'))
826
+ * .through('postTag', ['postId'], ['tagId'])
827
+ * .foreignKey({ forBothTables: { onDelete: 'CASCADE' } })
828
+ */
829
+ foreignKey(value?: boolean | HasAndBelongsToManyForeignKeyOptions): HasAndBelongsToMany<C, Fn, Options>;
830
+ }
831
+ interface ResolvedBelongsTo<Id extends string, C extends string[], References extends string[], Required extends boolean | undefined = undefined> {
832
+ type: 'belongsTo';
833
+ id: Id;
834
+ options: RelationRefsOptionsForRequired<C, References, Required>;
835
+ __required?: Required;
836
+ required(): ResolvedBelongsTo<Id, C, References, true>;
837
+ required(value: false): ResolvedBelongsTo<Id, C, References, false>;
838
+ /** @see BelongsTo.foreignKey */
839
+ foreignKey(value?: boolean | TableData.References.BaseOptions): ResolvedBelongsTo<Id, C, References, Required>;
840
+ }
841
+ interface ResolvedHasOne<Id extends string, C extends string[], References extends string[], Required extends boolean | undefined = undefined> {
842
+ type: 'hasOne';
843
+ id: Id;
844
+ options: RelationRefsOptionsForRequired<C, References, Required>;
845
+ __required?: Required;
846
+ required(): ResolvedHasOne<Id, C, References, true>;
847
+ required(value: false): ResolvedHasOne<Id, C, References, false>;
848
+ /** @see HasOne.foreignKey */
849
+ foreignKey(value?: boolean | TableData.References.BaseOptions): ResolvedHasOne<Id, C, References, Required>;
850
+ }
851
+ interface ResolvedHasMany<Id extends string, C extends string[], References extends string[]> {
852
+ type: 'hasMany';
853
+ id: Id;
854
+ options: RelationRefsOptions<C, References>;
855
+ /** @see HasMany.foreignKey */
856
+ foreignKey(value?: boolean | TableData.References.BaseOptions): ResolvedHasMany<Id, C, References>;
857
+ }
858
+ interface ResolvedHasAndBelongsToMany<Id extends string, C extends string[], References extends string[], Options = HasAndBelongsToManyOptions<C>> {
859
+ type: 'hasAndBelongsToMany';
860
+ id: Id;
861
+ options: Options;
862
+ through(joinTable: string, selfColumns: string | (string[] & {
863
+ length: C['length'];
864
+ }), relColumns: string | string[], options?: HasAndBelongsToManyThroughConfig): ResolvedHasAndBelongsToMany<Id, C, References, HasAndBelongsToManyThroughOptions<C, References>>;
865
+ }
866
+ interface RelationThroughOptions<Through extends string = string> {
867
+ through: Through;
868
+ source: string;
869
+ on?: RecordUnknown;
870
+ required?: boolean;
871
+ }
872
+ interface HasOneThrough<Fn extends Function, Required extends boolean | undefined = undefined> {
873
+ type: 'hasOne';
874
+ fn: Fn;
875
+ options: ResolveHasOneThroughOptions<RelationThroughOptions<TargetThrough<Fn>>, Required>;
876
+ __required?: Required;
877
+ required(): HasOneThrough<Fn, true>;
878
+ required(value: false): HasOneThrough<Fn, false>;
879
+ }
880
+ interface HasManyThrough<Fn extends Function> {
881
+ type: 'hasMany';
882
+ fn: Fn;
883
+ options: RelationThroughOptions<TargetThrough<Fn>>;
884
+ }
885
+ interface ResolvedHasOneThrough<Id extends string, Options> {
886
+ type: 'hasOne';
887
+ id: Id;
888
+ options: Options;
889
+ __required?: Options extends {
890
+ required?: infer Required;
891
+ } ? Required : undefined;
892
+ required(): ResolvedHasOneThrough<Id, Options & {
893
+ required: true;
894
+ }>;
895
+ required(value: false): ResolvedHasOneThrough<Id, Options & {
896
+ required: false;
897
+ }>;
898
+ }
899
+ interface ResolvedHasManyThrough<Id extends string, Options> {
900
+ type: 'hasMany';
901
+ id: Id;
902
+ options: Options;
903
+ }
904
+ }
905
+ export {};
298
906
  }
299
- type RelationConfigParams<T extends RelationConfigSelf, Relation extends RelationThunk> = Relation extends BelongsTo ? BelongsToParams<T, Relation['options']['columns'][number] & string> : Relation extends HasOne | HasMany ? HasOneParams<T, Relation['options']> : Relation extends HasAndBelongsToMany ? HasAndBelongsToManyParams<T, Relation['options']['columns'][number] & string> : never;
907
+ declare const createTableFactory: <SchemaConfig extends ColumnSchemaConfig = DefaultSchemaConfig, ColumnTypes = DefaultColumnTypes<SchemaConfig>>(options?: TableFactoryOptions<SchemaConfig, ColumnTypes>) => OrmTable.TableFactoryKit<SchemaConfig, ColumnTypes>;
300
908
  declare function transaction<Result>(this: {
301
909
  $qb: Db;
302
910
  }, fn: () => Promise<Result>): Promise<Result>;
@@ -334,17 +942,62 @@ interface OrchidORMTableHelper<T extends Query> {
334
942
  */
335
943
  makeHelper<Args extends unknown[], Result>(fn: (q: T, ...args: Args) => Result): OrchidORMQueryHelper<T, Args, Result>;
336
944
  }
337
- type OrchidORMTables<TC extends TableClasses = TableClasses, VC extends TableClasses = TableClasses> = { [K in keyof TC]: TC[K] extends {
945
+ /**
946
+ * Table definitions accepted by ORM setup.
947
+ */
948
+ interface OrmTableThunks {
949
+ /**
950
+ * ORM table or view definition keyed by the db instance property name.
951
+ */
952
+ [K: string]: TableClass | {
953
+ data: {
954
+ id: string;
955
+ name: string | undefined;
956
+ table: string | undefined;
957
+ relations: unknown;
958
+ columns: ColumnsShape;
959
+ types: unknown;
960
+ computed: unknown;
961
+ scopes: RecordUnknown | undefined;
962
+ softDelete?: true | string | undefined;
963
+ readOnly?: boolean | undefined;
964
+ materialized?: true | undefined;
965
+ tableData: unknown;
966
+ };
967
+ prototype: {
968
+ columns: {
969
+ shape: ColumnsShape;
970
+ };
971
+ };
972
+ instance(): ORMTableInput;
973
+ };
974
+ }
975
+ type OrmTableInstance<T> = T extends {
976
+ data: {
977
+ id: infer Id extends string;
978
+ table: infer Table extends string | undefined;
979
+ name: infer Name extends string | undefined;
980
+ columns: infer Columns extends ColumnsShape;
981
+ types: infer ColumnTypes;
982
+ relations: infer Relations;
983
+ computed: infer Computed;
984
+ scopes: infer Scopes extends RecordUnknown | undefined;
985
+ softDelete: infer SoftDelete extends true | string | undefined;
986
+ readOnly: infer ReadOnly extends boolean | undefined;
987
+ materialized: infer Materialized extends true | undefined;
988
+ tableData: infer Data extends TableDataItem[];
989
+ };
990
+ } ? OrmTable.Input<Id, Table, Name, Columns, ColumnTypes, OrmTable.Relations.Resolve<Relations>, Computed, Scopes, SoftDelete, ReadOnly, Materialized, Data[number]> : never;
991
+ type TableInstance<T> = T extends {
338
992
  new (): infer R extends ORMTableInput;
339
- } ? OrchidORMTableHelper<TableQueryBuilder<TC, VC, R>> : never };
340
- type OrchidORMBundle<TC extends TableClasses = TableClasses, VC extends TableClasses = TableClasses> = OrchidORMTables<TC> & {
341
- $views: OrchidORMTables<VC, TC>;
993
+ } ? R : OrmTableInstance<T>;
994
+ type OrchidORMTables<TT extends OrmTableThunks = OrmTableThunks, VT extends OrmTableThunks = OrmTableThunks> = { [K in keyof TT]: OrchidORMTableHelper<TableQueryBuilder<TT, VT, TableInstance<TT[K]>>>; };
995
+ type OrchidORMBundle<TT extends OrmTableThunks = OrmTableThunks, VT extends OrmTableThunks = OrmTableThunks> = OrchidORMTables<TT, VT> & {
996
+ $views: OrchidORMTables<VT, TT>;
342
997
  };
343
- type OrchidORMDbTables<TC extends TableClasses = TableClasses, VC extends TableClasses = TableClasses> = { [K in keyof TC]: TC[K] extends {
344
- new (): infer R extends ORMTableInput;
345
- } ? TableQueryBuilder<TC, VC, R> : never };
346
- type OrchidORM<TC extends TableClasses = TableClasses, VC extends TableClasses = TableClasses> = OrchidORMDbTables<TC, VC> & {
347
- $views: OrchidORMDbTables<VC, TC>;
998
+ type OrchidORMDbTables<TT extends OrmTableThunks = OrmTableThunks, VT extends OrmTableThunks = OrmTableThunks> = { [K in keyof TT]: TableQueryBuilder<TT, VT, TableInstance<TT[K]>>; };
999
+ type OrchidORM<TT extends OrmTableThunks = OrmTableThunks, VT extends OrmTableThunks = OrmTableThunks> = OrchidORMDbTables<TT, VT> & {
1000
+ $views: OrchidORMDbTables<VT, TT>;
348
1001
  } & OrchidORMMethods;
349
1002
  /**
350
1003
  * Identity helper for table row-level security configuration.
@@ -501,58 +1154,118 @@ interface OrchidORMMethods {
501
1154
  $close(): Promise<void>;
502
1155
  }
503
1156
  type OrchidOrmParam<Options> = true | null extends true ? 'Set strict: true to tsconfig' : Options;
504
- interface OrchidORMSetupOptions<V extends TableClasses = TableClasses> extends DbSharedOptions {
1157
+ interface OrchidORMSetupOptions<V extends OrmTableThunks = OrmTableThunks> extends DbSharedOptions {
505
1158
  /**
506
1159
  * First-class regular views exposed under db.$views.
507
1160
  */
508
1161
  views?: V;
509
1162
  }
510
- declare const bundleOrchidORM: <T extends TableClasses = EmptyObject, V extends TableClasses = EmptyObject>({
511
- tables,
512
- views
513
- }: {
1163
+ declare const bundleOrchidORM: <T extends OrmTableThunks = EmptyObject, V extends OrmTableThunks = EmptyObject>({ tables, views }: {
514
1164
  tables?: T;
515
1165
  views?: V;
516
1166
  }) => OrchidORMBundle<T, V>;
517
- declare const bundleOrchidORMTables: <T extends TableClasses>(tables: T) => OrchidORMBundle<T, EmptyObject>;
518
- declare const makeOrchidOrmDbWithAdapter: <T extends TableClasses, V extends TableClasses>(orm: OrchidORMBundle<T, V>, options: OrchidOrmParam<({
1167
+ declare const bundleOrchidORMTables: <T extends OrmTableThunks>(tables: T) => OrchidORMBundle<T, EmptyObject>;
1168
+ declare const makeOrchidOrmDbWithAdapter: <T extends OrmTableThunks, V extends OrmTableThunks>(orm: OrchidORMBundle<T, V>, options: OrchidOrmParam<({
519
1169
  db: Query;
520
1170
  } | {
521
1171
  adapter: Adapter;
522
1172
  }) & DbSharedOptions>) => OrchidORM<T, V>;
523
- declare const orchidORMWithAdapter: <T extends TableClasses, V extends TableClasses = EmptyObject>(options: OrchidOrmParam<({
1173
+ declare const orchidORMWithAdapter: <T extends OrmTableThunks, V extends OrmTableThunks = EmptyObject>(options: OrchidOrmParam<({
524
1174
  db: Query;
525
1175
  } | {
526
1176
  adapter: Adapter;
527
1177
  }) & OrchidORMSetupOptions<V>>, tables: T) => OrchidORM<T, V>;
1178
+ type RelationToOneDataForCreate<Rel extends {
1179
+ nestedCreateQuery: unknown;
1180
+ table: Query;
1181
+ }> = {
1182
+ create: Rel['nestedCreateQuery'];
1183
+ connect?: never;
1184
+ connectOrCreate?: never;
1185
+ } | {
1186
+ create?: never;
1187
+ connect: WhereArg<Rel['table']>;
1188
+ connectOrCreate?: never;
1189
+ } | {
1190
+ create?: never;
1191
+ connect?: never;
1192
+ connectOrCreate: {
1193
+ where: WhereArg<Rel['table']>;
1194
+ create: Rel['nestedCreateQuery'];
1195
+ };
1196
+ };
1197
+ interface RelationToOneDataForCreateSameQuerySelf extends CreateSelf, PickQuerySelectableRelations {}
1198
+ type RelationToOneDataForCreateSameQuery<Q extends RelationToOneDataForCreateSameQuerySelf> = {
1199
+ create: CreateData<Q>;
1200
+ connect?: never;
1201
+ connectOrCreate?: never;
1202
+ } | {
1203
+ create?: never;
1204
+ connect: WhereArg<Q>;
1205
+ connectOrCreate?: never;
1206
+ } | {
1207
+ create?: never;
1208
+ connect?: never;
1209
+ connectOrCreate: {
1210
+ where: WhereArg<Q>;
1211
+ create: CreateData<Q>;
1212
+ };
1213
+ };
1214
+ interface RelationThunkBase {
1215
+ type: string;
1216
+ id: string;
1217
+ options: unknown;
1218
+ }
1219
+ type RelationThunk = BelongsTo$1 | HasOne$1 | HasMany$1 | HasAndBelongsToMany$1;
1220
+ interface RelationThunks {
1221
+ [K: string]: RelationThunk;
1222
+ }
1223
+ type TableInstances<T extends OrmTableThunks> = { [K in keyof T]: TableInstance<T[K]>; };
1224
+ type TableClassInstances<T extends OrmTableThunks> = TableInstances<T>[keyof T];
1225
+ type RelationToTableInputById<Tables, Id extends string> = Tables extends {
1226
+ id: Id;
1227
+ } ? Tables : never;
1228
+ type RelationToTableInputByName<T extends OrmTableThunks, Id extends string> = { [K in keyof TableInstances<T>]: TableInstances<T>[K] extends {
1229
+ table: Id;
1230
+ } | {
1231
+ name: Id;
1232
+ } ? TableInstances<T>[K] : never; }[keyof T];
1233
+ type RelationToTableInputFromClasses<T extends OrmTableThunks, Id extends string> = RelationToTableInputById<TableClassInstances<T>, Id> extends (infer Result) ? [Result] extends [never] ? RelationToTableInputByName<T, Id> : Result : never;
1234
+ type RelationToTableInput<TT extends OrmTableThunks, VT extends OrmTableThunks, Rel extends RelationThunkBase> = RelationToTableInputFromClasses<TT, Rel['id']> extends (infer Result) ? [Result] extends [never] ? RelationToTableInputFromClasses<VT, Rel['id']> extends (infer Result) ? [Result] extends [never] ? false : Result : never : Result : never;
1235
+ type RelationTableToQuery<TT extends OrmTableThunks, VT extends OrmTableThunks, Rel extends RelationThunkBase> = RelationToTableInput<TT, VT, Rel> extends (infer Result extends ORMTableInput) ? TableQueryBuilder<TT, VT, Result> : never;
1236
+ interface RelationConfigSelf {
1237
+ columns: {
1238
+ shape: Column.Shape.QueryInit;
1239
+ };
1240
+ relations: RelationThunks;
1241
+ }
1242
+ type RelationConfigParams<T extends RelationConfigSelf, Relation extends RelationThunk> = Relation extends BelongsTo$1 ? BelongsToParams<T, Relation['options']['columns'][number] & string> : Relation extends HasOne$1 | HasMany$1 ? HasOneParams<T, Relation['options']> : Relation extends HasAndBelongsToMany$1 ? HasAndBelongsToManyParams<T, Relation['options']['columns'][number] & string> : never;
528
1243
  interface TableClass<T extends ORMTableInput = ORMTableInput> {
529
1244
  new (): T;
530
1245
  }
531
- interface TableClasses {
532
- [K: string]: TableClass;
533
- }
534
1246
  interface TableInfo {
535
1247
  definedAs: string;
536
1248
  db: OrchidORM;
537
1249
  getFilePath(): string;
538
1250
  name: string;
539
1251
  }
540
- interface Table extends Query, TableInfo {}
541
- type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo, Related> = Rel['options'] extends {
1252
+ interface Table$1 extends Query, TableInfo {}
1253
+ type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo$1, Related> = Rel['options'] extends {
542
1254
  required: infer Required extends boolean;
543
1255
  } ? Required : Rel['options'] extends {
544
1256
  on: unknown;
545
1257
  } ? false : BelongsToDefaultRequired<T, Rel, Related>;
546
- type BelongsToRelationInfo<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = RelationToTableInput<TC, VC, Rel> extends infer Related extends ORMTableInput ? BelongsToInfo<T, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel, Related>, BelongsToQuery<TableQueryBuilder<TC, VC, Related>, Name>> : never;
547
- type BelongsToCreateData<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = RelationToTableInput<TC, VC, Rel> extends infer Related extends ORMTableInput ? BelongsToDataForCreate<Name, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel, Related>, BelongsToQuery<TableQueryBuilder<TC, VC, Related>, Name>> : never;
548
- type BelongsToCreateDataColumns<T extends RelationConfigSelf> = { [K in keyof T['relations']]: T['relations'][K] extends BelongsTo ? T['relations'][K]['options']['columns'][number] & string : never }[keyof T['relations']];
549
- type BelongsToCreateDataForColumn<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf, Column extends string> = { [K in keyof T['relations']]: T['relations'][K] extends BelongsTo ? Column extends T['relations'][K]['options']['columns'][number] ? BelongsToCreateData<TC, VC, T, K & string, T['relations'][K]> : never : never }[keyof T['relations']];
550
- type RelationDataForCreate<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf, Name extends keyof T['relations'] & string> = T['relations'][Name] extends HasOne ? HasOneInfo<T, Name, T['relations'][Name], HasOneQuery<T, Name, RelationTableToQuery<TC, VC, T['relations'][Name]>>>['dataForCreate'] : T['relations'][Name] extends HasMany ? HasManyInfo<T, Name, T['relations'][Name], HasManyQuery<T, Name, RelationTableToQuery<TC, VC, T['relations'][Name]>>>['dataForCreate'] : T['relations'][Name] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, Name, T['relations'][Name]['options']['columns'][number] & string, HasAndBelongsToManyQuery<Name, RelationTableToQuery<TC, VC, T['relations'][Name]>>>['dataForCreate'] : never;
551
- type RelationDataForCreateOptionalNames<T extends RelationConfigSelf> = { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? never : K }[keyof T['relations'] & string];
552
- type BelongsToRelationsDataForCreate<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf> = { [Column in BelongsToCreateDataColumns<T>]: BelongsToCreateDataForColumn<TC, VC, T, Column> };
553
- type RelationsDataForCreateOptional<TC extends TableClasses, VC extends TableClasses, T extends RelationConfigSelf> = RelationDataForCreateOptionalNames<T> extends never ? EmptyObject : { [K in RelationDataForCreateOptionalNames<T>]: (u: RelationDataForCreate<TC, VC, T, K>) => void }[RelationDataForCreateOptionalNames<T>] extends ((u: infer Obj) => void) ? Obj : EmptyObject;
554
- interface TableQueryBuilder<TC extends TableClasses, VC extends TableClasses, T extends ORMTableInput> extends TableInfo, Db<T['table'] extends string ? T['table'] : T['name'], T['columns']['shape'], T['columns']['data'], T['types'], T['table'] extends string ? T['readOnly'] extends true ? true : undefined : T['readOnly'] extends false ? undefined : true, T> {
555
- relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? BelongsToRelationInfo<TC, VC, T, K, T['relations'][K]> : T['relations'][K] extends HasOne ? HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<TC, VC, T['relations'][K]>>> : T['relations'][K] extends HasMany ? HasManyInfo<T, K, T['relations'][K], HasManyQuery<T, K, RelationTableToQuery<TC, VC, T['relations'][K]>>> : T['relations'][K] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, K, T['relations'][K]['options']['columns'][number] & string, HasAndBelongsToManyQuery<K, RelationTableToQuery<TC, VC, T['relations'][K]>>> : never } : EmptyObject;
1258
+ type BelongsToRelationInfo<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo$1> = RelationToTableInput<TC, VC, Rel> extends (infer Related extends ORMTableInput) ? BelongsToInfo<T, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel, Related>, BelongsToQuery<TableQueryBuilder<TC, VC, Related>, Name>> : never;
1259
+ type BelongsToCreateData<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo$1> = RelationToTableInput<TC, VC, Rel> extends (infer Related extends ORMTableInput) ? BelongsToDataForCreate<Name, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel, Related>, BelongsToQuery<TableQueryBuilder<TC, VC, Related>, Name>> : never;
1260
+ type RelationDataForCreate<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends keyof T['relations'] & string, Rel> = Rel extends HasOne$1 ? HasOneInfo<T, Name, Rel, HasOneQuery<T, Name, RelationTableToQuery<TC, VC, Rel>>>['dataForCreate'] : Rel extends HasMany$1 ? HasManyInfo<T, Name, Rel, HasManyQuery<T, Name, RelationTableToQuery<TC, VC, Rel>>>['dataForCreate'] : Rel extends HasAndBelongsToMany$1 ? HasAndBelongsToManyInfo<T, Name, Rel['options']['columns'][number] & string, HasAndBelongsToManyQuery<Name, RelationTableToQuery<TC, VC, Rel>>>['dataForCreate'] : never;
1261
+ type RelationInfoForName<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends keyof T['relations'] & string, Rel> = Rel extends BelongsTo$1 ? BelongsToRelationInfo<TC, VC, T, Name, Rel> : Rel extends HasOne$1 ? HasOneInfo<T, Name, Rel, HasOneQuery<T, Name, RelationTableToQuery<TC, VC, Rel>>> : Rel extends HasMany$1 ? HasManyInfo<T, Name, Rel, HasManyQuery<T, Name, RelationTableToQuery<TC, VC, Rel>>> : Rel extends HasAndBelongsToMany$1 ? HasAndBelongsToManyInfo<T, Name, Rel['options']['columns'][number] & string, HasAndBelongsToManyQuery<Name, RelationTableToQuery<TC, VC, Rel>>> : never;
1262
+ type RelationDataForCreateOptionalNames<T extends RelationConfigSelf> = { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo$1 ? never : K; }[keyof T['relations'] & string];
1263
+ type RelationDataForCreateValue<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends keyof T['relations'] & string> = RelationDataForCreate<TC, VC, T, Name, T['relations'][Name]>[Name];
1264
+ type BelongsToRelationCreateData<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Name extends keyof T['relations']> = Name extends keyof T['relations'] & string ? T['relations'][Name] extends BelongsTo$1 ? BelongsToCreateData<TC, VC, T, Name, T['relations'][Name]> : never : never;
1265
+ type BelongsToRelationsDataForCreate<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf> = { [K in keyof T['relations'] as T['relations'][K] extends BelongsTo$1 ? T['relations'][K]['options']['columns'][number] & string : never]: BelongsToRelationCreateData<TC, VC, T, K>; };
1266
+ type RelationsDataForCreateOptional<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends RelationConfigSelf, Names extends keyof T['relations'] & string = RelationDataForCreateOptionalNames<T>> = [Names] extends [never] ? EmptyObject : { [K in Names]?: RelationDataForCreateValue<TC, VC, T, K>; };
1267
+ interface TableQueryBuilder<TC extends OrmTableThunks, VC extends OrmTableThunks, T extends ORMTableInput> extends TableInfo, Db<T['table'] extends string ? T['table'] : T['name'], T['columns']['shape'], T['columns']['data'], T['types'], T['table'] extends string ? T['readOnly'] extends true ? true : undefined : T['readOnly'] extends false ? undefined : true, T> {
1268
+ relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: RelationInfoForName<TC, VC, T, K, T['relations'][K]>; } : EmptyObject;
556
1269
  relationsDataForCreate: T extends RelationConfigSelf ? BelongsToRelationsDataForCreate<TC, VC, T> : EmptyObject;
557
1270
  relationsDataForCreateOptional: T extends RelationConfigSelf ? RelationsDataForCreateOptional<TC, VC, T> : EmptyObject;
558
1271
  }
@@ -574,6 +1287,7 @@ interface ORMTableInput extends PickORMTableInputColumnsAndComputed {
574
1287
  autoForeignKeys?: TableData.References.BaseOptions | boolean;
575
1288
  schema?: QuerySchema;
576
1289
  nameInDb?: string;
1290
+ snakeCase?: boolean;
577
1291
  types: unknown;
578
1292
  filePath: string;
579
1293
  language?: string;
@@ -603,9 +1317,30 @@ interface ORMTableInput extends PickORMTableInputColumnsAndComputed {
603
1317
  securityInvoker?: boolean;
604
1318
  withData?: boolean;
605
1319
  }
606
- type Queryable<T extends PickORMTableInputColumns> = ShallowSimplify<{ [K in keyof T['columns']['shape']]?: T['columns']['shape'][K]['__queryType'] }>;
607
- type DefaultSelect<T extends PickORMTableInputColumns> = ShallowSimplify<ColumnsShape.DefaultOutput<T['columns']['shape']>>;
608
- type Selectable<T extends PickORMTableInputColumnsAndComputed> = T['computed'] extends ((t: never) => infer R extends ComputedOptionsConfig) ? ShallowSimplify<ColumnsShape.Output<T['columns']['shape']> & { [K in keyof R]: R[K] extends {
1320
+ interface OrmLegacyTableForTypeHelpers {
1321
+ columns: {
1322
+ shape: Column.Shape.QueryInit;
1323
+ };
1324
+ computed?: ComputedOptionsFactory<never, never>;
1325
+ }
1326
+ interface OrmTableForTypeHelpers {
1327
+ data: {
1328
+ columns: Column.Shape.QueryInit;
1329
+ computed?: ComputedOptionsFactory<never, never>;
1330
+ };
1331
+ }
1332
+ type AnyTableForTypeHelpers = OrmLegacyTableForTypeHelpers | OrmTableForTypeHelpers;
1333
+ type Queryable<T extends AnyTableForTypeHelpers> = T extends {
1334
+ columns: {
1335
+ shape: infer Shape extends Column.Shape.QueryInit;
1336
+ };
1337
+ } | {
1338
+ data: {
1339
+ columns: infer Shape extends Column.Shape.QueryInit;
1340
+ };
1341
+ } ? { [K in keyof Shape]?: Shape[K]['__queryType']; } : never;
1342
+ type DefaultSelect<T extends AnyTableForTypeHelpers> = T extends OrmTableForTypeHelpers ? ColumnsShape.DefaultOutput<T['data']['columns']> : T extends OrmLegacyTableForTypeHelpers ? ColumnsShape.DefaultOutput<T['columns']['shape']> : never;
1343
+ type SelectableFromShapeAndComputed<Shape extends Column.Shape.QueryInit, Computed> = Computed extends undefined ? ColumnsShape.Output<Shape> : Computed extends ((t: never) => infer R extends ComputedOptionsConfig) ? ColumnsShape.Output<Shape> & { [K in keyof R]: R[K] extends {
609
1344
  result: {
610
1345
  value: infer Value extends Column.Pick.QueryColumn;
611
1346
  };
@@ -613,9 +1348,18 @@ type Selectable<T extends PickORMTableInputColumnsAndComputed> = T['computed'] e
613
1348
  result: {
614
1349
  value: infer Value extends Column.Pick.QueryColumn;
615
1350
  };
616
- }) ? Value['__outputType'] : never }> : ShallowSimplify<ColumnsShape.Output<T['columns']['shape']>>;
617
- type Insertable<T extends PickORMTableInputColumns> = ShallowSimplify<ColumnsShape.Input<T['columns']['shape']>>;
618
- type Updatable<T extends PickORMTableInputColumns> = ShallowSimplify<ColumnsShape.InputPartial<T['columns']['shape']>>;
1351
+ }) ? Value['__outputType'] : never; } : ColumnsShape.Output<Shape>;
1352
+ type Selectable<T extends AnyTableForTypeHelpers> = T extends OrmTableForTypeHelpers ? SelectableFromShapeAndComputed<T['data']['columns'], T['data']['computed']> : T extends OrmLegacyTableForTypeHelpers ? SelectableFromShapeAndComputed<T['columns']['shape'], T['computed']> : never;
1353
+ type Insertable<T extends AnyTableForTypeHelpers> = T extends {
1354
+ columns: {
1355
+ shape: infer Shape extends Column.Shape.QueryInit;
1356
+ };
1357
+ } | {
1358
+ data: {
1359
+ columns: infer Shape extends Column.Shape.QueryInit;
1360
+ };
1361
+ } ? ColumnsShape.Input<Shape> : never;
1362
+ type Updatable<T extends AnyTableForTypeHelpers> = T extends OrmTableForTypeHelpers ? ColumnsShape.InputPartial<T['data']['columns']> : T extends OrmLegacyTableForTypeHelpers ? ColumnsShape.InputPartial<T['columns']['shape']> : never;
619
1363
  type BeforeHookMethod = (cb: QueryBeforeHook) => void;
620
1364
  type BeforeActionHookMethod = (cb: QueryBeforeActionHook) => void;
621
1365
  type AfterHookMethod = (cb: QueryAfterHook) => void;
@@ -773,7 +1517,7 @@ interface BaseTableInstance<ColumnTypes> {
773
1517
  id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
774
1518
  options: Options;
775
1519
  };
776
- hasOne<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Through, Source>>(this: {
1520
+ hasOne<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Options extends HasOneOptions<Columns, Related, Through>>(this: {
777
1521
  columns: {
778
1522
  shape: Columns;
779
1523
  };
@@ -784,7 +1528,7 @@ interface BaseTableInstance<ColumnTypes> {
784
1528
  id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
785
1529
  options: Options;
786
1530
  };
787
- hasMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Source extends string, Options extends HasOneOptions<Columns, Related, Through, Source>>(this: {
1531
+ hasMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Options extends HasOneOptions<Columns, Related, Through>>(this: {
788
1532
  columns: {
789
1533
  shape: Columns;
790
1534
  };
@@ -795,7 +1539,7 @@ interface BaseTableInstance<ColumnTypes> {
795
1539
  id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
796
1540
  options: Options;
797
1541
  };
798
- hasAndBelongsToMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Options extends HasAndBelongsToManyOptions<Columns, Related>>(this: {
1542
+ hasAndBelongsToMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Options extends HasAndBelongsToManyOptions$1<Columns, Related>>(this: {
799
1543
  columns: {
800
1544
  shape: Columns;
801
1545
  };
@@ -876,27 +1620,9 @@ interface BaseMaterializedViewInstance<ColumnTypes> extends BaseTableInstance<Co
876
1620
  readonly materialized: true;
877
1621
  withData?: boolean;
878
1622
  }
879
- declare function createBaseTable<SchemaConfig extends ColumnSchemaConfig = DefaultSchemaConfig, ColumnTypes = DefaultColumnTypes<SchemaConfig>>({
880
- schemaConfig,
881
- columnTypes: columnTypesArg,
882
- snakeCase,
883
- filePath: filePathArg,
884
- nowSQL,
885
- exportAs,
886
- language,
887
- autoForeignKeys
888
- }?: {
889
- schemaConfig?: () => SchemaConfig;
890
- columnTypes?: ColumnTypes | ((t: DefaultColumnTypes<SchemaConfig>) => ColumnTypes);
891
- snakeCase?: boolean;
892
- filePath?: string;
893
- nowSQL?: string;
894
- exportAs?: string;
895
- language?: string;
896
- autoForeignKeys?: boolean | TableData.References.BaseOptions;
897
- }): BaseTableClass<SchemaConfig, ColumnTypes>;
1623
+ declare function createBaseTable<SchemaConfig extends ColumnSchemaConfig = DefaultSchemaConfig, ColumnTypes = DefaultColumnTypes<SchemaConfig>>({ schemaConfig, columnTypes: columnTypesArg, snakeCase, filePath: filePathArg, nowSQL, exportAs, language, autoForeignKeys }?: TableFactoryOptions<SchemaConfig, ColumnTypes>): BaseTableClass<SchemaConfig, ColumnTypes>;
898
1624
  type QueryMethods<T extends Query> = Record<string, (q: T, ...args: any[]) => any>;
899
- type QueryOne<T extends Query> = { [K in keyof T]: K extends 'returnType' ? Exclude<QueryReturnType, 'all' | undefined> : T[K] };
1625
+ type QueryOne<T extends Query> = { [K in keyof T]: K extends 'returnType' ? Exclude<QueryReturnType, 'all' | undefined> : T[K]; };
900
1626
  interface MethodsBase<T extends Query> {
901
1627
  queryMethods?: QueryMethods<T>;
902
1628
  queryOneMethods?: QueryMethods<QueryOne<T>>;
@@ -905,10 +1631,10 @@ interface MethodsBase<T extends Query> {
905
1631
  methods?: RecordUnknown;
906
1632
  }
907
1633
  type MapQueryMethods<BaseQuery extends Query, Method> = Method extends ((q: any, ...args: infer Args) => infer Result) ? <T extends BaseQuery>(this: T, ...args: Args) => Result extends Query ? MergeQuery<T, Result> : Result : never;
908
- type MapMethods<T extends Query, Methods extends MethodsBase<T>> = { [K in keyof Methods['queryMethods'] | keyof Methods['queryOneMethods'] | keyof Methods['queryWithWhereMethods'] | keyof Methods['queryOneWithWhereMethods'] | keyof Methods['methods']]: K extends keyof Methods['methods'] ? Methods['methods'][K] : K extends keyof Methods['queryOneWithWhereMethods'] ? MapQueryMethods<QueryOne<Query & QueryHasWhere>, Methods['queryOneWithWhereMethods'][K]> : K extends keyof Methods['queryWithWhereMethods'] ? MapQueryMethods<Query & QueryHasWhere, Methods['queryWithWhereMethods'][K]> : K extends keyof Methods['queryOneMethods'] ? MapQueryMethods<QueryOne<Query>, Methods['queryOneMethods'][K]> : K extends keyof Methods['queryMethods'] ? MapQueryMethods<Query, Methods['queryMethods'][K]> : never };
1634
+ type MapMethods<T extends Query, Methods extends MethodsBase<T>> = { [K in keyof Methods['queryMethods'] | keyof Methods['queryOneMethods'] | keyof Methods['queryWithWhereMethods'] | keyof Methods['queryOneWithWhereMethods'] | keyof Methods['methods']]: K extends keyof Methods['methods'] ? Methods['methods'][K] : K extends keyof Methods['queryOneWithWhereMethods'] ? MapQueryMethods<QueryOne<Query & QueryHasWhere>, Methods['queryOneWithWhereMethods'][K]> : K extends keyof Methods['queryWithWhereMethods'] ? MapQueryMethods<Query & QueryHasWhere, Methods['queryWithWhereMethods'][K]> : K extends keyof Methods['queryOneMethods'] ? MapQueryMethods<QueryOne<Query>, Methods['queryOneMethods'][K]> : K extends keyof Methods['queryMethods'] ? MapQueryMethods<Query, Methods['queryMethods'][K]> : never; };
909
1635
  type Repo<T extends Query, Methods extends MethodsBase<T>> = T & MapMethods<T, Methods>;
910
1636
  declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(table: T, methods: Methods) => Repo<(<Q extends {
911
- table: T["table"];
912
- shape: T["shape"];
1637
+ table: T['table'];
1638
+ shape: T['shape'];
913
1639
  }>(q: Q) => Query & Q & MapMethods<T, Methods>) & T, Methods>;
914
- export { BaseMaterializedViewInstance, BaseTableClass, BaseTableInstance, BaseViewInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidORMBundle, OrchidORMDbTables, OrchidORMTables, OrchidOrmParam, PickORMTableComputed, PickORMTableInputColumns, PickORMTableInputColumnsAndComputed, Queryable, Repo, Selectable, SetColumnsResult, Table, TableClass, TableClasses, TableInfo, TableQueryBuilder, Updatable, bundleOrchidORM, bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };
1640
+ export { BaseMaterializedViewInstance, BaseTableClass, BaseTableInstance, BaseViewInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidORMBundle, OrchidORMDbTables, OrchidORMSetupOptions, OrchidORMTables, OrchidOrmParam, OrmLegacyTableForTypeHelpers, OrmTable, OrmTableForTypeHelpers, OrmTableThunks, PickORMTableComputed, PickORMTableInputColumns, PickORMTableInputColumnsAndComputed, Queryable, Repo, Selectable, SetColumnsResult, Table$1 as Table, TableClass, TableInfo, TableInstance, TableQueryBuilder, Updatable, bundleOrchidORM, bundleOrchidORMTables, createBaseTable, createRepo, createTableFactory, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };