pqb 0.56.13 → 0.57.1

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.mjs CHANGED
@@ -12837,6 +12837,8 @@ class QueryMethods {
12837
12837
  /**
12838
12838
  * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
12839
12839
  *
12840
+ * The idea is similar to {@link modify}, the difference is that `modify` is per query, and `makeHelper` can be reused.
12841
+ *
12840
12842
  * ```ts
12841
12843
  * const defaultAuthorSelect = db.author.makeHelper((q) => {
12842
12844
  * return q.select('firstName', 'lastName');
@@ -12897,7 +12899,7 @@ class QueryMethods {
12897
12899
  };
12898
12900
  }
12899
12901
  /**
12900
- * `modify` allows modifying the query with helpers defined with {@link makeHelper}:
12902
+ * `useHelper` allows to use {@link makeHelper} in different queries:
12901
12903
  *
12902
12904
  * ```ts
12903
12905
  * const helper = db.table.makeHelper((q) => {
@@ -12905,9 +12907,9 @@ class QueryMethods {
12905
12907
  * return q.select('name').where({ active: true }).order({ createdAt: 'DESC' });
12906
12908
  * });
12907
12909
  *
12908
- * const record = await db.table.select('id').modify(helper).find(1);
12910
+ * const record = await db.table.select('id').useHelper(helper).find(1);
12909
12911
  *
12910
- * record.id; // id was selected before `modify`
12912
+ * record.id; // id was selected before `useHelper`
12911
12913
  * record.name; // name was selected by the function
12912
12914
  * ```
12913
12915
  *
@@ -12915,7 +12917,7 @@ class QueryMethods {
12915
12917
  * Use this sparingly as it complicates dealing with the result.
12916
12918
  *
12917
12919
  * ```ts
12918
- * const helper = db.table((q) => {
12920
+ * const helper = db.table.makeHelper((q) => {
12919
12921
  * if (Math.random() > 0.5) {
12920
12922
  * return q.select('one');
12921
12923
  * } else {
@@ -12923,7 +12925,7 @@ class QueryMethods {
12923
12925
  * }
12924
12926
  * });
12925
12927
  *
12926
- * const record = await db.table.modify(helper).find(1);
12928
+ * const record = await db.table.useHelper(helper).find(1);
12927
12929
  *
12928
12930
  * // TS error: we don't know for sure if the `one` was selected.
12929
12931
  * record.one;
@@ -12943,18 +12945,42 @@ class QueryMethods {
12943
12945
  * return q.select(select);
12944
12946
  * });
12945
12947
  *
12946
- * const record = await db.table.modify(helper, 'id').find(1);
12948
+ * const record = await db.table.useHelper(helper, 'id').find(1);
12947
12949
  * // record has type { id: number } | { name: string }
12948
12950
  * if ('id' in record) {
12949
12951
  * record.id;
12950
12952
  * }
12951
12953
  * ```
12952
12954
  *
12953
- * @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.
12955
+ * @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.
12954
12956
  */
12955
- modify(fn, ...args) {
12957
+ useHelper(fn, ...args) {
12956
12958
  return fn(this, ...args);
12957
12959
  }
12960
+ /**
12961
+ * `modify` is useful when you'd like to modify the query based on some condition.
12962
+ *
12963
+ * ```ts
12964
+ * // parameters coming from outside
12965
+ * const selectOneOrAnother = true;
12966
+ * const filterBySomething = true;
12967
+ *
12968
+ * type ResultType =
12969
+ * | { id: number; one: string }[]
12970
+ * | { id: number; another: string }[];
12971
+ * const result = await db.table
12972
+ * .select('id')
12973
+ * // conditional select results in a union type
12974
+ * .modify((q) => (includeName ? q.select('one') : q.select('another')))
12975
+ * // can use any query methods in modify
12976
+ * .modify((q) => (filterBySomething ? q.where({ something: true }) : q));
12977
+ * ```
12978
+ *
12979
+ * @param fn - accepts the current query as a parameters. Anything returned by the function will be the return type of the query.
12980
+ */
12981
+ modify(fn) {
12982
+ return fn(this);
12983
+ }
12958
12984
  /**
12959
12985
  * Narrows a part of the query output type.
12960
12986
  * Use with caution, type-safety isn't guaranteed with it.