pqb 0.3.2 → 0.3.3

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
@@ -22,343 +22,201 @@ declare const getRawSql: (raw: RawExpression) => string;
22
22
  declare const EMPTY_OBJECT: {};
23
23
  declare const getQueryParsers: (q: Query) => ColumnsParsers | undefined;
24
24
 
25
- declare type WithSelectable<T extends QueryBase, W extends keyof T['withData']> = T['withData'][W] extends WithDataItem ? StringKey<keyof T['withData'][W]['shape']> | `${T['withData'][W]['table']}.${StringKey<keyof T['withData'][W]['shape']>}` : never;
26
- declare type JoinArgs<T extends QueryBase, Q extends Query = Query, R extends keyof T['relations'] = keyof T['relations'], W extends keyof T['withData'] = keyof T['withData']> = [relation: R] | [
27
- query: Q,
28
- conditions: Record<Selectable<Q>, Selectable<T> | RawExpression> | RawExpression
29
- ] | [
30
- withAlias: W,
31
- conditions: Record<WithSelectable<T, W>, Selectable<T> | RawExpression> | RawExpression
32
- ] | [
33
- query: Q,
34
- leftColumn: Selectable<Q> | RawExpression,
35
- rightColumn: Selectable<T> | RawExpression
36
- ] | [
37
- withAlias: W,
38
- leftColumn: WithSelectable<T, W> | RawExpression,
39
- rightColumn: Selectable<T> | RawExpression
40
- ] | [
41
- query: Q,
42
- leftColumn: Selectable<Q> | RawExpression,
43
- op: string,
44
- rightColumn: Selectable<T> | RawExpression
45
- ] | [
46
- withAlias: W,
47
- leftColumn: WithSelectable<T, W> | RawExpression,
48
- op: string,
49
- rightColumn: Selectable<T> | RawExpression
50
- ];
51
- declare type JoinResult<T extends Query, Args extends JoinArgs<T>, A extends Query | keyof T['relations'] = Args[0]> = AddQueryJoinedTable<T, A extends Query ? A : T['relations'] extends Record<string, Relation> ? A extends keyof T['relations'] ? T['relations'][A]['model'] : A extends keyof T['withData'] ? T['withData'][A] extends WithDataItem ? {
52
- table: T['withData'][A]['table'];
53
- tableAlias: undefined;
54
- result: T['withData'][A]['shape'];
55
- } : never : never : never>;
56
- declare type JoinCallbackArg<T extends QueryBase> = Query | keyof T['withData'] | keyof T['relations'];
57
- declare type JoinCallback<T extends QueryBase, Arg extends JoinCallbackArg<T>> = (q: OnQueryBuilder<T, Arg extends keyof T['withData'] ? T['withData'][Arg] extends WithDataItem ? {
58
- query: QueryData;
59
- table: T['withData'][Arg]['table'];
60
- tableAlias: undefined;
61
- clone(): QueryBase;
62
- selectable: {
63
- [K in keyof T['withData'][Arg]['shape'] as `${T['withData'][Arg]['table']}.${StringKey<K>}`]: {
64
- as: StringKey<K>;
65
- column: T['withData'][Arg]['shape'][K];
66
- };
25
+ declare type NestedInsertOneItem = {
26
+ create?: Record<string, unknown>;
27
+ connect?: WhereArg<QueryBase>;
28
+ connectOrCreate?: {
29
+ where: WhereArg<QueryBase>;
30
+ create: Record<string, unknown>;
31
+ };
32
+ };
33
+ declare type NestedInsertManyItems = {
34
+ create?: Record<string, unknown>[];
35
+ connect?: WhereArg<QueryBase>[];
36
+ connectOrCreate?: {
37
+ where: WhereArg<QueryBase>;
38
+ create: Record<string, unknown>;
39
+ }[];
40
+ };
41
+ declare type NestedInsertItem = NestedInsertOneItem | NestedInsertManyItems;
42
+ declare type BelongsToNestedInsert = (query: Query, relationData: NestedInsertOneItem[]) => Promise<Record<string, unknown>[]>;
43
+ declare type HasOneNestedInsert = (query: Query, data: [
44
+ selfData: Record<string, unknown>,
45
+ relationData: NestedInsertOneItem
46
+ ][]) => Promise<void>;
47
+ declare type HasManyNestedInsert = (query: Query, data: [
48
+ selfData: Record<string, unknown>,
49
+ relationData: NestedInsertManyItems
50
+ ][]) => Promise<void>;
51
+ declare type NestedUpdateOneItem = {
52
+ disconnect?: boolean;
53
+ set?: WhereArg<QueryBase>;
54
+ delete?: boolean;
55
+ update?: UpdateData<Query>;
56
+ upsert?: {
57
+ update: UpdateData<Query>;
58
+ create: Record<string, unknown>;
59
+ };
60
+ create: Record<string, unknown>;
61
+ };
62
+ declare type NestedUpdateManyItems = {
63
+ disconnect?: MaybeArray<WhereArg<QueryBase>>;
64
+ set?: MaybeArray<WhereArg<QueryBase>>;
65
+ delete?: MaybeArray<WhereArg<QueryBase>>;
66
+ update?: {
67
+ where: MaybeArray<WhereArg<QueryBase>>;
68
+ data: UpdateData<Query>;
69
+ };
70
+ create: Record<string, unknown>[];
71
+ };
72
+ declare type NestedUpdateItem = NestedUpdateOneItem | NestedUpdateManyItems;
73
+ declare type BelongsToNestedUpdate = (q: Query, update: Record<string, unknown>, params: NestedUpdateOneItem, state: {
74
+ updateLater?: Record<string, unknown>;
75
+ updateLaterPromises?: Promise<void>[];
76
+ }) => boolean;
77
+ declare type HasOneNestedUpdate = (query: Query, data: Record<string, unknown>[], relationData: NestedUpdateOneItem) => Promise<void>;
78
+ declare type HasManyNestedUpdate = (query: Query, data: Record<string, unknown>[], relationData: NestedUpdateManyItems) => Promise<void>;
79
+ declare type BaseRelation = {
80
+ type: string;
81
+ key: string;
82
+ model: QueryWithTable;
83
+ query: QueryWithTable;
84
+ joinQuery(fromQuery: QueryBase, toQuery: Query): Query;
85
+ nestedCreateQuery: Query;
86
+ nestedInsert?: BelongsToNestedInsert | HasOneNestedInsert | HasManyNestedInsert;
87
+ nestedUpdate?: BelongsToNestedUpdate | HasOneNestedUpdate | HasManyNestedUpdate;
88
+ primaryKey: string;
89
+ options: {
90
+ scope?(q: QueryWithTable): QueryWithTable;
91
+ required?: boolean;
92
+ };
93
+ };
94
+ interface BelongsToRelation extends BaseRelation {
95
+ type: 'belongsTo';
96
+ returns: 'one';
97
+ options: BaseRelation['options'] & {
98
+ primaryKey: string;
99
+ foreignKey: string;
67
100
  };
68
- shape: T['withData'][Arg]['shape'];
69
- __model: Query;
70
- relations: RelationsBase;
71
- withData: WithDataBase;
72
- } : never : Arg extends Query ? Arg : T['relations'] extends Record<string, Relation> ? Arg extends keyof T['relations'] ? T['relations'][Arg]['model'] : never : never>) => OnQueryBuilder;
73
- declare type JoinCallbackResult<T extends Query, Arg extends JoinCallbackArg<T>> = AddQueryJoinedTable<T, Arg extends Query ? Arg : T['relations'] extends Record<string, Relation> ? Arg extends keyof T['relations'] ? T['relations'][Arg]['model'] : Arg extends keyof T['withData'] ? T['withData'][Arg] extends WithDataItem ? {
74
- table: T['withData'][Arg]['table'];
75
- tableAlias: undefined;
76
- result: T['withData'][Arg]['shape'];
77
- } : never : never : never>;
78
- declare class Join {
79
- join<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
80
- join<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
81
- _join<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
82
- _join<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
83
- innerJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
84
- innerJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
85
- _innerJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
86
- _innerJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
87
- leftJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
88
- leftJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
89
- _leftJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
90
- _leftJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
91
- leftOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
92
- leftOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
93
- _leftOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
94
- _leftOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
95
- rightJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
96
- rightJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
97
- _rightJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
98
- _rightJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
99
- rightOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
100
- rightOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
101
- _rightOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
102
- _rightOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
103
- fullOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
104
- fullOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
105
- _fullOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
106
- _fullOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
107
101
  }
108
- declare type OnArgs<Q extends {
109
- selectable: SelectableBase;
110
- }> = [leftColumn: keyof Q['selectable'], rightColumn: keyof Q['selectable']] | [
111
- leftColumn: keyof Q['selectable'],
112
- op: string,
113
- rightColumn: keyof Q['selectable']
114
- ];
115
- declare const pushQueryOn: <T extends QueryBase>(q: T, joinFrom: QueryBase | string, joinTo: QueryBase | string, ...on: OnArgs<QueryBase>) => T;
116
- declare const pushQueryOrOn: typeof pushQueryOn;
117
- declare const addQueryOn: typeof pushQueryOrOn;
118
- declare const addQueryOrOn: typeof pushQueryOrOn;
119
- declare type OnJsonPathEqualsArgs<T extends QueryBase> = [
120
- leftColumn: keyof T['selectable'],
121
- leftPath: string,
122
- rightColumn: keyof T['selectable'],
123
- rightPath: string
124
- ];
125
- declare class OnQueryBuilder<S extends QueryBase = QueryBase, J extends QueryBase = QueryBase> extends WhereQueryBuilder<Omit<J, 'selectable'> & {
126
- selectable: Omit<S['selectable'], keyof S['shape']> & J['selectable'];
127
- }> implements QueryBase {
128
- joinTo: QueryBase;
129
- constructor(q: QueryBase | string, shape: ColumnsShape, joinTo: QueryBase);
130
- on<T extends this>(this: T, ...args: OnArgs<T>): T;
131
- _on<T extends this>(this: T, ...args: OnArgs<T>): T;
132
- orOn<T extends this>(this: T, ...args: OnArgs<T>): T;
133
- _orOn<T extends this>(this: T, ...args: OnArgs<T>): T;
134
- onJsonPathEquals<T extends this>(this: T, ...args: OnJsonPathEqualsArgs<T>): T;
135
- _onJsonPathEquals<T extends this>(this: T, ...args: OnJsonPathEqualsArgs<T>): T;
102
+ interface HasOneRelation extends BaseRelation {
103
+ type: 'hasOne';
104
+ returns: 'one';
105
+ options: BaseRelation['options'] & ({
106
+ primaryKey: string;
107
+ foreignKey: string;
108
+ } | {
109
+ through: string;
110
+ source: string;
111
+ });
112
+ }
113
+ interface HasManyRelation extends BaseRelation {
114
+ type: 'hasMany';
115
+ returns: 'many';
116
+ options: BaseRelation['options'] & ({
117
+ primaryKey: string;
118
+ foreignKey: string;
119
+ } | {
120
+ through: string;
121
+ source: string;
122
+ });
136
123
  }
124
+ interface HasAndBelongsToManyRelation extends BaseRelation {
125
+ type: 'hasAndBelongsToMany';
126
+ returns: 'many';
127
+ options: BaseRelation['options'] & {
128
+ primaryKey: string;
129
+ foreignKey: string;
130
+ associationPrimaryKey: string;
131
+ associationForeignKey: string;
132
+ joinTable: string;
133
+ };
134
+ }
135
+ declare type Relation = BelongsToRelation | HasOneRelation | HasManyRelation | HasAndBelongsToManyRelation;
136
+ declare type RelationsBase = Record<never, Relation>;
137
+ declare type relationQueryKey = typeof relationQueryKey;
138
+ declare const relationQueryKey: unique symbol;
139
+ declare type isRequiredRelationKey = typeof isRequiredRelationKey;
140
+ declare const isRequiredRelationKey: unique symbol;
141
+ declare type RelationQueryBase = Query & {
142
+ [relationQueryKey]: string;
143
+ [isRequiredRelationKey]: boolean;
144
+ };
145
+ declare type RelationQuery<RelationName extends PropertyKey = string, Params extends Record<string, unknown> = never, Populate extends string = never, T extends Query = Query, Required extends boolean = boolean, Q extends RelationQueryBase = Omit<T, 'tableAlias'> & {
146
+ tableAlias: RelationName extends string ? RelationName : never;
147
+ [isRequiredRelationKey]: Required;
148
+ [relationQueryKey]: string;
149
+ }> = ((params: Params) => Q & {
150
+ [defaultsKey]: Record<Populate, true>;
151
+ }) & Q;
137
152
 
138
- declare type WhereArg<T extends QueryBase> = (Omit<{
139
- [K in keyof T['selectable']]?: T['selectable'][K]['column']['type'] | null | ColumnOperators<T['selectable'], K> | RawExpression;
140
- }, 'NOT' | 'OR' | 'IN' | 'EXISTS'> & {
141
- NOT?: MaybeArray<WhereArg<T>>;
142
- OR?: MaybeArray<WhereArg<T>>[];
143
- IN?: MaybeArray<{
144
- columns: (keyof T['selectable'])[];
145
- values: unknown[][] | Query | RawExpression;
146
- }>;
147
- EXISTS?: MaybeArray<JoinArgs<T> | JoinCallbackArg<T>>;
148
- }) | QueryBase | RawExpression | ((q: WhereQueryBuilder<T>) => WhereQueryBuilder);
149
- declare type WhereInColumn<T extends QueryBase> = keyof T['selectable'] | [keyof T['selectable'], ...(keyof T['selectable'])[]];
150
- declare type WhereInValues<T extends QueryBase, Column extends WhereInColumn<T>> = Column extends keyof T['selectable'] ? T['selectable'][Column]['column']['type'][] | Query | RawExpression : ({
151
- [I in keyof Column]: Column[I] extends keyof T['selectable'] ? T['selectable'][Column[I]]['column']['type'] : never;
152
- } & {
153
- length: Column extends {
154
- length: number;
155
- } ? Column['length'] : never;
156
- })[] | Query | RawExpression;
157
- declare type WhereResult<T extends QueryBase> = T & {
158
- hasWhere: true;
153
+ interface QueryResultRow {
154
+ [column: string]: any;
155
+ }
156
+ declare type TypeParsers = Record<number, (input: string) => unknown>;
157
+ declare type QueryInput = string | {
158
+ text: string;
159
+ values?: unknown[];
159
160
  };
160
- declare type WhereInArg<T extends Pick<Query, 'selectable'>> = {
161
- [K in keyof T['selectable']]?: T['selectable'][K]['column']['type'][] | Query | RawExpression;
161
+ declare type QueryResult<T extends QueryResultRow = any> = {
162
+ rowCount: number;
163
+ rows: T[];
162
164
  };
163
- declare const addWhere: <T extends Where>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
164
- declare const addWhereNot: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
165
- declare const addOr: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
166
- declare const addOrNot: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
167
- declare const addWhereIn: <T extends QueryBase>(q: T, and: boolean, arg: unknown, values: unknown[] | unknown[][] | Query | RawExpression | undefined, not?: boolean) => WhereResult<T>;
168
- declare abstract class Where implements QueryBase {
169
- abstract clone<T extends this>(this: T): T;
170
- abstract selectable: SelectableBase;
171
- abstract shape: ColumnsShape;
172
- abstract relations: RelationsBase;
173
- abstract withData: WithDataBase;
174
- abstract __model: Query;
175
- query: QueryData;
176
- table?: string;
177
- tableAlias?: string;
178
- where<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
179
- _where<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
180
- whereNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
181
- _whereNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
182
- and<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
183
- _and<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
184
- andNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
185
- _andNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
186
- or<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
187
- _or<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
188
- orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
189
- _orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
190
- whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): T;
191
- whereIn<T extends Where>(this: T, arg: WhereInArg<T>): T;
192
- _whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
193
- _whereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
194
- orWhereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
195
- orWhereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
196
- _orWhereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
197
- _orWhereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
198
- whereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
199
- whereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
200
- _whereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
201
- _whereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
202
- orWhereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
203
- orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
204
- _orWhereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
205
- _orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
206
- whereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
207
- whereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
208
- _whereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
209
- _whereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
210
- orWhereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
211
- orWhereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
212
- _orWhereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
213
- _orWhereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
214
- whereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
215
- whereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
216
- _whereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
217
- _whereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
218
- orWhereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
219
- orWhereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
220
- _orWhereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
221
- _orWhereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
165
+ declare type QueryArraysResult<R extends any[] = any[]> = {
166
+ rowCount: number;
167
+ rows: R[];
168
+ fields: {
169
+ name: string;
170
+ }[];
171
+ };
172
+ declare type AdapterOptions = Omit<PoolConfig, 'types'> & {
173
+ types?: TypeParsers;
174
+ };
175
+ declare class Adapter {
176
+ types: TypeParsers;
177
+ pool: Pool;
178
+ constructor({ types, ...config }: AdapterOptions);
179
+ query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers): Promise<QueryResult<T>>;
180
+ arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers): Promise<QueryArraysResult<R>>;
181
+ transaction<Result>(cb: (adapter: TransactionAdapter) => Promise<Result>): Promise<Result>;
182
+ close(): Promise<void>;
222
183
  }
223
- declare class WhereQueryBuilder<Q extends QueryBase = QueryBase> extends Where implements QueryBase {
224
- query: QueryData;
225
- selectable: Q['selectable'];
226
- shape: Q['shape'];
227
- relations: Q['relations'];
228
- __model: Query;
229
- withData: {};
230
- constructor(q: QueryBase | string, shape: ColumnsShape);
231
- clone<T extends this>(this: T): T;
184
+ declare class TransactionAdapter implements Adapter {
185
+ pool: Pool;
186
+ client: PoolClient;
187
+ types: TypeParsers;
188
+ constructor(pool: Pool, client: PoolClient, types: TypeParsers);
189
+ query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers): Promise<QueryResult<T>>;
190
+ arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers): Promise<QueryArraysResult<R>>;
191
+ transaction<Result>(cb: (adapter: TransactionAdapter) => Promise<Result>): Promise<Result>;
192
+ close(): Promise<void>;
232
193
  }
233
194
 
234
- declare type AggregateArg<T extends Query> = Expression<T> | Record<string, Expression<T>> | [Expression<T>, string];
235
- declare type AggregateOptions<T extends Query = Query, As extends string | undefined = any> = {
236
- as?: As;
237
- distinct?: boolean;
238
- order?: OrderArg<T> | OrderArg<T>[];
239
- filter?: WhereArg<T>;
240
- filterOr?: WhereArg<T>[];
241
- withinGroup?: boolean;
242
- over?: T['windows'][number] | WindowArgDeclaration<T>;
195
+ declare type QueryLogObject = {
196
+ colors: boolean;
197
+ beforeQuery(sql: Sql): unknown;
198
+ afterQuery(sql: Sql, logData: unknown): void;
199
+ onError(error: Error, sql: Sql, logData: unknown): void;
200
+ };
201
+ declare type QueryLogger = {
202
+ log(message: string): void;
203
+ error(message: string): void;
243
204
  };
244
- declare type Aggregate1ArgumentTypes<T extends Query = Query, C extends ColumnType = ColumnType> = {
245
- count: Expression<T, C>;
246
- avg: NumberExpression<T, C>;
247
- min: Expression<T, C>;
248
- max: Expression<T, C>;
249
- sum: NumberExpression<T, C>;
250
- bitAnd: NumberExpression<T, C>;
251
- bitOr: NumberExpression<T, C>;
252
- boolAnd: BooleanExpression<T, C>;
253
- boolOr: BooleanExpression<T, C>;
254
- every: BooleanExpression<T, C>;
255
- jsonAgg: Expression<T, C>;
256
- jsonbAgg: Expression<T, C>;
257
- xmlAgg: Expression<T, C>;
205
+ declare type QueryLogOptions = {
206
+ log?: boolean | Partial<QueryLogObject>;
207
+ logger?: QueryLogger;
258
208
  };
259
- declare const aggregate1FunctionNames: {
260
- readonly count: "count";
261
- readonly avg: "avg";
262
- readonly min: "min";
263
- readonly max: "max";
264
- readonly sum: "sum";
265
- readonly bitAnd: "bit_and";
266
- readonly bitOr: "bit_or";
267
- readonly boolAnd: "bool_and";
268
- readonly boolOr: "bool_or";
269
- readonly every: "every";
270
- readonly jsonAgg: "json_agg";
271
- readonly jsonbAgg: "jsonb_agg";
272
- readonly xmlAgg: "xmlagg";
209
+ declare const logColors: {
210
+ boldCyanBright: (message: string) => string;
211
+ boldBlue: (message: string) => string;
212
+ boldYellow: (message: string) => string;
213
+ boldMagenta: (message: string) => string;
214
+ boldRed: (message: string) => string;
273
215
  };
274
- declare type SelectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType> = AddQuerySelect<T, Record<CoalesceString<As, Func>, Value>>;
275
- declare type AT1<T extends Query> = Aggregate1ArgumentTypes<T>;
276
- declare type WindowFunctionOptions<T extends Query = Query, As extends string | undefined = any> = {
277
- as?: As;
278
- } & WindowArgDeclaration<T>;
279
- declare class Aggregate {
280
- selectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType>(this: T, functionName: Func, arg: AggregateArg<T>, options?: AggregateOptions<T, As>): SelectAgg<T, Func, As, Value>;
281
- _selectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType>(this: T, functionName: Func, arg: AggregateArg<T>, options?: AggregateOptions<T, As>, columnType?: ColumnType): SelectAgg<T, Func, As, Value>;
282
- count<T extends Query>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T>): SetQueryReturnsValue<T, NumberColumn>;
283
- _count<T extends Query>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T>): SetQueryReturnsValue<T, NumberColumn>;
284
- selectCount<T extends Query, As extends string | undefined = undefined>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T, As>): SelectAgg<T, 'count', As, NumberColumn>;
285
- _selectCount<T extends Query, As extends string | undefined = undefined>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T, As>): SelectAgg<T, 'count', As, NumberColumn>;
286
- avg<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['avg'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
287
- _avg<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['avg'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
288
- selectAvg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'avg', As, NullableColumn<NumberColumn>>;
289
- _selectAvg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'avg', As, NullableColumn<NumberColumn>>;
290
- min<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['min'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
291
- _min<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['min'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
292
- selectMin<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'min', As, NullableColumn<NumberColumn>>;
293
- _selectMin<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'min', As, NullableColumn<NumberColumn>>;
294
- max<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['max'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
295
- _max<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['max'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
296
- selectMax<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'max', As, NullableColumn<NumberColumn>>;
297
- _selectMax<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'max', As, NullableColumn<NumberColumn>>;
298
- sum<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['sum'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
299
- _sum<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['sum'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
300
- selectSum<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'sum', As, NullableColumn<NumberColumn>>;
301
- _selectSum<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'sum', As, NullableColumn<NumberColumn>>;
302
- bitAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
303
- _bitAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
304
- selectBitAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_and', As, NullableColumn<NumberColumn>>;
305
- _selectBitAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_and', As, NullableColumn<NumberColumn>>;
306
- bitOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
307
- _bitOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
308
- selectBitOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_or', As, NullableColumn<NumberColumn>>;
309
- _selectBitOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_or', As, NullableColumn<NumberColumn>>;
310
- boolAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
311
- _boolAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
312
- selectBoolAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_and', As, NullableColumn<BooleanColumn>>;
313
- _selectBoolAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_and', As, NullableColumn<BooleanColumn>>;
314
- boolOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
315
- _boolOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
316
- selectBoolOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_or', As, NullableColumn<BooleanColumn>>;
317
- _selectBoolOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_or', As, NullableColumn<BooleanColumn>>;
318
- every<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['every'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
319
- _every<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['every'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
320
- selectEvery<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'every', As, NullableColumn<BooleanColumn>>;
321
- _selectEvery<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'every', As, NullableColumn<BooleanColumn>>;
322
- jsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
323
- _jsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
324
- selectJsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
325
- _selectJsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
326
- jsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
327
- _jsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
328
- selectJsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
329
- _selectJsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
330
- xmlAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['xmlAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
331
- _xmlAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['xmlAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
332
- selectXmlAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Aggregate1ArgumentTypes<T>['xmlAgg'], options?: AggregateOptions<T, As>): SelectAgg<T, 'xmlagg', As, NullableColumn<StringColumn>>;
333
- _selectXmlAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Aggregate1ArgumentTypes<T>['xmlAgg'], options?: AggregateOptions<T, As>): SelectAgg<T, 'xmlagg', As, NullableColumn<StringColumn>>;
334
- jsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
335
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
336
- }>>>;
337
- _jsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
338
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
339
- }>>>;
340
- selectJsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_object_agg', As, NullableColumn<ColumnType<{
341
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
342
- }>>>;
343
- _selectJsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_object_agg', As, NullableColumn<ColumnType<{
344
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
345
- }>>>;
346
- jsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
347
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
348
- }>>>;
349
- _jsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
350
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
351
- }>>>;
352
- selectJsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_object_agg', As, NullableColumn<ColumnType<{
353
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
354
- }>>>;
355
- _selectJsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_object_agg', As, NullableColumn<ColumnType<{
356
- [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
357
- }>>>;
358
- stringAgg<T extends Query>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
359
- _stringAgg<T extends Query>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
360
- selectStringAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T, As>): SelectAgg<T, 'string_agg', As, NullableColumn<StringColumn>>;
361
- _selectStringAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T, As>): SelectAgg<T, 'string_agg', As, NullableColumn<StringColumn>>;
216
+ declare const logParamToLogObject: (logger: QueryLogger, log: QueryLogOptions['log']) => QueryLogObject | undefined;
217
+ declare class QueryLog {
218
+ log<T extends Query>(this: T, log?: boolean): T;
219
+ _log<T extends Query>(this: T, log?: boolean): T;
362
220
  }
363
221
 
364
222
  declare type BeforeCallback<T extends Query = Query> = (query: T) => void | Promise<void>;
@@ -382,267 +240,340 @@ declare class QueryCallbacks {
382
240
  _afterDelete<T extends Query>(this: T, cb: AfterCallback<T>): T;
383
241
  }
384
242
 
385
- declare type ClearStatement = 'with' | 'select' | 'where' | 'union' | 'using' | 'join' | 'group' | 'order' | 'having' | 'limit' | 'offset' | 'counters';
386
- declare class Clear {
387
- clear<T extends Query>(this: T, ...clears: ClearStatement[]): T;
388
- _clear<T extends Query>(this: T, ...clears: ClearStatement[]): T;
389
- }
390
-
391
- declare type ColumnInfo = {
392
- defaultValue: unknown;
243
+ declare type Sql = {
244
+ text: string;
245
+ values: unknown[];
246
+ };
247
+ declare const checkIfASimpleQuery: (q: QueryData) => boolean;
248
+ declare type CommonQueryData = {
249
+ adapter: Adapter;
250
+ handleResult(q: Query, result: QueryResult): Promise<unknown>;
251
+ returnType: QueryReturnType;
252
+ [relationQueryKey]?: string;
253
+ inTransaction?: boolean;
254
+ wrapInTransaction?: boolean;
255
+ throwOnNotFound?: boolean;
256
+ with?: WithItem[];
257
+ withShapes?: Record<string, ColumnsShape>;
258
+ schema?: string;
259
+ select?: SelectItem[];
260
+ as?: string;
261
+ from?: string | Query | RawExpression;
262
+ and?: WhereItem[];
263
+ or?: WhereItem[][];
264
+ coalesceValue?: unknown | RawExpression;
265
+ parsers?: ColumnsParsers;
266
+ notFoundDefault?: unknown;
267
+ defaults?: Record<string, unknown>;
268
+ beforeQuery?: BeforeCallback[];
269
+ afterQuery?: AfterCallback[];
270
+ log?: QueryLogObject;
271
+ logger: QueryLogger;
272
+ [toSqlCacheKey]?: Sql;
273
+ };
274
+ declare type SelectQueryData = CommonQueryData & {
275
+ type: undefined;
276
+ distinct?: Expression[];
277
+ fromOnly?: boolean;
278
+ join?: JoinItem[];
279
+ joinedParsers?: Record<string, ColumnsParsers>;
280
+ group?: (string | RawExpression)[];
281
+ having?: HavingItem[];
282
+ havingOr?: HavingItem[][];
283
+ window?: WindowItem[];
284
+ union?: {
285
+ arg: UnionItem;
286
+ kind: UnionKind;
287
+ wrap?: boolean;
288
+ }[];
289
+ order?: OrderItem[];
290
+ limit?: number;
291
+ offset?: number;
292
+ for?: {
293
+ type: 'UPDATE' | 'NO KEY UPDATE' | 'SHARE' | 'KEY SHARE';
294
+ tableNames?: string[] | RawExpression;
295
+ mode?: 'NO WAIT' | 'SKIP LOCKED';
296
+ };
297
+ };
298
+ declare type InsertQueryData = CommonQueryData & {
299
+ type: 'insert';
300
+ columns: string[];
301
+ values: unknown[][] | RawExpression;
302
+ using?: JoinItem[];
303
+ join?: JoinItem[];
304
+ joinedParsers?: Record<string, ColumnsParsers>;
305
+ onConflict?: {
306
+ type: 'ignore';
307
+ expr?: OnConflictItem;
308
+ } | {
309
+ type: 'merge';
310
+ expr?: OnConflictItem;
311
+ update?: OnConflictMergeUpdate;
312
+ };
313
+ beforeInsert?: BeforeCallback[];
314
+ afterInsert?: AfterCallback[];
315
+ };
316
+ declare type UpdateQueryDataObject = Record<string, RawExpression | {
317
+ op: string;
318
+ arg: unknown;
319
+ } | unknown>;
320
+ declare type UpdatedAtDataInjector = (data: UpdateQueryDataItem[]) => UpdateQueryDataItem | void;
321
+ declare type UpdateQueryDataItem = UpdateQueryDataObject | RawExpression | UpdatedAtDataInjector;
322
+ declare type UpdateQueryData = CommonQueryData & {
323
+ type: 'update';
324
+ updateData: UpdateQueryDataItem[];
325
+ beforeUpdate?: BeforeCallback[];
326
+ afterUpdate?: AfterCallback[];
327
+ };
328
+ declare type DeleteQueryData = CommonQueryData & {
329
+ type: 'delete';
330
+ join?: JoinItem[];
331
+ joinedParsers?: Record<string, ColumnsParsers>;
332
+ beforeDelete?: BeforeCallback[];
333
+ afterDelete?: AfterCallback[];
334
+ };
335
+ declare type TruncateQueryData = CommonQueryData & {
336
+ type: 'truncate';
337
+ restartIdentity?: boolean;
338
+ cascade?: boolean;
339
+ };
340
+ declare type ColumnInfoQueryData = CommonQueryData & {
341
+ type: 'columnInfo';
342
+ column?: string;
343
+ };
344
+ declare type QueryData = SelectQueryData | InsertQueryData | UpdateQueryData | DeleteQueryData | TruncateQueryData | ColumnInfoQueryData;
345
+ declare type WithItem = [
346
+ as: string,
347
+ options: WithOptions,
348
+ query: Query | RawExpression
349
+ ];
350
+ declare type WithOptions = {
351
+ columns?: string[];
352
+ recursive?: true;
353
+ materialized?: true;
354
+ notMaterialized?: true;
355
+ };
356
+ declare type JsonItem<As extends string = string, Type extends ColumnType = ColumnType> = {
357
+ __json: [
358
+ kind: 'set',
359
+ as: As,
360
+ type: Type,
361
+ column: string | JsonItem,
362
+ path: Array<string | number>,
363
+ value: unknown,
364
+ options?: {
365
+ createIfMissing?: boolean;
366
+ }
367
+ ] | [
368
+ kind: 'insert',
369
+ as: As,
370
+ type: Type,
371
+ column: string | JsonItem,
372
+ path: Array<string | number>,
373
+ value: unknown,
374
+ options?: {
375
+ insertAfter?: boolean;
376
+ }
377
+ ] | [
378
+ kind: 'remove',
379
+ as: As,
380
+ type: Type,
381
+ column: string | JsonItem,
382
+ path: Array<string | number>
383
+ ] | [
384
+ kind: 'pathQuery',
385
+ as: As,
386
+ type: Type,
387
+ column: string | JsonItem,
388
+ path: string,
389
+ options?: {
390
+ vars?: string;
391
+ silent?: boolean;
392
+ }
393
+ ];
394
+ };
395
+ declare type SelectItem = string | RelationQuery | AggregateItem | {
396
+ selectAs: Record<string, string | Query | RawExpression>;
397
+ } | SelectFunctionItem | JsonItem | RawExpression;
398
+ declare type SelectFunctionItem = {
399
+ function: string;
400
+ arguments: SelectItem[];
401
+ as?: string;
402
+ };
403
+ declare type JoinItem = {
393
404
  type: string;
394
- maxLength: number | null;
395
- nullable: boolean;
405
+ args: [relation: string] | [
406
+ arg: string | QueryWithTable,
407
+ conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase)
408
+ ] | [
409
+ arg: string | QueryWithTable,
410
+ leftColumn: string | RawExpression,
411
+ rightColumn: string | RawExpression
412
+ ] | [
413
+ arg: string | QueryWithTable,
414
+ leftColumn: string | RawExpression,
415
+ op: string,
416
+ rightColumn: string | RawExpression
417
+ ];
396
418
  };
397
- declare class ColumnInfoMethods {
398
- columnInfo<T extends Query, Column extends keyof T['shape'] | undefined = undefined>(this: T, column?: Column): SetQueryReturnsColumnInfo<T, Column>;
399
- _columnInfo<T extends Query, Column extends keyof T['shape'] | undefined = undefined>(this: T, column?: Column): SetQueryReturnsColumnInfo<T, Column>;
400
- }
401
-
402
- declare type DeleteArgs<T extends Query> = T['hasWhere'] extends true ? [forceAll?: boolean] : [true];
403
- declare type DeleteResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T;
404
- declare class Delete {
405
- del<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
406
- _del<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
407
- delete<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
408
- _delete<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
409
- }
410
-
411
- declare type ForQueryBuilder<Q extends Query> = Q & {
412
- noWait<T extends ForQueryBuilder<Q>>(this: T): T;
413
- _noWait<T extends ForQueryBuilder<Q>>(this: T): T;
414
- skipLocked<T extends ForQueryBuilder<Q>>(this: T): T;
415
- _skipLocked<T extends ForQueryBuilder<Q>>(this: T): T;
419
+ declare type WhereItem = (Omit<Record<string, unknown | Record<string, unknown | Query | RawExpression> | RawExpression>, 'NOT' | 'AND' | 'OR' | 'IN' | 'EXISTS' | 'ON' | 'ON_JSON_PATH_EQUALS'> & {
420
+ NOT?: MaybeArray<WhereItem>;
421
+ AND?: MaybeArray<WhereItem>;
422
+ OR?: MaybeArray<WhereItem>[];
423
+ IN?: MaybeArray<WhereInItem>;
424
+ EXISTS?: MaybeArray<JoinItem['args']>;
425
+ ON?: WhereOnItem | WhereJsonPathEqualsItem;
426
+ }) | ((q: unknown) => QueryBase) | Query | RawExpression;
427
+ declare type WhereInItem = {
428
+ columns: string[];
429
+ values: unknown[][] | Query | RawExpression;
416
430
  };
417
- declare class For {
418
- forUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
419
- _forUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
420
- forNoKeyUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
421
- _forNoKeyUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
422
- forShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
423
- _forShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
424
- forKeyShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
425
- _forKeyShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
426
- }
427
-
428
- declare type FromArgs<T extends Query> = [
429
- first: string | Query | RawExpression | Exclude<keyof T['withData'], symbol | number>,
430
- second?: string | {
431
- as?: string;
432
- only?: boolean;
433
- }
431
+ declare type WhereJsonPathEqualsItem = [
432
+ leftColumn: string,
433
+ leftPath: string,
434
+ rightColumn: string,
435
+ rightPath: string
434
436
  ];
435
- declare type FromResult<T extends Query, Args extends FromArgs<T>> = Args[1] extends string ? SetQueryTableAlias<T, Args[1]> : Args[1] extends {
436
- as: string;
437
- } ? SetQueryTableAlias<T, Args[1]['as']> : Args[0] extends string ? SetQueryTableAlias<T, Args[0]> : Args[0] extends Query ? SetQueryTableAlias<T, AliasOrTable<Args[0]>> : T;
438
- declare class From {
439
- from<T extends Query, Args extends FromArgs<T>>(this: T, ...args: Args): FromResult<T, Args>;
440
- _from<T extends Query, Args extends FromArgs<T>>(this: T, ...args: Args): FromResult<T, Args>;
441
- }
442
-
443
- declare type GetArg<T extends QueryBase> = StringKey<keyof T['selectable']> | RawExpression;
444
- declare type UnwrapRaw<T extends Query, Arg extends GetArg<T>> = Arg extends RawExpression ? Arg['__column'] : Exclude<Arg, RawExpression>;
445
- declare type GetResult<T extends Query, Arg extends GetArg<T>> = SetQueryReturnsValue<T, UnwrapRaw<T, Arg>>;
446
- declare type GetOptionalResult<T extends Query, Arg extends GetArg<T>> = SetQueryReturnsValueOptional<T, UnwrapRaw<T, Arg>>;
447
- declare type getValueKey = typeof getValueKey;
448
- declare const getValueKey: unique symbol;
449
- declare class QueryGet {
450
- get<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetResult<T, Arg>;
451
- _get<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetResult<T, Arg>;
452
- getOptional<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetOptionalResult<T, Arg>;
453
- _getOptional<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetOptionalResult<T, Arg>;
454
- }
455
-
456
- declare type HavingArgObject<T extends Query, Agg extends keyof Aggregate1ArgumentTypes<T>> = {
457
- [Column in Exclude<Aggregate1ArgumentTypes<T>[Agg], RawExpression>]?: T['selectable'][Column]['column']['type'] | (ColumnOperators<T['selectable'], Column> & AggregateOptions<T>);
437
+ declare type WhereOnItem = {
438
+ joinFrom: WhereOnJoinItem;
439
+ joinTo: WhereOnJoinItem;
440
+ on: [leftFullColumn: string, rightFullColumn: string] | [leftFullColumn: string, op: string, rightFullColumn: string];
458
441
  };
459
- declare type HavingArg<T extends Query = Query> = ({
460
- [Agg in keyof Aggregate1ArgumentTypes<T>]?: HavingArgObject<T, Agg>;
461
- } & {
462
- count?: number | HavingArgObject<T, 'count'>;
463
- }) | Query | RawExpression;
464
- declare class Having {
465
- having<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
466
- _having<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
467
- havingOr<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
468
- _havingOr<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
469
- }
470
-
471
- declare type InsertData<T extends Query, DefaultKeys extends PropertyKey = keyof T[defaultsKey], Data = SetOptional<T['inputType'], DefaultKeys>> = [keyof T['relations']] extends [never] ? Data : OmitBelongsToForeignKeys<T['relations'], Data> & InsertRelationData<T>;
472
- declare type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<Data, {
473
- [K in keyof R]: R[K] extends BelongsToRelation ? R[K]['options']['foreignKey'] : never;
474
- }[keyof R]>;
475
- declare type InsertRelationData<T extends Query> = {
476
- [K in keyof T['relations']]: T['relations'][K] extends BelongsToRelation ? InsertBelongsToData<T, K, T['relations'][K]> : T['relations'][K] extends HasOneRelation ? InsertHasOneData<T, K, T['relations'][K]> : T['relations'][K] extends HasManyRelation | HasAndBelongsToManyRelation ? InsertHasManyData<T, K, T['relations'][K]> : EmptyObject;
477
- }[keyof T['relations']];
478
- declare type InsertBelongsToData<T extends Query, Key extends keyof T['relations'], Rel extends BelongsToRelation> = SetOptional<{
479
- [K in Rel['options']['foreignKey']]: Rel['options']['foreignKey'] extends keyof T['inputType'] ? T['inputType'][Rel['options']['foreignKey']] : never;
480
- }, keyof T[defaultsKey]> | {
481
- [K in Key]: {
482
- create: InsertData<Rel['nestedCreateQuery']>;
483
- connect?: never;
484
- connectOrCreate?: never;
485
- } | {
486
- create?: never;
487
- connect: WhereArg<Rel['model']>;
488
- connectOrCreate?: never;
489
- } | {
490
- create?: never;
491
- connect?: never;
492
- connectOrCreate: {
493
- where: WhereArg<Rel['model']>;
494
- create: InsertData<Rel['nestedCreateQuery']>;
495
- };
442
+ declare type WhereOnJoinItem = {
443
+ table?: string;
444
+ query: {
445
+ as?: string;
496
446
  };
447
+ } | string;
448
+ declare type AggregateItemOptions = {
449
+ as?: string;
450
+ distinct?: boolean;
451
+ order?: OrderItem[];
452
+ filter?: WhereItem;
453
+ filterOr?: WhereItem[];
454
+ withinGroup?: boolean;
455
+ over?: string;
456
+ window?: WindowItem;
497
457
  };
498
- declare type InsertHasOneData<T extends Query, Key extends keyof T['relations'], Rel extends HasOneRelation> = 'through' extends Rel['options'] ? {} : {
499
- [K in Key]?: {
500
- create: InsertData<Rel['nestedCreateQuery']>;
501
- connect?: never;
502
- connectOrCreate?: never;
503
- } | {
504
- create?: never;
505
- connect: WhereArg<Rel['model']>;
506
- connectOrCreate?: never;
507
- } | {
508
- create?: never;
509
- connect?: never;
510
- connectOrCreate: {
511
- where?: WhereArg<Rel['model']>;
512
- create?: InsertData<Rel['nestedCreateQuery']>;
513
- };
514
- };
458
+ declare type SortDir = 'ASC' | 'DESC';
459
+ declare type OrderItem = string | Record<string, SortDir | {
460
+ dir: SortDir;
461
+ nulls: 'FIRST' | 'LAST';
462
+ }> | RawExpression;
463
+ declare type AggregateItemArg = Expression | Record<string, Expression> | [Expression, string];
464
+ declare type AggregateItem = {
465
+ function: string;
466
+ arg?: AggregateItemArg;
467
+ options: AggregateItemOptions;
515
468
  };
516
- declare type InsertHasManyData<T extends Query, Key extends keyof T['relations'], Rel extends HasManyRelation | HasAndBelongsToManyRelation> = 'through' extends Rel['options'] ? {} : {
517
- [K in Key]?: {
518
- create?: InsertData<Rel['nestedCreateQuery']>[];
519
- connect?: WhereArg<Rel['model']>[];
520
- connectOrCreate?: {
521
- where: WhereArg<Rel['model']>;
522
- create: InsertData<Rel['nestedCreateQuery']>;
523
- }[];
524
- };
469
+ declare type ColumnOperators<S extends SelectableBase, Column extends keyof S> = {
470
+ [O in keyof S[Column]['column']['operators']]?: S[Column]['column']['operators'][O]['type'];
471
+ };
472
+ declare type HavingItemObject = Record<string, unknown>;
473
+ declare type HavingItem = Record<string, HavingItemObject> | {
474
+ count?: number | HavingItemObject;
475
+ } | Query | RawExpression;
476
+ declare type WindowItem = Record<string, WindowDeclaration | RawExpression>;
477
+ declare type WindowDeclaration = {
478
+ partitionBy?: Expression | Expression[];
479
+ order?: OrderItem;
480
+ };
481
+ declare type UnionItem = Query | RawExpression;
482
+ declare type UnionKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'INTERSECT ALL' | 'EXCEPT' | 'EXCEPT ALL';
483
+ declare type OnConflictItem = string | string[] | RawExpression;
484
+ declare type OnConflictMergeUpdate = string | string[] | Record<string, unknown> | RawExpression;
485
+
486
+ declare type ToSqlCtx = {
487
+ whereQueryBuilder: typeof WhereQueryBuilder;
488
+ onQueryBuilder: typeof OnQueryBuilder;
489
+ sql: string[];
490
+ values: unknown[];
491
+ };
492
+ declare type toSqlCacheKey = typeof toSqlCacheKey;
493
+ declare const toSqlCacheKey: unique symbol;
494
+ declare type ToSqlOptions = {
495
+ clearCache?: boolean;
496
+ values?: unknown[];
497
+ };
498
+ declare const toSql: (model: Query, options?: ToSqlOptions) => Sql;
499
+
500
+ declare type SomeIsTrue<T extends unknown[]> = T extends [
501
+ infer Head,
502
+ ...infer Tail
503
+ ] ? Head extends true ? true : SomeIsTrue<Tail> : false;
504
+ declare type MaybeArray<T> = T | T[];
505
+ declare type SetOptional<T, K extends PropertyKey> = Omit<T, K> & {
506
+ [P in K]?: P extends keyof T ? T[P] : never;
525
507
  };
526
- declare type InsertRawData = {
527
- columns: string[];
528
- values: RawExpression;
508
+ declare type GetTypesOrRaw<T extends [...unknown[]]> = T extends [
509
+ infer Head,
510
+ ...infer Tail
511
+ ] ? [GetTypeOrRaw<Head>, ...GetTypesOrRaw<Tail>] : [];
512
+ declare type GetTypeOrRaw<T> = T | RawExpression;
513
+ declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
514
+ declare type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>;
515
+ declare type OptionalPropertyNames<T> = {
516
+ [K in keyof T]-?: {} extends {
517
+ [P in K]: T[K];
518
+ } ? K : never;
519
+ }[keyof T];
520
+ declare type SpreadProperties<L, R, K extends keyof L & keyof R> = {
521
+ [P in K]: L[P] | Exclude<R[P], undefined>;
529
522
  };
530
- declare type InsertOneResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T['returnType'] extends 'all' ? SetQueryReturnsOne<T> : T['returnType'] extends 'one' ? SetQueryReturnsOne<T> : T;
531
- declare type InsertManyResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T['returnType'] extends 'one' | 'oneOrThrow' ? SetQueryReturnsAll<T> : T;
532
- declare type OnConflictArg<T extends Query> = keyof T['shape'] | (keyof T['shape'])[] | RawExpression;
533
- declare class Insert {
534
- insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
535
- _insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
536
- insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
537
- _insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
538
- insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
539
- _insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
540
- create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
541
- _create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
542
- createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
543
- _createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
544
- createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
545
- _createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
546
- defaults<T extends Query, Data extends Partial<InsertData<T>>>(this: T, data: Data): T & {
547
- [defaultsKey]: Record<keyof Data, true>;
548
- };
549
- _defaults<T extends Query, Data extends Partial<InsertData<T>>>(this: T, data: Data): T & {
550
- [defaultsKey]: Record<keyof Data, true>;
523
+ declare type Id<T> = T extends infer U ? {
524
+ [K in keyof U]: U[K];
525
+ } : never;
526
+ declare type SpreadTwo<L, R> = Id<Pick<L, Exclude<keyof L, keyof R>> & Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> & Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> & SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>>;
527
+ declare type Spread<A extends readonly [...any]> = A extends [
528
+ infer L,
529
+ ...infer R
530
+ ] ? SpreadTwo<L, Spread<R>> : unknown;
531
+ declare type SimpleSpread<A extends readonly [...any]> = A extends [
532
+ infer L,
533
+ ...infer R
534
+ ] ? L & SimpleSpread<R> : {};
535
+ declare type FilterTuple<T extends readonly any[], E> = T extends [
536
+ infer F,
537
+ ...infer R
538
+ ] ? [F] extends [E] ? [F, ...FilterTuple<R, E>] : FilterTuple<R, E> : [];
539
+ declare type CoalesceString<Left extends string | undefined, Right extends string> = Left extends undefined ? Right : Left;
540
+ declare function applyMixins(derivedCtor: any, constructors: any[]): void;
541
+ declare const joinTruthy: (...strings: (string | false | undefined)[]) => string;
542
+ declare const getClonedQueryData: (query: QueryData) => QueryData;
543
+ declare const getQueryAs: (q: {
544
+ table?: string;
545
+ query: {
546
+ as?: string;
551
547
  };
552
- onConflict<T extends Query, Arg extends OnConflictArg<T>>(this: T, arg?: Arg): OnConflictQueryBuilder<T, Arg>;
553
- _onConflict<T extends Query, Arg extends OnConflictArg<T> | undefined = undefined>(this: T, arg?: Arg): OnConflictQueryBuilder<T, Arg>;
554
- }
555
- declare class OnConflictQueryBuilder<T extends Query, Arg extends OnConflictArg<T> | undefined> {
556
- private query;
557
- private onConflict;
558
- constructor(query: T, onConflict: Arg);
559
- ignore(): T;
560
- merge(update?: keyof T['shape'] | (keyof T['shape'])[] | Partial<T['inputType']> | RawExpression): T;
561
- }
548
+ }) => string;
549
+ declare const toArray: <T>(item: T) => T extends unknown[] ? T : [T];
550
+ declare const noop: () => void;
551
+ declare type EmptyObject = typeof emptyObject;
552
+ declare const emptyObject: {};
553
+ declare const makeRegexToFindInSql: (value: string) => RegExp;
554
+ declare const pushOrNewArrayToObject: <Obj extends {}, Key extends keyof Obj>(obj: Obj, key: Key, value: Exclude<Obj[Key], undefined> extends unknown[] ? Exclude<Obj[Key], undefined>[number] : never) => void;
555
+ declare const pushOrNewArray: <Arr extends unknown[]>(arr: Arr | undefined, value: Arr[number]) => Arr;
562
556
 
563
- declare type JsonColumnName<T extends Pick<Query, 'selectable'>> = StringKey<{
564
- [K in keyof T['selectable']]: T['selectable'][K]['column']['dataType'] extends 'jsonb' ? K : never;
565
- }[keyof T['selectable']]>;
566
- declare type ColumnOrJsonMethod<T extends Query> = JsonColumnName<T> | JsonItem;
567
- declare type JsonSetResult<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string, Type extends ColumnType = Column extends keyof T['shape'] ? T['shape'][Column] : Column extends JsonItem ? Column['__json'][2] : ColumnType> = JsonItem<As, Type> & (Type extends ColumnType ? AddQuerySelect<T, Record<As, Type>> : T);
568
- declare type JsonPathQueryResult<T extends Query, As extends string, Type extends ColumnType> = JsonItem & AddQuerySelect<T, {
569
- [K in As]: Type;
570
- }>;
571
- declare class Json {
572
- json<T extends Query>(this: T): SetQueryReturnsValueOptional<T, StringColumn>;
573
- _json<T extends Query>(this: T): SetQueryReturnsValueOptional<T, StringColumn>;
574
- jsonSet<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
575
- as?: As;
576
- createIfMissing?: boolean;
577
- }): JsonSetResult<T, Column, As>;
578
- _jsonSet<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
579
- as?: As;
580
- createIfMissing?: boolean;
581
- }): JsonSetResult<T, Column, As>;
582
- jsonInsert<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, ...args: [
583
- column: Column,
584
- path: Array<string | number>,
585
- value: unknown,
586
- options?: {
587
- as?: As;
588
- insertAfter?: boolean;
589
- }
590
- ]): JsonSetResult<T, Column, As>;
591
- _jsonInsert<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
592
- as?: As;
593
- insertAfter?: boolean;
594
- }): JsonSetResult<T, Column, As>;
595
- jsonRemove<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, ...args: [
596
- column: Column,
597
- path: Array<string | number>,
598
- options?: {
599
- as?: As;
600
- }
601
- ]): JsonSetResult<T, Column, As>;
602
- _jsonRemove<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, options?: {
603
- as?: As;
604
- }): JsonSetResult<T, Column, As>;
605
- jsonPathQuery<T extends Query, As extends string, Type extends ColumnType>(this: T, ...args: [
606
- type: Type,
607
- column: ColumnOrJsonMethod<T>,
608
- path: string,
609
- as: As,
610
- options?: {
611
- vars?: string;
612
- silent?: boolean;
613
- }
614
- ]): JsonPathQueryResult<T, As, Type>;
615
- _jsonPathQuery<T extends Query, As extends string, Type extends ColumnType>(this: T, type: Type, column: ColumnOrJsonMethod<T>, path: string, as: As, options?: {
616
- vars?: string;
617
- silent?: boolean;
618
- }): JsonPathQueryResult<T, As, Type>;
557
+ declare type ThenResult<Res> = (resolve?: (value: Res) => any, reject?: (error: any) => any) => Promise<Res | never>;
558
+ declare const queryMethodByReturnType: Record<QueryReturnType, 'query' | 'arrays'>;
559
+ declare class Then {
560
+ then(this: Query, resolve?: (result: any) => any, reject?: (error: any) => any): Promise<any>;
619
561
  }
562
+ declare const handleResult: CommonQueryData['handleResult'];
563
+ declare const parseResult: (q: Query, returnType: QueryReturnType | undefined, result: QueryResult) => unknown;
564
+ declare const parseRecord: (parsers: ColumnsParsers, row: any) => any;
620
565
 
621
- declare type QueryLogObject = {
622
- colors: boolean;
623
- beforeQuery(sql: Sql): unknown;
624
- afterQuery(sql: Sql, logData: unknown): void;
625
- onError(error: Error, sql: Sql, logData: unknown): void;
626
- };
627
- declare type QueryLogger = {
628
- log(message: string): void;
629
- error(message: string): void;
630
- };
631
- declare type QueryLogOptions = {
632
- log?: boolean | Partial<QueryLogObject>;
633
- logger?: QueryLogger;
634
- };
635
- declare const logColors: {
636
- boldCyanBright: (message: string) => string;
637
- boldBlue: (message: string) => string;
638
- boldYellow: (message: string) => string;
639
- boldMagenta: (message: string) => string;
640
- boldRed: (message: string) => string;
641
- };
642
- declare const logParamToLogObject: (logger: QueryLogger, log: QueryLogOptions['log']) => QueryLogObject | undefined;
643
- declare class QueryLog {
644
- log<T extends Query>(this: T, log?: boolean): T;
645
- _log<T extends Query>(this: T, log?: boolean): T;
566
+ declare type GetArg<T extends QueryBase> = StringKey<keyof T['selectable']> | RawExpression;
567
+ declare type UnwrapRaw<T extends Query, Arg extends GetArg<T>> = Arg extends RawExpression ? Arg['__column'] : Exclude<Arg, RawExpression>;
568
+ declare type GetResult<T extends Query, Arg extends GetArg<T>> = SetQueryReturnsValue<T, UnwrapRaw<T, Arg>>;
569
+ declare type GetOptionalResult<T extends Query, Arg extends GetArg<T>> = SetQueryReturnsValueOptional<T, UnwrapRaw<T, Arg>>;
570
+ declare type getValueKey = typeof getValueKey;
571
+ declare const getValueKey: unique symbol;
572
+ declare class QueryGet {
573
+ get<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetResult<T, Arg>;
574
+ _get<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetResult<T, Arg>;
575
+ getOptional<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetOptionalResult<T, Arg>;
576
+ _getOptional<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetOptionalResult<T, Arg>;
646
577
  }
647
578
 
648
579
  declare type SelectArg<T extends QueryBase> = StringKey<keyof T['selectable']> | (T['relations'] extends Record<string, Relation> ? StringKey<keyof T['relations']> : never) | SelectAsArg<T>;
@@ -654,7 +585,7 @@ declare type SelectResult<T extends Query, Args extends SelectArg<T>[], SelectAs
654
585
  }>;
655
586
  declare type SelectSubQueryResult<Arg extends Query & {
656
587
  [isRequiredRelationKey]?: boolean;
657
- }> = Arg['returnType'] extends 'all' ? ArrayOfColumnsObjects<Arg['result']> : Arg['returnType'] extends 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? PluckResultColumnType<Arg['result']['pluck']> : Arg[isRequiredRelationKey] extends true ? ColumnsObject<Arg['result']> : NullableColumn<ColumnsObject<Arg['result']>>;
588
+ }> = QueryReturnsAll<Arg['returnType']> extends true ? ArrayOfColumnsObjects<Arg['result']> : Arg['returnType'] extends 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? PluckResultColumnType<Arg['result']['pluck']> : Arg[isRequiredRelationKey] extends true ? ColumnsObject<Arg['result']> : NullableColumn<ColumnsObject<Arg['result']>>;
658
589
  declare const addParserForRawExpression: (q: Query, key: string | getValueKey, raw: RawExpression) => void;
659
590
  declare const addParserForSelectItem: <T extends Query>(q: T, as: string | getValueKey | undefined, key: string, arg: RawExpression<ColumnType<unknown, Operators, unknown>> | Exclude<keyof T["selectable"], number | symbol> | ((q: T) => Query)) => string | RawExpression | Query;
660
591
  declare const addParserToQuery: (query: QueryData, key: string | getValueKey, parser: ColumnParser) => void;
@@ -666,203 +597,248 @@ declare class Select {
666
597
  _selectAll<T extends Query>(this: T): QuerySelectAll<T>;
667
598
  }
668
599
 
669
- interface QueryResultRow {
670
- [column: string]: any;
671
- }
672
- declare type TypeParsers = Record<number, (input: string) => unknown>;
673
- declare type QueryInput = string | {
674
- text: string;
675
- values?: unknown[];
676
- };
677
- declare type QueryResult<T extends QueryResultRow = any> = {
678
- rowCount: number;
679
- rows: T[];
680
- };
681
- declare type QueryArraysResult<R extends any[] = any[]> = {
682
- rowCount: number;
683
- rows: R[];
684
- fields: {
685
- name: string;
686
- }[];
687
- };
688
- declare type AdapterOptions = Omit<PoolConfig, 'types'> & {
689
- types?: TypeParsers;
690
- };
691
- declare class Adapter {
692
- types: TypeParsers;
693
- pool: Pool;
694
- constructor({ types, ...config }: AdapterOptions);
695
- query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers): Promise<QueryResult<T>>;
696
- arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers): Promise<QueryArraysResult<R>>;
697
- transaction<Result>(cb: (adapter: TransactionAdapter) => Promise<Result>): Promise<Result>;
698
- close(): Promise<void>;
699
- }
700
- declare class TransactionAdapter implements Adapter {
701
- pool: Pool;
702
- client: PoolClient;
703
- types: TypeParsers;
704
- constructor(pool: Pool, client: PoolClient, types: TypeParsers);
705
- query<T extends QueryResultRow = any>(query: QueryInput, types?: TypeParsers): Promise<QueryResult<T>>;
706
- arrays<R extends any[] = any[]>(query: QueryInput, types?: TypeParsers): Promise<QueryArraysResult<R>>;
707
- transaction<Result>(cb: (adapter: TransactionAdapter) => Promise<Result>): Promise<Result>;
708
- close(): Promise<void>;
709
- }
710
-
711
- declare type ThenResult<Res> = (resolve?: (value: Res) => any, reject?: (error: any) => any) => Promise<Res | never>;
712
- declare const queryMethodByReturnType: Record<QueryReturnType, 'query' | 'arrays'>;
713
- declare class Then {
714
- then(this: Query, resolve?: (result: any) => any, reject?: (error: any) => any): Promise<any>;
715
- }
716
- declare const handleResult: CommonQueryData['handleResult'];
717
- declare const parseResult: (q: Query, returnType: QueryReturnType, result: QueryResult) => unknown;
718
- declare const parseRecord: (parsers: ColumnsParsers, row: any) => any;
719
-
720
- declare class Transaction {
721
- transaction<T extends Query, Result>(this: T, cb: (query: T) => Promise<Result>): Promise<Result>;
722
- transacting<T extends Query>(this: T, query: Query): T;
723
- _transacting<T extends Query>(this: T, query: Query): T;
724
- }
725
-
726
- declare type UnionArg<T extends Query> = (Omit<Query, 'result'> & {
727
- result: T['result'];
728
- }) | RawExpression;
729
- declare class Union {
730
- union<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
731
- _union<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
732
- unionAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
733
- _unionAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
734
- intersect<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
735
- _intersect<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
736
- intersectAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
737
- _intersectAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
738
- except<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
739
- _except<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
740
- exceptAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
741
- _exceptAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
742
- }
743
-
744
- declare type UpdateData<T extends Query> = {
745
- [K in keyof T['type']]?: T['type'][K] | RawExpression;
746
- } & (T['relations'] extends Record<string, Relation> ? {
747
- [K in keyof T['relations']]?: T['relations'][K] extends BelongsToRelation ? UpdateBelongsToData<T, T['relations'][K]> : T['relations'][K] extends HasOneRelation ? UpdateHasOneData<T, T['relations'][K]> : T['relations'][K] extends HasManyRelation ? UpdateHasManyData<T, T['relations'][K]> : T['relations'][K] extends HasAndBelongsToManyRelation ? UpdateHasAndBelongsToManyData<T['relations'][K]> : never;
748
- } : EmptyObject) & {
749
- __raw?: never;
600
+ declare type FromArgs<T extends Query> = [
601
+ first: string | Query | RawExpression | Exclude<keyof T['withData'], symbol | number>,
602
+ second?: string | {
603
+ as?: string;
604
+ only?: boolean;
605
+ }
606
+ ];
607
+ declare type FromResult<T extends Query, Args extends FromArgs<T>> = Args[1] extends string ? SetQueryTableAlias<T, Args[1]> : Args[1] extends {
608
+ as: string;
609
+ } ? SetQueryTableAlias<T, Args[1]['as']> : Args[0] extends string ? SetQueryTableAlias<T, Args[0]> : Args[0] extends Query ? SetQueryTableAlias<T, AliasOrTable<Args[0]>> : T;
610
+ declare class From {
611
+ from<T extends Query, Args extends FromArgs<T>>(this: T, ...args: Args): FromResult<T, Args>;
612
+ _from<T extends Query, Args extends FromArgs<T>>(this: T, ...args: Args): FromResult<T, Args>;
613
+ }
614
+
615
+ declare type WhereArg<T extends QueryBase> = (Omit<{
616
+ [K in keyof T['selectable']]?: T['selectable'][K]['column']['type'] | null | ColumnOperators<T['selectable'], K> | RawExpression;
617
+ }, 'NOT' | 'OR' | 'IN' | 'EXISTS'> & {
618
+ NOT?: MaybeArray<WhereArg<T>>;
619
+ OR?: MaybeArray<WhereArg<T>>[];
620
+ IN?: MaybeArray<{
621
+ columns: (keyof T['selectable'])[];
622
+ values: unknown[][] | Query | RawExpression;
623
+ }>;
624
+ EXISTS?: MaybeArray<JoinArgs<T> | JoinCallbackArg<T>>;
625
+ }) | QueryBase | RawExpression | ((q: WhereQueryBuilder<T>) => WhereQueryBuilder);
626
+ declare type WhereInColumn<T extends QueryBase> = keyof T['selectable'] | [keyof T['selectable'], ...(keyof T['selectable'])[]];
627
+ declare type WhereInValues<T extends QueryBase, Column extends WhereInColumn<T>> = Column extends keyof T['selectable'] ? T['selectable'][Column]['column']['type'][] | Query | RawExpression : ({
628
+ [I in keyof Column]: Column[I] extends keyof T['selectable'] ? T['selectable'][Column[I]]['column']['type'] : never;
629
+ } & {
630
+ length: Column extends {
631
+ length: number;
632
+ } ? Column['length'] : never;
633
+ })[] | Query | RawExpression;
634
+ declare type WhereResult<T extends QueryBase> = T & {
635
+ hasWhere: true;
750
636
  };
751
- declare type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> = {
752
- disconnect: boolean;
753
- } | {
754
- set: WhereArg<Rel['model']>;
755
- } | {
756
- delete: boolean;
757
- } | {
758
- update: UpdateData<Rel['model']>;
759
- } | {
760
- create: InsertData<Rel['nestedCreateQuery']>;
761
- } | (T['returnType'] extends 'all' ? never : {
762
- upsert: {
763
- update: UpdateData<Rel['model']>;
764
- create: InsertData<Rel['nestedCreateQuery']>;
765
- };
766
- });
767
- declare type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> = {
768
- disconnect: boolean;
769
- } | {
770
- delete: boolean;
771
- } | {
772
- update: UpdateData<Rel['model']>;
773
- } | (T['returnType'] extends 'all' ? never : {
774
- set: WhereArg<Rel['model']>;
775
- } | {
776
- upsert: {
777
- update: UpdateData<Rel['model']>;
778
- create: InsertData<Rel['nestedCreateQuery']>;
779
- };
780
- } | {
781
- create: InsertData<Rel['nestedCreateQuery']>;
782
- });
783
- declare type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
784
- disconnect?: MaybeArray<WhereArg<Rel['model']>>;
785
- delete?: MaybeArray<WhereArg<Rel['model']>>;
786
- update?: {
787
- where: MaybeArray<WhereArg<Rel['model']>>;
788
- data: UpdateData<Rel['model']>;
789
- };
790
- } & (T['returnType'] extends 'all' ? EmptyObject : {
791
- set?: MaybeArray<WhereArg<Rel['model']>>;
792
- create?: InsertData<Rel['nestedCreateQuery']>[];
793
- });
794
- declare type UpdateHasAndBelongsToManyData<Rel extends HasAndBelongsToManyRelation> = {
795
- disconnect?: MaybeArray<WhereArg<Rel['model']>>;
796
- set?: MaybeArray<WhereArg<Rel['model']>>;
797
- delete?: MaybeArray<WhereArg<Rel['model']>>;
798
- update?: {
799
- where: MaybeArray<WhereArg<Rel['model']>>;
800
- data: UpdateData<Rel['model']>;
801
- };
802
- create?: InsertData<Rel['nestedCreateQuery']>[];
637
+ declare type WhereInArg<T extends Pick<Query, 'selectable'>> = {
638
+ [K in keyof T['selectable']]?: T['selectable'][K]['column']['type'][] | Query | RawExpression;
803
639
  };
804
- declare type UpdateArgs<T extends Query, ForceAll extends boolean> = (T['hasWhere'] extends true ? true : ForceAll) extends true ? [update: UpdateData<T>] : [update: UpdateData<T>, forceAll: true];
805
- declare type UpdateRawArgs<T extends Query, ForceAll extends boolean> = (T['hasWhere'] extends true ? true : ForceAll) extends true ? [update: RawExpression] : [update: RawExpression, forceAll: true];
806
- declare type UpdateResult<T extends Query> = T['hasSelect'] extends false ? SetQueryReturnsRowCount<T> : T;
807
- declare type ChangeCountArg<T extends Query> = keyof T['shape'] | Partial<Record<keyof T['shape'], number>>;
808
- declare class Update {
809
- update<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
810
- _update<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
811
- updateRaw<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateRawArgs<T, ForceAll>): UpdateResult<T>;
812
- _updateRaw<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateRawArgs<T, ForceAll>): UpdateResult<T>;
813
- updateOrThrow<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
814
- _updateOrThrow<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
815
- increment<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
816
- _increment<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
817
- decrement<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
818
- _decrement<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
640
+ declare const addWhere: <T extends Where>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
641
+ declare const addWhereNot: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
642
+ declare const addOr: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
643
+ declare const addOrNot: <T extends QueryBase>(q: T, args: WhereArg<T>[]) => WhereResult<T>;
644
+ declare const addWhereIn: <T extends QueryBase>(q: T, and: boolean, arg: unknown, values: unknown[] | unknown[][] | Query | RawExpression | undefined, not?: boolean) => WhereResult<T>;
645
+ declare abstract class Where implements QueryBase {
646
+ abstract clone<T extends this>(this: T): T;
647
+ abstract selectable: SelectableBase;
648
+ abstract shape: ColumnsShape;
649
+ abstract relations: RelationsBase;
650
+ abstract withData: WithDataBase;
651
+ abstract __model: Query;
652
+ query: QueryData;
653
+ table?: string;
654
+ tableAlias?: string;
655
+ where<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
656
+ _where<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
657
+ whereNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
658
+ _whereNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
659
+ and<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
660
+ _and<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
661
+ andNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
662
+ _andNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
663
+ or<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
664
+ _or<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
665
+ orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
666
+ _orNot<T extends Where>(this: T, ...args: WhereArg<T>[]): WhereResult<T>;
667
+ whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): T;
668
+ whereIn<T extends Where>(this: T, arg: WhereInArg<T>): T;
669
+ _whereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
670
+ _whereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
671
+ orWhereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
672
+ orWhereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
673
+ _orWhereIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
674
+ _orWhereIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
675
+ whereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
676
+ whereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
677
+ _whereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
678
+ _whereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
679
+ orWhereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
680
+ orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
681
+ _orWhereNotIn<T extends Where, Column extends WhereInColumn<T>>(this: T, column: Column, values: WhereInValues<T, Column>): WhereResult<T>;
682
+ _orWhereNotIn<T extends Where>(this: T, arg: WhereInArg<T>): WhereResult<T>;
683
+ whereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
684
+ whereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
685
+ _whereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
686
+ _whereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
687
+ orWhereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
688
+ orWhereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
689
+ _orWhereExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
690
+ _orWhereExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
691
+ whereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
692
+ whereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
693
+ _whereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
694
+ _whereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
695
+ orWhereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
696
+ orWhereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
697
+ _orWhereNotExists<T extends Where, Args extends JoinArgs<T>>(this: T, ...args: Args): WhereResult<T>;
698
+ _orWhereNotExists<T extends Where, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): WhereResult<T>;
699
+ }
700
+ declare class WhereQueryBuilder<Q extends QueryBase = QueryBase> extends Where implements QueryBase {
701
+ query: QueryData;
702
+ selectable: Q['selectable'];
703
+ shape: Q['shape'];
704
+ relations: Q['relations'];
705
+ __model: Query;
706
+ withData: {};
707
+ constructor(q: QueryBase | string, shape: ColumnsShape);
708
+ clone<T extends this>(this: T): T;
709
+ }
710
+
711
+ declare type WithSelectable<T extends QueryBase, W extends keyof T['withData']> = T['withData'][W] extends WithDataItem ? StringKey<keyof T['withData'][W]['shape']> | `${T['withData'][W]['table']}.${StringKey<keyof T['withData'][W]['shape']>}` : never;
712
+ declare type JoinArgs<T extends QueryBase, Q extends Query = Query, R extends keyof T['relations'] = keyof T['relations'], W extends keyof T['withData'] = keyof T['withData']> = [relation: R] | [
713
+ query: Q,
714
+ conditions: Record<Selectable<Q>, Selectable<T> | RawExpression> | RawExpression
715
+ ] | [
716
+ withAlias: W,
717
+ conditions: Record<WithSelectable<T, W>, Selectable<T> | RawExpression> | RawExpression
718
+ ] | [
719
+ query: Q,
720
+ leftColumn: Selectable<Q> | RawExpression,
721
+ rightColumn: Selectable<T> | RawExpression
722
+ ] | [
723
+ withAlias: W,
724
+ leftColumn: WithSelectable<T, W> | RawExpression,
725
+ rightColumn: Selectable<T> | RawExpression
726
+ ] | [
727
+ query: Q,
728
+ leftColumn: Selectable<Q> | RawExpression,
729
+ op: string,
730
+ rightColumn: Selectable<T> | RawExpression
731
+ ] | [
732
+ withAlias: W,
733
+ leftColumn: WithSelectable<T, W> | RawExpression,
734
+ op: string,
735
+ rightColumn: Selectable<T> | RawExpression
736
+ ];
737
+ declare type JoinResult<T extends Query, Args extends JoinArgs<T>, A extends Query | keyof T['relations'] = Args[0]> = AddQueryJoinedTable<T, A extends Query ? A : T['relations'] extends Record<string, Relation> ? A extends keyof T['relations'] ? T['relations'][A]['model'] : A extends keyof T['withData'] ? T['withData'][A] extends WithDataItem ? {
738
+ table: T['withData'][A]['table'];
739
+ tableAlias: undefined;
740
+ result: T['withData'][A]['shape'];
741
+ } : never : never : never>;
742
+ declare type JoinCallbackArg<T extends QueryBase> = Query | keyof T['withData'] | keyof T['relations'];
743
+ declare type JoinCallback<T extends QueryBase, Arg extends JoinCallbackArg<T>> = (q: OnQueryBuilder<T, Arg extends keyof T['withData'] ? T['withData'][Arg] extends WithDataItem ? {
744
+ query: QueryData;
745
+ table: T['withData'][Arg]['table'];
746
+ tableAlias: undefined;
747
+ clone(): QueryBase;
748
+ selectable: {
749
+ [K in keyof T['withData'][Arg]['shape'] as `${T['withData'][Arg]['table']}.${StringKey<K>}`]: {
750
+ as: StringKey<K>;
751
+ column: T['withData'][Arg]['shape'][K];
752
+ };
753
+ };
754
+ shape: T['withData'][Arg]['shape'];
755
+ __model: Query;
756
+ relations: RelationsBase;
757
+ withData: WithDataBase;
758
+ } : never : Arg extends Query ? Arg : T['relations'] extends Record<string, Relation> ? Arg extends keyof T['relations'] ? T['relations'][Arg]['model'] : never : never>) => OnQueryBuilder;
759
+ declare type JoinCallbackResult<T extends Query, Arg extends JoinCallbackArg<T>> = AddQueryJoinedTable<T, Arg extends Query ? Arg : T['relations'] extends Record<string, Relation> ? Arg extends keyof T['relations'] ? T['relations'][Arg]['model'] : Arg extends keyof T['withData'] ? T['withData'][Arg] extends WithDataItem ? {
760
+ table: T['withData'][Arg]['table'];
761
+ tableAlias: undefined;
762
+ result: T['withData'][Arg]['shape'];
763
+ } : never : never : never>;
764
+ declare class Join {
765
+ join<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
766
+ join<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
767
+ _join<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
768
+ _join<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
769
+ innerJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
770
+ innerJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
771
+ _innerJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
772
+ _innerJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
773
+ leftJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
774
+ leftJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
775
+ _leftJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
776
+ _leftJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
777
+ leftOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
778
+ leftOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
779
+ _leftOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
780
+ _leftOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
781
+ rightJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
782
+ rightJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
783
+ _rightJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
784
+ _rightJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
785
+ rightOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
786
+ rightOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
787
+ _rightOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
788
+ _rightOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
789
+ fullOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
790
+ fullOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
791
+ _fullOuterJoin<T extends Query, Args extends JoinArgs<T>>(this: T, ...args: Args): JoinResult<T, Args>;
792
+ _fullOuterJoin<T extends Query, Arg extends JoinCallbackArg<T>>(this: T, arg: Arg, cb: JoinCallback<T, Arg>): JoinCallbackResult<T, Arg>;
819
793
  }
820
-
821
- declare type UpsertData<T extends Query> = {
822
- update: UpdateData<T>;
823
- create: InsertData<T>;
824
- };
825
- declare type UpsertResult<T extends Query> = T['hasSelect'] extends true ? SetQueryReturnsOne<T> : SetQueryReturnsVoid<T>;
826
- declare type UpsertThis = WhereResult<Query> & {
827
- returnType: 'one' | 'oneOrThrow';
828
- };
829
- declare class QueryUpsert {
830
- upsert<T extends UpsertThis>(this: T, data: UpsertData<T>): UpsertResult<T>;
831
- _upsert<T extends UpsertThis>(this: T, data: UpsertData<T>): UpsertResult<T>;
794
+ declare type OnArgs<Q extends {
795
+ selectable: SelectableBase;
796
+ }> = [leftColumn: keyof Q['selectable'], rightColumn: keyof Q['selectable']] | [
797
+ leftColumn: keyof Q['selectable'],
798
+ op: string,
799
+ rightColumn: keyof Q['selectable']
800
+ ];
801
+ declare const pushQueryOn: <T extends QueryBase>(q: T, joinFrom: QueryBase | string, joinTo: QueryBase | string, ...on: OnArgs<QueryBase>) => T;
802
+ declare const pushQueryOrOn: typeof pushQueryOn;
803
+ declare const addQueryOn: typeof pushQueryOrOn;
804
+ declare const addQueryOrOn: typeof pushQueryOrOn;
805
+ declare type OnJsonPathEqualsArgs<T extends QueryBase> = [
806
+ leftColumn: keyof T['selectable'],
807
+ leftPath: string,
808
+ rightColumn: keyof T['selectable'],
809
+ rightPath: string
810
+ ];
811
+ declare class OnQueryBuilder<S extends QueryBase = QueryBase, J extends QueryBase = QueryBase> extends WhereQueryBuilder<Omit<J, 'selectable'> & {
812
+ selectable: Omit<S['selectable'], keyof S['shape']> & J['selectable'];
813
+ }> implements QueryBase {
814
+ joinTo: QueryBase;
815
+ constructor(q: QueryBase | string, shape: ColumnsShape, joinTo: QueryBase);
816
+ on<T extends this>(this: T, ...args: OnArgs<T>): T;
817
+ _on<T extends this>(this: T, ...args: OnArgs<T>): T;
818
+ orOn<T extends this>(this: T, ...args: OnArgs<T>): T;
819
+ _orOn<T extends this>(this: T, ...args: OnArgs<T>): T;
820
+ onJsonPathEquals<T extends this>(this: T, ...args: OnJsonPathEqualsArgs<T>): T;
821
+ _onJsonPathEquals<T extends this>(this: T, ...args: OnJsonPathEqualsArgs<T>): T;
832
822
  }
833
823
 
834
- declare function timestamps<T extends ColumnType>(this: {
835
- timestamp(): T;
836
- }): {
837
- createdAt: T & {
838
- hasDefault: true;
839
- };
840
- updatedAt: T & {
841
- hasDefault: true;
842
- };
843
- };
844
-
845
824
  declare type DbTableOptions = {
846
825
  schema?: string;
847
826
  } & QueryLogOptions;
848
827
  interface Db<Table extends string | undefined = undefined, Shape extends ColumnsShape = Record<string, never>, Relations extends Query['relations'] = Query['relations']> extends QueryMethods {
849
828
  new (adapter: Adapter, queryBuilder: Db, table?: Table, shape?: Shape, options?: DbTableOptions): this;
850
829
  adapter: Adapter;
830
+ columns: (keyof ColumnShapeOutput<Shape>)[];
851
831
  queryBuilder: Db;
852
832
  whereQueryBuilder: Query['whereQueryBuilder'];
833
+ onQueryBuilder: Query['onQueryBuilder'];
853
834
  table: Table;
854
835
  shape: Shape;
855
836
  schema: TableSchema<Shape>;
856
837
  type: ColumnShapeOutput<Shape>;
857
838
  inputType: ColumnShapeInput<Shape>;
858
- returnType: 'all';
859
- then: ThenResult<Pick<ColumnShapeOutput<Shape>, DefaultSelectColumns<Shape>[number]>[]>;
860
839
  query: QueryData;
861
- columns: (keyof ColumnShapeOutput<Shape>)[];
862
- defaultSelectColumns: DefaultSelectColumns<Shape>;
863
- columnsParsers?: ColumnsParsers;
864
840
  result: Pick<Shape, DefaultSelectColumns<Shape>[number]>;
865
- hasSelect: false;
841
+ hasSelect: Query['hasSelect'];
866
842
  hasWhere: boolean;
867
843
  selectable: {
868
844
  [K in keyof Shape]: {
@@ -875,588 +851,363 @@ interface Db<Table extends string | undefined = undefined, Shape extends Columns
875
851
  column: Shape[K];
876
852
  };
877
853
  };
854
+ returnType: Query['returnType'];
855
+ then: ThenResult<Pick<ColumnShapeOutput<Shape>, DefaultSelectColumns<Shape>[number]>[]>;
878
856
  tableAlias: undefined;
879
- windows: PropertyKey[];
880
- withData: Query['withData'];
881
857
  joinedTables: Query['joinedTables'];
858
+ windows: Query['windows'];
859
+ defaultSelectColumns: DefaultSelectColumns<Shape>;
860
+ columnsParsers?: ColumnsParsers;
882
861
  relations: Relations;
862
+ withData: Query['withData'];
883
863
  [defaultsKey]: Record<{
884
864
  [K in keyof Shape]: Shape[K]['hasDefault'] extends true ? K : never;
885
865
  }[keyof Shape], true>;
886
866
  }
887
- declare class Db<Table extends string | undefined = undefined, Shape extends ColumnsShape = Record<string, never>, Relations extends Query['relations'] = Query['relations']> implements Query {
888
- adapter: Adapter;
889
- queryBuilder: Db;
890
- table: Table;
891
- shape: Shape;
892
- whereQueryBuilder: typeof WhereQueryBuilder;
893
- onQueryBuilder: typeof OnQueryBuilder;
894
- constructor(adapter: Adapter, queryBuilder: Db, table: Table, shape: Shape, options: DbTableOptions);
895
- }
896
- declare type DbResult<CT extends ColumnTypesBase> = Db & {
897
- <Table extends string, Shape extends ColumnsShape = ColumnsShape>(table: Table, shape?: ((t: CT) => Shape) | Shape, options?: DbTableOptions): Db<Table, Shape>;
898
- adapter: Adapter;
899
- close: Adapter['close'];
900
- };
901
- declare type DbOptions<CT extends ColumnTypesBase = ColumnTypes> = ({
902
- adapter: Adapter;
903
- } | Omit<AdapterOptions, 'log'>) & QueryLogOptions & {
904
- columnTypes?: CT;
905
- };
906
- declare const createDb: <CT extends ColumnTypesBase = {
907
- smallint: () => SmallIntColumn;
908
- integer: () => IntegerColumn;
909
- bigint: () => BigIntColumn;
910
- numeric: <Precision extends number | undefined = undefined, Scale extends number | undefined = undefined>(precision?: Precision | undefined, scale?: Scale | undefined) => DecimalColumn<Precision, Scale>;
911
- decimal: <Precision_1 extends number | undefined = undefined, Scale_1 extends number | undefined = undefined>(precision?: Precision_1 | undefined, scale?: Scale_1 | undefined) => DecimalColumn<Precision_1, Scale_1>;
912
- real: () => RealColumn;
913
- doublePrecision: () => DoublePrecisionColumn;
914
- smallSerial: () => SmallSerialColumn;
915
- serial: () => SerialColumn;
916
- bigSerial: () => BigSerialColumn;
917
- money: () => MoneyColumn;
918
- varchar: <Limit extends number | undefined = undefined>(limit?: Limit | undefined) => VarCharColumn<Limit>;
919
- char: <Limit_1 extends number | undefined = undefined>(limit?: Limit_1 | undefined) => CharColumn<Limit_1>;
920
- text: () => TextColumn;
921
- string: () => TextColumn;
922
- bytea: () => ByteaColumn;
923
- date: () => DateColumn;
924
- timestamp: <Precision_2 extends number | undefined = undefined>(precision?: Precision_2 | undefined) => TimestampColumn<Precision_2>;
925
- timestampWithTimeZone: <Precision_3 extends number | undefined = undefined>(precision?: Precision_3 | undefined) => TimestampWithTimeZoneColumn<Precision_3>;
926
- time: <Precision_4 extends number | undefined = undefined>(precision?: Precision_4 | undefined) => TimeColumn<Precision_4>;
927
- timeWithTimeZone: <Precision_5 extends number | undefined = undefined>(precision?: Precision_5 | undefined) => TimeWithTimeZoneColumn<Precision_5>;
928
- interval: <Fields extends string | undefined = undefined, Precision_6 extends number | undefined = undefined>(fields?: Fields | undefined, precision?: Precision_6 | undefined) => IntervalColumn<Fields, Precision_6>;
929
- boolean: () => BooleanColumn;
930
- enum: <U extends string, T extends [U, ...U[]]>(dataType: string, type: T) => EnumColumn<U, T>;
931
- point: () => PointColumn;
932
- line: () => LineColumn;
933
- lseg: () => LsegColumn;
934
- box: () => BoxColumn;
935
- path: () => PathColumn;
936
- polygon: () => PolygonColumn;
937
- circle: () => CircleColumn;
938
- cidr: () => CidrColumn;
939
- inet: () => InetColumn;
940
- macaddr: () => MacAddrColumn;
941
- macaddr8: () => MacAddr8Column;
942
- bit: <Length extends number>(length: Length) => BitColumn<Length>;
943
- bitVarying: <Length_1 extends number | undefined = undefined>(length?: Length_1 | undefined) => BitVaryingColumn<Length_1 | undefined>;
944
- tsvector: () => TsVectorColumn;
945
- tsquery: () => TsQueryColumn;
946
- uuid: () => UUIDColumn;
947
- xml: () => XMLColumn;
948
- json: <Type extends JSONTypeAny>(schemaOrFn: Type | ((j: {
949
- set: <Value extends JSONTypeAny>(valueType: Value) => JSONSet<Value>;
950
- tuple: <T_1 extends [] | JSONTupleItems, Rest extends JSONTypeAny | null = null>(items: T_1, rest?: Rest) => JSONTuple<T_1, Rest>;
951
- union: <T_2 extends [JSONTypeAny, JSONTypeAny, ...JSONTypeAny[]]>(types: T_2) => JSONUnion<T_2>;
952
- any: () => JSONAny;
953
- bigint: () => JSONBigInt;
954
- boolean: () => JSONBoolean;
955
- date: () => JSONDate;
956
- nan: () => JSONNaN;
957
- never: () => JSONNever;
958
- null: () => JSONNull;
959
- number: () => JSONNumber;
960
- string: () => JSONString;
961
- undefined: () => JSONUndefined;
962
- unknown: () => JSONUnknown;
963
- void: () => JSONVoid;
964
- array: <Type_1 extends JSONTypeAny>(element: Type_1) => JSONArray<Type_1, "many">;
965
- discriminatedUnion: <Discriminator extends string, DiscriminatorValue extends Primitive, Types extends [JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, JSONDiscriminatedObject<Discriminator, DiscriminatorValue>, ...JSONDiscriminatedObject<Discriminator, DiscriminatorValue>[]]>(discriminator: Discriminator, options: Types) => JSONDiscriminatedUnion<Discriminator, DiscriminatorValue, Types[number]>;
966
- enum: <U_1 extends string, T_3 extends [U_1, ...U_1[]]>(options: T_3) => JSONEnum<U_1, T_3>;
967
- instanceOf: <T_4 extends new (...args: any[]) => any>(cls: T_4) => JSONInstanceOf<T_4>;
968
- intersection: <Left extends JSONTypeAny, Right extends JSONTypeAny>(left: Left, right: Right) => JSONIntersection<Left, Right>;
969
- lazy: <T_5 extends JSONTypeAny>(fn: () => T_5) => JSONLazy<T_5>;
970
- literal: <T_6 extends Primitive>(value: T_6) => JSONLiteral<T_6>;
971
- map: <Key extends JSONTypeAny, Value_1 extends JSONTypeAny>(keyType: Key, valueType: Value_1) => JSONMap<Key, Value_1>;
972
- nativeEnum: <T_7 extends EnumLike>(givenEnum: T_7) => JSONNativeEnum<T_7>;
973
- nullable: <T_8 extends JSONTypeAny>(type: T_8) => JSONNullable<T_8>;
974
- nullish: <T_9 extends JSONTypeAny>(type: T_9) => JSONNullish<T_9>;
975
- object: <T_10 extends JSONObjectShape, UnknownKeys extends UnknownKeysParam = "strip", Catchall extends JSONTypeAny = JSONTypeAny>(shape: T_10) => JSONObject<T_10, UnknownKeys, Catchall, JSONTypeAny extends Catchall ? addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }> extends infer T_11 ? { [k in keyof T_11]: addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }>[k]; } : never : (addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }> extends infer T_11 ? { [k in keyof T_11]: addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }>[k]; } : never) & {
976
- [k: string]: Catchall["type"];
977
- } extends infer T_12 ? { [k_2 in keyof T_12]: ((addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }> extends infer T_11 ? { [k in keyof T_11]: addQuestionMarks<{ [k_1 in keyof T_10]: T_10[k_1]["type"]; }>[k]; } : never) & {
978
- [k: string]: Catchall["type"];
979
- })[k_2]; } : never>;
980
- optional: <T_13 extends JSONTypeAny>(type: T_13) => JSONOptional<T_13>;
981
- record: typeof record;
982
- }) => Type)) => JSONColumn<Type>;
983
- jsonText: () => JSONTextColumn;
984
- array: <Item extends ColumnType<unknown, Operators, unknown>>(item: Item) => ArrayColumn<Item>;
985
- timestamps: typeof timestamps;
986
- primaryKey(columns: string[], options?: {
987
- name?: string | undefined;
988
- } | undefined): {};
989
- index(columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): {};
990
- unique(columns: MaybeArray<string | IndexColumnOptions>, options?: IndexOptions): {};
991
- foreignKey: {
992
- <Model extends ForeignKeyModelWithColumns, Columns extends [Exclude<keyof InstanceType<Model>["columns"]["shape"], number | symbol>, ...Exclude<keyof InstanceType<Model>["columns"]["shape"], number | symbol>[]]>(columns: string[], fn: () => Model, foreignColumns: Columns, options?: ForeignKeyOptions | undefined): {};
993
- <Table extends string, Columns_1 extends [string, ...string[]]>(columns: string[], table: Table, foreignColumns: Columns_1, options?: ForeignKeyOptions | undefined): {};
994
- };
995
- }>({ log, logger, columnTypes: ct, ...options }: DbOptions<CT>) => DbResult<CT>;
996
-
997
- declare type WithArgsOptions = Omit<WithOptions, 'columns'> & {
998
- columns?: boolean | string[];
999
- };
1000
- declare type WithArgs = [string, ColumnsShape, RawExpression] | [string, WithArgsOptions, ColumnsShape, RawExpression] | [string, Query | ((qb: Db) => Query)] | [string, WithArgsOptions, Query | ((qb: Db) => Query)];
1001
- declare type WithShape<Args extends WithArgs> = Args[1] extends Query ? Args[1]['result'] : Args[1] extends (qb: Db) => Query ? ReturnType<Args[1]>['result'] : Args[2] extends Query ? Args[2]['result'] : Args[2] extends (qb: Db) => Query ? ReturnType<Args[2]>['result'] : Args[1] extends ColumnsShape ? Args[1] : Args[2] extends ColumnsShape ? Args[2] : Args[2] extends (t: ColumnTypes) => ColumnsShape ? ReturnType<Args[2]> : never;
1002
- declare type WithResult<T extends Query, Args extends WithArgs, Shape extends ColumnsShape> = AddQueryWith<T, {
1003
- table: Args[0];
1004
- shape: Shape;
1005
- type: ColumnShapeOutput<Shape>;
1006
- }>;
1007
- declare class With {
1008
- with<T extends Query, Args extends WithArgs, Shape extends ColumnsShape = WithShape<Args>>(this: T, ...args: Args): WithResult<T, Args, Shape>;
1009
- _with<T extends Query, Args extends WithArgs, Shape extends ColumnsShape = WithShape<Args>>(this: T, ...args: Args): WithResult<T, Args, Shape>;
1010
- }
1011
-
1012
- declare type NestedInsertOneItem = {
1013
- create?: Record<string, unknown>;
1014
- connect?: WhereArg<QueryBase>;
1015
- connectOrCreate?: {
1016
- where: WhereArg<QueryBase>;
1017
- create: Record<string, unknown>;
1018
- };
1019
- };
1020
- declare type NestedInsertManyItems = {
1021
- create?: Record<string, unknown>[];
1022
- connect?: WhereArg<QueryBase>[];
1023
- connectOrCreate?: {
1024
- where: WhereArg<QueryBase>;
1025
- create: Record<string, unknown>;
1026
- }[];
1027
- };
1028
- declare type NestedInsertItem = NestedInsertOneItem | NestedInsertManyItems;
1029
- declare type BelongsToNestedInsert = (query: Query, relationData: NestedInsertOneItem[]) => Promise<Record<string, unknown>[]>;
1030
- declare type HasOneNestedInsert = (query: Query, data: [
1031
- selfData: Record<string, unknown>,
1032
- relationData: NestedInsertOneItem
1033
- ][]) => Promise<void>;
1034
- declare type HasManyNestedInsert = (query: Query, data: [
1035
- selfData: Record<string, unknown>,
1036
- relationData: NestedInsertManyItems
1037
- ][]) => Promise<void>;
1038
- declare type NestedUpdateOneItem = {
1039
- disconnect?: boolean;
1040
- set?: WhereArg<QueryBase>;
1041
- delete?: boolean;
1042
- update?: UpdateData<Query>;
1043
- upsert?: {
1044
- update: UpdateData<Query>;
1045
- create: Record<string, unknown>;
1046
- };
1047
- create: Record<string, unknown>;
1048
- };
1049
- declare type NestedUpdateManyItems = {
1050
- disconnect?: MaybeArray<WhereArg<QueryBase>>;
1051
- set?: MaybeArray<WhereArg<QueryBase>>;
1052
- delete?: MaybeArray<WhereArg<QueryBase>>;
1053
- update?: {
1054
- where: MaybeArray<WhereArg<QueryBase>>;
1055
- data: UpdateData<Query>;
1056
- };
1057
- create: Record<string, unknown>[];
1058
- };
1059
- declare type NestedUpdateItem = NestedUpdateOneItem | NestedUpdateManyItems;
1060
- declare type BelongsToNestedUpdate = (q: Query, update: Record<string, unknown>, params: NestedUpdateOneItem, state: {
1061
- updateLater?: Record<string, unknown>;
1062
- updateLaterPromises?: Promise<void>[];
1063
- }) => boolean;
1064
- declare type HasOneNestedUpdate = (query: Query, data: Record<string, unknown>[], relationData: NestedUpdateOneItem) => Promise<void>;
1065
- declare type HasManyNestedUpdate = (query: Query, data: Record<string, unknown>[], relationData: NestedUpdateManyItems) => Promise<void>;
1066
- declare type BaseRelation = {
1067
- type: string;
1068
- key: string;
1069
- model: QueryWithTable;
1070
- query: QueryWithTable;
1071
- joinQuery(fromQuery: QueryBase, toQuery: Query): Query;
1072
- nestedCreateQuery: Query;
1073
- nestedInsert?: BelongsToNestedInsert | HasOneNestedInsert | HasManyNestedInsert;
1074
- nestedUpdate?: BelongsToNestedUpdate | HasOneNestedUpdate | HasManyNestedUpdate;
1075
- primaryKey: string;
1076
- options: {
1077
- scope?(q: QueryWithTable): QueryWithTable;
1078
- required?: boolean;
1079
- };
1080
- };
1081
- interface BelongsToRelation extends BaseRelation {
1082
- type: 'belongsTo';
1083
- returns: 'one';
1084
- options: BaseRelation['options'] & {
1085
- primaryKey: string;
1086
- foreignKey: string;
1087
- };
1088
- }
1089
- interface HasOneRelation extends BaseRelation {
1090
- type: 'hasOne';
1091
- returns: 'one';
1092
- options: BaseRelation['options'] & ({
1093
- primaryKey: string;
1094
- foreignKey: string;
1095
- } | {
1096
- through: string;
1097
- source: string;
1098
- });
1099
- }
1100
- interface HasManyRelation extends BaseRelation {
1101
- type: 'hasMany';
1102
- returns: 'many';
1103
- options: BaseRelation['options'] & ({
1104
- primaryKey: string;
1105
- foreignKey: string;
1106
- } | {
1107
- through: string;
1108
- source: string;
1109
- });
1110
- }
1111
- interface HasAndBelongsToManyRelation extends BaseRelation {
1112
- type: 'hasAndBelongsToMany';
1113
- returns: 'many';
1114
- options: BaseRelation['options'] & {
1115
- primaryKey: string;
1116
- foreignKey: string;
1117
- associationPrimaryKey: string;
1118
- associationForeignKey: string;
1119
- joinTable: string;
1120
- };
1121
- }
1122
- declare type Relation = BelongsToRelation | HasOneRelation | HasManyRelation | HasAndBelongsToManyRelation;
1123
- declare type RelationsBase = Record<never, Relation>;
1124
- declare type relationQueryKey = typeof relationQueryKey;
1125
- declare const relationQueryKey: unique symbol;
1126
- declare type isRequiredRelationKey = typeof isRequiredRelationKey;
1127
- declare const isRequiredRelationKey: unique symbol;
1128
- declare type RelationQueryBase = Query & {
1129
- [relationQueryKey]: string;
1130
- [isRequiredRelationKey]: boolean;
1131
- };
1132
- declare type RelationQuery<RelationName extends PropertyKey = string, Params extends Record<string, unknown> = never, Populate extends string = never, T extends Query = Query, Required extends boolean = boolean, Q extends RelationQueryBase = Omit<T, 'tableAlias'> & {
1133
- tableAlias: RelationName extends string ? RelationName : never;
1134
- [isRequiredRelationKey]: Required;
1135
- [relationQueryKey]: string;
1136
- }> = ((params: Params) => Q & {
1137
- [defaultsKey]: Record<Populate, true>;
1138
- }) & Q;
1139
-
1140
- declare type Sql = {
1141
- text: string;
1142
- values: unknown[];
1143
- };
1144
- declare const queryKeysOfNotSimpleQuery: (keyof SelectQueryData)[];
1145
- declare type CommonQueryData = {
867
+ declare class Db<Table extends string | undefined = undefined, Shape extends ColumnsShape = Record<string, never>, Relations extends Query['relations'] = Query['relations']> implements Query {
1146
868
  adapter: Adapter;
1147
- handleResult(q: Query, result: QueryResult): Promise<unknown>;
1148
- returnType: QueryReturnType;
1149
- [relationQueryKey]?: string;
1150
- inTransaction?: boolean;
1151
- wrapInTransaction?: boolean;
1152
- throwOnNotFound?: boolean;
1153
- take?: boolean;
1154
- with?: WithItem[];
1155
- withShapes?: Record<string, ColumnsShape>;
1156
- schema?: string;
1157
- select?: SelectItem[];
1158
- as?: string;
1159
- from?: string | Query | RawExpression;
1160
- and?: WhereItem[];
1161
- or?: WhereItem[][];
1162
- coalesceValue?: unknown | RawExpression;
1163
- parsers?: ColumnsParsers;
1164
- notFoundDefault?: unknown;
1165
- defaults?: Record<string, unknown>;
1166
- beforeQuery?: BeforeCallback[];
1167
- afterQuery?: AfterCallback[];
1168
- log?: QueryLogObject;
1169
- logger: QueryLogger;
1170
- [toSqlCacheKey]?: Sql;
1171
- };
1172
- declare type SelectQueryData = CommonQueryData & {
1173
- type: undefined;
1174
- distinct?: Expression[];
1175
- fromOnly?: boolean;
1176
- join?: JoinItem[];
1177
- joinedParsers?: Record<string, ColumnsParsers>;
1178
- group?: (string | RawExpression)[];
1179
- having?: HavingItem[];
1180
- havingOr?: HavingItem[][];
1181
- window?: WindowItem[];
1182
- union?: {
1183
- arg: UnionItem;
1184
- kind: UnionKind;
1185
- wrap?: boolean;
1186
- }[];
1187
- order?: OrderItem[];
1188
- limit?: number;
1189
- offset?: number;
1190
- for?: {
1191
- type: 'UPDATE' | 'NO KEY UPDATE' | 'SHARE' | 'KEY SHARE';
1192
- tableNames?: string[] | RawExpression;
1193
- mode?: 'NO WAIT' | 'SKIP LOCKED';
1194
- };
1195
- };
1196
- declare type InsertQueryData = CommonQueryData & {
1197
- type: 'insert';
1198
- columns: string[];
1199
- values: unknown[][] | RawExpression;
1200
- using?: JoinItem[];
1201
- join?: JoinItem[];
1202
- joinedParsers?: Record<string, ColumnsParsers>;
1203
- onConflict?: {
1204
- type: 'ignore';
1205
- expr?: OnConflictItem;
1206
- } | {
1207
- type: 'merge';
1208
- expr?: OnConflictItem;
1209
- update?: OnConflictMergeUpdate;
1210
- };
1211
- beforeInsert?: BeforeCallback[];
1212
- afterInsert?: AfterCallback[];
1213
- };
1214
- declare type UpdateQueryDataObject = Record<string, RawExpression | {
1215
- op: string;
1216
- arg: unknown;
1217
- } | unknown>;
1218
- declare type UpdatedAtDataInjector = (data: UpdateQueryDataItem[]) => UpdateQueryDataItem | void;
1219
- declare type UpdateQueryDataItem = UpdateQueryDataObject | RawExpression | UpdatedAtDataInjector;
1220
- declare type UpdateQueryData = CommonQueryData & {
1221
- type: 'update';
1222
- updateData: UpdateQueryDataItem[];
1223
- beforeUpdate?: BeforeCallback[];
1224
- afterUpdate?: AfterCallback[];
1225
- };
1226
- declare type DeleteQueryData = CommonQueryData & {
1227
- type: 'delete';
1228
- join?: JoinItem[];
1229
- joinedParsers?: Record<string, ColumnsParsers>;
1230
- beforeDelete?: BeforeCallback[];
1231
- afterDelete?: AfterCallback[];
1232
- };
1233
- declare type TruncateQueryData = CommonQueryData & {
1234
- type: 'truncate';
1235
- restartIdentity?: boolean;
1236
- cascade?: boolean;
869
+ queryBuilder: Db;
870
+ table: Table;
871
+ shape: Shape;
872
+ whereQueryBuilder: typeof WhereQueryBuilder;
873
+ onQueryBuilder: typeof OnQueryBuilder;
874
+ constructor(adapter: Adapter, queryBuilder: Db, table: Table, shape: Shape, options: DbTableOptions);
875
+ }
876
+ declare type DbResult<CT extends ColumnTypesBase> = Db & {
877
+ <Table extends string, Shape extends ColumnsShape = ColumnsShape>(table: Table, shape?: ((t: CT) => Shape) | Shape, options?: DbTableOptions): Db<Table, Shape>;
878
+ adapter: Adapter;
879
+ close: Adapter['close'];
1237
880
  };
1238
- declare type ColumnInfoQueryData = CommonQueryData & {
1239
- type: 'columnInfo';
1240
- column?: string;
881
+ declare type DbOptions<CT extends ColumnTypesBase> = ({
882
+ adapter: Adapter;
883
+ } | Omit<AdapterOptions, 'log'>) & QueryLogOptions & {
884
+ columnTypes: CT;
1241
885
  };
1242
- declare type QueryData = SelectQueryData | InsertQueryData | UpdateQueryData | DeleteQueryData | TruncateQueryData | ColumnInfoQueryData;
1243
- declare type WithItem = [
1244
- as: string,
1245
- options: WithOptions,
1246
- query: Query | RawExpression
1247
- ];
1248
- declare type WithOptions = {
1249
- columns?: string[];
1250
- recursive?: true;
1251
- materialized?: true;
1252
- notMaterialized?: true;
886
+ declare const createDb: <CT extends ColumnTypesBase>({ log, logger, columnTypes: ct, ...options }: DbOptions<CT>) => DbResult<CT>;
887
+
888
+ declare type WithArgsOptions = Omit<WithOptions, 'columns'> & {
889
+ columns?: boolean | string[];
1253
890
  };
1254
- declare type JsonItem<As extends string = string, Type extends ColumnType = ColumnType> = {
1255
- __json: [
1256
- kind: 'set',
1257
- as: As,
1258
- type: Type,
1259
- column: string | JsonItem,
891
+ declare type WithArgs = [string, ColumnsShape, RawExpression] | [string, WithArgsOptions, ColumnsShape, RawExpression] | [string, Query | ((qb: Db) => Query)] | [string, WithArgsOptions, Query | ((qb: Db) => Query)];
892
+ declare type WithShape<Args extends WithArgs> = Args[1] extends Query ? Args[1]['result'] : Args[1] extends (qb: Db) => Query ? ReturnType<Args[1]>['result'] : Args[2] extends Query ? Args[2]['result'] : Args[2] extends (qb: Db) => Query ? ReturnType<Args[2]>['result'] : Args[1] extends ColumnsShape ? Args[1] : Args[2] extends ColumnsShape ? Args[2] : Args[2] extends (t: ColumnTypes) => ColumnsShape ? ReturnType<Args[2]> : never;
893
+ declare type WithResult<T extends Query, Args extends WithArgs, Shape extends ColumnsShape> = AddQueryWith<T, {
894
+ table: Args[0];
895
+ shape: Shape;
896
+ type: ColumnShapeOutput<Shape>;
897
+ }>;
898
+ declare class With {
899
+ with<T extends Query, Args extends WithArgs, Shape extends ColumnsShape = WithShape<Args>>(this: T, ...args: Args): WithResult<T, Args, Shape>;
900
+ _with<T extends Query, Args extends WithArgs, Shape extends ColumnsShape = WithShape<Args>>(this: T, ...args: Args): WithResult<T, Args, Shape>;
901
+ }
902
+
903
+ declare type UnionArg<T extends Query> = (Omit<Query, 'result'> & {
904
+ result: T['result'];
905
+ }) | RawExpression;
906
+ declare class Union {
907
+ union<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
908
+ _union<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
909
+ unionAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
910
+ _unionAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
911
+ intersect<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
912
+ _intersect<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
913
+ intersectAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
914
+ _intersectAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
915
+ except<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
916
+ _except<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
917
+ exceptAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
918
+ _exceptAll<T extends Query>(this: T, args: UnionArg<T>[], wrap?: boolean): T;
919
+ }
920
+
921
+ declare type JsonColumnName<T extends Pick<Query, 'selectable'>> = StringKey<{
922
+ [K in keyof T['selectable']]: T['selectable'][K]['column']['dataType'] extends 'jsonb' ? K : never;
923
+ }[keyof T['selectable']]>;
924
+ declare type ColumnOrJsonMethod<T extends Query> = JsonColumnName<T> | JsonItem;
925
+ declare type JsonSetResult<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string, Type extends ColumnType = Column extends keyof T['shape'] ? T['shape'][Column] : Column extends JsonItem ? Column['__json'][2] : ColumnType> = JsonItem<As, Type> & (Type extends ColumnType ? AddQuerySelect<T, Record<As, Type>> : T);
926
+ declare type JsonPathQueryResult<T extends Query, As extends string, Type extends ColumnType> = JsonItem & AddQuerySelect<T, {
927
+ [K in As]: Type;
928
+ }>;
929
+ declare class Json {
930
+ json<T extends Query>(this: T): SetQueryReturnsValueOptional<T, StringColumn>;
931
+ _json<T extends Query>(this: T): SetQueryReturnsValueOptional<T, StringColumn>;
932
+ jsonSet<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
933
+ as?: As;
934
+ createIfMissing?: boolean;
935
+ }): JsonSetResult<T, Column, As>;
936
+ _jsonSet<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
937
+ as?: As;
938
+ createIfMissing?: boolean;
939
+ }): JsonSetResult<T, Column, As>;
940
+ jsonInsert<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, ...args: [
941
+ column: Column,
1260
942
  path: Array<string | number>,
1261
943
  value: unknown,
1262
944
  options?: {
1263
- createIfMissing?: boolean;
945
+ as?: As;
946
+ insertAfter?: boolean;
1264
947
  }
1265
- ] | [
1266
- kind: 'insert',
1267
- as: As,
1268
- type: Type,
1269
- column: string | JsonItem,
948
+ ]): JsonSetResult<T, Column, As>;
949
+ _jsonInsert<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, value: unknown, options?: {
950
+ as?: As;
951
+ insertAfter?: boolean;
952
+ }): JsonSetResult<T, Column, As>;
953
+ jsonRemove<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, ...args: [
954
+ column: Column,
1270
955
  path: Array<string | number>,
1271
- value: unknown,
1272
956
  options?: {
1273
- insertAfter?: boolean;
957
+ as?: As;
1274
958
  }
1275
- ] | [
1276
- kind: 'remove',
1277
- as: As,
1278
- type: Type,
1279
- column: string | JsonItem,
1280
- path: Array<string | number>
1281
- ] | [
1282
- kind: 'pathQuery',
1283
- as: As,
959
+ ]): JsonSetResult<T, Column, As>;
960
+ _jsonRemove<T extends Query, Column extends ColumnOrJsonMethod<T>, As extends string = Column extends JsonItem ? Column['__json'][1] : Column>(this: T, column: Column, path: Array<string | number>, options?: {
961
+ as?: As;
962
+ }): JsonSetResult<T, Column, As>;
963
+ jsonPathQuery<T extends Query, As extends string, Type extends ColumnType>(this: T, ...args: [
1284
964
  type: Type,
1285
- column: string | JsonItem,
965
+ column: ColumnOrJsonMethod<T>,
1286
966
  path: string,
967
+ as: As,
1287
968
  options?: {
1288
969
  vars?: string;
1289
970
  silent?: boolean;
1290
971
  }
1291
- ];
972
+ ]): JsonPathQueryResult<T, As, Type>;
973
+ _jsonPathQuery<T extends Query, As extends string, Type extends ColumnType>(this: T, type: Type, column: ColumnOrJsonMethod<T>, path: string, as: As, options?: {
974
+ vars?: string;
975
+ silent?: boolean;
976
+ }): JsonPathQueryResult<T, As, Type>;
977
+ }
978
+
979
+ declare type InsertData<T extends Query, DefaultKeys extends PropertyKey = keyof T[defaultsKey], Data = SetOptional<T['inputType'], DefaultKeys>> = [keyof T['relations']] extends [never] ? Data : OmitBelongsToForeignKeys<T['relations'], Data> & InsertRelationData<T>;
980
+ declare type OmitBelongsToForeignKeys<R extends RelationsBase, Data> = Omit<Data, {
981
+ [K in keyof R]: R[K] extends BelongsToRelation ? R[K]['options']['foreignKey'] : never;
982
+ }[keyof R]>;
983
+ declare type InsertRelationData<T extends Query> = {
984
+ [K in keyof T['relations']]: T['relations'][K] extends BelongsToRelation ? InsertBelongsToData<T, K, T['relations'][K]> : T['relations'][K] extends HasOneRelation ? InsertHasOneData<T, K, T['relations'][K]> : T['relations'][K] extends HasManyRelation | HasAndBelongsToManyRelation ? InsertHasManyData<T, K, T['relations'][K]> : EmptyObject;
985
+ }[keyof T['relations']];
986
+ declare type InsertBelongsToData<T extends Query, Key extends keyof T['relations'], Rel extends BelongsToRelation> = SetOptional<{
987
+ [K in Rel['options']['foreignKey']]: Rel['options']['foreignKey'] extends keyof T['inputType'] ? T['inputType'][Rel['options']['foreignKey']] : never;
988
+ }, keyof T[defaultsKey]> | {
989
+ [K in Key]: {
990
+ create: InsertData<Rel['nestedCreateQuery']>;
991
+ connect?: never;
992
+ connectOrCreate?: never;
993
+ } | {
994
+ create?: never;
995
+ connect: WhereArg<Rel['model']>;
996
+ connectOrCreate?: never;
997
+ } | {
998
+ create?: never;
999
+ connect?: never;
1000
+ connectOrCreate: {
1001
+ where: WhereArg<Rel['model']>;
1002
+ create: InsertData<Rel['nestedCreateQuery']>;
1003
+ };
1004
+ };
1292
1005
  };
1293
- declare type SelectItem = string | RelationQuery | AggregateItem | {
1294
- selectAs: Record<string, string | Query | RawExpression>;
1295
- } | SelectFunctionItem | JsonItem | RawExpression;
1296
- declare type SelectFunctionItem = {
1297
- function: string;
1298
- arguments: SelectItem[];
1299
- as?: string;
1006
+ declare type InsertHasOneData<T extends Query, Key extends keyof T['relations'], Rel extends HasOneRelation> = 'through' extends Rel['options'] ? {} : {
1007
+ [K in Key]?: {
1008
+ create: InsertData<Rel['nestedCreateQuery']>;
1009
+ connect?: never;
1010
+ connectOrCreate?: never;
1011
+ } | {
1012
+ create?: never;
1013
+ connect: WhereArg<Rel['model']>;
1014
+ connectOrCreate?: never;
1015
+ } | {
1016
+ create?: never;
1017
+ connect?: never;
1018
+ connectOrCreate: {
1019
+ where?: WhereArg<Rel['model']>;
1020
+ create?: InsertData<Rel['nestedCreateQuery']>;
1021
+ };
1022
+ };
1300
1023
  };
1301
- declare type JoinItem = {
1302
- type: string;
1303
- args: [relation: string] | [
1304
- arg: string | QueryWithTable,
1305
- conditions: Record<string, string | RawExpression> | RawExpression | ((q: unknown) => QueryBase)
1306
- ] | [
1307
- arg: string | QueryWithTable,
1308
- leftColumn: string | RawExpression,
1309
- rightColumn: string | RawExpression
1310
- ] | [
1311
- arg: string | QueryWithTable,
1312
- leftColumn: string | RawExpression,
1313
- op: string,
1314
- rightColumn: string | RawExpression
1315
- ];
1024
+ declare type InsertHasManyData<T extends Query, Key extends keyof T['relations'], Rel extends HasManyRelation | HasAndBelongsToManyRelation> = 'through' extends Rel['options'] ? {} : {
1025
+ [K in Key]?: {
1026
+ create?: InsertData<Rel['nestedCreateQuery']>[];
1027
+ connect?: WhereArg<Rel['model']>[];
1028
+ connectOrCreate?: {
1029
+ where: WhereArg<Rel['model']>;
1030
+ create: InsertData<Rel['nestedCreateQuery']>;
1031
+ }[];
1032
+ };
1316
1033
  };
1317
- declare type WhereItem = (Omit<Record<string, unknown | Record<string, unknown | Query | RawExpression> | RawExpression>, 'NOT' | 'AND' | 'OR' | 'IN' | 'EXISTS' | 'ON' | 'ON_JSON_PATH_EQUALS'> & {
1318
- NOT?: MaybeArray<WhereItem>;
1319
- AND?: MaybeArray<WhereItem>;
1320
- OR?: MaybeArray<WhereItem>[];
1321
- IN?: MaybeArray<WhereInItem>;
1322
- EXISTS?: MaybeArray<JoinItem['args']>;
1323
- ON?: WhereOnItem | WhereJsonPathEqualsItem;
1324
- }) | ((q: unknown) => QueryBase) | Query | RawExpression;
1325
- declare type WhereInItem = {
1034
+ declare type InsertRawData = {
1326
1035
  columns: string[];
1327
- values: unknown[][] | Query | RawExpression;
1328
- };
1329
- declare type WhereJsonPathEqualsItem = [
1330
- leftColumn: string,
1331
- leftPath: string,
1332
- rightColumn: string,
1333
- rightPath: string
1334
- ];
1335
- declare type WhereOnItem = {
1336
- joinFrom: WhereOnJoinItem;
1337
- joinTo: WhereOnJoinItem;
1338
- on: [leftFullColumn: string, rightFullColumn: string] | [leftFullColumn: string, op: string, rightFullColumn: string];
1036
+ values: RawExpression;
1339
1037
  };
1340
- declare type WhereOnJoinItem = {
1341
- table?: string;
1342
- query: {
1343
- as?: string;
1038
+ declare type InsertOneResult<T extends Query> = T['hasSelect'] extends true ? QueryReturnsAll<T['returnType']> extends true ? SetQueryReturnsOne<T> : T['returnType'] extends 'one' ? SetQueryReturnsOne<T> : T : SetQueryReturnsRowCount<T>;
1039
+ declare type InsertManyResult<T extends Query> = T['hasSelect'] extends true ? T['returnType'] extends 'one' | 'oneOrThrow' ? SetQueryReturnsAll<T> : T : SetQueryReturnsRowCount<T>;
1040
+ declare type OnConflictArg<T extends Query> = keyof T['shape'] | (keyof T['shape'])[] | RawExpression;
1041
+ declare class Insert {
1042
+ insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
1043
+ _insert<T extends Query>(this: T, data: InsertData<T>): InsertOneResult<T>;
1044
+ insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
1045
+ _insertMany<T extends Query>(this: T, data: InsertData<T>[]): InsertManyResult<T>;
1046
+ insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
1047
+ _insertRaw<T extends Query>(this: T, data: InsertRawData): InsertManyResult<T>;
1048
+ create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
1049
+ _create<T extends Query>(this: T, data: InsertData<T>): SetQueryReturnsOne<T>;
1050
+ createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
1051
+ _createMany<T extends Query>(this: T, data: InsertData<T>[]): SetQueryReturnsAll<T>;
1052
+ createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
1053
+ _createRaw<T extends Query>(this: T, data: InsertRawData): SetQueryReturnsAll<T>;
1054
+ defaults<T extends Query, Data extends Partial<InsertData<T>>>(this: T, data: Data): T & {
1055
+ [defaultsKey]: Record<keyof Data, true>;
1344
1056
  };
1345
- } | string;
1346
- declare type AggregateItemOptions = {
1347
- as?: string;
1348
- distinct?: boolean;
1349
- order?: OrderItem[];
1350
- filter?: WhereItem;
1351
- filterOr?: WhereItem[];
1352
- withinGroup?: boolean;
1353
- over?: string;
1354
- window?: WindowItem;
1355
- };
1356
- declare type SortDir = 'ASC' | 'DESC';
1357
- declare type OrderItem = string | Record<string, SortDir | {
1358
- dir: SortDir;
1359
- nulls: 'FIRST' | 'LAST';
1360
- }> | RawExpression;
1361
- declare type AggregateItemArg = Expression | Record<string, Expression> | [Expression, string];
1362
- declare type AggregateItem = {
1363
- function: string;
1364
- arg?: AggregateItemArg;
1365
- options: AggregateItemOptions;
1366
- };
1367
- declare type ColumnOperators<S extends SelectableBase, Column extends keyof S> = {
1368
- [O in keyof S[Column]['column']['operators']]?: S[Column]['column']['operators'][O]['type'];
1057
+ _defaults<T extends Query, Data extends Partial<InsertData<T>>>(this: T, data: Data): T & {
1058
+ [defaultsKey]: Record<keyof Data, true>;
1059
+ };
1060
+ onConflict<T extends Query, Arg extends OnConflictArg<T>>(this: T, arg?: Arg): OnConflictQueryBuilder<T, Arg>;
1061
+ _onConflict<T extends Query, Arg extends OnConflictArg<T> | undefined = undefined>(this: T, arg?: Arg): OnConflictQueryBuilder<T, Arg>;
1062
+ }
1063
+ declare class OnConflictQueryBuilder<T extends Query, Arg extends OnConflictArg<T> | undefined> {
1064
+ private query;
1065
+ private onConflict;
1066
+ constructor(query: T, onConflict: Arg);
1067
+ ignore(): T;
1068
+ merge(update?: keyof T['shape'] | (keyof T['shape'])[] | Partial<T['inputType']> | RawExpression): T;
1069
+ }
1070
+
1071
+ declare type UpdateData<T extends Query> = {
1072
+ [K in keyof T['type']]?: T['type'][K] | RawExpression;
1073
+ } & (T['relations'] extends Record<string, Relation> ? {
1074
+ [K in keyof T['relations']]?: T['relations'][K] extends BelongsToRelation ? UpdateBelongsToData<T, T['relations'][K]> : T['relations'][K] extends HasOneRelation ? UpdateHasOneData<T, T['relations'][K]> : T['relations'][K] extends HasManyRelation ? UpdateHasManyData<T, T['relations'][K]> : T['relations'][K] extends HasAndBelongsToManyRelation ? UpdateHasAndBelongsToManyData<T['relations'][K]> : never;
1075
+ } : EmptyObject) & {
1076
+ __raw?: never;
1369
1077
  };
1370
- declare type HavingItemObject = Record<string, unknown>;
1371
- declare type HavingItem = Record<string, HavingItemObject> | {
1372
- count?: number | HavingItemObject;
1373
- } | Query | RawExpression;
1374
- declare type WindowItem = Record<string, WindowDeclaration | RawExpression>;
1375
- declare type WindowDeclaration = {
1376
- partitionBy?: Expression | Expression[];
1377
- order?: OrderItem;
1078
+ declare type UpdateBelongsToData<T extends Query, Rel extends BelongsToRelation> = {
1079
+ disconnect: boolean;
1080
+ } | {
1081
+ set: WhereArg<Rel['model']>;
1082
+ } | {
1083
+ delete: boolean;
1084
+ } | {
1085
+ update: UpdateData<Rel['model']>;
1086
+ } | {
1087
+ create: InsertData<Rel['nestedCreateQuery']>;
1088
+ } | (QueryReturnsAll<T['returnType']> extends true ? never : {
1089
+ upsert: {
1090
+ update: UpdateData<Rel['model']>;
1091
+ create: InsertData<Rel['nestedCreateQuery']>;
1092
+ };
1093
+ });
1094
+ declare type UpdateHasOneData<T extends Query, Rel extends HasOneRelation> = {
1095
+ disconnect: boolean;
1096
+ } | {
1097
+ delete: boolean;
1098
+ } | {
1099
+ update: UpdateData<Rel['model']>;
1100
+ } | (QueryReturnsAll<T['returnType']> extends true ? never : {
1101
+ set: WhereArg<Rel['model']>;
1102
+ } | {
1103
+ upsert: {
1104
+ update: UpdateData<Rel['model']>;
1105
+ create: InsertData<Rel['nestedCreateQuery']>;
1106
+ };
1107
+ } | {
1108
+ create: InsertData<Rel['nestedCreateQuery']>;
1109
+ });
1110
+ declare type UpdateHasManyData<T extends Query, Rel extends HasManyRelation> = {
1111
+ disconnect?: MaybeArray<WhereArg<Rel['model']>>;
1112
+ delete?: MaybeArray<WhereArg<Rel['model']>>;
1113
+ update?: {
1114
+ where: MaybeArray<WhereArg<Rel['model']>>;
1115
+ data: UpdateData<Rel['model']>;
1116
+ };
1117
+ } & (QueryReturnsAll<T['returnType']> extends true ? EmptyObject : {
1118
+ set?: MaybeArray<WhereArg<Rel['model']>>;
1119
+ create?: InsertData<Rel['nestedCreateQuery']>[];
1120
+ });
1121
+ declare type UpdateHasAndBelongsToManyData<Rel extends HasAndBelongsToManyRelation> = {
1122
+ disconnect?: MaybeArray<WhereArg<Rel['model']>>;
1123
+ set?: MaybeArray<WhereArg<Rel['model']>>;
1124
+ delete?: MaybeArray<WhereArg<Rel['model']>>;
1125
+ update?: {
1126
+ where: MaybeArray<WhereArg<Rel['model']>>;
1127
+ data: UpdateData<Rel['model']>;
1128
+ };
1129
+ create?: InsertData<Rel['nestedCreateQuery']>[];
1378
1130
  };
1379
- declare type UnionItem = Query | RawExpression;
1380
- declare type UnionKind = 'UNION' | 'UNION ALL' | 'INTERSECT' | 'INTERSECT ALL' | 'EXCEPT' | 'EXCEPT ALL';
1381
- declare type OnConflictItem = string | string[] | RawExpression;
1382
- declare type OnConflictMergeUpdate = string | string[] | Record<string, unknown> | RawExpression;
1131
+ declare type UpdateArgs<T extends Query, ForceAll extends boolean> = (T['hasWhere'] extends true ? true : ForceAll) extends true ? [update: UpdateData<T>] : [update: UpdateData<T>, forceAll: true];
1132
+ declare type UpdateRawArgs<T extends Query, ForceAll extends boolean> = (T['hasWhere'] extends true ? true : ForceAll) extends true ? [update: RawExpression] : [update: RawExpression, forceAll: true];
1133
+ declare type UpdateResult<T extends Query> = T['hasSelect'] extends true ? T : SetQueryReturnsRowCount<T>;
1134
+ declare type ChangeCountArg<T extends Query> = keyof T['shape'] | Partial<Record<keyof T['shape'], number>>;
1135
+ declare class Update {
1136
+ update<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
1137
+ _update<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
1138
+ updateRaw<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateRawArgs<T, ForceAll>): UpdateResult<T>;
1139
+ _updateRaw<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateRawArgs<T, ForceAll>): UpdateResult<T>;
1140
+ updateOrThrow<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
1141
+ _updateOrThrow<T extends Query, ForceAll extends boolean = false>(this: T, ...args: UpdateArgs<T, ForceAll>): UpdateResult<T>;
1142
+ increment<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
1143
+ _increment<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
1144
+ decrement<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
1145
+ _decrement<T extends Query>(this: T, data: ChangeCountArg<T>): UpdateResult<T>;
1146
+ }
1383
1147
 
1384
- declare type ToSqlCtx = {
1385
- whereQueryBuilder: typeof WhereQueryBuilder;
1386
- onQueryBuilder: typeof OnQueryBuilder;
1387
- sql: string[];
1388
- values: unknown[];
1389
- };
1390
- declare type toSqlCacheKey = typeof toSqlCacheKey;
1391
- declare const toSqlCacheKey: unique symbol;
1392
- declare type ToSqlOptions = {
1393
- clearCache?: boolean;
1394
- values?: unknown[];
1148
+ declare type DeleteArgs<T extends Query> = T['hasWhere'] extends true ? [forceAll?: boolean] : [true];
1149
+ declare type DeleteResult<T extends Query> = T['hasSelect'] extends true ? T : SetQueryReturnsRowCount<T>;
1150
+ declare class Delete {
1151
+ del<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
1152
+ _del<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
1153
+ delete<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
1154
+ _delete<T extends Query>(this: T, ...args: DeleteArgs<T>): DeleteResult<T>;
1155
+ }
1156
+
1157
+ declare class Transaction {
1158
+ transaction<T extends Query, Result>(this: T, cb: (query: T) => Promise<Result>): Promise<Result>;
1159
+ transacting<T extends Query>(this: T, query: Query): T;
1160
+ _transacting<T extends Query>(this: T, query: Query): T;
1161
+ }
1162
+
1163
+ declare type ForQueryBuilder<Q extends Query> = Q & {
1164
+ noWait<T extends ForQueryBuilder<Q>>(this: T): T;
1165
+ _noWait<T extends ForQueryBuilder<Q>>(this: T): T;
1166
+ skipLocked<T extends ForQueryBuilder<Q>>(this: T): T;
1167
+ _skipLocked<T extends ForQueryBuilder<Q>>(this: T): T;
1395
1168
  };
1396
- declare const toSql: (model: Query, options?: ToSqlOptions) => Sql;
1169
+ declare class For {
1170
+ forUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1171
+ _forUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1172
+ forNoKeyUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1173
+ _forNoKeyUpdate<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1174
+ forShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1175
+ _forShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1176
+ forKeyShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1177
+ _forKeyShare<T extends Query>(this: T, tableNames?: string[] | RawExpression): ForQueryBuilder<T>;
1178
+ }
1397
1179
 
1398
- declare type SomeIsTrue<T extends unknown[]> = T extends [
1399
- infer Head,
1400
- ...infer Tail
1401
- ] ? Head extends true ? true : SomeIsTrue<Tail> : false;
1402
- declare type MaybeArray<T> = T | T[];
1403
- declare type SetOptional<T, K extends PropertyKey> = Omit<T, K> & {
1404
- [P in K]?: P extends keyof T ? T[P] : never;
1180
+ declare type ColumnInfo = {
1181
+ defaultValue: unknown;
1182
+ type: string;
1183
+ maxLength: number | null;
1184
+ nullable: boolean;
1405
1185
  };
1406
- declare type GetTypesOrRaw<T extends [...unknown[]]> = T extends [
1407
- infer Head,
1408
- ...infer Tail
1409
- ] ? [GetTypeOrRaw<Head>, ...GetTypesOrRaw<Tail>] : [];
1410
- declare type GetTypeOrRaw<T> = T | RawExpression;
1411
- declare type UnionToIntersection<U> = (U extends any ? (k: U) => void : never) extends (k: infer I) => void ? I : never;
1412
- declare type UnionToOvlds<U> = UnionToIntersection<U extends any ? (f: U) => void : never>;
1413
- declare type PopPropertyKeyUnion<U> = UnionToOvlds<U> extends (a: infer A extends PropertyKey) => void ? A : never;
1414
- declare type IsUnion$1<T> = [T] extends [UnionToIntersection<T>] ? false : true;
1415
- declare type PropertyKeyUnionToArray<T, A extends PropertyKey[] = []> = IsUnion$1<T> extends true ? PropertyKeyUnionToArray<Exclude<T, PopPropertyKeyUnion<T>>, [
1416
- PopPropertyKeyUnion<T>,
1417
- ...A
1418
- ]> : [T, ...A];
1419
- declare type OptionalPropertyNames<T> = {
1420
- [K in keyof T]-?: {} extends {
1421
- [P in K]: T[K];
1422
- } ? K : never;
1423
- }[keyof T];
1424
- declare type SpreadProperties<L, R, K extends keyof L & keyof R> = {
1425
- [P in K]: L[P] | Exclude<R[P], undefined>;
1186
+ declare class ColumnInfoMethods {
1187
+ columnInfo<T extends Query, Column extends keyof T['shape'] | undefined = undefined>(this: T, column?: Column): SetQueryReturnsColumnInfo<T, Column>;
1188
+ _columnInfo<T extends Query, Column extends keyof T['shape'] | undefined = undefined>(this: T, column?: Column): SetQueryReturnsColumnInfo<T, Column>;
1189
+ }
1190
+
1191
+ declare type ClearStatement = 'with' | 'select' | 'where' | 'union' | 'using' | 'join' | 'group' | 'order' | 'having' | 'limit' | 'offset' | 'counters';
1192
+ declare class Clear {
1193
+ clear<T extends Query>(this: T, ...clears: ClearStatement[]): T;
1194
+ _clear<T extends Query>(this: T, ...clears: ClearStatement[]): T;
1195
+ }
1196
+
1197
+ declare type HavingArgObject<T extends Query, Agg extends keyof Aggregate1ArgumentTypes<T>> = {
1198
+ [Column in Exclude<Aggregate1ArgumentTypes<T>[Agg], RawExpression>]?: T['selectable'][Column]['column']['type'] | (ColumnOperators<T['selectable'], Column> & AggregateOptions<T>);
1426
1199
  };
1427
- declare type Id<T> = T extends infer U ? {
1428
- [K in keyof U]: U[K];
1429
- } : never;
1430
- declare type SpreadTwo<L, R> = Id<Pick<L, Exclude<keyof L, keyof R>> & Pick<R, Exclude<keyof R, OptionalPropertyNames<R>>> & Pick<R, Exclude<OptionalPropertyNames<R>, keyof L>> & SpreadProperties<L, R, OptionalPropertyNames<R> & keyof L>>;
1431
- declare type Spread<A extends readonly [...any]> = A extends [
1432
- infer L,
1433
- ...infer R
1434
- ] ? SpreadTwo<L, Spread<R>> : unknown;
1435
- declare type SimpleSpread<A extends readonly [...any]> = A extends [
1436
- infer L,
1437
- ...infer R
1438
- ] ? L & SimpleSpread<R> : {};
1439
- declare type FilterTuple<T extends readonly any[], E> = T extends [
1440
- infer F,
1441
- ...infer R
1442
- ] ? [F] extends [E] ? [F, ...FilterTuple<R, E>] : FilterTuple<R, E> : [];
1443
- declare type CoalesceString<Left extends string | undefined, Right extends string> = Left extends undefined ? Right : Left;
1444
- declare function applyMixins(derivedCtor: any, constructors: any[]): void;
1445
- declare const joinTruthy: (...strings: (string | false | undefined)[]) => string;
1446
- declare const getClonedQueryData: (query: QueryData) => QueryData;
1447
- declare const getQueryAs: (q: {
1448
- table?: string;
1449
- query: {
1450
- as?: string;
1451
- };
1452
- }) => string;
1453
- declare const toArray: <T>(item: T) => T extends unknown[] ? T : [T];
1454
- declare const noop: () => void;
1455
- declare type EmptyObject = typeof emptyObject;
1456
- declare const emptyObject: {};
1457
- declare const makeRegexToFindInSql: (value: string) => RegExp;
1458
- declare const pushOrNewArrayToObject: <Obj extends {}, Key extends keyof Obj>(obj: Obj, key: Key, value: Exclude<Obj[Key], undefined> extends unknown[] ? Exclude<Obj[Key], undefined>[number] : never) => void;
1459
- declare const pushOrNewArray: <Arr extends unknown[]>(arr: Arr | undefined, value: Arr[number]) => Arr;
1200
+ declare type HavingArg<T extends Query = Query> = ({
1201
+ [Agg in keyof Aggregate1ArgumentTypes<T>]?: HavingArgObject<T, Agg>;
1202
+ } & {
1203
+ count?: number | HavingArgObject<T, 'count'>;
1204
+ }) | Query | RawExpression;
1205
+ declare class Having {
1206
+ having<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
1207
+ _having<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
1208
+ havingOr<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
1209
+ _havingOr<T extends Query>(this: T, ...args: HavingArg<T>[]): T;
1210
+ }
1460
1211
 
1461
1212
  declare class Window {
1462
1213
  selectRowNumber<T extends Query, As extends string | undefined = undefined>(this: T, options: WindowFunctionOptions<T, As>): SelectAgg<T, 'row_number', As, IntegerColumn>;
@@ -1471,22 +1222,51 @@ declare class Window {
1471
1222
  _selectCumeDist<T extends Query, As extends string | undefined = undefined>(this: T, options: WindowFunctionOptions<T, As>): SelectAgg<T, 'cume_dist', As, IntegerColumn>;
1472
1223
  }
1473
1224
 
1225
+ declare type UpsertData<T extends Query> = {
1226
+ update: UpdateData<T>;
1227
+ create: InsertData<T>;
1228
+ };
1229
+ declare type UpsertResult<T extends Query> = T['hasSelect'] extends true ? SetQueryReturnsOne<T> : SetQueryReturnsVoid<T>;
1230
+ declare type UpsertThis = WhereResult<Query> & {
1231
+ returnType: 'one' | 'oneOrThrow';
1232
+ };
1233
+ declare class QueryUpsert {
1234
+ upsert<T extends UpsertThis>(this: T, data: UpsertData<T>): UpsertResult<T>;
1235
+ _upsert<T extends UpsertThis>(this: T, data: UpsertData<T>): UpsertResult<T>;
1236
+ }
1237
+
1238
+ declare type MergeQuery<T extends Query, Q extends Query, ReturnType extends QueryReturnType = QueryReturnType extends Q['returnType'] ? T['returnType'] : Q['returnType']> = Omit<T, 'result' | 'returnType' | 'then'> & {
1239
+ hasSelect: Q['hasSelect'] extends true ? true : T['hasSelect'];
1240
+ hasWhere: Q['hasWhere'] extends true ? true : T['hasWhere'];
1241
+ result: T['hasSelect'] extends true ? Spread<[T['result'], Q['result']]> : Q['result'];
1242
+ returnType: ReturnType;
1243
+ then: T['hasSelect'] extends true ? QueryThen<ReturnType, Spread<[T['result'], Q['result']]>> : QueryThen<ReturnType, Q['result']>;
1244
+ selectable: T['selectable'] & Q['selectable'];
1245
+ joinedTables: T['joinedTables'] & Q['joinedTables'];
1246
+ windows: T['windows'] & Q['windows'];
1247
+ withData: T['withData'] & Q['withData'];
1248
+ };
1249
+ declare class MergeQueryMethods {
1250
+ merge<T extends Query, Q extends Query>(this: T, q: Q): MergeQuery<T, Q>;
1251
+ _merge<T extends Query, Q extends Query>(this: T, q: Q): MergeQuery<T, Q>;
1252
+ }
1253
+
1474
1254
  declare type WindowArg<T extends Query> = Record<string, WindowArgDeclaration<T> | RawExpression>;
1475
1255
  declare type WindowArgDeclaration<T extends Query = Query> = {
1476
1256
  partitionBy?: Expression<T> | Expression<T>[];
1477
1257
  order?: OrderArg<T>;
1478
1258
  };
1479
- declare type WindowResult<T extends Query, W extends WindowArg<T>> = SetQueryWindows<T, PropertyKeyUnionToArray<keyof W>>;
1259
+ declare type WindowResult<T extends Query, W extends WindowArg<T>> = SetQueryWindows<T, Record<keyof W, true>>;
1480
1260
  declare type OrderArg<T extends Query> = keyof T['selectable'] | {
1481
1261
  [K in Selectable<T>]?: SortDir | {
1482
1262
  dir: SortDir;
1483
1263
  nulls: 'FIRST' | 'LAST';
1484
1264
  };
1485
1265
  } | RawExpression;
1486
- interface QueryMethods extends Aggregate, Select, From, Join, With, Union, Json, Insert, Update, Delete, Transaction, For, ColumnInfoMethods, Where, Clear, Having, Window, Then, QueryLog, QueryCallbacks, QueryUpsert, QueryGet {
1266
+ interface QueryMethods extends Aggregate, Select, From, Join, With, Union, Json, Insert, Update, Delete, Transaction, For, ColumnInfoMethods, Where, Clear, Having, Window, Then, QueryLog, QueryCallbacks, QueryUpsert, QueryGet, MergeQueryMethods {
1487
1267
  }
1488
1268
  declare class QueryMethods {
1489
- windows: PropertyKey[];
1269
+ windows: EmptyObject;
1490
1270
  __model: Query;
1491
1271
  all<T extends Query>(this: T): SetQueryReturnsAll<T>;
1492
1272
  _all<T extends Query>(this: T): SetQueryReturnsAll<T>;
@@ -1540,6 +1320,136 @@ declare class QueryMethods {
1540
1320
  }): SetQueryReturnsVoid<T>;
1541
1321
  }
1542
1322
 
1323
+ declare type AggregateArg<T extends Query> = Expression<T> | Record<string, Expression<T>> | [Expression<T>, string];
1324
+ declare type AggregateOptions<T extends Query = Query, As extends string | undefined = any> = {
1325
+ as?: As;
1326
+ distinct?: boolean;
1327
+ order?: OrderArg<T> | OrderArg<T>[];
1328
+ filter?: WhereArg<T>;
1329
+ filterOr?: WhereArg<T>[];
1330
+ withinGroup?: boolean;
1331
+ over?: keyof T['windows'] | WindowArgDeclaration<T>;
1332
+ };
1333
+ declare type Aggregate1ArgumentTypes<T extends Query = Query, C extends ColumnType = ColumnType> = {
1334
+ count: Expression<T, C>;
1335
+ avg: NumberExpression<T, C>;
1336
+ min: Expression<T, C>;
1337
+ max: Expression<T, C>;
1338
+ sum: NumberExpression<T, C>;
1339
+ bitAnd: NumberExpression<T, C>;
1340
+ bitOr: NumberExpression<T, C>;
1341
+ boolAnd: BooleanExpression<T, C>;
1342
+ boolOr: BooleanExpression<T, C>;
1343
+ every: BooleanExpression<T, C>;
1344
+ jsonAgg: Expression<T, C>;
1345
+ jsonbAgg: Expression<T, C>;
1346
+ xmlAgg: Expression<T, C>;
1347
+ };
1348
+ declare const aggregate1FunctionNames: {
1349
+ readonly count: "count";
1350
+ readonly avg: "avg";
1351
+ readonly min: "min";
1352
+ readonly max: "max";
1353
+ readonly sum: "sum";
1354
+ readonly bitAnd: "bit_and";
1355
+ readonly bitOr: "bit_or";
1356
+ readonly boolAnd: "bool_and";
1357
+ readonly boolOr: "bool_or";
1358
+ readonly every: "every";
1359
+ readonly jsonAgg: "json_agg";
1360
+ readonly jsonbAgg: "jsonb_agg";
1361
+ readonly xmlAgg: "xmlagg";
1362
+ };
1363
+ declare type SelectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType> = AddQuerySelect<T, Record<CoalesceString<As, Func>, Value>>;
1364
+ declare type AT1<T extends Query> = Aggregate1ArgumentTypes<T>;
1365
+ declare type WindowFunctionOptions<T extends Query = Query, As extends string | undefined = any> = {
1366
+ as?: As;
1367
+ } & WindowArgDeclaration<T>;
1368
+ declare class Aggregate {
1369
+ selectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType>(this: T, functionName: Func, arg: AggregateArg<T>, options?: AggregateOptions<T, As>): SelectAgg<T, Func, As, Value>;
1370
+ _selectAgg<T extends Query, Func extends string, As extends string | undefined, Value extends ColumnType>(this: T, functionName: Func, arg: AggregateArg<T>, options?: AggregateOptions<T, As>, columnType?: ColumnType): SelectAgg<T, Func, As, Value>;
1371
+ count<T extends Query>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T>): SetQueryReturnsValue<T, NumberColumn>;
1372
+ _count<T extends Query>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T>): SetQueryReturnsValue<T, NumberColumn>;
1373
+ selectCount<T extends Query, As extends string | undefined = undefined>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T, As>): SelectAgg<T, 'count', As, NumberColumn>;
1374
+ _selectCount<T extends Query, As extends string | undefined = undefined>(this: T, arg?: AT1<T>['count'] | '*', options?: AggregateOptions<T, As>): SelectAgg<T, 'count', As, NumberColumn>;
1375
+ avg<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['avg'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1376
+ _avg<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['avg'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1377
+ selectAvg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'avg', As, NullableColumn<NumberColumn>>;
1378
+ _selectAvg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'avg', As, NullableColumn<NumberColumn>>;
1379
+ min<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['min'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1380
+ _min<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['min'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1381
+ selectMin<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'min', As, NullableColumn<NumberColumn>>;
1382
+ _selectMin<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'min', As, NullableColumn<NumberColumn>>;
1383
+ max<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['max'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1384
+ _max<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['max'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1385
+ selectMax<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'max', As, NullableColumn<NumberColumn>>;
1386
+ _selectMax<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'max', As, NullableColumn<NumberColumn>>;
1387
+ sum<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['sum'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1388
+ _sum<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['sum'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1389
+ selectSum<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'sum', As, NullableColumn<NumberColumn>>;
1390
+ _selectSum<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'sum', As, NullableColumn<NumberColumn>>;
1391
+ bitAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1392
+ _bitAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1393
+ selectBitAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_and', As, NullableColumn<NumberColumn>>;
1394
+ _selectBitAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_and', As, NullableColumn<NumberColumn>>;
1395
+ bitOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1396
+ _bitOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['bitOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<NumberColumn>>;
1397
+ selectBitOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_or', As, NullableColumn<NumberColumn>>;
1398
+ _selectBitOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bit_or', As, NullableColumn<NumberColumn>>;
1399
+ boolAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1400
+ _boolAnd<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolAnd'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1401
+ selectBoolAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_and', As, NullableColumn<BooleanColumn>>;
1402
+ _selectBoolAnd<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_and', As, NullableColumn<BooleanColumn>>;
1403
+ boolOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1404
+ _boolOr<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['boolOr'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1405
+ selectBoolOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_or', As, NullableColumn<BooleanColumn>>;
1406
+ _selectBoolOr<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'bool_or', As, NullableColumn<BooleanColumn>>;
1407
+ every<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['every'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1408
+ _every<T extends Query>(this: T, arg: Aggregate1ArgumentTypes<T>['every'], options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<BooleanColumn>>;
1409
+ selectEvery<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'every', As, NullableColumn<BooleanColumn>>;
1410
+ _selectEvery<T extends Query, As extends string | undefined = undefined>(this: T, arg: Expression<T>, options?: AggregateOptions<T, As>): SelectAgg<T, 'every', As, NullableColumn<BooleanColumn>>;
1411
+ jsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1412
+ _jsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1413
+ selectJsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1414
+ _selectJsonAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1415
+ jsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1416
+ _jsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1417
+ selectJsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1418
+ _selectJsonbAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['jsonbAgg'], As extends string | undefined = undefined>(this: T, arg: Expr, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_agg', As, NullableColumn<ArrayColumn<ExpressionOutput<T, Expr>>>>;
1419
+ xmlAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['xmlAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
1420
+ _xmlAgg<T extends Query, Expr extends Aggregate1ArgumentTypes<T>['xmlAgg']>(this: T, arg: Expr, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
1421
+ selectXmlAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Aggregate1ArgumentTypes<T>['xmlAgg'], options?: AggregateOptions<T, As>): SelectAgg<T, 'xmlagg', As, NullableColumn<StringColumn>>;
1422
+ _selectXmlAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: Aggregate1ArgumentTypes<T>['xmlAgg'], options?: AggregateOptions<T, As>): SelectAgg<T, 'xmlagg', As, NullableColumn<StringColumn>>;
1423
+ jsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
1424
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1425
+ }>>>;
1426
+ _jsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
1427
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1428
+ }>>>;
1429
+ selectJsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_object_agg', As, NullableColumn<ColumnType<{
1430
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1431
+ }>>>;
1432
+ _selectJsonObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'json_object_agg', As, NullableColumn<ColumnType<{
1433
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1434
+ }>>>;
1435
+ jsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
1436
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1437
+ }>>>;
1438
+ _jsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>>(this: T, obj: Obj, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<ColumnType<{
1439
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1440
+ }>>>;
1441
+ selectJsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_object_agg', As, NullableColumn<ColumnType<{
1442
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1443
+ }>>>;
1444
+ _selectJsonbObjectAgg<T extends Query, Obj extends Record<string, Expression<T>>, As extends string | undefined = undefined>(this: T, obj: Obj, options?: AggregateOptions<T, As>): SelectAgg<T, 'jsonb_object_agg', As, NullableColumn<ColumnType<{
1445
+ [K in keyof Obj]: ExpressionOutput<T, Obj[K]>['type'];
1446
+ }>>>;
1447
+ stringAgg<T extends Query>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
1448
+ _stringAgg<T extends Query>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T>): SetQueryReturnsValue<T, NullableColumn<StringColumn>>;
1449
+ selectStringAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T, As>): SelectAgg<T, 'string_agg', As, NullableColumn<StringColumn>>;
1450
+ _selectStringAgg<T extends Query, As extends string | undefined = undefined>(this: T, arg: StringExpression<T>, delimiter: string, options?: AggregateOptions<T, As>): SelectAgg<T, 'string_agg', As, NullableColumn<StringColumn>>;
1451
+ }
1452
+
1543
1453
  declare type ColumnParser = (input: unknown) => unknown;
1544
1454
  declare type ColumnsParsers = Record<string | getValueKey, ColumnParser>;
1545
1455
  declare type SelectableBase = Record<PropertyKey, {
@@ -1585,7 +1495,7 @@ declare type Query = QueryMethods & {
1585
1495
  then: ThenResult<unknown>;
1586
1496
  tableAlias: string | undefined;
1587
1497
  joinedTables: Record<string, Pick<Query, 'result' | 'tableAlias' | 'table'>>;
1588
- windows: PropertyKey[];
1498
+ windows: EmptyObject;
1589
1499
  defaultSelectColumns: string[];
1590
1500
  columnsParsers?: ColumnsParsers;
1591
1501
  relations: RelationsBase;
@@ -1600,21 +1510,23 @@ declare type DefaultSelectColumns<S extends ColumnsShape> = {
1600
1510
  [K in keyof S]: S[K]['isHidden'] extends true ? never : K;
1601
1511
  }[StringKey<keyof S>][];
1602
1512
  declare type QueryReturnType = 'all' | 'one' | 'oneOrThrow' | 'rows' | 'pluck' | 'value' | 'valueOrThrow' | 'rowCount' | 'void';
1513
+ declare const queryTypeWithLimitOne: Record<QueryReturnType, true | undefined>;
1603
1514
  declare type JoinedTablesBase = Record<string, Pick<Query, 'result' | 'tableAlias' | 'table'>>;
1604
- declare type QueryThen<ReturnType extends QueryReturnType, Result extends ColumnsShape> = ReturnType extends 'all' ? ThenResult<ColumnShapeOutput<Result>[]> : ReturnType extends 'one' ? ThenResult<ColumnShapeOutput<Result> | undefined> : ReturnType extends 'oneOrThrow' ? ThenResult<ColumnShapeOutput<Result>> : ReturnType extends 'value' ? Result extends {
1515
+ declare type QueryReturnsAll<T extends QueryReturnType> = (QueryReturnType extends T ? 'all' : T) extends 'all' ? true : false;
1516
+ declare type QueryThen<ReturnType extends QueryReturnType, Result extends ColumnsShape> = QueryReturnsAll<ReturnType> extends true ? ThenResult<ColumnShapeOutput<Result>[]> : ReturnType extends 'one' ? ThenResult<ColumnShapeOutput<Result> | undefined> : ReturnType extends 'oneOrThrow' ? ThenResult<ColumnShapeOutput<Result>> : ReturnType extends 'value' ? Result extends {
1605
1517
  value: ColumnType;
1606
1518
  } ? ThenResult<Result['value']['type'] | undefined> : never : ReturnType extends 'valueOrThrow' ? Result extends {
1607
1519
  value: ColumnType;
1608
1520
  } ? ThenResult<Result['value']['type']> : never : ReturnType extends 'rows' ? ThenResult<ColumnShapeOutput<Result>[keyof Result][][]> : ReturnType extends 'pluck' ? Result extends {
1609
1521
  pluck: ColumnType;
1610
1522
  } ? ThenResult<Result['pluck']['type'][]> : never : ReturnType extends 'rowCount' ? ThenResult<number> : ReturnType extends 'void' ? ThenResult<void> : never;
1611
- declare type AddQuerySelect<T extends Pick<Query, 'hasSelect' | 'result' | 'then' | 'returnType'>, Result extends ColumnsShape> = T['hasSelect'] extends false ? Omit<T, 'hasSelect' | 'result' | 'then'> & {
1523
+ declare type AddQuerySelect<T extends Pick<Query, 'hasSelect' | 'result' | 'then' | 'returnType'>, Result extends ColumnsShape> = T['hasSelect'] extends true ? Omit<T, 'result' | 'then'> & {
1524
+ result: Spread<[T['result'], Result]>;
1525
+ then: QueryThen<T['returnType'], Spread<[T['result'], Result]>>;
1526
+ } : Omit<T, 'result' | 'then'> & {
1612
1527
  hasSelect: true;
1613
1528
  result: Result;
1614
1529
  then: QueryThen<T['returnType'], Result>;
1615
- } : Omit<T, 'result' | 'then'> & {
1616
- result: Spread<[T['result'], Result]>;
1617
- then: QueryThen<T['returnType'], Spread<[T['result'], Result]>>;
1618
1530
  };
1619
1531
  declare type QuerySelectAll<T extends Query> = Omit<T, 'hasSelect' | 'result' | 'then'> & {
1620
1532
  hasSelect: true;
@@ -1690,7 +1602,7 @@ declare type SetQueryWith<T extends Query, WithData extends Record<string, WithD
1690
1602
  declare type AddQueryWith<T extends Query, With extends WithDataItem> = SetQueryWith<T, Spread<[T['withData'], {
1691
1603
  [K in With['table']]: With;
1692
1604
  }]>>;
1693
- declare type SetQueryWindows<T extends Query, W extends PropertyKey[]> = Omit<T, 'windows'> & {
1605
+ declare type SetQueryWindows<T extends Query, W extends EmptyObject> = T & {
1694
1606
  windows: W;
1695
1607
  };
1696
1608
 
@@ -4251,6 +4163,17 @@ declare class ArrayColumn<Item extends ColumnType> extends ColumnType<Item['type
4251
4163
  parseFn: (input: unknown) => unknown[];
4252
4164
  }
4253
4165
 
4166
+ declare function timestamps<T extends ColumnType>(this: {
4167
+ timestamp(): T;
4168
+ }): {
4169
+ createdAt: T & {
4170
+ hasDefault: true;
4171
+ };
4172
+ updatedAt: T & {
4173
+ hasDefault: true;
4174
+ };
4175
+ };
4176
+
4254
4177
  declare type ColumnTypes = typeof columnTypes;
4255
4178
  declare type TableData = {
4256
4179
  primaryKey?: {
@@ -4347,7 +4270,6 @@ declare namespace utils {
4347
4270
  declare type Value = any;
4348
4271
  declare const quote: (value: Value) => string;
4349
4272
 
4350
- declare const removeFromQuery: <T extends Query>(q: T, key: string) => T;
4351
4273
  declare const pushQueryArray: <T extends {
4352
4274
  query: QueryData;
4353
4275
  }>(q: T, key: string, value: unknown) => T;
@@ -4371,4 +4293,4 @@ declare class UnhandledTypeError extends PormInternalError {
4371
4293
  constructor(value: never);
4372
4294
  }
4373
4295
 
4374
- export { Adapter, AdapterOptions, AddQueryJoinedTable, AddQuerySelect, AddQueryWith, AfterCallback, Aggregate, Aggregate1ArgumentTypes, AggregateArg, AggregateItem, AggregateItemArg, AggregateItemOptions, AggregateOptions, AliasOrTable, AnyColumnType, AnyColumnTypeCreator, ArrayCardinality, ArrayColumn, ArrayData, ArrayOfColumnsObjects, AssertArray, BaseNumberData, BaseRelation, BaseStringData, BeforeCallback, BelongsToNestedInsert, BelongsToNestedUpdate, BelongsToRelation, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BooleanExpression, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, Clear, ClearStatement, CoalesceString, ColumnData, ColumnInfo, ColumnInfoMethods, ColumnInfoQueryData, ColumnInput, ColumnNameOfModel, ColumnOperators, ColumnOutput, ColumnParser, ColumnShapeInput, ColumnShapeOutput, ColumnType, ColumnTypes, ColumnTypesBase, ColumnsObject, ColumnsParsers, ColumnsShape, CommonQueryData, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, GetTypeOrRaw, GetTypesOrRaw, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, Insert, InsertData, InsertQueryData, IntegerBaseColumn, IntegerColumn, IntervalColumn, IsEqual, JSONAny, JSONArray, JSONBigInt, JSONBoolean, JSONColumn, JSONDate, JSONDiscriminatedObject, JSONDiscriminatedUnion, JSONEnum, JSONInstanceOf, JSONIntersection, JSONLazy, JSONLiteral, JSONMap, JSONNaN, JSONNativeEnum, JSONNever, JSONNotNullable, JSONNotNullish, JSONNull, JSONNullable, JSONNullish, JSONNumber, JSONObject, JSONObjectShape, JSONOptional, JSONRecord, JSONRecordKeyType, JSONRequired, JSONSet, JSONString, JSONTextColumn, JSONTuple, JSONTupleItems, JSONType, JSONTypeAny, JSONTypeData, JSONTypes, JSONUndefined, JSONUnion, JSONUnknown, JSONVoid, Join, JoinArgs, JoinCallback, JoinCallbackArg, JoinItem, JoinedTablesBase, Json, JsonItem, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MaybeArray, Merge, MoneyColumn, MoreThanOneRowError, NestedInsertItem, NestedInsertManyItems, NestedInsertOneItem, NestedUpdateItem, NestedUpdateManyItems, NestedUpdateOneItem, NotFoundError, NullableColumn, NumberAsStringBaseColumn, NumberBaseColumn, NumberColumn, NumberColumnData, NumberExpression, OnConflictItem, OnConflictMergeUpdate, OnConflictQueryBuilder, OnQueryBuilder, Operator, Operators, OrderArg, OrderItem, OutputTypeOfTuple, OutputTypeOfTupleWithRest, OwnTypeProps, PathColumn, PluckResultColumnType, PointColumn, PolygonColumn, PormError, PormInternalError, Primitive, PropertyKeyUnionToArray, Query, QueryArraysResult, QueryBase, QueryCallbacks, QueryData, QueryGet, QueryInput, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryResultRow, QueryReturnType, QuerySelectAll, QueryUpsert, QueryWithTable, RawExpression, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationsBase, Select, SelectAgg, SelectArg, SelectFunctionItem, SelectItem, SelectQueryData, Selectable, SelectableBase, SerialColumn, SetOptional, SetQueryJoinedTables, SetQueryReturns, SetQueryReturnsAll, SetQueryReturnsColumnInfo, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsPluck, SetQueryReturnsRowCount, SetQueryReturnsRows, SetQueryReturnsValue, SetQueryReturnsValueOptional, SetQueryReturnsVoid, SetQueryTableAlias, SetQueryWindows, SetQueryWith, SimpleSpread, SingleColumnIndexOptions, SmallIntColumn, SmallSerialColumn, SomeIsTrue, SortDir, Spread, Sql, StringColumn, StringExpression, StringKey, TableData, TableSchema, TextBaseColumn, TextColumn, TextColumnData, Then, ThenResult, TimeColumn, TimeInterval, TimeWithTimeZoneColumn, TimestampColumn, TimestampWithTimeZoneColumn, ToSqlCtx, ToSqlOptions, Transaction, TransactionAdapter, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionToArray, UnionToIntersection, UnionToOvlds, UnknownKeysParam, Update, UpdateData, UpdateQueryData, UpdateQueryDataItem, UpdateQueryDataObject, UpdatedAtDataInjector, UpsertData, UpsertResult, UpsertThis, ValidationContext, VarCharColumn, Where, WhereArg, WhereInArg, WhereInColumn, WhereInItem, WhereInValues, WhereItem, WhereJsonPathEqualsItem, WhereOnItem, WhereOnJoinItem, WhereQueryBuilder, WhereResult, WindowArg, WindowArgDeclaration, WindowDeclaration, WindowFunctionOptions, WindowItem, With, WithDataBase, WithDataItem, WithItem, WithOptions, XMLColumn, addOr, addOrNot, addParserForRawExpression, addParserForSelectItem, addParserToQuery, addQueryOn, addQueryOrOn, addQuestionMarks, addWhere, addWhereIn, addWhereNot, aggregate1FunctionNames, applyMixins, array, arrayToEnum, baseObjectOutputType, columnTypes, utils as columnUtils, constructType, createDb, createOperator, defaultsKey, discriminatedUnion, emptyObject, enumType, flatten, getClonedQueryData, getColumnTypes, getQueryAs, getQueryParsers, getRaw, getRawSql, getTableData, getValidEnumValues, getValueKey, handleResult, identity, instanceOf, intersection, isRaw, isRequiredRelationKey, joinTruthy, jsonTypes, lazy, literal, logColors, logParamToLogObject, makeRegexToFindInSql, map, nativeEnum, newTableData, noop, notNullable, notNullish, nullable, nullish, object, optional, parseRecord, parseResult, processSelectArg, pushOrNewArray, pushOrNewArrayToObject, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryKeysOfNotSimpleQuery, queryMethodByReturnType, quote, raw, record, relationQueryKey, removeFromQuery, required, resetTableData, scalarTypes, set, setQueryObjectValue, toArray, toSql, toSqlCacheKey, tuple, union };
4296
+ export { Adapter, AdapterOptions, AddQueryJoinedTable, AddQuerySelect, AddQueryWith, AfterCallback, Aggregate, Aggregate1ArgumentTypes, AggregateArg, AggregateItem, AggregateItemArg, AggregateItemOptions, AggregateOptions, AliasOrTable, AnyColumnType, AnyColumnTypeCreator, ArrayCardinality, ArrayColumn, ArrayData, ArrayOfColumnsObjects, AssertArray, BaseNumberData, BaseRelation, BaseStringData, BeforeCallback, BelongsToNestedInsert, BelongsToNestedUpdate, BelongsToRelation, BigIntColumn, BigSerialColumn, BitColumn, BitVaryingColumn, BooleanColumn, BooleanExpression, BoxColumn, ByteaColumn, CharColumn, CidrColumn, CircleColumn, Clear, ClearStatement, CoalesceString, ColumnData, ColumnInfo, ColumnInfoMethods, ColumnInfoQueryData, ColumnInput, ColumnNameOfModel, ColumnOperators, ColumnOutput, ColumnParser, ColumnShapeInput, ColumnShapeOutput, ColumnType, ColumnTypes, ColumnTypesBase, ColumnsObject, ColumnsParsers, ColumnsShape, CommonQueryData, DateBaseColumn, DateColumn, DateColumnData, DateTimeBaseClass, DateTimeColumnData, DateTimeWithTimeZoneBaseClass, Db, DbOptions, DbTableOptions, DecimalBaseColumn, DecimalColumn, DecimalColumnData, DeepPartial, DefaultSelectColumns, Delete, DeleteQueryData, DoublePrecisionColumn, DropMode, EMPTY_OBJECT, EmptyObject, EnumColumn, EnumLike, Except, Expression, ExpressionOfType, ExpressionOutput, FilterTuple, For, ForeignKey, ForeignKeyModel, ForeignKeyModelWithColumns, ForeignKeyOptions, From, GetArg, GetTypeOrRaw, GetTypesOrRaw, HasAndBelongsToManyRelation, HasManyNestedInsert, HasManyNestedUpdate, HasManyRelation, HasOneNestedInsert, HasOneNestedUpdate, HasOneRelation, Having, HavingArg, HavingItem, IndexColumnOptions, IndexOptions, InetColumn, Insert, InsertData, InsertQueryData, IntegerBaseColumn, IntegerColumn, IntervalColumn, IsEqual, JSONAny, JSONArray, JSONBigInt, JSONBoolean, JSONColumn, JSONDate, JSONDiscriminatedObject, JSONDiscriminatedUnion, JSONEnum, JSONInstanceOf, JSONIntersection, JSONLazy, JSONLiteral, JSONMap, JSONNaN, JSONNativeEnum, JSONNever, JSONNotNullable, JSONNotNullish, JSONNull, JSONNullable, JSONNullish, JSONNumber, JSONObject, JSONObjectShape, JSONOptional, JSONRecord, JSONRecordKeyType, JSONRequired, JSONSet, JSONString, JSONTextColumn, JSONTuple, JSONTupleItems, JSONType, JSONTypeAny, JSONTypeData, JSONTypes, JSONUndefined, JSONUnion, JSONUnknown, JSONVoid, Join, JoinArgs, JoinCallback, JoinCallbackArg, JoinItem, JoinedTablesBase, Json, JsonItem, LimitedTextBaseColumn, LineColumn, LsegColumn, MacAddr8Column, MacAddrColumn, MaybeArray, Merge, MergeQuery, MergeQueryMethods, MoneyColumn, MoreThanOneRowError, NestedInsertItem, NestedInsertManyItems, NestedInsertOneItem, NestedUpdateItem, NestedUpdateManyItems, NestedUpdateOneItem, NotFoundError, NullableColumn, NumberAsStringBaseColumn, NumberBaseColumn, NumberColumn, NumberColumnData, NumberExpression, OnConflictItem, OnConflictMergeUpdate, OnConflictQueryBuilder, OnQueryBuilder, Operator, Operators, OrderArg, OrderItem, OutputTypeOfTuple, OutputTypeOfTupleWithRest, OwnTypeProps, PathColumn, PluckResultColumnType, PointColumn, PolygonColumn, PormError, PormInternalError, Primitive, Query, QueryArraysResult, QueryBase, QueryCallbacks, QueryData, QueryGet, QueryInput, QueryLog, QueryLogObject, QueryLogOptions, QueryLogger, QueryMethods, QueryResult, QueryResultRow, QueryReturnType, QueryReturnsAll, QuerySelectAll, QueryThen, QueryUpsert, QueryWithTable, RawExpression, RealColumn, Relation, RelationQuery, RelationQueryBase, RelationsBase, Select, SelectAgg, SelectArg, SelectFunctionItem, SelectItem, SelectQueryData, Selectable, SelectableBase, SerialColumn, SetOptional, SetQueryJoinedTables, SetQueryReturns, SetQueryReturnsAll, SetQueryReturnsColumnInfo, SetQueryReturnsOne, SetQueryReturnsOneOptional, SetQueryReturnsPluck, SetQueryReturnsRowCount, SetQueryReturnsRows, SetQueryReturnsValue, SetQueryReturnsValueOptional, SetQueryReturnsVoid, SetQueryTableAlias, SetQueryWindows, SetQueryWith, SimpleSpread, SingleColumnIndexOptions, SmallIntColumn, SmallSerialColumn, SomeIsTrue, SortDir, Spread, Sql, StringColumn, StringExpression, StringKey, TableData, TableSchema, TextBaseColumn, TextColumn, TextColumnData, Then, ThenResult, TimeColumn, TimeInterval, TimeWithTimeZoneColumn, TimestampColumn, TimestampWithTimeZoneColumn, ToSqlCtx, ToSqlOptions, Transaction, TransactionAdapter, TruncateQueryData, TsQueryColumn, TsVectorColumn, TypeParsers, UUIDColumn, UnhandledTypeError, Union, UnionArg, UnionItem, UnionToArray, UnionToIntersection, UnionToOvlds, UnknownKeysParam, Update, UpdateData, UpdateQueryData, UpdateQueryDataItem, UpdateQueryDataObject, UpdatedAtDataInjector, UpsertData, UpsertResult, UpsertThis, ValidationContext, VarCharColumn, Where, WhereArg, WhereInArg, WhereInColumn, WhereInItem, WhereInValues, WhereItem, WhereJsonPathEqualsItem, WhereOnItem, WhereOnJoinItem, WhereQueryBuilder, WhereResult, WindowArg, WindowArgDeclaration, WindowDeclaration, WindowFunctionOptions, WindowItem, With, WithDataBase, WithDataItem, WithItem, WithOptions, XMLColumn, addOr, addOrNot, addParserForRawExpression, addParserForSelectItem, addParserToQuery, addQueryOn, addQueryOrOn, addQuestionMarks, addWhere, addWhereIn, addWhereNot, aggregate1FunctionNames, applyMixins, array, arrayToEnum, baseObjectOutputType, checkIfASimpleQuery, columnTypes, utils as columnUtils, constructType, createDb, createOperator, defaultsKey, discriminatedUnion, emptyObject, enumType, flatten, getClonedQueryData, getColumnTypes, getQueryAs, getQueryParsers, getRaw, getRawSql, getTableData, getValidEnumValues, getValueKey, handleResult, identity, instanceOf, intersection, isRaw, isRequiredRelationKey, joinTruthy, jsonTypes, lazy, literal, logColors, logParamToLogObject, makeRegexToFindInSql, map, nativeEnum, newTableData, noop, notNullable, notNullish, nullable, nullish, object, optional, parseRecord, parseResult, processSelectArg, pushOrNewArray, pushOrNewArrayToObject, pushQueryArray, pushQueryOn, pushQueryOrOn, pushQueryValue, queryMethodByReturnType, queryTypeWithLimitOne, quote, raw, record, relationQueryKey, required, resetTableData, scalarTypes, set, setQueryObjectValue, toArray, toSql, toSqlCacheKey, tuple, union };