pqb 0.11.31 → 0.11.32

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1,5 +1,5 @@
1
1
  import * as orchid_core from 'orchid-core';
2
- import { Sql, QueryBaseCommon, ColumnsShapeBase, QueryInternal, QueryMetaBase, EmptyObject, RawExpression, ColumnTypeBase, MaybeArray, QueryResultRow, AdapterBase, QueryInput, ColumnsParsers, getValueKey, StringKey, ColumnOutput, QueryThen, QueryCatch, NullableColumn, EmptyTuple, ColumnTypesBase, ColumnShapeOutput, DefaultSelectColumns, DbBase, SetOptional, Spread, CoalesceString, QueryCommon, BaseNumberData, Code, ColumnWithDefault, numberTypeMethods, BaseStringData, PrimaryKeyColumn, DateTypeMethods, DateColumnData, EncodeColumn, ParseColumn, JSONTypeAny, record, ArrayMethodsData, arrayMethods, ForeignKeyTable, name, ColumnNameOfTable, ColumnDataBase, BaseOperators, ValidationContext, MessageParam } from 'orchid-core';
2
+ import { Sql, QueryBaseCommon, ColumnsShapeBase, QueryInternal, QueryMetaBase, EmptyObject, RawExpression, ColumnTypeBase, MaybeArray, QueryResultRow, AdapterBase, QueryInput, ColumnsParsers, getValueKey, StringKey, ColumnOutput, QueryThen, QueryCatch, NullableColumn, EmptyTuple, ColumnTypesBase, ColumnShapeOutput, DefaultSelectColumns, DbBase, SetOptional, MergeObjects, CoalesceString, QueryCommon, Spread, BaseNumberData, Code, ColumnWithDefault, numberTypeMethods, BaseStringData, PrimaryKeyColumn, DateTypeMethods, DateColumnData, EncodeColumn, ParseColumn, JSONTypeAny, record, ArrayMethodsData, arrayMethods, ForeignKeyTable, name, ColumnNameOfTable, ColumnDataBase, BaseOperators, ValidationContext, MessageParam } from 'orchid-core';
3
3
  import { PoolConfig, Pool, PoolClient } from 'pg';
4
4
  import { inspect } from 'util';
5
5
  import { AsyncLocalStorage } from 'node:async_hooks';
@@ -1436,15 +1436,10 @@ declare class QueryGet {
1436
1436
  _getOptional<T extends Query, Arg extends GetArg<T>>(this: T, arg: Arg): GetOptionalResult<T, Arg>;
1437
1437
  }
1438
1438
 
1439
- type MergeQuery<T extends Query, Q extends Query, ReturnType extends QueryReturnType = QueryReturnType extends Q['returnType'] ? T['returnType'] : Q['returnType'], Data = T['meta']['hasSelect'] extends true ? GetQueryResult<ReturnType, Spread<[T['result'], Q['result']]>> : GetQueryResult<ReturnType, Q['result']>> = Omit<T, 'result' | 'returnType' | 'then' | 'catch' | 'selectable' | 'windows' | 'withData'> & {
1440
- meta: Q['meta'];
1441
- result: T['meta']['hasSelect'] extends true ? Spread<[T['result'], Q['result']]> : Q['result'];
1442
- returnType: ReturnType;
1443
- then: QueryThen<Data>;
1444
- catch: QueryCatch<Data>;
1445
- selectable: T['selectable'] & Q['selectable'];
1446
- windows: T['windows'] & Q['windows'];
1447
- withData: T['withData'] & Q['withData'];
1439
+ type MergeQuery<T extends Query, Q extends Query, ReturnType extends QueryReturnType = QueryReturnType extends Q['returnType'] ? T['returnType'] : Q['returnType'], Result extends ColumnsShapeBase = T['meta']['hasSelect'] extends true ? Q['meta']['hasSelect'] extends true ? {
1440
+ [K in keyof T['result'] | keyof Q['result']]: K extends keyof Q['result'] ? Q['result'][K] : T['result'][K];
1441
+ } : T['result'] : Q['result'], Data = GetQueryResult<ReturnType, Result>> = {
1442
+ [K in keyof T]: K extends 'meta' ? MergeObjects<T['meta'], Q['meta']> : K extends 'result' ? Result : K extends 'returnType' ? ReturnType : K extends 'then' ? QueryThen<Data> : K extends 'catch' ? QueryCatch<Data> : K extends 'selectable' ? T['selectable'] & Q['selectable'] : K extends 'windows' ? MergeObjects<T['windows'], Q['windows']> : K extends 'withData' ? MergeObjects<T['withData'], Q['withData']> : T[K];
1448
1443
  };
1449
1444
  declare class MergeQueryMethods {
1450
1445
  merge<T extends Query, Q extends Query>(this: T, q: Q): MergeQuery<T, Q>;
@@ -1510,6 +1505,9 @@ type OrderArg<T extends Query, Key extends PropertyKey = keyof T['selectable'] |
1510
1505
  } | RawExpression;
1511
1506
  type OrderArgs<T extends Query> = OrderArg<T>[] | [TemplateStringsArray, ...unknown[]];
1512
1507
  type FindArgs<T extends Query> = [T['shape'][T['singlePrimaryKey']]['type'] | RawExpression] | [TemplateStringsArray, ...unknown[]];
1508
+ type QueryHelper<T extends Query, Args extends unknown[], Result> = <Q extends {
1509
+ [K in keyof T]: K extends 'then' ? QueryThen<unknown> : K extends 'result' ? ColumnsShapeBase : T[K];
1510
+ }>(q: Q, ...args: Args) => Result extends Query ? MergeQuery<Q, Result> : Result;
1513
1511
  interface QueryMethods extends Omit<AsMethods, 'result'>, Aggregate, Select, From, Join, With, Union, Json, Create, Update, Delete, Transaction, For, ColumnInfoMethods, Omit<Where, 'result'>, Clear, Having, Window, Then, QueryLog, QueryHooks, QueryUpsertOrCreate, QueryGet, MergeQueryMethods, RawSqlMethods, CopyMethods {
1514
1512
  }
1515
1513
  declare class QueryMethods {
@@ -1562,6 +1560,44 @@ declare class QueryMethods {
1562
1560
  restartIdentity?: boolean;
1563
1561
  cascade?: boolean;
1564
1562
  }): SetQueryReturnsVoid<T>;
1563
+ /**
1564
+ * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
1565
+ *
1566
+ * ```ts
1567
+ * const defaultAuthorSelect = db.author.makeHelper((q) => {
1568
+ * return q.select('firstName', 'lastName');
1569
+ * });
1570
+ *
1571
+ * // this will select id, firstName, lastName with a correct TS type
1572
+ * // and return a single record
1573
+ * const result = await defaultAuthorSelect(db.author.select('id').find(1));
1574
+ * ```
1575
+ *
1576
+ * Such helper is available for relation queries inside `select`:
1577
+ *
1578
+ * ```ts
1579
+ * await db.book.select({
1580
+ * author: (book) => defaultAuthorSelect(book.author),
1581
+ * });
1582
+ * ```
1583
+ *
1584
+ * Helper can accept additional arguments:
1585
+ *
1586
+ * ```ts
1587
+ * const selectFollowing = db.user.makeHelper((q, currentUser: { id: number }) => {
1588
+ * return q.select({
1589
+ * following: (q) =>
1590
+ * q.followers.where({ followerId: currentUser.id }).exists(),
1591
+ * });
1592
+ * });
1593
+ *
1594
+ * // select some columns and the `following` boolean field from users
1595
+ * await selectFollowing(db.user.select('id', 'name'), currentUser);
1596
+ * ```
1597
+ *
1598
+ * @param fn - helper function
1599
+ */
1600
+ makeHelper<T extends Query, Args extends unknown[], Result>(this: T, fn: (q: T, ...args: Args) => Result): QueryHelper<T, Args, Result>;
1565
1601
  }
1566
1602
 
1567
1603
  type AggregateArg<T extends Query> = Expression<T> | Record<string, Expression<T>> | [Expression<T>, string];
package/dist/index.js CHANGED
@@ -3658,11 +3658,22 @@ class Adapter {
3658
3658
  }
3659
3659
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
3660
3660
  query(query, types2) {
3661
- return performQuery(this.pool, query, types2, this.schema);
3661
+ return performQuery(
3662
+ this.pool,
3663
+ query,
3664
+ types2,
3665
+ this.schema
3666
+ );
3662
3667
  }
3663
3668
  // eslint-disable-next-line @typescript-eslint/no-explicit-any
3664
3669
  arrays(query, types2) {
3665
- return performQuery(this.pool, query, types2, this.schema, "array");
3670
+ return performQuery(
3671
+ this.pool,
3672
+ query,
3673
+ types2,
3674
+ this.schema,
3675
+ "array"
3676
+ );
3666
3677
  }
3667
3678
  async transaction(begin, cb) {
3668
3679
  const client = await this.pool.connect();
@@ -6582,6 +6593,46 @@ class QueryMethods {
6582
6593
  }
6583
6594
  return this._exec();
6584
6595
  }
6596
+ /**
6597
+ * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
6598
+ *
6599
+ * ```ts
6600
+ * const defaultAuthorSelect = db.author.makeHelper((q) => {
6601
+ * return q.select('firstName', 'lastName');
6602
+ * });
6603
+ *
6604
+ * // this will select id, firstName, lastName with a correct TS type
6605
+ * // and return a single record
6606
+ * const result = await defaultAuthorSelect(db.author.select('id').find(1));
6607
+ * ```
6608
+ *
6609
+ * Such helper is available for relation queries inside `select`:
6610
+ *
6611
+ * ```ts
6612
+ * await db.book.select({
6613
+ * author: (book) => defaultAuthorSelect(book.author),
6614
+ * });
6615
+ * ```
6616
+ *
6617
+ * Helper can accept additional arguments:
6618
+ *
6619
+ * ```ts
6620
+ * const selectFollowing = db.user.makeHelper((q, currentUser: { id: number }) => {
6621
+ * return q.select({
6622
+ * following: (q) =>
6623
+ * q.followers.where({ followerId: currentUser.id }).exists(),
6624
+ * });
6625
+ * });
6626
+ *
6627
+ * // select some columns and the `following` boolean field from users
6628
+ * await selectFollowing(db.user.select('id', 'name'), currentUser);
6629
+ * ```
6630
+ *
6631
+ * @param fn - helper function
6632
+ */
6633
+ makeHelper(fn) {
6634
+ return fn;
6635
+ }
6585
6636
  }
6586
6637
  orchidCore.applyMixins(QueryMethods, [
6587
6638
  QueryBase,