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.mjs CHANGED
@@ -7091,7 +7091,8 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7091
7091
  }
7092
7092
  }
7093
7093
  } else {
7094
- list.push(selectedObjectToSQL(ctx, quotedAs, item));
7094
+ const sql = item.toSQL(ctx, quotedAs);
7095
+ list.push(ctx.aliasValue ? `${sql} ${quotedAs}` : sql);
7095
7096
  aliases?.push("");
7096
7097
  }
7097
7098
  }
@@ -7149,10 +7150,6 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect,
7149
7150
  }
7150
7151
  return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs, jsonList);
7151
7152
  };
7152
- function selectedObjectToSQL(ctx, quotedAs, item) {
7153
- const sql = item.toSQL(ctx, quotedAs);
7154
- return ctx.aliasValue ? `${sql} "${quotedAs}"` : sql;
7155
- }
7156
7153
  const selectAllSql = (query, quotedAs, jsonList) => {
7157
7154
  if (jsonList) {
7158
7155
  Object.assign(jsonList, query.selectAllShape);
@@ -12840,6 +12837,8 @@ class QueryMethods {
12840
12837
  /**
12841
12838
  * Use `makeHelper` to make a query helper - a function where you can modify the query, and reuse this function across different places.
12842
12839
  *
12840
+ * The idea is similar to {@link modify}, the difference is that `modify` is per query, and `makeHelper` can be reused.
12841
+ *
12843
12842
  * ```ts
12844
12843
  * const defaultAuthorSelect = db.author.makeHelper((q) => {
12845
12844
  * return q.select('firstName', 'lastName');
@@ -12900,7 +12899,7 @@ class QueryMethods {
12900
12899
  };
12901
12900
  }
12902
12901
  /**
12903
- * `modify` allows modifying the query with helpers defined with {@link makeHelper}:
12902
+ * `useHelper` allows to use {@link makeHelper} in different queries:
12904
12903
  *
12905
12904
  * ```ts
12906
12905
  * const helper = db.table.makeHelper((q) => {
@@ -12908,9 +12907,9 @@ class QueryMethods {
12908
12907
  * return q.select('name').where({ active: true }).order({ createdAt: 'DESC' });
12909
12908
  * });
12910
12909
  *
12911
- * const record = await db.table.select('id').modify(helper).find(1);
12910
+ * const record = await db.table.select('id').useHelper(helper).find(1);
12912
12911
  *
12913
- * record.id; // id was selected before `modify`
12912
+ * record.id; // id was selected before `useHelper`
12914
12913
  * record.name; // name was selected by the function
12915
12914
  * ```
12916
12915
  *
@@ -12918,7 +12917,7 @@ class QueryMethods {
12918
12917
  * Use this sparingly as it complicates dealing with the result.
12919
12918
  *
12920
12919
  * ```ts
12921
- * const helper = db.table((q) => {
12920
+ * const helper = db.table.makeHelper((q) => {
12922
12921
  * if (Math.random() > 0.5) {
12923
12922
  * return q.select('one');
12924
12923
  * } else {
@@ -12926,7 +12925,7 @@ class QueryMethods {
12926
12925
  * }
12927
12926
  * });
12928
12927
  *
12929
- * const record = await db.table.modify(helper).find(1);
12928
+ * const record = await db.table.useHelper(helper).find(1);
12930
12929
  *
12931
12930
  * // TS error: we don't know for sure if the `one` was selected.
12932
12931
  * record.one;
@@ -12946,18 +12945,42 @@ class QueryMethods {
12946
12945
  * return q.select(select);
12947
12946
  * });
12948
12947
  *
12949
- * const record = await db.table.modify(helper, 'id').find(1);
12948
+ * const record = await db.table.useHelper(helper, 'id').find(1);
12950
12949
  * // record has type { id: number } | { name: string }
12951
12950
  * if ('id' in record) {
12952
12951
  * record.id;
12953
12952
  * }
12954
12953
  * ```
12955
12954
  *
12956
- * @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.
12957
12956
  */
12958
- modify(fn, ...args) {
12957
+ useHelper(fn, ...args) {
12959
12958
  return fn(this, ...args);
12960
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
+ }
12961
12984
  /**
12962
12985
  * Narrows a part of the query output type.
12963
12986
  * Use with caution, type-safety isn't guaranteed with it.