orchid-orm 1.76.5 → 1.77.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/bun.d.ts +4 -4
- package/dist/bun.js.map +1 -1
- package/dist/bun.mjs.map +1 -1
- package/dist/index.d.ts +871 -133
- package/dist/index.js +611 -32
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +612 -34
- package/dist/index.mjs.map +1 -1
- package/dist/migrations/index.js +104 -63
- package/dist/migrations/index.js.map +1 -1
- package/dist/migrations/index.mjs +106 -65
- package/dist/migrations/index.mjs.map +1 -1
- package/dist/node-postgres.d.ts +4 -4
- package/dist/node-postgres.js.map +1 -1
- package/dist/node-postgres.mjs.map +1 -1
- package/dist/postgres-js.d.ts +4 -4
- package/dist/postgres-js.js.map +1 -1
- package/dist/postgres-js.mjs.map +1 -1
- package/package.json +4 -4
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,
|
|
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
|
}
|
|
@@ -13,10 +13,10 @@ interface BelongsToOptions<Columns extends Column.Shape.QueryInit = Column.Shape
|
|
|
13
13
|
on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
|
|
14
14
|
}
|
|
15
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 {
|
|
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;
|
|
@@ -61,28 +61,29 @@ 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
|
|
71
|
+
interface RelationThroughOptions$1<Through extends PropertyKey = string> {
|
|
72
72
|
through: Through;
|
|
73
|
-
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 ?
|
|
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?: {
|
|
@@ -115,24 +116,24 @@ 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
|
|
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
|
|
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;
|
|
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;
|
|
127
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;
|
|
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 {
|
|
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:
|
|
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 ?
|
|
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
139
|
}> : never };
|
|
@@ -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,6 +173,7 @@ 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;
|
|
@@ -231,72 +233,678 @@ interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends str
|
|
|
231
233
|
add?: MaybeArray<WhereArg<Q>>;
|
|
232
234
|
};
|
|
233
235
|
}
|
|
234
|
-
|
|
235
|
-
|
|
236
|
-
|
|
237
|
-
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
247
|
-
|
|
248
|
-
|
|
249
|
-
|
|
250
|
-
|
|
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
|
-
|
|
254
|
-
|
|
255
|
-
|
|
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
|
-
|
|
271
|
-
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
277
|
-
|
|
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
|
-
|
|
280
|
-
|
|
281
|
-
|
|
282
|
-
|
|
283
|
-
|
|
284
|
-
type
|
|
285
|
-
|
|
286
|
-
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
|
|
297
|
-
|
|
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
|
-
|
|
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
|
-
|
|
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
|
-
} ?
|
|
340
|
-
type
|
|
341
|
-
|
|
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<
|
|
344
|
-
|
|
345
|
-
|
|
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,121 @@ 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
|
|
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
|
|
1163
|
+
declare const bundleOrchidORM: <T extends OrmTableThunks = EmptyObject, V extends OrmTableThunks = EmptyObject>({
|
|
511
1164
|
tables,
|
|
512
1165
|
views
|
|
513
1166
|
}: {
|
|
514
1167
|
tables?: T;
|
|
515
1168
|
views?: V;
|
|
516
1169
|
}) => OrchidORMBundle<T, V>;
|
|
517
|
-
declare const bundleOrchidORMTables: <T extends
|
|
518
|
-
declare const makeOrchidOrmDbWithAdapter: <T extends
|
|
1170
|
+
declare const bundleOrchidORMTables: <T extends OrmTableThunks>(tables: T) => OrchidORMBundle<T, EmptyObject>;
|
|
1171
|
+
declare const makeOrchidOrmDbWithAdapter: <T extends OrmTableThunks, V extends OrmTableThunks>(orm: OrchidORMBundle<T, V>, options: OrchidOrmParam<({
|
|
519
1172
|
db: Query;
|
|
520
1173
|
} | {
|
|
521
1174
|
adapter: Adapter;
|
|
522
1175
|
}) & DbSharedOptions>) => OrchidORM<T, V>;
|
|
523
|
-
declare const orchidORMWithAdapter: <T extends
|
|
1176
|
+
declare const orchidORMWithAdapter: <T extends OrmTableThunks, V extends OrmTableThunks = EmptyObject>(options: OrchidOrmParam<({
|
|
524
1177
|
db: Query;
|
|
525
1178
|
} | {
|
|
526
1179
|
adapter: Adapter;
|
|
527
1180
|
}) & OrchidORMSetupOptions<V>>, tables: T) => OrchidORM<T, V>;
|
|
1181
|
+
type RelationToOneDataForCreate<Rel extends {
|
|
1182
|
+
nestedCreateQuery: unknown;
|
|
1183
|
+
table: Query;
|
|
1184
|
+
}> = {
|
|
1185
|
+
create: Rel['nestedCreateQuery'];
|
|
1186
|
+
connect?: never;
|
|
1187
|
+
connectOrCreate?: never;
|
|
1188
|
+
} | {
|
|
1189
|
+
create?: never;
|
|
1190
|
+
connect: WhereArg<Rel['table']>;
|
|
1191
|
+
connectOrCreate?: never;
|
|
1192
|
+
} | {
|
|
1193
|
+
create?: never;
|
|
1194
|
+
connect?: never;
|
|
1195
|
+
connectOrCreate: {
|
|
1196
|
+
where: WhereArg<Rel['table']>;
|
|
1197
|
+
create: Rel['nestedCreateQuery'];
|
|
1198
|
+
};
|
|
1199
|
+
};
|
|
1200
|
+
interface RelationToOneDataForCreateSameQuerySelf extends CreateSelf, PickQuerySelectableRelations {}
|
|
1201
|
+
type RelationToOneDataForCreateSameQuery<Q extends RelationToOneDataForCreateSameQuerySelf> = {
|
|
1202
|
+
create: CreateData<Q>;
|
|
1203
|
+
connect?: never;
|
|
1204
|
+
connectOrCreate?: never;
|
|
1205
|
+
} | {
|
|
1206
|
+
create?: never;
|
|
1207
|
+
connect: WhereArg<Q>;
|
|
1208
|
+
connectOrCreate?: never;
|
|
1209
|
+
} | {
|
|
1210
|
+
create?: never;
|
|
1211
|
+
connect?: never;
|
|
1212
|
+
connectOrCreate: {
|
|
1213
|
+
where: WhereArg<Q>;
|
|
1214
|
+
create: CreateData<Q>;
|
|
1215
|
+
};
|
|
1216
|
+
};
|
|
1217
|
+
interface RelationThunkBase {
|
|
1218
|
+
type: string;
|
|
1219
|
+
id: string;
|
|
1220
|
+
options: unknown;
|
|
1221
|
+
}
|
|
1222
|
+
type RelationThunk = BelongsTo$1 | HasOne$1 | HasMany$1 | HasAndBelongsToMany$1;
|
|
1223
|
+
interface RelationThunks {
|
|
1224
|
+
[K: string]: RelationThunk;
|
|
1225
|
+
}
|
|
1226
|
+
type TableInstances<T extends OrmTableThunks> = { [K in keyof T]: TableInstance<T[K]> };
|
|
1227
|
+
type TableClassInstances<T extends OrmTableThunks> = TableInstances<T>[keyof T];
|
|
1228
|
+
type RelationToTableInputById<Tables, Id extends string> = Tables extends {
|
|
1229
|
+
id: Id;
|
|
1230
|
+
} ? Tables : never;
|
|
1231
|
+
type RelationToTableInputByName<T extends OrmTableThunks, Id extends string> = { [K in keyof TableInstances<T>]: TableInstances<T>[K] extends {
|
|
1232
|
+
table: Id;
|
|
1233
|
+
} | {
|
|
1234
|
+
name: Id;
|
|
1235
|
+
} ? TableInstances<T>[K] : never }[keyof T];
|
|
1236
|
+
type RelationToTableInputFromClasses<T extends OrmTableThunks, Id extends string> = RelationToTableInputById<TableClassInstances<T>, Id> extends infer Result ? [Result] extends [never] ? RelationToTableInputByName<T, Id> : Result : never;
|
|
1237
|
+
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;
|
|
1238
|
+
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;
|
|
1239
|
+
interface RelationConfigSelf {
|
|
1240
|
+
columns: {
|
|
1241
|
+
shape: Column.Shape.QueryInit;
|
|
1242
|
+
};
|
|
1243
|
+
relations: RelationThunks;
|
|
1244
|
+
}
|
|
1245
|
+
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
1246
|
interface TableClass<T extends ORMTableInput = ORMTableInput> {
|
|
529
1247
|
new (): T;
|
|
530
1248
|
}
|
|
531
|
-
interface TableClasses {
|
|
532
|
-
[K: string]: TableClass;
|
|
533
|
-
}
|
|
534
1249
|
interface TableInfo {
|
|
535
1250
|
definedAs: string;
|
|
536
1251
|
db: OrchidORM;
|
|
537
1252
|
getFilePath(): string;
|
|
538
1253
|
name: string;
|
|
539
1254
|
}
|
|
540
|
-
interface Table extends Query, TableInfo {}
|
|
541
|
-
type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo, Related> = Rel['options'] extends {
|
|
1255
|
+
interface Table$1 extends Query, TableInfo {}
|
|
1256
|
+
type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo$1, Related> = Rel['options'] extends {
|
|
542
1257
|
required: infer Required extends boolean;
|
|
543
1258
|
} ? Required : Rel['options'] extends {
|
|
544
1259
|
on: unknown;
|
|
545
1260
|
} ? false : BelongsToDefaultRequired<T, Rel, Related>;
|
|
546
|
-
type BelongsToRelationInfo<TC extends
|
|
547
|
-
type BelongsToCreateData<TC extends
|
|
548
|
-
type
|
|
549
|
-
type
|
|
550
|
-
type
|
|
551
|
-
type
|
|
552
|
-
type
|
|
553
|
-
type
|
|
554
|
-
|
|
555
|
-
|
|
1261
|
+
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;
|
|
1262
|
+
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;
|
|
1263
|
+
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;
|
|
1264
|
+
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;
|
|
1265
|
+
type RelationDataForCreateOptionalNames<T extends RelationConfigSelf> = { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo$1 ? never : K }[keyof T['relations'] & string];
|
|
1266
|
+
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];
|
|
1267
|
+
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;
|
|
1268
|
+
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> };
|
|
1269
|
+
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> };
|
|
1270
|
+
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> {
|
|
1271
|
+
relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: RelationInfoForName<TC, VC, T, K, T['relations'][K]> } : EmptyObject;
|
|
556
1272
|
relationsDataForCreate: T extends RelationConfigSelf ? BelongsToRelationsDataForCreate<TC, VC, T> : EmptyObject;
|
|
557
1273
|
relationsDataForCreateOptional: T extends RelationConfigSelf ? RelationsDataForCreateOptional<TC, VC, T> : EmptyObject;
|
|
558
1274
|
}
|
|
@@ -574,6 +1290,7 @@ interface ORMTableInput extends PickORMTableInputColumnsAndComputed {
|
|
|
574
1290
|
autoForeignKeys?: TableData.References.BaseOptions | boolean;
|
|
575
1291
|
schema?: QuerySchema;
|
|
576
1292
|
nameInDb?: string;
|
|
1293
|
+
snakeCase?: boolean;
|
|
577
1294
|
types: unknown;
|
|
578
1295
|
filePath: string;
|
|
579
1296
|
language?: string;
|
|
@@ -603,9 +1320,30 @@ interface ORMTableInput extends PickORMTableInputColumnsAndComputed {
|
|
|
603
1320
|
securityInvoker?: boolean;
|
|
604
1321
|
withData?: boolean;
|
|
605
1322
|
}
|
|
606
|
-
|
|
607
|
-
|
|
608
|
-
|
|
1323
|
+
interface OrmLegacyTableForTypeHelpers {
|
|
1324
|
+
columns: {
|
|
1325
|
+
shape: Column.Shape.QueryInit;
|
|
1326
|
+
};
|
|
1327
|
+
computed?: ComputedOptionsFactory<never, never>;
|
|
1328
|
+
}
|
|
1329
|
+
interface OrmTableForTypeHelpers {
|
|
1330
|
+
data: {
|
|
1331
|
+
columns: Column.Shape.QueryInit;
|
|
1332
|
+
computed?: ComputedOptionsFactory<never, never>;
|
|
1333
|
+
};
|
|
1334
|
+
}
|
|
1335
|
+
type AnyTableForTypeHelpers = OrmLegacyTableForTypeHelpers | OrmTableForTypeHelpers;
|
|
1336
|
+
type Queryable<T extends AnyTableForTypeHelpers> = T extends {
|
|
1337
|
+
columns: {
|
|
1338
|
+
shape: infer Shape extends Column.Shape.QueryInit;
|
|
1339
|
+
};
|
|
1340
|
+
} | {
|
|
1341
|
+
data: {
|
|
1342
|
+
columns: infer Shape extends Column.Shape.QueryInit;
|
|
1343
|
+
};
|
|
1344
|
+
} ? { [K in keyof Shape]?: Shape[K]['__queryType'] } : never;
|
|
1345
|
+
type DefaultSelect<T extends AnyTableForTypeHelpers> = T extends OrmTableForTypeHelpers ? ColumnsShape.DefaultOutput<T['data']['columns']> : T extends OrmLegacyTableForTypeHelpers ? ColumnsShape.DefaultOutput<T['columns']['shape']> : never;
|
|
1346
|
+
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
1347
|
result: {
|
|
610
1348
|
value: infer Value extends Column.Pick.QueryColumn;
|
|
611
1349
|
};
|
|
@@ -613,9 +1351,18 @@ type Selectable<T extends PickORMTableInputColumnsAndComputed> = T['computed'] e
|
|
|
613
1351
|
result: {
|
|
614
1352
|
value: infer Value extends Column.Pick.QueryColumn;
|
|
615
1353
|
};
|
|
616
|
-
}) ? Value['__outputType'] : never }
|
|
617
|
-
type
|
|
618
|
-
type
|
|
1354
|
+
}) ? Value['__outputType'] : never } : ColumnsShape.Output<Shape>;
|
|
1355
|
+
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;
|
|
1356
|
+
type Insertable<T extends AnyTableForTypeHelpers> = T extends {
|
|
1357
|
+
columns: {
|
|
1358
|
+
shape: infer Shape extends Column.Shape.QueryInit;
|
|
1359
|
+
};
|
|
1360
|
+
} | {
|
|
1361
|
+
data: {
|
|
1362
|
+
columns: infer Shape extends Column.Shape.QueryInit;
|
|
1363
|
+
};
|
|
1364
|
+
} ? ColumnsShape.Input<Shape> : never;
|
|
1365
|
+
type Updatable<T extends AnyTableForTypeHelpers> = T extends OrmTableForTypeHelpers ? ColumnsShape.InputPartial<T['data']['columns']> : T extends OrmLegacyTableForTypeHelpers ? ColumnsShape.InputPartial<T['columns']['shape']> : never;
|
|
619
1366
|
type BeforeHookMethod = (cb: QueryBeforeHook) => void;
|
|
620
1367
|
type BeforeActionHookMethod = (cb: QueryBeforeActionHook) => void;
|
|
621
1368
|
type AfterHookMethod = (cb: QueryAfterHook) => void;
|
|
@@ -773,7 +1520,7 @@ interface BaseTableInstance<ColumnTypes> {
|
|
|
773
1520
|
id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
|
|
774
1521
|
options: Options;
|
|
775
1522
|
};
|
|
776
|
-
hasOne<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string,
|
|
1523
|
+
hasOne<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Options extends HasOneOptions<Columns, Related, Through>>(this: {
|
|
777
1524
|
columns: {
|
|
778
1525
|
shape: Columns;
|
|
779
1526
|
};
|
|
@@ -784,7 +1531,7 @@ interface BaseTableInstance<ColumnTypes> {
|
|
|
784
1531
|
id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
|
|
785
1532
|
options: Options;
|
|
786
1533
|
};
|
|
787
|
-
hasMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string,
|
|
1534
|
+
hasMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Through extends string, Options extends HasOneOptions<Columns, Related, Through>>(this: {
|
|
788
1535
|
columns: {
|
|
789
1536
|
shape: Columns;
|
|
790
1537
|
};
|
|
@@ -795,7 +1542,7 @@ interface BaseTableInstance<ColumnTypes> {
|
|
|
795
1542
|
id: Related['id'] extends string ? Related['id'] : Related['table'] extends string ? Related['table'] : Related['name'];
|
|
796
1543
|
options: Options;
|
|
797
1544
|
};
|
|
798
|
-
hasAndBelongsToMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Options extends HasAndBelongsToManyOptions<Columns, Related>>(this: {
|
|
1545
|
+
hasAndBelongsToMany<Columns extends Column.Shape.QueryInit, Related extends ORMTableInput, Options extends HasAndBelongsToManyOptions$1<Columns, Related>>(this: {
|
|
799
1546
|
columns: {
|
|
800
1547
|
shape: Columns;
|
|
801
1548
|
};
|
|
@@ -885,16 +1632,7 @@ declare function createBaseTable<SchemaConfig extends ColumnSchemaConfig = Defau
|
|
|
885
1632
|
exportAs,
|
|
886
1633
|
language,
|
|
887
1634
|
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>;
|
|
1635
|
+
}?: TableFactoryOptions<SchemaConfig, ColumnTypes>): BaseTableClass<SchemaConfig, ColumnTypes>;
|
|
898
1636
|
type QueryMethods<T extends Query> = Record<string, (q: T, ...args: any[]) => any>;
|
|
899
1637
|
type QueryOne<T extends Query> = { [K in keyof T]: K extends 'returnType' ? Exclude<QueryReturnType, 'all' | undefined> : T[K] };
|
|
900
1638
|
interface MethodsBase<T extends Query> {
|
|
@@ -911,4 +1649,4 @@ declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(tabl
|
|
|
911
1649
|
table: T["table"];
|
|
912
1650
|
shape: T["shape"];
|
|
913
1651
|
}>(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,
|
|
1652
|
+
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 };
|