orchid-orm 1.73.1 → 1.75.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,67 +1,74 @@
1
- import { Adapter, AfterCommitStandaloneHook, AfterHook, Column, ColumnSchemaConfig, ColumnsShape, ComputedColumnsFromOptions, ComputedOptionsConfig, ComputedOptionsFactory, CreateData, CreateManyMethodsNames, CreateMethodsNames, CreateSelf, DbSharedOptions, DbSqlMethod, DbTableOptionScopes, DefaultColumnTypes, DefaultSchemaConfig, DeleteMethodsNames, EmptyObject, FromArg, FromResult, Grant, IsQuery, IsolationLevel, JoinQueryMethod, MapTableScopesOption, MaybeArray, MergeQuery, PickQuerySelectableRelations, QueryAfterHook, QueryBeforeActionHook, QueryBeforeHook, QueryData, QueryHasWhere, QueryReturnType, QuerySchema, QueryScopes, RawSqlBase, RecordUnknown, RelationConfigBase, Rls, SelectableFromShape, ShallowSimplify, ShapeColumnPrimaryKeys, ShapeUniqueColumns, StorageOptions, TableData, TableDataFn, TableDataItem, TableDataItemsUniqueColumnTuples, TableDataItemsUniqueColumns, TableDataItemsUniqueConstraints, TransactionOptions, UniqueConstraints, 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, QueryReturnType, QuerySchema, QueryScopes, RawSqlBase, RecordUnknown, RelationConfigBase, Rls, SelectableFromShape, ShallowSimplify, StorageOptions, TableData, TableDataFn, TableDataItem, TransactionOptions, UpdateData, WhereArg } from "pqb/internal";
2
2
  import { Db, Query } from "pqb";
3
3
  export * from "pqb";
4
- interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
4
+ interface BelongsTo extends RelationThunkBase {
5
+ type: 'belongsTo';
6
+ options: BelongsToOptions;
7
+ }
8
+ interface BelongsToOptions<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput> {
5
9
  required?: boolean;
6
- columns: Column[];
7
- references: (keyof Shape)[];
10
+ columns: (keyof Columns)[];
11
+ references: (keyof Related['columns']['shape'])[];
8
12
  foreignKey?: boolean | TableData.References.Options;
9
- on?: ColumnsShape.InputPartial<Shape>;
10
- }
11
- interface RelationThroughOptions<Through extends PropertyKey = string, Source extends PropertyKey = string> {
12
- through: Through;
13
- source: Source;
14
- }
15
- interface HasOne extends RelationThunkBase {
16
- type: 'hasOne';
17
- options: HasOneOptions;
13
+ on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
18
14
  }
19
- interface RelationHasOneThroughOptions<Through extends string, Source extends string> extends RelationThroughOptions<Through, Source> {
20
- required?: boolean;
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> = Rel['related']['softDelete'] extends true | string ? false : { [K in Rel['options']['columns'][number] & string]: T['columns']['shape'][K]['data']['isNullable'] extends true ? false : true }[Rel['options']['columns'][number] & string] extends true ? true : false;
17
+ 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;
18
+ interface BelongsToDataForCreate<Name extends string, FK extends string, Required, Q extends Query> {
19
+ columns: FK;
20
+ nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: never };
21
21
  }
22
- 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>;
23
- 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;
24
- 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 & HasRelJoin;
25
- 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 & HasRelJoin : HasOneQueryThrough<Name, TableQuery>;
26
- interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasOne, Q extends Query> extends RelationConfigBase {
22
+ interface BelongsToInfo<T extends RelationConfigSelf, FK extends string, Required, Q extends Query> extends RelationConfigBase {
27
23
  returnsOne: true;
28
- required: T['relations'][Name]['options']['required'];
24
+ required: Required;
29
25
  query: Q;
30
- params: HasOneParams<T, Rel['options']>;
31
- omitForeignKeyInCreate: never;
32
- dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : RelationToOneDataForCreate<{
33
- nestedCreateQuery: CreateData<Q>;
34
- table: Q;
35
- }> : never };
36
- dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
26
+ params: BelongsToParams<T, FK>;
27
+ omitForeignKeyInCreate: FK;
28
+ dataForUpdate: {
37
29
  disconnect: boolean;
38
30
  } | {
31
+ set: WhereArg<Q>;
32
+ } | (Q extends Query.Pick.IsNotReadOnly ? {
39
33
  delete: boolean;
40
34
  } | {
41
35
  update: UpdateData<Q>;
42
- } : never;
43
- dataForUpdateOne: Q extends Query.Pick.IsNotReadOnly ? {
36
+ } | {
37
+ create: CreateData<Q>;
38
+ } : never);
39
+ dataForUpdateOne: {
44
40
  disconnect: boolean;
45
41
  } | {
46
42
  set: WhereArg<Q>;
47
- } | {
43
+ } | (Q extends Query.Pick.IsNotReadOnly ? {
48
44
  delete: boolean;
49
45
  } | {
50
46
  update: UpdateData<Q>;
47
+ } | {
48
+ create: CreateData<Q>;
51
49
  } | {
52
50
  upsert: {
53
51
  update: UpdateData<Q>;
54
52
  create: CreateData<Q> | (() => CreateData<Q>);
55
53
  };
56
- } | {
57
- create: CreateData<Q>;
58
- } : never;
54
+ } : never);
55
+ }
56
+ interface RelationRefsOptions<Column extends PropertyKey = string, Shape extends Column.Shape.QueryInit = Column.Shape.QueryInit> {
57
+ required?: boolean;
58
+ columns: Column[];
59
+ references: (keyof Shape)[];
60
+ foreignKey?: boolean | TableData.References.Options;
61
+ on?: ColumnsShape.InputPartial<Shape>;
62
+ }
63
+ interface RelationThroughOptions<Through extends PropertyKey = string, Source extends PropertyKey = string> {
64
+ through: Through;
65
+ source: Source;
59
66
  }
60
67
  interface HasMany extends RelationThunkBase {
61
68
  type: 'hasMany';
62
69
  options: HasOneOptions;
63
70
  }
64
- 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 & HasRelJoin : HasOneQueryThrough<Name, TableQuery>;
71
+ 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>;
65
72
  interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasMany, Q extends Query> extends RelationConfigBase {
66
73
  returnsOne: false;
67
74
  query: Q;
@@ -100,63 +107,50 @@ interface HasManyInfo<T extends RelationConfigSelf, Name extends string, Rel ext
100
107
  };
101
108
  } : never;
102
109
  }
103
- interface RelJoin extends JoinQueryMethod {
104
- <T extends Query>(this: T): T;
105
- }
106
- interface HasRelJoin {
107
- join: RelJoin;
108
- }
109
- interface BelongsTo extends RelationThunkBase {
110
- type: 'belongsTo';
111
- options: BelongsToOptions;
110
+ interface HasOne extends RelationThunkBase {
111
+ type: 'hasOne';
112
+ options: HasOneOptions;
112
113
  }
113
- interface BelongsToOptions<Columns extends Column.Shape.QueryInit = Column.Shape.QueryInit, Related extends ORMTableInput = ORMTableInput> {
114
+ interface RelationHasOneThroughOptions<Through extends string, Source extends string> extends RelationThroughOptions<Through, Source> {
114
115
  required?: boolean;
115
- columns: (keyof Columns)[];
116
- references: (keyof Related['columns']['shape'])[];
117
- foreignKey?: boolean | TableData.References.Options;
118
- on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
119
116
  }
120
- type BelongsToParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type'] };
121
- type BelongsToColumnsRequired<T extends RelationConfigSelf, FK extends string> = { [K in FK]: T['columns']['shape'][K]['data']['isNullable'] extends true ? false : true }[FK] extends true ? true : false;
122
- 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 & HasRelJoin;
123
- interface BelongsToDataForCreate<Name extends string, FK extends string, Required, Q extends Query> {
124
- columns: FK;
125
- nested: Q extends Query.Pick.IsNotReadOnly ? Required extends true ? { [Key in Name]: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: RelationToOneDataForCreateSameQuery<Q> } : { [Key in Name]?: never };
126
- }
127
- interface BelongsToInfo<T extends RelationConfigSelf, FK extends string, Required, Q extends Query> extends RelationConfigBase {
117
+ 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>;
118
+ 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;
119
+ 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;
120
+ 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>;
121
+ interface HasOneInfo<T extends RelationConfigSelf, Name extends string, Rel extends HasOne, Q extends Query> extends RelationConfigBase {
128
122
  returnsOne: true;
129
- required: Required;
123
+ required: T['relations'][Name]['options']['required'];
130
124
  query: Q;
131
- params: BelongsToParams<T, FK>;
132
- omitForeignKeyInCreate: FK;
133
- dataForUpdate: {
125
+ params: HasOneParams<T, Rel['options']>;
126
+ omitForeignKeyInCreate: never;
127
+ dataForCreate: { [K in Name]?: Q extends Query.Pick.IsNotReadOnly ? T['relations'][Name]['options'] extends RelationThroughOptions ? EmptyObject : RelationToOneDataForCreate<{
128
+ nestedCreateQuery: CreateData<Q>;
129
+ table: Q;
130
+ }> : never };
131
+ dataForUpdate: Q extends Query.Pick.IsNotReadOnly ? {
134
132
  disconnect: boolean;
135
133
  } | {
136
- set: WhereArg<Q>;
137
- } | (Q extends Query.Pick.IsNotReadOnly ? {
138
134
  delete: boolean;
139
135
  } | {
140
136
  update: UpdateData<Q>;
141
- } | {
142
- create: CreateData<Q>;
143
- } : never);
144
- dataForUpdateOne: {
137
+ } : never;
138
+ dataForUpdateOne: Q extends Query.Pick.IsNotReadOnly ? {
145
139
  disconnect: boolean;
146
140
  } | {
147
141
  set: WhereArg<Q>;
148
- } | (Q extends Query.Pick.IsNotReadOnly ? {
142
+ } | {
149
143
  delete: boolean;
150
144
  } | {
151
145
  update: UpdateData<Q>;
152
- } | {
153
- create: CreateData<Q>;
154
146
  } | {
155
147
  upsert: {
156
148
  update: UpdateData<Q>;
157
149
  create: CreateData<Q> | (() => CreateData<Q>);
158
150
  };
159
- } : never);
151
+ } | {
152
+ create: CreateData<Q>;
153
+ } : never;
160
154
  }
161
155
  interface HasAndBelongsToMany extends RelationThunkBase {
162
156
  type: 'hasAndBelongsToMany';
@@ -177,7 +171,7 @@ interface HasAndBelongsToManyOptions<Columns extends Column.Shape.QueryInit = Co
177
171
  on?: ColumnsShape.InputPartial<Related['columns']['shape']>;
178
172
  }
179
173
  type HasAndBelongsToManyParams<T extends RelationConfigSelf, FK extends string> = { [Name in FK]: T['columns']['shape'][Name]['__type'] };
180
- 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 & HasRelJoin;
174
+ 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;
181
175
  interface HasAndBelongsToManyInfo<T extends RelationConfigSelf, Name extends string, FK extends string, Q extends Query> extends RelationConfigBase {
182
176
  returnsOne: false;
183
177
  query: Q;
@@ -276,7 +270,7 @@ interface RelationThunks {
276
270
  }
277
271
  type RelationTableToQuery<Relation> = Relation extends {
278
272
  related: infer R extends ORMTableInput;
279
- } ? TableToDb<R, R['table'] extends string ? R['table'] : R['name'], R['table'] extends string ? R['readOnly'] extends true ? true : undefined : R['readOnly'] extends false ? undefined : true> : never;
273
+ } ? TableQueryBuilder<R> : never;
280
274
  interface RelationConfigSelf {
281
275
  columns: {
282
276
  shape: Column.Shape.QueryInit;
@@ -323,19 +317,19 @@ interface OrchidORMTableHelper<T extends Query> {
323
317
  }
324
318
  type OrchidORMTables<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
325
319
  new (): infer R extends ORMTableInput;
326
- } ? OrchidORMTableHelper<TableToDb<R, R['table'], R['readOnly'] extends true ? true : undefined>> : never };
320
+ } ? OrchidORMTableHelper<TableQueryBuilder<R>> : never };
327
321
  type OrchidORMViews<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
328
322
  new (): infer R extends ORMTableInput;
329
- } ? OrchidORMTableHelper<TableToDb<R, R['name'], R['readOnly'] extends false ? undefined : true, R['materialized'] extends true ? true : undefined>> : never };
323
+ } ? OrchidORMTableHelper<TableQueryBuilder<R>> : never };
330
324
  type OrchidORMBundle<T extends TableClasses = TableClasses, V extends TableClasses = TableClasses> = OrchidORMTables<T> & {
331
325
  $views: OrchidORMViews<V>;
332
326
  };
333
327
  type OrchidORMDbTables<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
334
328
  new (): infer R extends ORMTableInput;
335
- } ? TableToDb<R, R['table'], R['readOnly'] extends true ? true : undefined> : never };
329
+ } ? TableQueryBuilder<R> : never };
336
330
  type OrchidORMDbViews<T extends TableClasses = TableClasses> = { [K in keyof T]: T[K] extends {
337
331
  new (): infer R extends ORMTableInput;
338
- } ? TableToDb<R, R['name'], R['readOnly'] extends false ? undefined : true, R['materialized'] extends true ? true : undefined> : never };
332
+ } ? TableQueryBuilder<R> : never };
339
333
  type OrchidORM<T extends TableClasses = TableClasses, V extends TableClasses = TableClasses> = OrchidORMDbTables<T> & {
340
334
  $views: OrchidORMDbViews<V>;
341
335
  } & OrchidORMMethods;
@@ -535,7 +529,7 @@ type BelongsToRequired<T extends RelationConfigSelf, Rel extends BelongsTo> = Re
535
529
  required: infer Required extends boolean;
536
530
  } ? Required : Rel['options'] extends {
537
531
  on: unknown;
538
- } ? false : BelongsToColumnsRequired<T, Rel['options']['columns'][number] & string>;
532
+ } ? false : BelongsToDefaultRequired<T, Rel>;
539
533
  type BelongsToRelationInfo<T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = BelongsToInfo<T, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel>, BelongsToQuery<RelationTableToQuery<Rel>, Name>>;
540
534
  type BelongsToCreateData<T extends RelationConfigSelf, Name extends string, Rel extends BelongsTo> = BelongsToDataForCreate<Name, Rel['options']['columns'][number] & string, BelongsToRequired<T, Rel>, BelongsToQuery<RelationTableToQuery<Rel>, Name>>;
541
535
  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']];
@@ -544,7 +538,7 @@ type RelationDataForCreate<T extends RelationConfigSelf, Name extends keyof T['r
544
538
  type RelationDataForCreateOptionalNames<T extends RelationConfigSelf> = { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? never : K }[keyof T['relations'] & string];
545
539
  type BelongsToRelationsDataForCreate<T extends RelationConfigSelf> = { [Column in BelongsToCreateDataColumns<T>]: BelongsToCreateDataForColumn<T, Column> };
546
540
  type RelationsDataForCreateOptional<T extends RelationConfigSelf> = RelationDataForCreateOptionalNames<T> extends never ? EmptyObject : { [K in RelationDataForCreateOptionalNames<T>]: (u: RelationDataForCreate<T, K>) => void }[RelationDataForCreateOptionalNames<T>] extends ((u: infer Obj) => void) ? Obj : EmptyObject;
547
- interface TableToDb<T extends ORMTableInput, Name extends string | undefined, ReadOnly extends true | undefined, Materialized extends true | undefined = undefined> extends TableInfo, Db<Name, T['columns']['shape'], keyof ShapeColumnPrimaryKeys<T['columns']['shape']> extends never ? never : ShapeColumnPrimaryKeys<T['columns']['shape']>, ShapeUniqueColumns<T['columns']['shape']> | TableDataItemsUniqueColumns<T['columns']['shape'], T['columns']['data']>, TableDataItemsUniqueColumnTuples<T['columns']['shape'], T['columns']['data']>, UniqueConstraints<T['columns']['shape']> | TableDataItemsUniqueConstraints<T['columns']['data']>, T['types'], T['columns']['shape'] & ComputedColumnsFromOptions<T['computed']>, MapTableScopesOption<T>, ColumnsShape.DefaultSelectKeys<T['columns']['shape']>, ReadOnly, Materialized> {
541
+ interface TableQueryBuilder<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> {
548
542
  relations: T extends RelationConfigSelf ? { [K in keyof T['relations'] & string]: T['relations'][K] extends BelongsTo ? BelongsToRelationInfo<T, K, T['relations'][K]> : T['relations'][K] extends HasOne ? HasOneInfo<T, K, T['relations'][K], HasOneQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasMany ? HasManyInfo<T, K, T['relations'][K], HasManyQuery<T, K, RelationTableToQuery<T['relations'][K]>>> : T['relations'][K] extends HasAndBelongsToMany ? HasAndBelongsToManyInfo<T, K, T['relations'][K]['options']['columns'][number] & string, HasAndBelongsToManyQuery<K, RelationTableToQuery<T['relations'][K]>>> : never } : EmptyObject;
549
543
  relationsDataForCreate: T extends RelationConfigSelf ? BelongsToRelationsDataForCreate<T> : EmptyObject;
550
544
  relationsDataForCreateOptional: T extends RelationConfigSelf ? RelationsDataForCreateOptional<T> : EmptyObject;
@@ -632,6 +626,7 @@ interface BaseTableInstance<ColumnTypes> {
632
626
  q: QueryData;
633
627
  language?: string;
634
628
  filePath: string;
629
+ materialized?: true;
635
630
  /**
636
631
  * Keep this table-like definition available at runtime, but exclude it from
637
632
  * generated migration DDL reconciliation.
@@ -897,4 +892,4 @@ declare const createRepo: <T extends Query, Methods extends MethodsBase<T>>(tabl
897
892
  table: T["table"];
898
893
  shape: T["shape"];
899
894
  }>(q: Q) => Query & Q & MapMethods<T, Methods>) & T, Methods>;
900
- export { BaseMaterializedViewInstance, BaseTableClass, BaseTableInstance, BaseViewInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidORMBundle, OrchidORMDbTables, OrchidORMDbViews, OrchidORMTableHelper, OrchidORMTables, OrchidORMViews, OrchidOrmParam, PickORMTableComputed, PickORMTableInputColumns, PickORMTableInputColumnsAndComputed, Queryable, Repo, Selectable, SetColumnsResult, Table, TableClass, TableClasses, TableInfo, TableToDb, Updatable, bundleOrchidORM, bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };
895
+ export { BaseMaterializedViewInstance, BaseTableClass, BaseTableInstance, BaseViewInstance, DefaultSelect, FromQuery, Insertable, MapMethods, MapQueryMethods, MethodsBase, ORMTableInput, OrchidORM, OrchidORMBundle, OrchidORMDbTables, OrchidORMDbViews, OrchidORMTables, OrchidORMViews, OrchidOrmParam, PickORMTableComputed, PickORMTableInputColumns, PickORMTableInputColumnsAndComputed, Queryable, Repo, Selectable, SetColumnsResult, Table, TableClass, TableClasses, TableInfo, TableQueryBuilder, Updatable, bundleOrchidORM, bundleOrchidORMTables, createBaseTable, createRepo, defineRls, makeOrchidOrmDbWithAdapter, orchidORMWithAdapter, setGrants };
package/dist/index.js CHANGED
@@ -209,7 +209,7 @@ function joinHasThrough(q, baseQuery, joiningQuery, throughRelation, sourceRelat
209
209
  function joinHasRelation(baseQuery, joiningQuery, primaryKeys, foreignKeys, len) {
210
210
  const baseAs = (0, pqb_internal.getQueryAs)(baseQuery);
211
211
  const q = joiningQuery.clone();
212
- (0, pqb_internal.setQueryObjectValueImmutable)(q, "joinedShapes", baseAs, baseQuery.q.shape);
212
+ (0, pqb_internal.setQueryObjectValueImmutable)(q, "joinedShapes", baseAs, baseQuery.q.selectShape);
213
213
  for (let i = 0; i < len; i++) (0, pqb_internal.pushQueryOnForOuter)(q, baseQuery, joiningQuery, foreignKeys[i], `${baseAs}.${primaryKeys[i]}`);
214
214
  return q;
215
215
  }
@@ -410,9 +410,10 @@ var BelongsToVirtualColumn = class extends pqb_internal.VirtualColumn {
410
410
  this.nestedUpdate(queryForUpdate, set, data);
411
411
  }
412
412
  };
413
- const getBelongsToRequired = (tableConfig, relation) => {
413
+ const getBelongsToRequired = (tableConfig, relatedTableConfig, relation) => {
414
414
  const { required } = relation.options;
415
415
  if (typeof required === "boolean") return required;
416
+ if (relatedTableConfig.softDelete) return false;
416
417
  return relation.options.columns.every((key) => {
417
418
  return !tableConfig.columns.shape[key].data.isNullable;
418
419
  });
@@ -437,7 +438,7 @@ const makeBelongsToMethod = (tableConfig, table, relation, relationName, query)
437
438
  const join = (baseQuery, joiningQuery, primaryKeys, foreignKeys) => {
438
439
  const baseAs = (0, pqb_internal.getQueryAs)(baseQuery);
439
440
  const q = joiningQuery.clone();
440
- (0, pqb_internal.setQueryObjectValueImmutable)(q, "joinedShapes", baseAs, baseQuery.q.shape);
441
+ (0, pqb_internal.setQueryObjectValueImmutable)(q, "joinedShapes", baseAs, baseQuery.q.selectShape);
441
442
  for (let i = 0; i < len; i++) (0, pqb_internal.pushQueryOnForOuter)(q, baseQuery, joiningQuery, primaryKeys[i], `${baseAs}.${foreignKeys[i]}`);
442
443
  return q;
443
444
  };
@@ -1129,7 +1130,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1129
1130
  const foreignAs = (0, pqb_internal.getQueryAs)(joiningQuery);
1130
1131
  return joinQuery(baseQuery, (0, pqb_internal.getQueryAs)(baseQuery), foreignAs, {
1131
1132
  ...baseQuery.q.joinedShapes,
1132
- [foreignAs]: joiningQuery.q.shape
1133
+ [foreignAs]: joiningQuery.q.selectShape
1133
1134
  });
1134
1135
  };
1135
1136
  return {
@@ -1147,7 +1148,7 @@ const makeHasAndBelongsToManyMethod = (tableConfig, table, qb, relation, relatio
1147
1148
  virtualColumn: new HasAndBelongsToManyVirtualColumn(subQuery, pqb_internal.internalSchemaConfig, relationName, state),
1148
1149
  joinQuery: joinQueryChainHOF((0, pqb_internal.getPrimaryKeys)(query), reverseJoin, (joiningQuery, baseQuery) => joinQuery(joiningQuery, (0, pqb_internal.getQueryAs)(baseQuery), (0, pqb_internal.getQueryAs)(joiningQuery), {
1149
1150
  ...joiningQuery.q.joinedShapes,
1150
- [baseQuery.q.as || baseQuery.table]: baseQuery.q.shape
1151
+ [baseQuery.q.as || baseQuery.table]: baseQuery.q.selectShape
1151
1152
  })),
1152
1153
  reverseJoin,
1153
1154
  modifyRelatedQuery(relationQuery) {
@@ -1359,6 +1360,7 @@ const applyRelations = (qb, tables, result, schema) => {
1359
1360
  const data = {
1360
1361
  relationName,
1361
1362
  relation,
1363
+ otherTable: otherTable[1],
1362
1364
  dbTable,
1363
1365
  otherDbTable
1364
1366
  };
@@ -1402,7 +1404,7 @@ const delayRelation = (delayedRelations, table, relationName, data) => {
1402
1404
  if (tableRelations[relationName]) tableRelations[relationName].push(data);
1403
1405
  else tableRelations[relationName] = [data];
1404
1406
  };
1405
- const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTable }, delayedRelations, schema) => {
1407
+ const applyRelation = (table, qb, { relationName, relation, otherTable, dbTable, otherDbTable }, delayedRelations, schema) => {
1406
1408
  const baseQuery = Object.create(otherDbTable);
1407
1409
  baseQuery.baseQuery = baseQuery;
1408
1410
  const query = baseQuery.as(relationName);
@@ -1415,21 +1417,12 @@ const applyRelation = (table, qb, { relationName, relation, dbTable, otherDbTabl
1415
1417
  else if (type === "hasAndBelongsToMany") data = makeHasAndBelongsToManyMethod(table, dbTable, qb, relation, relationName, query, schema);
1416
1418
  else throw new Error(`Unknown relation type ${type}`);
1417
1419
  if (data.returns === "one") {
1418
- if (type === "belongsTo" ? getBelongsToRequired(table, relation) : relation.options.required) (0, pqb_internal._queryTake)(query);
1420
+ if (type === "belongsTo" ? getBelongsToRequired(table, otherTable, relation) : relation.options.required) (0, pqb_internal._queryTake)(query);
1419
1421
  else (0, pqb_internal._queryTakeOptional)(query);
1420
1422
  query.q.returnsOne = true;
1421
1423
  }
1422
- if (data.virtualColumn) dbTable.shape[relationName] = dbTable.q.shape[relationName] = data.virtualColumn;
1424
+ if (data.virtualColumn) dbTable.shape[relationName] = dbTable.q.selectShape[relationName] = data.virtualColumn;
1423
1425
  baseQuery.joinQuery = data.joinQuery;
1424
- const { join: originalJoin } = baseQuery;
1425
- baseQuery.join = function(...args) {
1426
- if (args.length) return originalJoin.apply(this, args);
1427
- else {
1428
- const q = this.clone();
1429
- q.q.innerJoinLateral = true;
1430
- return q;
1431
- }
1432
- };
1433
1426
  dbTable.relations[relationName] = {
1434
1427
  table: otherDbTable,
1435
1428
  query,