pqb 0.56.12 → 0.57.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.d.ts CHANGED
@@ -1808,9 +1808,9 @@ type WhereArg<T extends PickQueryMetaRelations> = {
1808
1808
  * ```
1809
1809
  */
1810
1810
  type WhereQueryBuilder<T extends PickQueryRelations> = EmptyObject extends T['relations'] ? {
1811
- [K in keyof T]: K extends keyof Where | keyof ExpressionMethods | 'table' | 'get' | 'columnTypes' | 'meta' | 'relations' | 'modify' | 'result' | 'returnType' | 'withData' | 'windows' | 'then' ? T[K] : never;
1811
+ [K in keyof T]: K extends keyof Where | keyof ExpressionMethods | 'table' | 'get' | 'columnTypes' | 'meta' | 'relations' | 'useHelper' | 'modify' | 'result' | 'returnType' | 'withData' | 'windows' | 'then' ? T[K] : never;
1812
1812
  } : {
1813
- [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['query'] : K extends keyof T & (keyof Where | keyof ExpressionMethods | 'table' | 'get' | 'columnTypes' | 'meta' | 'relations' | 'modify' | 'result' | 'returnType' | 'withData' | 'windows' | 'then') ? T[K] : never;
1813
+ [K in keyof T['relations'] | keyof T]: K extends keyof T['relations'] ? T['relations'][K]['query'] : K extends keyof T & (keyof Where | keyof ExpressionMethods | 'table' | 'get' | 'columnTypes' | 'meta' | 'relations' | 'useHelper' | 'modify' | 'result' | 'returnType' | 'withData' | 'windows' | 'then') ? T[K] : never;
1814
1814
  };
1815
1815
  type WhereArgs<T extends PickQueryMetaRelations> = WhereArg<T>[];
1816
1816
  type WhereNotArgs<T extends PickQueryMetaRelations> = [WhereArg<T>];
@@ -7820,6 +7820,8 @@ declare class QueryMethods<ColumnTypes> {
7820
7820
  /**
7821
7821
  * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
7822
7822
  *
7823
+ * The idea is similar to {@link modify}, the difference is that `modify` is per query, and `makeHelper` can be reused.
7824
+ *
7823
7825
  * ```ts
7824
7826
  * const defaultAuthorSelect = db.author.makeHelper((q) => {
7825
7827
  * return q.select('firstName', 'lastName');
@@ -7870,7 +7872,7 @@ declare class QueryMethods<ColumnTypes> {
7870
7872
  */
7871
7873
  makeHelper<T extends PickQueryMetaShape, Args extends unknown[], Result>(this: T, fn: (q: T, ...args: Args) => Result): QueryHelper<T, Args, Result>;
7872
7874
  /**
7873
- * `modify` allows modifying the query with helpers defined with {@link makeHelper}:
7875
+ * `useHelper` allows to use {@link makeHelper} in different queries:
7874
7876
  *
7875
7877
  * ```ts
7876
7878
  * const helper = db.table.makeHelper((q) => {
@@ -7878,9 +7880,9 @@ declare class QueryMethods<ColumnTypes> {
7878
7880
  * return q.select('name').where({ active: true }).order({ createdAt: 'DESC' });
7879
7881
  * });
7880
7882
  *
7881
- * const record = await db.table.select('id').modify(helper).find(1);
7883
+ * const record = await db.table.select('id').useHelper(helper).find(1);
7882
7884
  *
7883
- * record.id; // id was selected before `modify`
7885
+ * record.id; // id was selected before `useHelper`
7884
7886
  * record.name; // name was selected by the function
7885
7887
  * ```
7886
7888
  *
@@ -7888,7 +7890,7 @@ declare class QueryMethods<ColumnTypes> {
7888
7890
  * Use this sparingly as it complicates dealing with the result.
7889
7891
  *
7890
7892
  * ```ts
7891
- * const helper = db.table((q) => {
7893
+ * const helper = db.table.makeHelper((q) => {
7892
7894
  * if (Math.random() > 0.5) {
7893
7895
  * return q.select('one');
7894
7896
  * } else {
@@ -7896,7 +7898,7 @@ declare class QueryMethods<ColumnTypes> {
7896
7898
  * }
7897
7899
  * });
7898
7900
  *
7899
- * const record = await db.table.modify(helper).find(1);
7901
+ * const record = await db.table.useHelper(helper).find(1);
7900
7902
  *
7901
7903
  * // TS error: we don't know for sure if the `one` was selected.
7902
7904
  * record.one;
@@ -7916,16 +7918,38 @@ declare class QueryMethods<ColumnTypes> {
7916
7918
  * return q.select(select);
7917
7919
  * });
7918
7920
  *
7919
- * const record = await db.table.modify(helper, 'id').find(1);
7921
+ * const record = await db.table.useHelper(helper, 'id').find(1);
7920
7922
  * // record has type { id: number } | { name: string }
7921
7923
  * if ('id' in record) {
7922
7924
  * record.id;
7923
7925
  * }
7924
7926
  * ```
7925
7927
  *
7926
- * @param fn - function to modify the query with. The result type will be merged with the main query as if the `merge` method was used.
7928
+ * @param fn - function to useHelper the query with. The result type will be merged with the main query as if the `merge` method was used.
7929
+ */
7930
+ useHelper<T extends PickQueryTableMetaResultReturnTypeWithDataWindowsThen, Fn extends IsQueryHelperForTable<T['table']>>(this: T, fn: Fn, ...args: Fn['args']): Fn['result'] extends PickQueryMetaResultReturnTypeWithDataWindowsThen ? MergeQuery<T, Fn['result']> : Fn['result'];
7931
+ /**
7932
+ * `modify` is useful when you'd like to modify the query based on some condition.
7933
+ *
7934
+ * ```ts
7935
+ * // parameters coming from outside
7936
+ * const selectOneOrAnother = true;
7937
+ * const filterBySomething = true;
7938
+ *
7939
+ * type ResultType =
7940
+ * | { id: number; one: string }[]
7941
+ * | { id: number; another: string }[];
7942
+ * const result = await db.table
7943
+ * .select('id')
7944
+ * // conditional select results in a union type
7945
+ * .modify((q) => (includeName ? q.select('one') : q.select('another')))
7946
+ * // can use any query methods in modify
7947
+ * .modify((q) => (filterBySomething ? q.where({ something: true }) : q));
7948
+ * ```
7949
+ *
7950
+ * @param fn - accepts the current query as a parameters. Anything returned by the function will be the return type of the query.
7927
7951
  */
7928
- modify<T extends PickQueryTableMetaResultReturnTypeWithDataWindowsThen, Fn extends IsQueryHelperForTable<T['table']>>(this: T, fn: Fn, ...args: Fn['args']): Fn['result'] extends PickQueryMetaResultReturnTypeWithDataWindowsThen ? MergeQuery<T, Fn['result']> : Fn['result'];
7952
+ modify<T, R>(this: T, fn: (q: T) => R): R;
7929
7953
  /**
7930
7954
  * Narrows a part of the query output type.
7931
7955
  * Use with caution, type-safety isn't guaranteed with it.
package/dist/index.js CHANGED
@@ -7093,7 +7093,8 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7093
7093
  }
7094
7094
  }
7095
7095
  } else {
7096
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
7096
+ const sql = item.toSQL(ctx, quotedAs);
7097
+ list.push(ctx.aliasValue ? `${sql} ${quotedAs}` : sql);
7097
7098
  aliases?.push("");
7098
7099
  }
7099
7100
  }
@@ -7151,10 +7152,6 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7151
7152
  }
7152
7153
  return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs, jsonList);
7153
7154
  };
7154
- function selectedObjectToSQL(ctx, quotedAs, item) {
7155
- const sql = item.toSQL(ctx, quotedAs);
7156
- return ctx.aliasValue ? `${sql} "${quotedAs}"` : sql;
7157
- }
7158
7155
  const selectAllSql = (query, quotedAs, jsonList) => {
7159
7156
  if (jsonList) {
7160
7157
  Object.assign(jsonList, query.selectAllShape);
@@ -12842,6 +12839,8 @@ class QueryMethods {
12842
12839
  /**
12843
12840
  * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
12844
12841
  *
12842
+ * The idea is similar to {@link modify}, the difference is that `modify` is per query, and `makeHelper` can be reused.
12843
+ *
12845
12844
  * ```ts
12846
12845
  * const defaultAuthorSelect = db.author.makeHelper((q) => {
12847
12846
  * return q.select('firstName', 'lastName');
@@ -12902,7 +12901,7 @@ class QueryMethods {
12902
12901
  };
12903
12902
  }
12904
12903
  /**
12905
- * `modify` allows modifying the query with helpers defined with {@link makeHelper}:
12904
+ * `useHelper` allows to use {@link makeHelper} in different queries:
12906
12905
  *
12907
12906
  * ```ts
12908
12907
  * const helper = db.table.makeHelper((q) => {
@@ -12910,9 +12909,9 @@ class QueryMethods {
12910
12909
  * return q.select('name').where({ active: true }).order({ createdAt: 'DESC' });
12911
12910
  * });
12912
12911
  *
12913
- * const record = await db.table.select('id').modify(helper).find(1);
12912
+ * const record = await db.table.select('id').useHelper(helper).find(1);
12914
12913
  *
12915
- * record.id; // id was selected before `modify`
12914
+ * record.id; // id was selected before `useHelper`
12916
12915
  * record.name; // name was selected by the function
12917
12916
  * ```
12918
12917
  *
@@ -12920,7 +12919,7 @@ class QueryMethods {
12920
12919
  * Use this sparingly as it complicates dealing with the result.
12921
12920
  *
12922
12921
  * ```ts
12923
- * const helper = db.table((q) => {
12922
+ * const helper = db.table.makeHelper((q) => {
12924
12923
  * if (Math.random() > 0.5) {
12925
12924
  * return q.select('one');
12926
12925
  * } else {
@@ -12928,7 +12927,7 @@ class QueryMethods {
12928
12927
  * }
12929
12928
  * });
12930
12929
  *
12931
- * const record = await db.table.modify(helper).find(1);
12930
+ * const record = await db.table.useHelper(helper).find(1);
12932
12931
  *
12933
12932
  * // TS error: we don't know for sure if the `one` was selected.
12934
12933
  * record.one;
@@ -12948,18 +12947,42 @@ class QueryMethods {
12948
12947
  * return q.select(select);
12949
12948
  * });
12950
12949
  *
12951
- * const record = await db.table.modify(helper, 'id').find(1);
12950
+ * const record = await db.table.useHelper(helper, 'id').find(1);
12952
12951
  * // record has type { id: number } | { name: string }
12953
12952
  * if ('id' in record) {
12954
12953
  * record.id;
12955
12954
  * }
12956
12955
  * ```
12957
12956
  *
12958
- * @param fn - function to modify the query with. The result type will be merged with the main query as if the `merge` method was used.
12957
+ * @param fn - function to useHelper the query with. The result type will be merged with the main query as if the `merge` method was used.
12959
12958
  */
12960
- modify(fn, ...args) {
12959
+ useHelper(fn, ...args) {
12961
12960
  return fn(this, ...args);
12962
12961
  }
12962
+ /**
12963
+ * `modify` is useful when you'd like to modify the query based on some condition.
12964
+ *
12965
+ * ```ts
12966
+ * // parameters coming from outside
12967
+ * const selectOneOrAnother = true;
12968
+ * const filterBySomething = true;
12969
+ *
12970
+ * type ResultType =
12971
+ * | { id: number; one: string }[]
12972
+ * | { id: number; another: string }[];
12973
+ * const result = await db.table
12974
+ * .select('id')
12975
+ * // conditional select results in a union type
12976
+ * .modify((q) => (includeName ? q.select('one') : q.select('another')))
12977
+ * // can use any query methods in modify
12978
+ * .modify((q) => (filterBySomething ? q.where({ something: true }) : q));
12979
+ * ```
12980
+ *
12981
+ * @param fn - accepts the current query as a parameters. Anything returned by the function will be the return type of the query.
12982
+ */
12983
+ modify(fn) {
12984
+ return fn(this);
12985
+ }
12963
12986
  /**
12964
12987
  * Narrows a part of the query output type.
12965
12988
  * Use with caution, type-safety isn't guaranteed with it.