pqb 0.56.13 → 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
@@ -12839,6 +12839,8 @@ class QueryMethods {
12839
12839
  /**
12840
12840
  * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
12841
12841
  *
12842
+ * The idea is similar to {@link modify}, the difference is that `modify` is per query, and `makeHelper` can be reused.
12843
+ *
12842
12844
  * ```ts
12843
12845
  * const defaultAuthorSelect = db.author.makeHelper((q) => {
12844
12846
  * return q.select('firstName', 'lastName');
@@ -12899,7 +12901,7 @@ class QueryMethods {
12899
12901
  };
12900
12902
  }
12901
12903
  /**
12902
- * `modify` allows modifying the query with helpers defined with {@link makeHelper}:
12904
+ * `useHelper` allows to use {@link makeHelper} in different queries:
12903
12905
  *
12904
12906
  * ```ts
12905
12907
  * const helper = db.table.makeHelper((q) => {
@@ -12907,9 +12909,9 @@ class QueryMethods {
12907
12909
  * return q.select('name').where({ active: true }).order({ createdAt: 'DESC' });
12908
12910
  * });
12909
12911
  *
12910
- * const record = await db.table.select('id').modify(helper).find(1);
12912
+ * const record = await db.table.select('id').useHelper(helper).find(1);
12911
12913
  *
12912
- * record.id; // id was selected before `modify`
12914
+ * record.id; // id was selected before `useHelper`
12913
12915
  * record.name; // name was selected by the function
12914
12916
  * ```
12915
12917
  *
@@ -12917,7 +12919,7 @@ class QueryMethods {
12917
12919
  * Use this sparingly as it complicates dealing with the result.
12918
12920
  *
12919
12921
  * ```ts
12920
- * const helper = db.table((q) => {
12922
+ * const helper = db.table.makeHelper((q) => {
12921
12923
  * if (Math.random() > 0.5) {
12922
12924
  * return q.select('one');
12923
12925
  * } else {
@@ -12925,7 +12927,7 @@ class QueryMethods {
12925
12927
  * }
12926
12928
  * });
12927
12929
  *
12928
- * const record = await db.table.modify(helper).find(1);
12930
+ * const record = await db.table.useHelper(helper).find(1);
12929
12931
  *
12930
12932
  * // TS error: we don't know for sure if the `one` was selected.
12931
12933
  * record.one;
@@ -12945,18 +12947,42 @@ class QueryMethods {
12945
12947
  * return q.select(select);
12946
12948
  * });
12947
12949
  *
12948
- * const record = await db.table.modify(helper, 'id').find(1);
12950
+ * const record = await db.table.useHelper(helper, 'id').find(1);
12949
12951
  * // record has type { id: number } | { name: string }
12950
12952
  * if ('id' in record) {
12951
12953
  * record.id;
12952
12954
  * }
12953
12955
  * ```
12954
12956
  *
12955
- * @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.
12956
12958
  */
12957
- modify(fn, ...args) {
12959
+ useHelper(fn, ...args) {
12958
12960
  return fn(this, ...args);
12959
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
+ }
12960
12986
  /**
12961
12987
  * Narrows a part of the query output type.
12962
12988
  * Use with caution, type-safety isn't guaranteed with it.