pqb 0.71.1 → 0.71.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -2084,11 +2084,11 @@ interface QueryInternal$1<SinglePrimaryKey = any, UniqueColumns = any, UniqueCol
2084
2084
  nestedCreateBatchMax: number;
2085
2085
  }
2086
2086
  type SQLQueryArgs = TemplateLiteralArgs$1 | [RawSqlBase$1];
2087
- interface RawSqlBase$1 extends Expression$1 {
2087
+ interface RawSqlBase$1<T extends Column$1.Pick.QueryColumn = Column$1.Pick.QueryColumn> extends Expression$1<T> {
2088
2088
  _sql: string | TemplateLiteralArgs$1;
2089
2089
  _values?: RawSQLValues;
2090
2090
  }
2091
- interface RawSql$1<T extends Column$1.Pick.QueryColumn, ColumnTypes> extends Expression$1<T>, RawSqlBase$1, ExpressionTypeMethod {}
2091
+ interface RawSql$1<T extends Column$1.Pick.QueryColumn, ColumnTypes> extends Expression$1<T>, RawSqlBase$1<T>, ExpressionTypeMethod {}
2092
2092
  declare class RawSql$1<T extends Column$1.Pick.QueryColumn = Column$1.Pick.QueryColumn, ColumnTypes = DefaultColumnTypes$1<ColumnSchemaConfig$1>> extends Expression$1<T> {
2093
2093
  result: {
2094
2094
  value: T;
@@ -5112,6 +5112,246 @@ interface OperatorsArray$1<T> extends Ord$1<T[]> {
5112
5112
  _opType: number | { [K in Exclude<keyof OperatorsNumber, '__hasSelect'>]?: OperatorsNumber[K]['_opType'] };
5113
5113
  };
5114
5114
  }
5115
+ interface ColumnsShape$1 {
5116
+ [K: string]: Column$1;
5117
+ }
5118
+ declare namespace ColumnsShape$1 {
5119
+ export type DefaultSelectKeys<S extends Column$1.QueryColumnsInit> = { [K in keyof S]: S[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof S];
5120
+ export type DefaultOutput<Set extends Column$1.QueryColumnsInit> = { [K in DefaultSelectKeys<Set>]: Set[K]['__outputType'] };
5121
+ export type Input<Shape extends Column$1.QueryColumnsInit, AppReadOnly = { [K in keyof Shape]: Shape[K]['data']['appReadOnly'] extends true ? K : never }[keyof Shape], Optional extends keyof Shape = { [K in keyof Shape]: Shape[K]['data']['optional'] extends true ? K : never }[keyof Shape]> = { [K in Exclude<keyof Shape, AppReadOnly | Optional>]: Shape[K]['__inputType'] } & { [K in Exclude<Optional, AppReadOnly>]?: Shape[K]['__inputType'] };
5122
+ export type InputPartial<Shape extends Column$1.QueryColumnsInit> = { [K in keyof Shape]?: Shape[K]['__inputType'] };
5123
+ export type Output<Shape extends Column$1.QueryColumns> = { [K in keyof Shape]: Shape[K]['__outputType'] };
5124
+ export type DefaultSelectOutput<Shape extends Column$1.QueryColumnsInit> = { [K in { [K in keyof Shape]: Shape[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof Shape]]: Shape[K]['__outputType'] };
5125
+ export interface MapToObjectColumn<Shape extends Column$1.QueryColumns> {
5126
+ dataType: 'object';
5127
+ __type: { [K in keyof Shape]: Shape[K]['__type'] };
5128
+ __outputType: ShallowSimplify$1<ObjectOutput<Shape>>;
5129
+ __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] };
5130
+ operators: OperatorsAny;
5131
+ }
5132
+ export interface MapToNullableObjectColumn<Shape extends Column$1.QueryColumns> {
5133
+ dataType: 'object';
5134
+ __type: { [K in keyof Shape]: Shape[K]['__type'] };
5135
+ __outputType: ShallowSimplify$1<ObjectOutput<Shape>> | undefined;
5136
+ __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] } | null;
5137
+ operators: OperatorsAny;
5138
+ }
5139
+ export interface MapToPluckColumn<Shape extends Column$1.QueryColumns> {
5140
+ dataType: 'array';
5141
+ __type: Shape['pluck']['__type'][];
5142
+ __outputType: Shape['pluck']['__outputType'][];
5143
+ __queryType: Shape['pluck']['__queryType'][];
5144
+ operators: OperatorsAny;
5145
+ }
5146
+ export interface MapToObjectArrayColumn<Shape extends Column$1.QueryColumns> {
5147
+ dataType: 'array';
5148
+ __type: { [K in keyof Shape]: Shape[K]['__type'] }[];
5149
+ __outputType: ShallowSimplify$1<ObjectOutput<Shape>>[];
5150
+ __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] }[];
5151
+ operators: OperatorsAny;
5152
+ }
5153
+ type ObjectOutput<Shape extends Column$1.QueryColumns> = { [K in keyof Shape]: Shape[K]['__outputType'] };
5154
+ export {};
5155
+ }
5156
+ interface SelectSelf extends PickQuerySelectable, PickQueryHasSelect, PickQueryDefaultSelect, PickQueryShape$1, PickQueryRelations$1, PickQueryResult, PickQueryReturnType, PickQueryWithData {}
5157
+ type SelectArgs<T extends SelectSelf> = ('*' | keyof T['__selectable'])[];
5158
+ interface SubQueryAddition<T extends PickQueryWithData> extends IsSubQuery {
5159
+ withData: T['withData'];
5160
+ }
5161
+ type SelectAsFnArg<T extends PickQueryRelationsWithData> = EmptyObject$1 extends T['relations'] ? T : { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? RelationQueryMaybeSingle<T['relations'][K]> & SubQueryAddition<T> : K extends keyof T ? T[K] : never };
5162
+ interface SelectAsArg<T extends SelectSelf> {
5163
+ [K: string]: keyof T['__selectable'] | Expression$1 | ((q: SelectAsFnArg<T>) => unknown);
5164
+ }
5165
+ type SelectAsFnReturnType = {
5166
+ result: Column$1.QueryColumns;
5167
+ returnType: Exclude<QueryReturnType$1, 'rows'>;
5168
+ } | Expression$1;
5169
+ interface SelectAsCheckReturnTypes {
5170
+ [K: string]: PropertyKey | Expression$1 | ((q: never) => SelectAsFnReturnType);
5171
+ }
5172
+ type SelectReturnType<T extends PickQueryReturnType> = T['returnType'] extends 'valueOrThrow' ? 'oneOrThrow' : T extends 'value' ? 'one' : T['returnType'] extends 'pluck' ? 'all' : T['returnType'];
5173
+ type SelectResult<T extends SelectSelf, Columns extends PropertyKey[]> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends 'result' ? { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5174
+ type SelectResultObj<T extends SelectSelf, Obj> = Obj extends SelectAsCheckReturnTypes ? { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never } : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never }> : T[K] } : `Invalid return type of ${{ [K in keyof Obj]: Obj[K] extends ((...args: any[]) => any) ? ReturnType<Obj[K]> extends SelectAsFnReturnType ? never : K : never }[keyof Obj] & string}`;
5175
+ type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey[], Obj> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? // Combine previously selected items, all columns if * was provided,
5176
+ { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5177
+ interface AllowedRelationOneQueryForSelectable extends IsSubQuery {
5178
+ result: Column$1.QueryColumns;
5179
+ returnType: 'value' | 'valueOrThrow' | 'one' | 'oneOrThrow';
5180
+ }
5181
+ type SelectAsSelectable<Obj> = UnionToIntersection<{ [K in keyof Obj]: Obj[K] extends ((q: never) => infer R extends AllowedRelationOneQueryForSelectable) ? { [C in R['returnType'] extends 'value' | 'valueOrThrow' ? K : keyof R['result'] as R['returnType'] extends 'value' | 'valueOrThrow' ? K : `${K & string}.${C & string}`]: {
5182
+ as: C;
5183
+ column: R['returnType'] extends 'value' | 'valueOrThrow' ? R['result']['value'] : R['result'][C & keyof R['result']];
5184
+ } } : never }[keyof Obj]>;
5185
+ type SelectAsValueResult<T extends SelectSelf, Arg> = Arg extends keyof T['__selectable'] ? T['__selectable'][Arg]['column'] : Arg extends Expression$1 ? Arg['result']['value'] : Arg extends ((q: never) => IsQuery$1) ? SelectSubQueryResult<ReturnType<Arg>> : Arg extends ((q: never) => Expression$1) ? ReturnType<Arg>['result']['value'] : Arg extends ((q: never) => IsQuery$1 | Expression$1) ? SelectSubQueryResult<Exclude<ReturnType<Arg>, Expression$1>> | Exclude<ReturnType<Arg>, IsQuery$1>['result']['value'] : never;
5186
+ type SelectSubQueryResult<Arg extends SelectSelf> = Arg['returnType'] extends undefined | 'all' ? ColumnsShape$1.MapToObjectArrayColumn<Arg['result']> : Arg['returnType'] extends 'value' | 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? ColumnsShape$1.MapToPluckColumn<Arg['result']> : Arg['returnType'] extends 'one' ? ColumnsShape$1.MapToNullableObjectColumn<Arg['result']> : ColumnsShape$1.MapToObjectColumn<Arg['result']>;
5187
+ declare class Select {
5188
+ /**
5189
+ * Takes a list of columns to be selected, and by default, the query builder will select all columns of the table.
5190
+ *
5191
+ * The last argument can be an object. Keys of the object are column aliases, value can be a column name, sub-query, or raw SQL expression.
5192
+ *
5193
+ * ```ts
5194
+ * import { sql } from './baseTable'
5195
+ *
5196
+ * // select columns of the table:
5197
+ * db.table.select('id', 'name', { idAlias: 'id' });
5198
+ *
5199
+ * // accepts columns with table names:
5200
+ * db.table.select('user.id', 'user.name', { nameAlias: 'user.name' });
5201
+ *
5202
+ * // table name may refer to the current table or a joined table:
5203
+ * db.table
5204
+ * .join(db.message, 'authorId', 'user.id')
5205
+ * .select('user.name', 'message.text', { textAlias: 'message.text' });
5206
+ *
5207
+ * // select value from the sub-query,
5208
+ * // this sub-query should return a single record and a single column:
5209
+ * db.table.select({
5210
+ * subQueryResult: Otherdb.table.select('column').take(),
5211
+ * });
5212
+ *
5213
+ * // select raw SQL value, specify the returning type via <generic> syntax:
5214
+ * db.table.select({
5215
+ * raw: sql<number>`1 + 2`,
5216
+ * });
5217
+ *
5218
+ * // select raw SQL value, the resulting type can be set by providing a column type in such way:
5219
+ * db.table.select({
5220
+ * raw: sql`1 + 2`.type((t) => t.integer()),
5221
+ * });
5222
+ *
5223
+ * // same raw SQL query as above, but the sql is returned from a callback
5224
+ * db.table.select({
5225
+ * raw: () => sql`1 + 2`.type((t) => t.integer()),
5226
+ * });
5227
+ * ```
5228
+ *
5229
+ * When you use the ORM and defined relations, `select` can also accept callbacks with related table queries:
5230
+ *
5231
+ * ```ts
5232
+ * await db.author.select({
5233
+ * allBooks: (q) => q.books,
5234
+ * firstBook: (q) => q.books.order({ createdAt: 'ASC' }).take(),
5235
+ * booksCount: (q) => q.books.count(),
5236
+ * });
5237
+ * ```
5238
+ *
5239
+ * When you're selecting a relation that's connected via `belongsTo` or `hasOne`, it becomes available to use in `order` or in `where`:
5240
+ *
5241
+ * ```ts
5242
+ * // select books with their authors included, order by author name and filter by author column:
5243
+ * await db.books
5244
+ * .select({
5245
+ * author: (q) => q.author,
5246
+ * })
5247
+ * .order('author.name')
5248
+ * .where({ 'author.isPopular': true });
5249
+ * ```
5250
+ */
5251
+ select<T extends SelectSelf, Columns extends SelectArgs<T>>(this: T, ...args: Columns): SelectResult<T, Columns>;
5252
+ select<T extends SelectSelf, Obj extends SelectAsArg<T>>(this: T, obj: Obj): SelectResultObj<T, Obj>;
5253
+ select<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(this: T, ...args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
5254
+ /**
5255
+ * When querying the table or creating records, all columns are selected by default,
5256
+ * but updating and deleting queries are returning affected row counts by default.
5257
+ *
5258
+ * Use `selectAll` to select all columns. If the `.select` method was applied before it will be discarded.
5259
+ *
5260
+ * ```ts
5261
+ * const selectFull = await db.table
5262
+ * .select('id', 'name') // discarded by `selectAll`
5263
+ * .selectAll();
5264
+ *
5265
+ * const updatedFull = await db.table.selectAll().where(conditions).update(data);
5266
+ *
5267
+ * const deletedFull = await db.table.selectAll().where(conditions).delete();
5268
+ * ```
5269
+ */
5270
+ selectAll<T extends SelectSelf>(this: T): SelectResult<T, ['*']>;
5271
+ }
5272
+ type SelectItem = string | SelectAs | Expression$1 | undefined;
5273
+ interface SelectAs {
5274
+ selectAs: SelectAsValue;
5275
+ }
5276
+ interface SelectAsValue {
5277
+ [K: string]: string | Query | Expression$1 | undefined;
5278
+ }
5279
+ type QueryDataTransform = QueryDataTransformFn | {
5280
+ map: (record: unknown, index: number, array: unknown) => unknown;
5281
+ thisArg?: unknown;
5282
+ };
5283
+ interface QueryDataTransformFn {
5284
+ (data: unknown, queryData: unknown): unknown;
5285
+ }
5286
+ /**
5287
+ * See `transform` query method.
5288
+ * This helper applies all transform functions to a result.
5289
+ *
5290
+ * @param queryData - query data
5291
+ * @param returnType - return type of the query, for proper `map` handling
5292
+ * @param fns - array of transform functions, can be undefined
5293
+ * @param result - query result to transform
5294
+ */
5295
+ declare class QueryTransform {
5296
+ /**
5297
+ * Transform the result of the query right after loading it.
5298
+ *
5299
+ * `transform` method should be called in the last order, other methods can't be chained after calling it.
5300
+ *
5301
+ * It is meant to transform the whole result of a query, for transforming individual records consider using {@link QueryMap.map}.
5302
+ *
5303
+ * The [hooks](/guide/hooks.html) that are going to run after the query will receive the query result **before** transferring.
5304
+ *
5305
+ * Consider the following example of a cursor-based pagination by `id`:
5306
+ *
5307
+ * ```ts
5308
+ * const lastId: number | undefined = req.query.cursor;
5309
+ *
5310
+ * type Result = {
5311
+ * nodes: { id: number; text: string }[];
5312
+ * cursor?: number;
5313
+ * };
5314
+ *
5315
+ * // result is only for demo, it will be inferred
5316
+ * const posts: Result = await db.post
5317
+ * .select('id', 'text')
5318
+ * .where({ id: { lt: lastId } })
5319
+ * .order({ id: 'DESC' })
5320
+ * .limit(100)
5321
+ * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id }));
5322
+ * ```
5323
+ *
5324
+ * You can also use the `tranform` on nested sub-queries:
5325
+ *
5326
+ * ```ts
5327
+ * type Result = {
5328
+ * nodes: {
5329
+ * id: number;
5330
+ * text: string;
5331
+ * comments: { nodes: { id: number; text: string }[]; cursor?: number };
5332
+ * }[];
5333
+ * cursor?: number;
5334
+ * };
5335
+ *
5336
+ * const postsWithComments: Result = await db.post
5337
+ * .select('id', 'text')
5338
+ * .select({
5339
+ * comments: (q) =>
5340
+ * q.comments
5341
+ * .select('id', 'text')
5342
+ * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id })),
5343
+ * })
5344
+ * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id }));
5345
+ * ```
5346
+ *
5347
+ * @param fn - function to transform query result with
5348
+ */
5349
+ transform<T extends IsQuery$1, Result>(this: T, fn: (input: T extends {
5350
+ then: QueryThen<infer Data>;
5351
+ } ? Data : never, queryData: QueryData$1) => Result): { [K in keyof T]: K extends 'returnType' ? 'valueOrThrow' : K extends 'result' ? {
5352
+ value: Column$1.Pick.QueryColumnOfType<Result>;
5353
+ } : K extends 'then' ? QueryThen<Result> : T[K] };
5354
+ }
5115
5355
  type SelectableOrExpression<T extends PickQuerySelectable = PickQuerySelectable, C extends Column$1.Pick.QueryColumn = Column$1.Pick.QueryColumn> = '*' | keyof T['__selectable'] | Expression$1<C>;
5116
5356
  type SelectableOrExpressions<T extends PickQuerySelectable = PickQuerySelectable, C extends Column$1.Pick.QueryColumn = Column$1.Pick.QueryColumn> = ('*' | keyof T['__selectable'] | Expression$1<C>)[];
5117
5357
  type ExpressionOutput<T extends PickQuerySelectable, Expr extends SelectableOrExpression<T>> = Expr extends keyof T['__selectable'] ? T['__selectable'][Expr]['column'] : Expr extends Expression$1 ? Expr['result']['value'] : never;
@@ -5122,6 +5362,7 @@ interface ExpressionData$1 extends HasBeforeAndBeforeSet {
5122
5362
  before?: QueryBeforeHook$1[];
5123
5363
  dynamicBefore?: boolean;
5124
5364
  getColumn?: Column$1;
5365
+ transform?: QueryDataTransform[];
5125
5366
  }
5126
5367
  declare abstract class Expression$1<T extends Column$1.Pick.QueryColumn = Column$1.Pick.QueryColumn> {
5127
5368
  abstract result: {
@@ -5132,6 +5373,23 @@ declare abstract class Expression$1<T extends Column$1.Pick.QueryColumn = Column
5132
5373
  kind: 'select';
5133
5374
  };
5134
5375
  toSQL(ctx: ToSqlValues$1, quotedAs?: string): string;
5376
+ /**
5377
+ * Transform the expression value after loading it.
5378
+ *
5379
+ * It is meant to transform expressions selected by a query.
5380
+ *
5381
+ * @param fn - function to transform expression value with
5382
+ */
5383
+ transform<Self extends {
5384
+ result: {
5385
+ value: Column$1.Pick.QueryColumn;
5386
+ };
5387
+ q: ExpressionData$1;
5388
+ }, Result>(this: Self, fn: (input: Self['result']['value']['__type'], queryData: ExpressionData$1) => Result): Omit<Self, 'result'> & {
5389
+ result: {
5390
+ value: Column$1.Pick.QueryColumnOfType<Result>;
5391
+ };
5392
+ };
5135
5393
  abstract makeSQL(ctx: ToSqlValues$1, quotedAs?: string): string;
5136
5394
  }
5137
5395
  type TemplateLiteralArgs$1 = [strings: TemplateStringsArray, ...values: unknown[]];
@@ -5676,163 +5934,6 @@ declare class FromMethods {
5676
5934
  */
5677
5935
  only<T>(this: T, only?: boolean): T;
5678
5936
  }
5679
- interface ColumnsShape$1 {
5680
- [K: string]: Column$1;
5681
- }
5682
- declare namespace ColumnsShape$1 {
5683
- export type DefaultSelectKeys<S extends Column$1.QueryColumnsInit> = { [K in keyof S]: S[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof S];
5684
- export type DefaultOutput<Set extends Column$1.QueryColumnsInit> = { [K in DefaultSelectKeys<Set>]: Set[K]['__outputType'] };
5685
- export type Input<Shape extends Column$1.QueryColumnsInit, AppReadOnly = { [K in keyof Shape]: Shape[K]['data']['appReadOnly'] extends true ? K : never }[keyof Shape], Optional extends keyof Shape = { [K in keyof Shape]: Shape[K]['data']['optional'] extends true ? K : never }[keyof Shape]> = { [K in Exclude<keyof Shape, AppReadOnly | Optional>]: Shape[K]['__inputType'] } & { [K in Exclude<Optional, AppReadOnly>]?: Shape[K]['__inputType'] };
5686
- export type InputPartial<Shape extends Column$1.QueryColumnsInit> = { [K in keyof Shape]?: Shape[K]['__inputType'] };
5687
- export type Output<Shape extends Column$1.QueryColumns> = { [K in keyof Shape]: Shape[K]['__outputType'] };
5688
- export type DefaultSelectOutput<Shape extends Column$1.QueryColumnsInit> = { [K in { [K in keyof Shape]: Shape[K]['data']['explicitSelect'] extends true | undefined ? never : K }[keyof Shape]]: Shape[K]['__outputType'] };
5689
- export interface MapToObjectColumn<Shape extends Column$1.QueryColumns> {
5690
- dataType: 'object';
5691
- __type: { [K in keyof Shape]: Shape[K]['__type'] };
5692
- __outputType: ShallowSimplify$1<ObjectOutput<Shape>>;
5693
- __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] };
5694
- operators: OperatorsAny;
5695
- }
5696
- export interface MapToNullableObjectColumn<Shape extends Column$1.QueryColumns> {
5697
- dataType: 'object';
5698
- __type: { [K in keyof Shape]: Shape[K]['__type'] };
5699
- __outputType: ShallowSimplify$1<ObjectOutput<Shape>> | undefined;
5700
- __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] } | null;
5701
- operators: OperatorsAny;
5702
- }
5703
- export interface MapToPluckColumn<Shape extends Column$1.QueryColumns> {
5704
- dataType: 'array';
5705
- __type: Shape['pluck']['__type'][];
5706
- __outputType: Shape['pluck']['__outputType'][];
5707
- __queryType: Shape['pluck']['__queryType'][];
5708
- operators: OperatorsAny;
5709
- }
5710
- export interface MapToObjectArrayColumn<Shape extends Column$1.QueryColumns> {
5711
- dataType: 'array';
5712
- __type: { [K in keyof Shape]: Shape[K]['__type'] }[];
5713
- __outputType: ShallowSimplify$1<ObjectOutput<Shape>>[];
5714
- __queryType: { [K in keyof Shape]: Shape[K]['__queryType'] }[];
5715
- operators: OperatorsAny;
5716
- }
5717
- type ObjectOutput<Shape extends Column$1.QueryColumns> = { [K in keyof Shape]: Shape[K]['__outputType'] };
5718
- export {};
5719
- }
5720
- interface SelectSelf extends PickQuerySelectable, PickQueryHasSelect, PickQueryDefaultSelect, PickQueryShape$1, PickQueryRelations$1, PickQueryResult, PickQueryReturnType, PickQueryWithData {}
5721
- type SelectArgs<T extends SelectSelf> = ('*' | keyof T['__selectable'])[];
5722
- interface SubQueryAddition<T extends PickQueryWithData> extends IsSubQuery {
5723
- withData: T['withData'];
5724
- }
5725
- type SelectAsFnArg<T extends PickQueryRelationsWithData> = EmptyObject$1 extends T['relations'] ? T : { [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? RelationQueryMaybeSingle<T['relations'][K]> & SubQueryAddition<T> : K extends keyof T ? T[K] : never };
5726
- interface SelectAsArg<T extends SelectSelf> {
5727
- [K: string]: keyof T['__selectable'] | Expression$1 | ((q: SelectAsFnArg<T>) => unknown);
5728
- }
5729
- type SelectAsFnReturnType = {
5730
- result: Column$1.QueryColumns;
5731
- returnType: Exclude<QueryReturnType$1, 'rows'>;
5732
- } | Expression$1;
5733
- interface SelectAsCheckReturnTypes {
5734
- [K: string]: PropertyKey | Expression$1 | ((q: never) => SelectAsFnReturnType);
5735
- }
5736
- type SelectReturnType<T extends PickQueryReturnType> = T['returnType'] extends 'valueOrThrow' ? 'oneOrThrow' : T extends 'value' ? 'one' : T['returnType'] extends 'pluck' ? 'all' : T['returnType'];
5737
- type SelectResult<T extends SelectSelf, Columns extends PropertyKey[]> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends 'result' ? { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in '*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number] as T['__selectable'][K]['as']]: T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5738
- type SelectResultObj<T extends SelectSelf, Obj> = Obj extends SelectAsCheckReturnTypes ? { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never } : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? keyof Obj | keyof T['result'] : keyof Obj]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : K extends keyof T['result'] ? T['result'][K] : never }> : T[K] } : `Invalid return type of ${{ [K in keyof Obj]: Obj[K] extends ((...args: any[]) => any) ? ReturnType<Obj[K]> extends SelectAsFnReturnType ? never : K : never }[keyof Obj] & string}`;
5739
- type SelectResultColumnsAndObj<T extends SelectSelf, Columns extends PropertyKey[], Obj> = { [K in keyof T]: K extends '__hasSelect' ? true : K extends '__selectable' ? T['__selectable'] & SelectAsSelectable<Obj> : K extends 'result' ? // Combine previously selected items, all columns if * was provided,
5740
- { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown) : K extends 'returnType' ? SelectReturnType<T> : K extends 'then' ? QueryThenByReturnType<SelectReturnType<T>, { [K in ('*' extends Columns[number] ? Exclude<Columns[number], '*'> | T['__defaultSelect'] : Columns[number]) | keyof Obj as K extends Columns[number] ? T['__selectable'][K]['as'] : K]: K extends keyof Obj ? SelectAsValueResult<T, Obj[K]> : T['__selectable'][K]['column'] } & (T['__hasSelect'] extends (T['returnType'] extends 'value' | 'valueOrThrow' ? never : true) ? Omit<T['result'], Columns[number]> : unknown)> : T[K] };
5741
- interface AllowedRelationOneQueryForSelectable extends IsSubQuery {
5742
- result: Column$1.QueryColumns;
5743
- returnType: 'value' | 'valueOrThrow' | 'one' | 'oneOrThrow';
5744
- }
5745
- type SelectAsSelectable<Obj> = UnionToIntersection<{ [K in keyof Obj]: Obj[K] extends ((q: never) => infer R extends AllowedRelationOneQueryForSelectable) ? { [C in R['returnType'] extends 'value' | 'valueOrThrow' ? K : keyof R['result'] as R['returnType'] extends 'value' | 'valueOrThrow' ? K : `${K & string}.${C & string}`]: {
5746
- as: C;
5747
- column: R['returnType'] extends 'value' | 'valueOrThrow' ? R['result']['value'] : R['result'][C & keyof R['result']];
5748
- } } : never }[keyof Obj]>;
5749
- type SelectAsValueResult<T extends SelectSelf, Arg> = Arg extends keyof T['__selectable'] ? T['__selectable'][Arg]['column'] : Arg extends Expression$1 ? Arg['result']['value'] : Arg extends ((q: never) => IsQuery$1) ? SelectSubQueryResult<ReturnType<Arg>> : Arg extends ((q: never) => Expression$1) ? ReturnType<Arg>['result']['value'] : Arg extends ((q: never) => IsQuery$1 | Expression$1) ? SelectSubQueryResult<Exclude<ReturnType<Arg>, Expression$1>> | Exclude<ReturnType<Arg>, IsQuery$1>['result']['value'] : never;
5750
- type SelectSubQueryResult<Arg extends SelectSelf> = Arg['returnType'] extends undefined | 'all' ? ColumnsShape$1.MapToObjectArrayColumn<Arg['result']> : Arg['returnType'] extends 'value' | 'valueOrThrow' ? Arg['result']['value'] : Arg['returnType'] extends 'pluck' ? ColumnsShape$1.MapToPluckColumn<Arg['result']> : Arg['returnType'] extends 'one' ? ColumnsShape$1.MapToNullableObjectColumn<Arg['result']> : ColumnsShape$1.MapToObjectColumn<Arg['result']>;
5751
- declare class Select {
5752
- /**
5753
- * Takes a list of columns to be selected, and by default, the query builder will select all columns of the table.
5754
- *
5755
- * The last argument can be an object. Keys of the object are column aliases, value can be a column name, sub-query, or raw SQL expression.
5756
- *
5757
- * ```ts
5758
- * import { sql } from './baseTable'
5759
- *
5760
- * // select columns of the table:
5761
- * db.table.select('id', 'name', { idAlias: 'id' });
5762
- *
5763
- * // accepts columns with table names:
5764
- * db.table.select('user.id', 'user.name', { nameAlias: 'user.name' });
5765
- *
5766
- * // table name may refer to the current table or a joined table:
5767
- * db.table
5768
- * .join(db.message, 'authorId', 'user.id')
5769
- * .select('user.name', 'message.text', { textAlias: 'message.text' });
5770
- *
5771
- * // select value from the sub-query,
5772
- * // this sub-query should return a single record and a single column:
5773
- * db.table.select({
5774
- * subQueryResult: Otherdb.table.select('column').take(),
5775
- * });
5776
- *
5777
- * // select raw SQL value, specify the returning type via <generic> syntax:
5778
- * db.table.select({
5779
- * raw: sql<number>`1 + 2`,
5780
- * });
5781
- *
5782
- * // select raw SQL value, the resulting type can be set by providing a column type in such way:
5783
- * db.table.select({
5784
- * raw: sql`1 + 2`.type((t) => t.integer()),
5785
- * });
5786
- *
5787
- * // same raw SQL query as above, but the sql is returned from a callback
5788
- * db.table.select({
5789
- * raw: () => sql`1 + 2`.type((t) => t.integer()),
5790
- * });
5791
- * ```
5792
- *
5793
- * When you use the ORM and defined relations, `select` can also accept callbacks with related table queries:
5794
- *
5795
- * ```ts
5796
- * await db.author.select({
5797
- * allBooks: (q) => q.books,
5798
- * firstBook: (q) => q.books.order({ createdAt: 'ASC' }).take(),
5799
- * booksCount: (q) => q.books.count(),
5800
- * });
5801
- * ```
5802
- *
5803
- * When you're selecting a relation that's connected via `belongsTo` or `hasOne`, it becomes available to use in `order` or in `where`:
5804
- *
5805
- * ```ts
5806
- * // select books with their authors included, order by author name and filter by author column:
5807
- * await db.books
5808
- * .select({
5809
- * author: (q) => q.author,
5810
- * })
5811
- * .order('author.name')
5812
- * .where({ 'author.isPopular': true });
5813
- * ```
5814
- */
5815
- select<T extends SelectSelf, Columns extends SelectArgs<T>>(this: T, ...args: Columns): SelectResult<T, Columns>;
5816
- select<T extends SelectSelf, Obj extends SelectAsArg<T>>(this: T, obj: Obj): SelectResultObj<T, Obj>;
5817
- select<T extends SelectSelf, Columns extends SelectArgs<T>, Obj extends SelectAsArg<T>>(this: T, ...args: [...columns: Columns, obj: Obj]): SelectResultColumnsAndObj<T, Columns, Obj>;
5818
- /**
5819
- * When querying the table or creating records, all columns are selected by default,
5820
- * but updating and deleting queries are returning affected row counts by default.
5821
- *
5822
- * Use `selectAll` to select all columns. If the `.select` method was applied before it will be discarded.
5823
- *
5824
- * ```ts
5825
- * const selectFull = await db.table
5826
- * .select('id', 'name') // discarded by `selectAll`
5827
- * .selectAll();
5828
- *
5829
- * const updatedFull = await db.table.selectAll().where(conditions).update(data);
5830
- *
5831
- * const deletedFull = await db.table.selectAll().where(conditions).delete();
5832
- * ```
5833
- */
5834
- selectAll<T extends SelectSelf>(this: T): SelectResult<T, ['*']>;
5835
- }
5836
5937
  interface QueryGetSelf extends PickQuerySelectable, PickQueryRelationsWithData {}
5837
5938
  type GetArg<T extends QueryGetSelf> = GetStringArg<T> | Expression$1 | ((q: SelectAsFnArg<T>) => Expression$1 | Query.Pick.SingleValueResult);
5838
5939
  type GetStringArg<T extends PickQuerySelectable> = keyof T['__selectable'] & string;
@@ -6615,13 +6716,6 @@ declare class QueryOrCreate {
6615
6716
  */
6616
6717
  orCreate<T extends UpsertThis$1>(this: T, data: OrCreateArg<CreateData$1<T>>): UpsertResult<T>;
6617
6718
  }
6618
- type SelectItem = string | SelectAs | Expression$1 | undefined;
6619
- interface SelectAs {
6620
- selectAs: SelectAsValue;
6621
- }
6622
- interface SelectAsValue {
6623
- [K: string]: string | Query | Expression$1 | undefined;
6624
- }
6625
6719
  type IsolationLevel$1 = 'SERIALIZABLE' | 'REPEATABLE READ' | 'READ COMMITTED' | 'READ UNCOMMITTED';
6626
6720
  interface TransactionOptions$1 extends SqlSessionState$1 {
6627
6721
  /**
@@ -7002,82 +7096,6 @@ type WrapQueryArg = FromQuerySelf;
7002
7096
  declare class QueryWrap {
7003
7097
  wrap<T extends IsQuery$1, Q extends WrapQueryArg, As extends string = 't'>(this: T, query: Q, as?: As): SetQueryTableAlias<Q, As>;
7004
7098
  }
7005
- type QueryDataTransform = QueryDataTransformFn | {
7006
- map: (record: unknown, index: number, array: unknown) => unknown;
7007
- thisArg?: unknown;
7008
- };
7009
- interface QueryDataTransformFn {
7010
- (data: unknown, queryData: unknown): unknown;
7011
- }
7012
- /**
7013
- * See `transform` query method.
7014
- * This helper applies all transform functions to a result.
7015
- *
7016
- * @param queryData - query data
7017
- * @param returnType - return type of the query, for proper `map` handling
7018
- * @param fns - array of transform functions, can be undefined
7019
- * @param result - query result to transform
7020
- */
7021
- declare class QueryTransform {
7022
- /**
7023
- * Transform the result of the query right after loading it.
7024
- *
7025
- * `transform` method should be called in the last order, other methods can't be chained after calling it.
7026
- *
7027
- * It is meant to transform the whole result of a query, for transforming individual records consider using {@link QueryMap.map}.
7028
- *
7029
- * The [hooks](/guide/hooks.html) that are going to run after the query will receive the query result **before** transferring.
7030
- *
7031
- * Consider the following example of a cursor-based pagination by `id`:
7032
- *
7033
- * ```ts
7034
- * const lastId: number | undefined = req.query.cursor;
7035
- *
7036
- * type Result = {
7037
- * nodes: { id: number; text: string }[];
7038
- * cursor?: number;
7039
- * };
7040
- *
7041
- * // result is only for demo, it will be inferred
7042
- * const posts: Result = await db.post
7043
- * .select('id', 'text')
7044
- * .where({ id: { lt: lastId } })
7045
- * .order({ id: 'DESC' })
7046
- * .limit(100)
7047
- * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id }));
7048
- * ```
7049
- *
7050
- * You can also use the `tranform` on nested sub-queries:
7051
- *
7052
- * ```ts
7053
- * type Result = {
7054
- * nodes: {
7055
- * id: number;
7056
- * text: string;
7057
- * comments: { nodes: { id: number; text: string }[]; cursor?: number };
7058
- * }[];
7059
- * cursor?: number;
7060
- * };
7061
- *
7062
- * const postsWithComments: Result = await db.post
7063
- * .select('id', 'text')
7064
- * .select({
7065
- * comments: (q) =>
7066
- * q.comments
7067
- * .select('id', 'text')
7068
- * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id })),
7069
- * })
7070
- * .transform((nodes) => ({ nodes, cursor: nodes.at(-1)?.id }));
7071
- * ```
7072
- *
7073
- * @param fn - function to transform query result with
7074
- */
7075
- transform<T extends IsQuery$1, Result>(this: T, fn: (input: T extends {
7076
- then: QueryThen<infer Data>;
7077
- } ? Data : never, queryData: QueryData$1) => Result): { [K in keyof T]: K extends 'returnType' ? 'valueOrThrow' : K extends 'result' ? {
7078
- value: Column$1.Pick.QueryColumnOfType<Result>;
7079
- } : K extends 'then' ? QueryThen<Result> : T[K] };
7080
- }
7081
7099
  type AfterHook$1<Select extends PropertyKey[], Shape extends Column$1.QueryColumns> = QueryAfterHook$1<{ [K in Select[number]]: K extends keyof Shape ? Shape[K]['__outputType'] : never }[]>;
7082
7100
  type HookSelectArg<T extends PickQueryShape$1> = (keyof T['shape'] & string)[];
7083
7101
  declare class QueryHookUtils$1<T extends PickQueryInputType$1> {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "pqb",
3
- "version": "0.71.1",
3
+ "version": "0.71.2",
4
4
  "description": "Postgres query builder",
5
5
  "keywords": [
6
6
  "pg",