pqb 0.67.0 → 0.67.2

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
@@ -2696,11 +2696,172 @@ interface AggregateOptions<T extends PickQuerySelectableResultRelationsWindows>
2696
2696
  over?: Over<T>;
2697
2697
  }
2698
2698
  type Over<T extends PickQuerySelectableResultWindows> = keyof T['windows'] | WindowArgDeclaration<T>;
2699
- type QueryGetSelf = PickQuerySelectable;
2700
- type GetArg<T extends QueryGetSelf> = GetStringArg<T> | Expression;
2699
+ interface ColumnsShape {
2700
+ [K: string]: Column;
2701
+ }
2702
+ declare namespace ColumnsShape {
2703
+ export type DefaultSelectKeys<S extends Column.QueryColumnsInit> = { [K in keyof S]: S[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof S];
2704
+ export type DefaultOutput<Set extends Column.QueryColumnsInit> = { [K in DefaultSelectKeys<Set>]: Set[K]['outputType'] };
2705
+ export type Input<Shape extends Column.QueryColumnsInit, AppReadOnly = { [K in keyof Shape]: Shape[K]['data']['appReadOnly'] extends true ? K : never }[keyof Shape], Optional extends keyof Shape = { [K in keyof Shape]: Shape[K]['data']['optional'] extends true ? K : never }[keyof Shape]> = { [K in Exclude<keyof Shape, AppReadOnly | Optional>]: Shape[K]['inputType'] } & { [K in Exclude<Optional, AppReadOnly>]?: Shape[K]['inputType'] };
2706
+ export type InputPartial<Shape extends Column.QueryColumnsInit> = { [K in keyof Shape]?: Shape[K]['inputType'] };
2707
+ export type Output<Shape extends Column.QueryColumns> = { [K in keyof Shape]: Shape[K]['outputType'] };
2708
+ export type DefaultSelectOutput<Shape extends Column.QueryColumnsInit> = { [K in { [K in keyof Shape]: Shape[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof Shape]]: Shape[K]['outputType'] };
2709
+ export interface MapToObjectColumn<Shape extends Column.QueryColumns> {
2710
+ dataType: 'object';
2711
+ type: { [K in keyof Shape]: Shape[K]['type'] };
2712
+ outputType: ShallowSimplify<ObjectOutput<Shape>>;
2713
+ queryType: { [K in keyof Shape]: Shape[K]['queryType'] };
2714
+ operators: OperatorsAny;
2715
+ }
2716
+ export interface MapToNullableObjectColumn<Shape extends Column.QueryColumns> {
2717
+ dataType: 'object';
2718
+ type: { [K in keyof Shape]: Shape[K]['type'] };
2719
+ outputType: ShallowSimplify<ObjectOutput<Shape>> | undefined;
2720
+ queryType: { [K in keyof Shape]: Shape[K]['queryType'] } | null;
2721
+ operators: OperatorsAny;
2722
+ }
2723
+ export interface MapToPluckColumn<Shape extends Column.QueryColumns> {
2724
+ dataType: 'array';
2725
+ type: Shape['pluck']['type'][];
2726
+ outputType: Shape['pluck']['outputType'][];
2727
+ queryType: Shape['pluck']['queryType'][];
2728
+ operators: OperatorsAny;
2729
+ }
2730
+ export interface MapToObjectArrayColumn<Shape extends Column.QueryColumns> {
2731
+ dataType: 'array';
2732
+ type: { [K in keyof Shape]: Shape[K]['type'] }[];
2733
+ outputType: ShallowSimplify<ObjectOutput<Shape>>[];
2734
+ queryType: { [K in keyof Shape]: Shape[K]['queryType'] }[];
2735
+ operators: OperatorsAny;
2736
+ }
2737
+ type ObjectOutput<Shape extends Column.QueryColumns> = { [K in keyof Shape]: Shape[K]['outputType'] };
2738
+ export {};
2739
+ }
2740
+ interface SelectSelf extends PickQuerySelectable, PickQueryHasSelect, PickQueryDefaultSelect, PickQueryShape, PickQueryRelations, PickQueryResult, PickQueryReturnType, PickQueryWithData {}
2741
+ type SelectArgs<T extends SelectSelf> = ('*' | keyof T['__selectable'])[];
2742
+ interface SubQueryAddition<T extends PickQueryWithData> extends IsSubQuery {
2743
+ withData: T['withData'];
2744
+ }
2745
+ type SelectAsFnArg<T extends PickQueryRelationsWithData> = EmptyObject extends T['relations'] ? T : { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['maybeSingle'] & SubQueryAddition<T> : K extends keyof T ? T[K] : never };
2746
+ interface SelectAsArg<T extends SelectSelf> {
2747
+ [K: string]: keyof T['__selectable'] | Expression | ((q: SelectAsFnArg<T>) => unknown);
2748
+ }
2749
+ type SelectAsFnReturnType = {
2750
+ result: Column.QueryColumns;
2751
+ returnType: Exclude<QueryReturnType, 'rows'>;
2752
+ } | Expression;
2753
+ interface SelectAsCheckReturnTypes {
2754
+ [K: string]: PropertyKey | Expression | ((q: never) => SelectAsFnReturnType);
2755
+ }
2756
+ type SelectReturnType<T extends PickQueryReturnType> = T['returnType'] extends 'valueOrThrow' ? 'oneOrThrow' : T extends 'value' ? 'one' : T['returnType'] extends 'pluck' ? 'all' : T['returnType'];
2757
+ type SelectResult<T extends SelectSelf, Columns extends PropertyKey[]> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends 'result' ? { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
2758
+ type SelectResultObj<T extends SelectSelf, Obj> = Obj extends SelectAsCheckReturnTypes ? { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never } : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never }> : T[K] } : `Invalid return type of ${{ [K in keyof Obj]: Obj[K] extends ((...args: any[]) => any) ? ReturnType<Obj[K]> extends SelectAsFnReturnType ? never : K : never }[keyof Obj] & string}`;
2759
+ type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey[], Obj> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? // Combine previously selected items, all columns if * was provided,
2760
+ { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
2761
+ interface AllowedRelationOneQueryForSelectable extends IsSubQuery {
2762
+ result: Column.QueryColumns;
2763
+ returnType: 'value' | 'valueOrThrow' | 'one' | 'oneOrThrow';
2764
+ }
2765
+ type SelectAsSelectable<Obj> = UnionToIntersection<{ [K in keyof Obj]: Obj[K] extends ((q: never) => infer R extends AllowedRelationOneQueryForSelectable) ? { [C in R['returnType'] extends 'value' | 'valueOrThrow' ? K : keyof R['result'] as R['returnType'] extends 'value' | 'valueOrThrow' ? K : `${K & string}.${C & string}`]: {
2766
+ as: C;
2767
+ column: R['returnType'] extends 'value' | 'valueOrThrow' ? R['result']['value'] : R['result'][C & keyof R['result']];
2768
+ } } : never }[keyof Obj]>;
2769
+ type SelectAsValueResult<T extends SelectSelf, Arg> = Arg extends keyof T['__selectable'] ? T['__selectable'][Arg]['column'] : Arg extends Expression ? Arg['result']['value'] : Arg extends ((q: never) => IsQuery) ? SelectSubQueryResult<ReturnType<Arg>> : Arg extends ((q: never) => Expression) ? ReturnType<Arg>['result']['value'] : Arg extends ((q: never) => IsQuery | Expression) ? SelectSubQueryResult<Exclude<ReturnType<Arg>, Expression>> | Exclude<ReturnType<Arg>, IsQuery>['result']['value'] : never;
2770
+ type SelectSubQueryResult<Arg extends SelectSelf> = Arg['returnType'] extends undefined | 'all' ? ColumnsShape.MapToObjectArrayColumn<Arg['result']> : Arg['returnType'] extends 'value' | 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? ColumnsShape.MapToPluckColumn<Arg['result']> : Arg['returnType'] extends 'one' ? ColumnsShape.MapToNullableObjectColumn<Arg['result']> : ColumnsShape.MapToObjectColumn<Arg['result']>;
2771
+ declare function _querySelect<T extends SelectSelf, Columns extends SelectArgs<T>>(q: T, columns: Columns): SelectResult<T, Columns>;
2772
+ declare function _querySelect<T extends SelectSelf, Obj extends SelectAsArg<T>>(q: T, obj: Obj): SelectResultObj<T, Obj>;
2773
+ declare function _querySelect<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(q: T, args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
2774
+ declare class Select {
2775
+ /**
2776
+ * Takes a list of columns to be selected, and by default, the query builder will select all columns of the table.
2777
+ *
2778
+ * The last argument can be an object. Keys of the object are column aliases, value can be a column name, sub-query, or raw SQL expression.
2779
+ *
2780
+ * ```ts
2781
+ * import { sql } from './baseTable'
2782
+ *
2783
+ * // select columns of the table:
2784
+ * db.table.select('id', 'name', { idAlias: 'id' });
2785
+ *
2786
+ * // accepts columns with table names:
2787
+ * db.table.select('user.id', 'user.name', { nameAlias: 'user.name' });
2788
+ *
2789
+ * // table name may refer to the current table or a joined table:
2790
+ * db.table
2791
+ * .join(db.message, 'authorId', 'user.id')
2792
+ * .select('user.name', 'message.text', { textAlias: 'message.text' });
2793
+ *
2794
+ * // select value from the sub-query,
2795
+ * // this sub-query should return a single record and a single column:
2796
+ * db.table.select({
2797
+ * subQueryResult: Otherdb.table.select('column').take(),
2798
+ * });
2799
+ *
2800
+ * // select raw SQL value, specify the returning type via <generic> syntax:
2801
+ * db.table.select({
2802
+ * raw: sql<number>`1 + 2`,
2803
+ * });
2804
+ *
2805
+ * // select raw SQL value, the resulting type can be set by providing a column type in such way:
2806
+ * db.table.select({
2807
+ * raw: sql`1 + 2`.type((t) => t.integer()),
2808
+ * });
2809
+ *
2810
+ * // same raw SQL query as above, but the sql is returned from a callback
2811
+ * db.table.select({
2812
+ * raw: () => sql`1 + 2`.type((t) => t.integer()),
2813
+ * });
2814
+ * ```
2815
+ *
2816
+ * When you use the ORM and defined relations, `select` can also accept callbacks with related table queries:
2817
+ *
2818
+ * ```ts
2819
+ * await db.author.select({
2820
+ * allBooks: (q) => q.books,
2821
+ * firstBook: (q) => q.books.order({ createdAt: 'ASC' }).take(),
2822
+ * booksCount: (q) => q.books.count(),
2823
+ * });
2824
+ * ```
2825
+ *
2826
+ * When you're selecting a relation that's connected via `belongsTo` or `hasOne`, it becomes available to use in `order` or in `where`:
2827
+ *
2828
+ * ```ts
2829
+ * // select books with their authors included, order by author name and filter by author column:
2830
+ * await db.books
2831
+ * .select({
2832
+ * author: (q) => q.author,
2833
+ * })
2834
+ * .order('author.name')
2835
+ * .where({ 'author.isPopular': true });
2836
+ * ```
2837
+ */
2838
+ select<T extends SelectSelf, Columns extends SelectArgs<T>>(this: T, ...args: Columns): SelectResult<T, Columns>;
2839
+ select<T extends SelectSelf, Obj extends SelectAsArg<T>>(this: T, obj: Obj): SelectResultObj<T, Obj>;
2840
+ select<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(this: T, ...args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
2841
+ /**
2842
+ * When querying the table or creating records, all columns are selected by default,
2843
+ * but updating and deleting queries are returning affected row counts by default.
2844
+ *
2845
+ * Use `selectAll` to select all columns. If the `.select` method was applied before it will be discarded.
2846
+ *
2847
+ * ```ts
2848
+ * const selectFull = await db.table
2849
+ * .select('id', 'name') // discarded by `selectAll`
2850
+ * .selectAll();
2851
+ *
2852
+ * const updatedFull = await db.table.selectAll().where(conditions).update(data);
2853
+ *
2854
+ * const deletedFull = await db.table.selectAll().where(conditions).delete();
2855
+ * ```
2856
+ */
2857
+ selectAll<T extends SelectSelf>(this: T): SelectResult<T, ['*']>;
2858
+ }
2859
+ interface QueryGetSelf extends PickQuerySelectable, PickQueryRelationsWithData {}
2860
+ type GetArg<T extends QueryGetSelf> = GetStringArg<T> | Expression | ((q: SelectAsFnArg<T>) => Expression | Query.Pick.SingleValueResult);
2701
2861
  type GetStringArg<T extends PickQuerySelectable> = keyof T['__selectable'] & string;
2702
- type GetResult<T extends QueryGetSelf, Arg extends GetArg<T>> = Arg extends string ? SetQueryReturnsValueOrThrow<T, Arg> : Arg extends Expression ? SetQueryReturnsColumnOrThrow<T, Arg['result']['value']> : never;
2703
- type GetResultOptional<T extends QueryGetSelf, Arg extends GetArg<T>> = Arg extends string ? SetQueryReturnsValueOptional<T, Arg> : Arg extends Expression ? SetQueryReturnsColumnOptional<T, Arg['result']['value']> : never;
2862
+ type ResolveGetArgColumn<Arg> = Arg extends Expression ? Arg['result']['value'] : Arg extends ((q: never) => infer R) ? R extends Expression ? R['result']['value'] : R extends Query.Pick.SingleValueResult ? R['result']['value'] : never : never;
2863
+ type GetResult<T extends QueryGetSelf, Arg extends GetArg<T>> = Arg extends string ? SetQueryReturnsValueOrThrow<T, Arg> : SetQueryReturnsColumnOrThrow<T, ResolveGetArgColumn<Arg>>;
2864
+ type GetResultOptional<T extends QueryGetSelf, Arg extends GetArg<T>> = Arg extends string ? SetQueryReturnsValueOptional<T, Arg> : SetQueryReturnsColumnOptional<T, ResolveGetArgColumn<Arg>>;
2704
2865
  type HeadlineSearchArg<T extends PickQueryTsQuery> = Exclude<T['__tsQuery'], undefined>;
2705
2866
  interface HeadlineParams<T extends PickQuerySelectable> {
2706
2867
  text?: SelectableOrExpressionOfType<T, Column.Pick.QueryColumnOfType<string>>;
@@ -4294,6 +4455,7 @@ declare class QueryScope {
4294
4455
  type DeleteMethodsNames = 'delete';
4295
4456
  type DeleteArgs<T extends PickQueryHasWhere> = T['__hasWhere'] extends true ? EmptyTuple : [never];
4296
4457
  type DeleteResult<T extends PickQueryHasSelectResultReturnType> = T['__hasSelect'] extends true ? T : T['returnType'] extends undefined | 'all' ? SetQueryReturnsRowCountMany<T> : SetQueryReturnsRowCount<T>;
4458
+ interface DeleteSelf extends PickQueryHasSelectHasWhereResultReturnType, Query.Pick.IsNotReadOnly {}
4297
4459
  declare const _queryDelete: <T extends PickQueryHasSelectResultReturnType>(query: T) => DeleteResult<T>;
4298
4460
  declare class QueryDelete {
4299
4461
  /**
@@ -4355,10 +4517,10 @@ declare class QueryDelete {
4355
4517
  * .from('b');
4356
4518
  * ```
4357
4519
  */
4358
- delete<T extends PickQueryHasSelectHasWhereResultReturnType>(this: T, ..._args: DeleteArgs<T>): DeleteResult<T>;
4520
+ delete<T extends DeleteSelf>(this: T, ..._args: DeleteArgs<T>): DeleteResult<T>;
4359
4521
  }
4360
4522
  type SoftDeleteOption<Shape extends Column.QueryColumns> = true | keyof Shape;
4361
- interface QueryWithSoftDelete extends PickQueryResult, PickQueryReturnType, PickQueryHasSelect, PickQueryHasWhere {
4523
+ interface QueryWithSoftDelete extends PickQueryResult, PickQueryReturnType, PickQueryHasSelect, PickQueryHasWhere, Query.Pick.IsNotReadOnly {
4362
4524
  __scopes: NonDeletedScope;
4363
4525
  }
4364
4526
  interface NonDeletedScope {
@@ -4659,6 +4821,7 @@ interface QueryInternal<SinglePrimaryKey = any, UniqueColumns = any, UniqueColum
4659
4821
  snakeCase?: boolean;
4660
4822
  noPrimaryKey: boolean;
4661
4823
  comment?: string;
4824
+ readOnly?: boolean;
4662
4825
  primaryKeys?: string[];
4663
4826
  singlePrimaryKey: SinglePrimaryKey;
4664
4827
  uniqueColumns: UniqueColumns;
@@ -4748,6 +4911,10 @@ interface DbTableOptions<ColumnTypes, Table extends string | undefined, Shape ex
4748
4911
  * Table comment, for migrations generator
4749
4912
  */
4750
4913
  comment?: string;
4914
+ /**
4915
+ * Disallow runtime create, update, and delete operations.
4916
+ */
4917
+ readOnly?: true | undefined;
4751
4918
  /**
4752
4919
  * Computed SQL or JS columns definitions
4753
4920
  */
@@ -4758,10 +4925,10 @@ interface DbTableOptions<ColumnTypes, Table extends string | undefined, Shape ex
4758
4925
  nowSQL?: string;
4759
4926
  }
4760
4927
  type DbTableOptionScopes<Table extends string | undefined, Shape extends Column.QueryColumns, Keys extends string = string> = { [K in Keys]: (q: ScopeArgumentQuery<Table, Shape>) => IsQuery };
4761
- interface QueryBuilder extends Query {
4928
+ interface QueryBuilder extends Query.NotReadOnlyQuery {
4762
4929
  returnType: undefined;
4763
4930
  }
4764
- declare class Db<Table extends string | undefined = undefined, Shape extends Column.QueryColumnsInit = Column.QueryColumnsInit, PrimaryKeys = never, UniqueColumns = never, UniqueColumnTuples = never, UniqueConstraints = never, ColumnTypes = DefaultColumnTypes<ColumnSchemaConfig>, ShapeWithComputed extends Column.QueryColumnsInit = Shape, Scopes extends RecordUnknown | undefined = EmptyObject, DefaultSelect extends keyof Shape = keyof Shape> extends QueryMethods<ColumnTypes> implements Query {
4931
+ declare class Db<Table extends string | undefined = undefined, Shape extends Column.QueryColumnsInit = Column.QueryColumnsInit, PrimaryKeys = never, UniqueColumns = never, UniqueColumnTuples = never, UniqueConstraints = never, ColumnTypes = DefaultColumnTypes<ColumnSchemaConfig>, ShapeWithComputed extends Column.QueryColumnsInit = Shape, Scopes extends RecordUnknown | undefined = EmptyObject, DefaultSelect extends keyof Shape = keyof Shape, ReadOnly extends true | undefined = undefined> extends QueryMethods<ColumnTypes> implements Query {
4765
4932
  adapterNotInTransaction: Adapter;
4766
4933
  qb: QueryBuilder;
4767
4934
  table: Table;
@@ -4770,6 +4937,7 @@ declare class Db<Table extends string | undefined = undefined, Shape extends Col
4770
4937
  __isQuery: true;
4771
4938
  __as: Table & string;
4772
4939
  __selectable: SelectableFromShape<ShapeWithComputed, Table>;
4940
+ readOnly: ReadOnly;
4773
4941
  __hasSelect: boolean;
4774
4942
  __hasWhere: boolean;
4775
4943
  __defaults: { [K in { [K in keyof Shape]: unknown extends Shape[K]['data']['default'] ? never : K }[keyof Shape]]: true };
@@ -4859,9 +5027,7 @@ declare class Db<Table extends string | undefined = undefined, Shape extends Col
4859
5027
  queryArrays<R extends any[] = any[]>(...args: SQLQueryArgs): Promise<QueryResult<R>>;
4860
5028
  }
4861
5029
  interface DbTableConstructor<ColumnTypes> {
4862
- <Table extends string, Shape extends Column.QueryColumnsInit, Data extends MaybeArray<TableDataItem>, Options extends DbTableOptions<ColumnTypes, Table, Shape>>(table: Table, shape?: ((t: ColumnTypes) => Shape) | Shape, tableData?: TableDataFn<Shape, Data>, options?: Options): Db<Table, Shape, keyof ShapeColumnPrimaryKeys<Shape> extends never ? never : ShapeColumnPrimaryKeys<Shape>, ShapeUniqueColumns<Shape> | TableDataItemsUniqueColumns<Shape, Data>, TableDataItemsUniqueColumnTuples<Shape, Data>, UniqueConstraints<Shape> | TableDataItemsUniqueConstraints<Data>, ColumnTypes, Shape & ComputedColumnsFromOptions<Options['computed']>, MapTableScopesOption<Options>, ColumnsShape.DefaultSelectKeys<Shape>> & {
4863
- ko: Shape;
4864
- };
5030
+ <Table extends string, Shape extends Column.QueryColumnsInit, Data extends MaybeArray<TableDataItem>, Options extends DbTableOptions<ColumnTypes, Table, Shape>>(table: Table, shape?: ((t: ColumnTypes) => Shape) | Shape, tableData?: TableDataFn<Shape, Data>, options?: Options): Db<Table, Shape, keyof ShapeColumnPrimaryKeys<Shape> extends never ? never : ShapeColumnPrimaryKeys<Shape>, ShapeUniqueColumns<Shape> | TableDataItemsUniqueColumns<Shape, Data>, TableDataItemsUniqueColumnTuples<Shape, Data>, UniqueConstraints<Shape> | TableDataItemsUniqueConstraints<Data>, ColumnTypes, Shape & ComputedColumnsFromOptions<Options['computed']>, MapTableScopesOption<Options>, ColumnsShape.DefaultSelectKeys<Shape>, Options['readOnly'] extends true ? true : undefined>;
4865
5031
  }
4866
5032
  interface DbSqlMethod<ColumnTypes> {
4867
5033
  <T>(...args: StaticSQLArgs): RawSql<Column.Pick.QueryColumnOfType<T>, ColumnTypes>;
@@ -5927,166 +6093,6 @@ declare class QueryTransaction {
5927
6093
  */
5928
6094
  recoverable<T>(this: T): T;
5929
6095
  }
5930
- interface ColumnsShape {
5931
- [K: string]: Column;
5932
- }
5933
- declare namespace ColumnsShape {
5934
- export type DefaultSelectKeys<S extends Column.QueryColumnsInit> = { [K in keyof S]: S[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof S];
5935
- export type DefaultOutput<Set extends Column.QueryColumnsInit> = { [K in DefaultSelectKeys<Set>]: Set[K]['outputType'] };
5936
- export type Input<Shape extends Column.QueryColumnsInit, AppReadOnly = { [K in keyof Shape]: Shape[K]['data']['appReadOnly'] extends true ? K : never }[keyof Shape], Optional extends keyof Shape = { [K in keyof Shape]: Shape[K]['data']['optional'] extends true ? K : never }[keyof Shape]> = { [K in Exclude<keyof Shape, AppReadOnly | Optional>]: Shape[K]['inputType'] } & { [K in Exclude<Optional, AppReadOnly>]?: Shape[K]['inputType'] };
5937
- export type InputPartial<Shape extends Column.QueryColumnsInit> = { [K in keyof Shape]?: Shape[K]['inputType'] };
5938
- export type Output<Shape extends Column.QueryColumns> = { [K in keyof Shape]: Shape[K]['outputType'] };
5939
- export type DefaultSelectOutput<Shape extends Column.QueryColumnsInit> = { [K in { [K in keyof Shape]: Shape[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof Shape]]: Shape[K]['outputType'] };
5940
- export interface MapToObjectColumn<Shape extends Column.QueryColumns> {
5941
- dataType: 'object';
5942
- type: { [K in keyof Shape]: Shape[K]['type'] };
5943
- outputType: ShallowSimplify<ObjectOutput<Shape>>;
5944
- queryType: { [K in keyof Shape]: Shape[K]['queryType'] };
5945
- operators: OperatorsAny;
5946
- }
5947
- export interface MapToNullableObjectColumn<Shape extends Column.QueryColumns> {
5948
- dataType: 'object';
5949
- type: { [K in keyof Shape]: Shape[K]['type'] };
5950
- outputType: ShallowSimplify<ObjectOutput<Shape>> | undefined;
5951
- queryType: { [K in keyof Shape]: Shape[K]['queryType'] } | null;
5952
- operators: OperatorsAny;
5953
- }
5954
- export interface MapToPluckColumn<Shape extends Column.QueryColumns> {
5955
- dataType: 'array';
5956
- type: Shape['pluck']['type'][];
5957
- outputType: Shape['pluck']['outputType'][];
5958
- queryType: Shape['pluck']['queryType'][];
5959
- operators: OperatorsAny;
5960
- }
5961
- export interface MapToObjectArrayColumn<Shape extends Column.QueryColumns> {
5962
- dataType: 'array';
5963
- type: { [K in keyof Shape]: Shape[K]['type'] }[];
5964
- outputType: ShallowSimplify<ObjectOutput<Shape>>[];
5965
- queryType: { [K in keyof Shape]: Shape[K]['queryType'] }[];
5966
- operators: OperatorsAny;
5967
- }
5968
- type ObjectOutput<Shape extends Column.QueryColumns> = { [K in keyof Shape]: Shape[K]['outputType'] };
5969
- export {};
5970
- }
5971
- interface SelectSelf extends PickQuerySelectable, PickQueryHasSelect, PickQueryDefaultSelect, PickQueryShape, PickQueryRelations, PickQueryResult, PickQueryReturnType, PickQueryWithData {}
5972
- type SelectArgs<T extends SelectSelf> = ('*' | keyof T['__selectable'])[];
5973
- interface SubQueryAddition<T extends PickQueryWithData> extends IsSubQuery {
5974
- withData: T['withData'];
5975
- }
5976
- type SelectAsFnArg<T extends PickQueryRelationsWithData> = EmptyObject extends T['relations'] ? T : { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['maybeSingle'] & SubQueryAddition<T> : K extends keyof T ? T[K] : never };
5977
- interface SelectAsArg<T extends SelectSelf> {
5978
- [K: string]: keyof T['__selectable'] | Expression | ((q: SelectAsFnArg<T>) => unknown);
5979
- }
5980
- type SelectAsFnReturnType = {
5981
- result: Column.QueryColumns;
5982
- returnType: Exclude<QueryReturnType, 'rows'>;
5983
- } | Expression;
5984
- interface SelectAsCheckReturnTypes {
5985
- [K: string]: PropertyKey | Expression | ((q: never) => SelectAsFnReturnType);
5986
- }
5987
- type SelectReturnType<T extends PickQueryReturnType> = T['returnType'] extends 'valueOrThrow' ? 'oneOrThrow' : T extends 'value' ? 'one' : T['returnType'] extends 'pluck' ? 'all' : T['returnType'];
5988
- type SelectResult<T extends SelectSelf, Columns extends PropertyKey[]> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends 'result' ? { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5989
- type SelectResultObj<T extends SelectSelf, Obj> = Obj extends SelectAsCheckReturnTypes ? { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never } : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never }> : T[K] } : `Invalid return type of ${{ [K in keyof Obj]: Obj[K] extends ((...args: any[]) => any) ? ReturnType<Obj[K]> extends SelectAsFnReturnType ? never : K : never }[keyof Obj] & string}`;
5990
- type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey[], Obj> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? // Combine previously selected items, all columns if * was provided,
5991
- { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5992
- interface AllowedRelationOneQueryForSelectable extends IsSubQuery {
5993
- result: Column.QueryColumns;
5994
- returnType: 'value' | 'valueOrThrow' | 'one' | 'oneOrThrow';
5995
- }
5996
- type SelectAsSelectable<Obj> = UnionToIntersection<{ [K in keyof Obj]: Obj[K] extends ((q: never) => infer R extends AllowedRelationOneQueryForSelectable) ? { [C in R['returnType'] extends 'value' | 'valueOrThrow' ? K : keyof R['result'] as R['returnType'] extends 'value' | 'valueOrThrow' ? K : `${K & string}.${C & string}`]: {
5997
- as: C;
5998
- column: R['returnType'] extends 'value' | 'valueOrThrow' ? R['result']['value'] : R['result'][C & keyof R['result']];
5999
- } } : never }[keyof Obj]>;
6000
- type SelectAsValueResult<T extends SelectSelf, Arg> = Arg extends keyof T['__selectable'] ? T['__selectable'][Arg]['column'] : Arg extends Expression ? Arg['result']['value'] : Arg extends ((q: never) => IsQuery) ? SelectSubQueryResult<ReturnType<Arg>> : Arg extends ((q: never) => Expression) ? ReturnType<Arg>['result']['value'] : Arg extends ((q: never) => IsQuery | Expression) ? SelectSubQueryResult<Exclude<ReturnType<Arg>, Expression>> | Exclude<ReturnType<Arg>, IsQuery>['result']['value'] : never;
6001
- type SelectSubQueryResult<Arg extends SelectSelf> = Arg['returnType'] extends undefined | 'all' ? ColumnsShape.MapToObjectArrayColumn<Arg['result']> : Arg['returnType'] extends 'value' | 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? ColumnsShape.MapToPluckColumn<Arg['result']> : Arg['returnType'] extends 'one' ? ColumnsShape.MapToNullableObjectColumn<Arg['result']> : ColumnsShape.MapToObjectColumn<Arg['result']>;
6002
- declare function _querySelect<T extends SelectSelf, Columns extends SelectArgs<T>>(q: T, columns: Columns): SelectResult<T, Columns>;
6003
- declare function _querySelect<T extends SelectSelf, Obj extends SelectAsArg<T>>(q: T, obj: Obj): SelectResultObj<T, Obj>;
6004
- declare function _querySelect<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(q: T, args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
6005
- declare class Select {
6006
- /**
6007
- * Takes a list of columns to be selected, and by default, the query builder will select all columns of the table.
6008
- *
6009
- * The last argument can be an object. Keys of the object are column aliases, value can be a column name, sub-query, or raw SQL expression.
6010
- *
6011
- * ```ts
6012
- * import { sql } from './baseTable'
6013
- *
6014
- * // select columns of the table:
6015
- * db.table.select('id', 'name', { idAlias: 'id' });
6016
- *
6017
- * // accepts columns with table names:
6018
- * db.table.select('user.id', 'user.name', { nameAlias: 'user.name' });
6019
- *
6020
- * // table name may refer to the current table or a joined table:
6021
- * db.table
6022
- * .join(db.message, 'authorId', 'user.id')
6023
- * .select('user.name', 'message.text', { textAlias: 'message.text' });
6024
- *
6025
- * // select value from the sub-query,
6026
- * // this sub-query should return a single record and a single column:
6027
- * db.table.select({
6028
- * subQueryResult: Otherdb.table.select('column').take(),
6029
- * });
6030
- *
6031
- * // select raw SQL value, specify the returning type via <generic> syntax:
6032
- * db.table.select({
6033
- * raw: sql<number>`1 + 2`,
6034
- * });
6035
- *
6036
- * // select raw SQL value, the resulting type can be set by providing a column type in such way:
6037
- * db.table.select({
6038
- * raw: sql`1 + 2`.type((t) => t.integer()),
6039
- * });
6040
- *
6041
- * // same raw SQL query as above, but the sql is returned from a callback
6042
- * db.table.select({
6043
- * raw: () => sql`1 + 2`.type((t) => t.integer()),
6044
- * });
6045
- * ```
6046
- *
6047
- * When you use the ORM and defined relations, `select` can also accept callbacks with related table queries:
6048
- *
6049
- * ```ts
6050
- * await db.author.select({
6051
- * allBooks: (q) => q.books,
6052
- * firstBook: (q) => q.books.order({ createdAt: 'ASC' }).take(),
6053
- * booksCount: (q) => q.books.count(),
6054
- * });
6055
- * ```
6056
- *
6057
- * When you're selecting a relation that's connected via `belongsTo` or `hasOne`, it becomes available to use in `order` or in `where`:
6058
- *
6059
- * ```ts
6060
- * // select books with their authors included, order by author name and filter by author column:
6061
- * await db.books
6062
- * .select({
6063
- * author: (q) => q.author,
6064
- * })
6065
- * .order('author.name')
6066
- * .where({ 'author.isPopular': true });
6067
- * ```
6068
- */
6069
- select<T extends SelectSelf, Columns extends SelectArgs<T>>(this: T, ...args: Columns): SelectResult<T, Columns>;
6070
- select<T extends SelectSelf, Obj extends SelectAsArg<T>>(this: T, obj: Obj): SelectResultObj<T, Obj>;
6071
- select<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(this: T, ...args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
6072
- /**
6073
- * When querying the table or creating records, all columns are selected by default,
6074
- * but updating and deleting queries are returning affected row counts by default.
6075
- *
6076
- * Use `selectAll` to select all columns. If the `.select` method was applied before it will be discarded.
6077
- *
6078
- * ```ts
6079
- * const selectFull = await db.table
6080
- * .select('id', 'name') // discarded by `selectAll`
6081
- * .selectAll();
6082
- *
6083
- * const updatedFull = await db.table.selectAll().where(conditions).update(data);
6084
- *
6085
- * const deletedFull = await db.table.selectAll().where(conditions).delete();
6086
- * ```
6087
- */
6088
- selectAll<T extends SelectSelf>(this: T): SelectResult<T, ['*']>;
6089
- }
6090
6096
  type SelectItem = string | SelectAs | Expression | undefined;
6091
6097
  interface SelectAs {
6092
6098
  selectAs: SelectAsValue;
@@ -7485,13 +7491,12 @@ declare class QueryCreateFrom {
7485
7491
  */
7486
7492
  insertForEachFrom<T extends CreateSelf>(this: T, query: IsQuery): InsertManyFromResult<T>;
7487
7493
  }
7488
- interface CreateSelf extends IsQuery, PickQueryHasSelect, PickQueryDefaults, PickQueryResult, PickQueryRelations, PickQueryWithData, PickQueryReturnType, PickQueryShape, PickQueryUniqueProperties, PickQueryInputType {}
7494
+ interface CreateSelf extends PickQueryHasSelect, PickQueryDefaults, PickQueryResult, PickQueryRelations, PickQueryWithData, PickQueryReturnType, PickQueryShape, PickQueryUniqueProperties, PickQueryInputType, Query.Pick.IsNotReadOnly {}
7489
7495
  type CreateData<T extends CreateSelf> = EmptyObject extends T['relations'] ? CreateDataWithDefaults<T, keyof T['__defaults']> : CreateRelationsData<T>;
7490
7496
  type CreateDataWithDefaults<T extends CreateSelf, Defaults extends PropertyKey> = { [K in keyof T['inputType'] as K extends Defaults ? never : K]: K extends Defaults ? never : CreateColumn<T, K> } & { [K in Defaults]?: K extends keyof T['inputType'] ? CreateColumn<T, K> : never };
7491
7497
  type CreateDataWithDefaultsForRelations<T extends CreateSelf, Defaults extends keyof T['inputType'], OmitFKeys extends PropertyKey> = { [K in keyof T['inputType'] as K extends Defaults | OmitFKeys ? never : K]: K extends Defaults | OmitFKeys ? never : CreateColumn<T, K> } & { [K in Defaults as K extends OmitFKeys ? never : K]?: CreateColumn<T, K> };
7492
7498
  type CreateColumn<T extends CreateSelf, K extends keyof T['inputType']> = T['inputType'][K] | ((q: T) => QueryOrExpression<T['inputType'][K]>);
7493
- type CreateRelationsData<T extends CreateSelf> = CreateDataWithDefaultsForRelations<T, keyof T['__defaults'], T['relations'][keyof T['relations']]['omitForeignKeyInCreate']> & CreateBelongsToData<T> & T['relations'][keyof T['relations']]['optionalDataForCreate'];
7494
- type CreateBelongsToData<T extends CreateSelf> = [T['relations'][keyof T['relations']]['dataForCreate']] extends [never] ? EmptyObject : CreateRelationsDataOmittingFKeys<T, T['relations'][keyof T['relations']]['dataForCreate']>;
7499
+ type CreateRelationsData<T extends CreateSelf> = CreateDataWithDefaultsForRelations<T, keyof T['__defaults'], T['relations'][keyof T['relations']]['omitForeignKeyInCreate']> & CreateRelationsDataOmittingFKeys<T, T['relations'][keyof T['relations']]['dataForCreate']> & T['relations'][keyof T['relations']]['optionalDataForCreate'];
7495
7500
  type CreateRelationsDataOmittingFKeys<T extends CreateSelf, Union> = (Union extends RelationConfigDataForCreate ? (u: Union['columns'] extends keyof T['__defaults'] ? { [P in Exclude<Union['columns'] & keyof T['inputType'], keyof T['__defaults']>]: CreateColumn<T, P> } & { [P in keyof T['__defaults'] & Union['columns']]?: CreateColumn<T, P> } & Partial<Union['nested']> : { [P in Union['columns'] & keyof T['inputType']]: CreateColumn<T, P> } | Union['nested']) => void : never) extends ((u: infer Obj) => void) ? Obj : never;
7496
7501
  type CreateResult<T extends CreateSelf, Data> = T extends {
7497
7502
  isCount: true;
@@ -7876,23 +7881,24 @@ declare class OnConflictQueryBuilder<T extends CreateSelf, Arg extends OnConflic
7876
7881
  except: keyof T['shape'] | (keyof T['shape'])[];
7877
7882
  }): T;
7878
7883
  }
7879
- interface UpdateSelf extends PickQuerySelectable, PickQueryResult, PickQueryRelations, PickQueryWithData, PickQueryReturnType, PickQueryShape, PickQueryInputType, PickQueryAs, PickQueryHasSelect, PickQueryHasWhere {}
7880
- type UpdateData<T extends UpdateSelf> = EmptyObject extends T['relations'] ? { [K in keyof T['inputType']]?: UpdateColumn<T, K> } : { [K in keyof T['inputType'] | keyof T['relations']]?: K extends keyof T['inputType'] ? UpdateColumn<T, K> : UpdateRelationData<T, T['relations'][K]> };
7881
- type UpdateColumn<T extends UpdateSelf, Key extends keyof T['inputType']> = T['inputType'][Key] | ((q: { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['query'] : K extends keyof T ? T[K] : never }) => QueryOrExpression<T['inputType'][Key]>);
7882
- type UpdateRelationData<T extends UpdateSelf, Rel extends RelationConfigBase> = T['returnType'] extends undefined | 'all' ? Rel['dataForUpdate'] : Rel['dataForUpdateOne'];
7884
+ interface UpdateSelf extends PickQuerySelectable, PickQueryResult, PickQueryRelations, PickQueryWithData, PickQueryReturnType, PickQueryShape, PickQueryInputType, PickQueryAs, PickQueryHasSelect, PickQueryHasWhere, Query.Pick.IsNotReadOnly {}
7885
+ type UpdateData<T extends UpdateSelf> = { [K in keyof T['inputType'] | keyof T['relations']]?: K extends keyof T['inputType'] ? T['inputType'][K] | ((q: { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['query'] : K extends keyof T ? T[K] : never }) => QueryOrExpression<T['inputType'][K]>) : T['returnType'] extends undefined | 'all' ? T['relations'][K]['dataForUpdate'] : T['relations'][K]['dataForUpdateOne'] };
7883
7886
  type UpdateArg<T extends UpdateSelf> = T['__hasWhere'] extends true ? UpdateData<T> : 'Update statement must have where conditions. To update all prefix `update` with `all()`';
7884
7887
  type UpdateResult<T extends UpdateSelf> = T['__hasSelect'] extends true ? T : T['returnType'] extends undefined | 'all' ? SetQueryReturnsRowCountMany<T> : SetQueryReturnsRowCount<T>;
7885
7888
  type NumericColumns<T extends UpdateSelf> = { [K in keyof T['inputType']]: Exclude<T['shape'][K]['queryType'], string> extends number | bigint | null ? K : never }[keyof T['inputType']];
7886
7889
  type ChangeCountArg<T extends UpdateSelf> = NumericColumns<T> | { [K in NumericColumns<T>]?: T['shape'][K]['type'] extends number | null ? number : number | string | bigint };
7887
- type UpdateManyBySelf = UpdateSelf & PickQueryResultReturnTypeUniqueColumns & PickQueryUniqueProperties;
7888
- type UpdateManyData<T extends UpdateSelf> = ({ [K in keyof T['shape'] as T['shape'][K] extends {
7889
- data: {
7890
- primaryKey: string;
7890
+ interface UpdateManyBySelf extends UpdateSelf {
7891
+ internal: {
7892
+ uniqueColumns: unknown;
7893
+ uniqueColumnNames: unknown;
7894
+ uniqueColumnTuples: unknown;
7895
+ uniqueConstraints: unknown;
7891
7896
  };
7892
- } ? K : never]: T['shape'][K]['queryType'] | Expression } & { [P in keyof T['inputType']]?: T['inputType'][P] | Expression })[];
7897
+ }
7898
+ type UpdateManyData<T extends UpdateSelf> = ({ [K in keyof T['shape'] as T['shape'][K] extends Column.Modifiers.IsPrimaryKey<string> ? K : never]: T['shape'][K]['queryType'] | Expression } & { [P in keyof T['inputType']]?: T['inputType'][P] | Expression })[];
7893
7899
  type UpdateManyByKeys<T extends UpdateManyBySelf> = T['internal']['uniqueColumnNames'] | T['internal']['uniqueColumnTuples'];
7894
- type UpdateManyByKeyColumns<Keys> = Keys extends string ? Keys : Keys extends unknown[] ? Keys[number] & string : never;
7895
- type UpdateManyByData<T extends UpdateSelf, K extends string> = ({ [P in K & keyof T['inputType']]-?: T['inputType'][P] } & { [P in keyof T['inputType'] as P extends K ? never : P]?: T['inputType'][P] | Expression })[];
7900
+ type UpdateManyByKeyColumns<K> = K extends string[] ? K[number] : K;
7901
+ type UpdateManyByData<T extends UpdateSelf, K> = ({ [P in K & keyof T['inputType']]: T['inputType'][P] } & { [P in keyof T['inputType']]?: P extends K ? T['inputType'][P] : T['inputType'][P] | Expression })[];
7896
7902
  type UpdateManyResult<T extends UpdateSelf> = T['__hasSelect'] extends true ? T['returnType'] extends 'one' | 'oneOrThrow' ? SetQueryReturnsAllResult<T, T['result']> : T['returnType'] extends 'value' | 'valueOrThrow' ? SetQueryReturnsPluckColumnResult<T, T['result']> : SetQueryResult<T, T['result']> : SetQueryReturnsRowCountMany<T>;
7897
7903
  declare const _queryUpdate: <T extends UpdateSelf>(updateSelf: T, arg: UpdateArg<T>) => UpdateResult<T>;
7898
7904
  declare const _queryUpdateOrThrow: <T extends UpdateSelf>(q: T, arg: UpdateArg<T>) => UpdateResult<T>;
@@ -8326,7 +8332,7 @@ declare class QueryUpdate {
8326
8332
  * ]);
8327
8333
  * ```
8328
8334
  */
8329
- updateManyBy<T extends UpdateManyBySelf, Keys extends UpdateManyByKeys<T>, K extends string = UpdateManyByKeyColumns<Keys>>(this: T, keys: Keys, data: UpdateManyByData<T, K>): UpdateManyResult<T> & QueryHasWhere;
8335
+ updateManyBy<T extends UpdateManyBySelf, Keys extends UpdateManyByKeys<T>, K = UpdateManyByKeyColumns<Keys>>(this: T, keys: Keys, data: UpdateManyByData<T, K>): UpdateManyResult<T> & QueryHasWhere;
8330
8336
  /**
8331
8337
  * Same as {@link updateManyBy}, but skips records with no matching key rather than throwing.
8332
8338
  *
@@ -8337,7 +8343,7 @@ declare class QueryUpdate {
8337
8343
  * ]);
8338
8344
  * ```
8339
8345
  */
8340
- updateManyByOptional<T extends UpdateManyBySelf, Keys extends UpdateManyByKeys<T>, K extends string = UpdateManyByKeyColumns<Keys>>(this: T, keys: Keys, data: UpdateManyByData<T, K>): UpdateManyResult<T> & QueryHasWhere;
8346
+ updateManyByOptional<T extends UpdateManyBySelf, Keys extends UpdateManyByKeys<T>, K = UpdateManyByKeyColumns<Keys>>(this: T, keys: Keys, data: UpdateManyByData<T, K>): UpdateManyResult<T> & QueryHasWhere;
8341
8347
  }
8342
8348
  declare abstract class VirtualColumn<Schema extends ColumnSchemaConfig, InputSchema extends Schema['type'] = ReturnType<Schema['never']>> extends Column<Schema, unknown, InputSchema, OperatorsAny> {
8343
8349
  dataType: string;
@@ -9337,7 +9343,7 @@ type UpsertData<T extends UpsertThis, Update extends UpdateData<T>> = {
9337
9343
  data: Update;
9338
9344
  create: UpsertCreate<keyof Update, CreateData<T>> | ((update: Update) => UpsertCreate<keyof Update, CreateData<T>>);
9339
9345
  };
9340
- declare const _queryUpsert: (q: Query, data: UpsertData<UpsertThis, UpdateData<Query>>) => Query;
9346
+ declare const _queryUpsert: (q: Query, data: UpsertData<UpsertThis, UpdateData<UpdateSelf>>) => Query;
9341
9347
  declare class QueryUpsert {
9342
9348
  /**
9343
9349
  * `upsert` tries to update a single record, and then it creates the record if it doesn't yet exist.
@@ -9481,6 +9487,23 @@ declare class QueryGet {
9481
9487
  */
9482
9488
  getOptional<T extends QueryGetSelf, Arg extends GetArg<T>>(this: T, arg: Arg): GetResultOptional<T, Arg>;
9483
9489
  }
9490
+ interface QueryPluckSelf extends PickQuerySelectable, PickQueryRelationsWithData {}
9491
+ type PluckArg<T extends QueryPluckSelf> = SelectableOrExpression<T> | ((q: SelectAsFnArg<T>) => Expression | Query.Pick.SingleValueResult);
9492
+ type PluckResult<T extends QueryPluckSelf, S extends PluckArg<T>> = S extends ((q: never) => infer R) ? R extends Expression ? SetQueryReturnsPluck<T, R> : R extends Query.Pick.SingleValueResult ? { [K in keyof T]: K extends '__hasSelect' ? true : K extends 'result' ? {
9493
+ pluck: R['result']['value'];
9494
+ } : K extends 'returnType' ? 'pluck' : K extends 'then' ? QueryThen<R['result']['value']['outputType'][]> : T[K] } : never : S extends SelectableOrExpression<T> ? SetQueryReturnsPluck<T, S> : never;
9495
+ declare class QueryPluck {
9496
+ /**
9497
+ * `.pluck` returns a single array of a single selected column values:
9498
+ *
9499
+ * ```ts
9500
+ * const ids = await db.table.select('id').pluck();
9501
+ * // ids are an array of all users' id like [1, 2, 3]
9502
+ * ```
9503
+ * @param select - column name or a raw SQL
9504
+ */
9505
+ pluck<T extends QueryPluckSelf, S extends PluckArg<T>>(this: T, select: S): PluckResult<T, S>;
9506
+ }
9484
9507
  interface MergeQueryArg extends PickQueryTable, PickQuerySelectable, PickQueryResult, PickQueryReturnType, PickQueryWithData, PickQueryWindows, PickQueryThen, PickQueryHasSelect, PickQueryHasWhere {}
9485
9508
  type MergeQuery<T extends MergeQueryArg, Q extends MergeQueryArg> = { [K in keyof T]: K extends '__hasWhere' | '__hasSelect' ? T[K] & Q[K] : K extends '__selectable' | 'windows' | 'withData' ? Q[K] & Omit<T[K], keyof Q[K]> : K extends 'result' ? MergeQueryResult<T, Q> : K extends 'returnType' ? Q['returnType'] extends undefined ? T['returnType'] : Q['returnType'] : K extends 'then' ? Q['returnType'] extends undefined ? QueryThenByQuery<T, MergeQueryResult<T, Q>> : Q['returnType'] extends 'all' | 'one' | 'oneOrThrow' | 'rows' ? QueryThenByQuery<Q, MergeQueryResult<T, Q>> : Q['__hasSelect'] extends true ? Q['then'] : T['__hasSelect'] extends true ? T['then'] : Q['then'] : T[K] };
9486
9509
  type MergeQueryResult<T extends PickQueryHasSelectResult, Q extends PickQueryHasSelectResult> = T['__hasSelect'] extends true ? Q['__hasSelect'] extends true ? Omit<T['result'], keyof Q['result']> & Q['result'] : T['result'] : Q['result'];
@@ -9707,7 +9730,7 @@ declare class QueryTruncate {
9707
9730
  *
9708
9731
  * @param options - truncate options, may have `cascade: true` and `restartIdentity: true`
9709
9732
  */
9710
- truncate<T>(this: T, options?: {
9733
+ truncate<T extends Query.Pick.IsNotReadOnly>(this: T, options?: {
9711
9734
  restartIdentity?: boolean;
9712
9735
  cascade?: boolean;
9713
9736
  }): SetQueryReturnsVoid<T>;
@@ -9750,7 +9773,7 @@ interface NarrowPluckTypeResult<T extends PickQueryResultReturnType, Narrow> ext
9750
9773
  }
9751
9774
  type QueryIfResult<T extends PickQueryResultReturnType, R extends PickQueryResult> = { [K in keyof T]: K extends 'result' ? { [K in keyof T['result'] | keyof R['result']]: K extends keyof T['result'] ? K extends keyof R['result'] ? R['result'][K] | T['result'][K] : T['result'][K] : Column.Modifiers.QueryColumnToOptional<R['result'][K]> } : K extends 'then' ? QueryIfResultThen<T, R> : T[K] };
9752
9775
  type QueryIfResultThen<T extends PickQueryResultReturnType, R extends PickQueryResult> = T['returnType'] extends undefined | 'all' ? QueryThenShallowSimplifyArr<{ [K in keyof T['result']]: K extends keyof R['result'] ? T['result'][K]['outputType'] | R['result'][K]['outputType'] : T['result'][K]['outputType'] } & { [K in keyof R['result'] as K extends keyof T['result'] ? never : K]?: R['result'][K]['outputType'] }> : T['returnType'] extends 'one' ? QueryThenShallowSimplifyOptional<{ [K in keyof T['result']]: K extends keyof R['result'] ? T['result'][K]['outputType'] | R['result'][K]['outputType'] : T['result'][K]['outputType'] } & { [K in keyof R['result'] as K extends keyof T['result'] ? never : K]?: R['result'][K]['outputType'] }> : T['returnType'] extends 'oneOrThrow' ? QueryThenShallowSimplify<{ [K in keyof T['result']]: K extends keyof R['result'] ? T['result'][K]['outputType'] | R['result'][K]['outputType'] : T['result'][K]['outputType'] } & { [K in keyof R['result'] as K extends keyof T['result'] ? never : K]?: R['result'][K]['outputType'] }> : T['returnType'] extends 'value' ? QueryThen<T['result']['value']['outputType'] | R['result']['value']['outputType'] | undefined> : T['returnType'] extends 'valueOrThrow' ? QueryThen<T['result']['value']['outputType'] | R['result']['value']['outputType']> : T['returnType'] extends 'rows' ? QueryThen<(T['result'][keyof T['result']]['outputType'] | R['result'][keyof R['result']]['outputType'])[][]> : T['returnType'] extends 'pluck' ? QueryThen<(T['result']['pluck']['outputType'] | R['result']['pluck']['outputType'])[]> : QueryThen<void>;
9753
- interface QueryMethods<ColumnTypes> extends QueryClone, QueryAsMethods, AggregateMethods, QueryDistinct, Select, FromMethods, QueryJoin, QueryLimitOffset, CteQuery, Union, QueryJsonMethods, QueryCreate, QueryCreateFrom, QueryUpdate, QueryDelete, QueryStorage, QueryTransaction, QueryTruncate, For, Where, SearchMethods, Clear, Having, QueryCatchers, QueryLog, QueryOrder, QueryWithSchema, QueryHooks, QueryUpsert, QueryOrCreate, QueryGet, MergeQueryMethods, QuerySql<ColumnTypes>, QueryTransform, QueryMap, QueryScope, SoftDeleteMethods, QueryExpressions, QueryWrap, QueryWindow {}
9776
+ interface QueryMethods<ColumnTypes> extends QueryClone, QueryAsMethods, AggregateMethods, QueryDistinct, Select, FromMethods, QueryJoin, QueryLimitOffset, CteQuery, Union, QueryJsonMethods, QueryCreate, QueryCreateFrom, QueryUpdate, QueryDelete, QueryStorage, QueryTransaction, QueryTruncate, For, Where, SearchMethods, Clear, Having, QueryCatchers, QueryLog, QueryOrder, QueryWithSchema, QueryHooks, QueryUpsert, QueryOrCreate, QueryGet, QueryPluck, MergeQueryMethods, QuerySql<ColumnTypes>, QueryTransform, QueryMap, QueryScope, SoftDeleteMethods, QueryExpressions, QueryWrap, QueryWindow {}
9754
9777
  declare class QueryMethods<ColumnTypes> {
9755
9778
  /**
9756
9779
  * `.all` is a default behavior, that returns an array of objects:
@@ -9805,16 +9828,6 @@ declare class QueryMethods<ColumnTypes> {
9805
9828
  * ```
9806
9829
  */
9807
9830
  rows<T extends PickQueryResult>(this: T): SetQueryReturnsRows<T>;
9808
- /**
9809
- * `.pluck` returns a single array of a single selected column values:
9810
- *
9811
- * ```ts
9812
- * const ids = await db.table.select('id').pluck();
9813
- * // ids are an array of all users' id like [1, 2, 3]
9814
- * ```
9815
- * @param select - column name or a raw SQL
9816
- */
9817
- pluck<T extends PickQuerySelectable, S extends SelectableOrExpression<T>>(this: T, select: S): SetQueryReturnsPluck<T, S>;
9818
9831
  /**
9819
9832
  * `.exec` won't parse the response at all, and returns undefined:
9820
9833
  *
@@ -10273,12 +10286,27 @@ interface Query extends IsQuery, PickQueryTable, PickQueryShape, PickQuerySelect
10273
10286
  relations: RelationsBase;
10274
10287
  relationQueries: IsQueries;
10275
10288
  error: new (message: string, length: number, name: QueryErrorName) => QueryError;
10289
+ readOnly: true | undefined;
10276
10290
  }
10277
10291
  declare namespace Query {
10292
+ interface NotReadOnlyQuery extends Query {
10293
+ readOnly: undefined;
10294
+ }
10278
10295
  namespace Order {
10279
10296
  type Arg<T extends Order.ArgThis> = Order.Arg<T>;
10280
10297
  type Args<T extends Order.ArgThis> = Order.Args<T>;
10281
10298
  }
10299
+ namespace Pick {
10300
+ interface SingleValueResult {
10301
+ result: {
10302
+ value: Column.Pick.OutputType;
10303
+ };
10304
+ returnType: 'value' | 'valueOrThrow';
10305
+ }
10306
+ interface IsNotReadOnly {
10307
+ readOnly: undefined;
10308
+ }
10309
+ }
10282
10310
  }
10283
10311
  type SelectableOfType<T extends PickQuerySelectable, Type> = { [K in keyof T['__selectable']]: T['__selectable'][K]['column']['type'] extends Type | null ? K : never }[keyof T['__selectable']];
10284
10312
  type SelectableOrExpressionOfType<T extends PickQuerySelectable, C extends Column.Pick.Type> = SelectableOfType<T, C['type']> | Expression<Column.Pick.QueryColumnOfType<C['type'] | null>>;
@@ -10348,7 +10376,7 @@ interface RelationConfigBase extends IsQuery {
10348
10376
  modifyRelatedQuery?(relatedQuery: IsQuery): (query: IsQuery) => void;
10349
10377
  maybeSingle: PickQuerySelectableReturnType;
10350
10378
  omitForeignKeyInCreate: PropertyKey;
10351
- dataForCreate?: RelationConfigDataForCreate;
10379
+ dataForCreate: RelationConfigDataForCreate | undefined;
10352
10380
  optionalDataForCreate: unknown;
10353
10381
  dataForUpdate: unknown;
10354
10382
  dataForUpdateOne: unknown;