pqb 0.45.5 → 0.46.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
@@ -1946,13 +1946,39 @@ const _queryAll = (q) => {
1946
1946
  q.q.all = true;
1947
1947
  return q;
1948
1948
  };
1949
- const _queryTake = (q) => {
1950
- q.q.returnType = "oneOrThrow";
1951
- return q;
1949
+ const _queryTake = (query) => {
1950
+ const q = query.q;
1951
+ switch (q.returnType) {
1952
+ case "valueOrThrow":
1953
+ case "pluck":
1954
+ case "void":
1955
+ break;
1956
+ case "value": {
1957
+ q.returnType = "valueOrThrow";
1958
+ break;
1959
+ }
1960
+ default: {
1961
+ q.returnType = "oneOrThrow";
1962
+ }
1963
+ }
1964
+ return query;
1952
1965
  };
1953
- const _queryTakeOptional = (q) => {
1954
- q.q.returnType = "one";
1955
- return q;
1966
+ const _queryTakeOptional = (query) => {
1967
+ const q = query.q;
1968
+ switch (q.returnType) {
1969
+ case "value":
1970
+ case "pluck":
1971
+ case "void":
1972
+ break;
1973
+ case "valueOrThrow": {
1974
+ q.returnType = "value";
1975
+ break;
1976
+ }
1977
+ default: {
1978
+ q.returnType = "one";
1979
+ }
1980
+ }
1981
+ return query;
1956
1982
  };
1957
1983
  const _queryExec = (q) => {
1958
1984
  q.q.returnType = "void";
@@ -2136,16 +2162,27 @@ const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
2136
2162
  }
2137
2163
  };
2138
2164
 
2165
+ const noneResult = (q, queryData, type) => {
2166
+ if (!type || type === "all" || type === "rows" || type === "pluck") {
2167
+ return [];
2168
+ } else if (type === "one" || type === "value" || type === "void") {
2169
+ return queryData.notFoundDefault;
2170
+ } else if (type === "valueOrThrow" && queryData.returning) {
2171
+ return 0;
2172
+ } else {
2173
+ throw new NotFoundError(q);
2174
+ }
2175
+ };
2139
2176
  const noneMethods = {
2140
- // `then` resolves or rejects based on return type of the query.
2177
+ // `then` resolves or rejects based on a return type of the query.
2141
2178
  // It is `async` so it returns a chainable Promise.
2142
2179
  async then(resolve, reject) {
2143
- const type = this.q.returnType;
2144
- if (!type || type === "all" || type === "rows" || type === "pluck")
2145
- resolve?.([]);
2146
- else if (type === "one" || type === "value" || type === "void") resolve?.();
2147
- else if (type === "valueOrThrow" && this.q.returning) resolve?.(0);
2148
- else reject?.(new NotFoundError(this));
2180
+ try {
2181
+ const result = noneResult(this, this.q, this.q.returnType);
2182
+ resolve?.(result);
2183
+ } catch (err) {
2184
+ reject?.(err);
2185
+ }
2149
2186
  },
2150
2187
  // `catch` returns a Promise, so it is chainable with then/catch.
2151
2188
  catch: () => new Promise(noop)
@@ -2154,6 +2191,11 @@ const _queryNone = (q) => {
2154
2191
  if (isQueryNone(q)) return q;
2155
2192
  q = extendQuery(q, noneMethods);
2156
2193
  pushQueryValueImmutable(q, "and", new RawSQL("false"));
2194
+ pushQueryValueImmutable(
2195
+ q,
2196
+ "transform",
2197
+ (_, queryData) => noneResult(q, queryData, queryData.returnType)
2198
+ );
2157
2199
  return q;
2158
2200
  };
2159
2201
  const isQueryNone = (q) => q.then === noneMethods.then;
@@ -2211,7 +2253,9 @@ const _join = (query, require, type, first, args) => {
2211
2253
  args,
2212
2254
  joinSubQuery
2213
2255
  );
2214
- if (joinKey && "s" in joinArgs && joinArgs.s) {
2256
+ if (require && "r" in joinArgs && isQueryNone(joinArgs.r)) {
2257
+ return _queryNone(query);
2258
+ } else if (joinKey && "s" in joinArgs && joinArgs.s) {
2215
2259
  const j = "j" in joinArgs ? joinArgs.r ?? joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
2216
2260
  const jq = j.q;
2217
2261
  if (jq.select || !jq.selectAllColumns) {
@@ -2238,8 +2282,6 @@ const _join = (query, require, type, first, args) => {
2238
2282
  computeds
2239
2283
  );
2240
2284
  }
2241
- } else if (require && "r" in joinArgs && isQueryNone(joinArgs.r)) {
2242
- return _queryNone(query);
2243
2285
  } else {
2244
2286
  addAllShapesAndParsers(
2245
2287
  query,
@@ -2939,12 +2981,13 @@ const runAfterCommit = async (afterCommit, result) => {
2939
2981
  }
2940
2982
  };
2941
2983
 
2942
- const applyBatchTransforms = (query, batches) => {
2943
- if (query.transform) {
2984
+ const applyBatchTransforms = (q, batches) => {
2985
+ if (q.transform) {
2944
2986
  for (const item of batches) {
2945
2987
  item.parent[item.key] = applyTransforms(
2946
- query.returnType,
2947
- query.transform,
2988
+ q,
2989
+ q.returnType,
2990
+ q.transform,
2948
2991
  item.data
2949
2992
  );
2950
2993
  }
@@ -3387,7 +3430,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3387
3430
  );
3388
3431
  }
3389
3432
  if (query.transform) {
3390
- result = applyTransforms(returnType, query.transform, result);
3433
+ result = applyTransforms(query, returnType, query.transform, result);
3391
3434
  }
3392
3435
  return resolve?.(result);
3393
3436
  } catch (err) {
@@ -5944,7 +5987,8 @@ const skipQueryKeysForSubQuery = {
5944
5987
  returnsOne: true,
5945
5988
  aliases: true,
5946
5989
  sqlCache: true,
5947
- defaults: true
5990
+ defaults: true,
5991
+ transform: true
5948
5992
  };
5949
5993
  const getIsJoinSubQuery = (query) => {
5950
5994
  const {
@@ -11716,25 +11760,31 @@ class QueryMethods {
11716
11760
  return _queryAll(_clone(this));
11717
11761
  }
11718
11762
  /**
11719
- * Takes a single record, adds `LIMIT 1`.
11720
- * Throws when not found.
11763
+ * Use `take` to "take" a single record. It adds `LIMIT 1`, throws a `NotFoundError` when not found.
11721
11764
  *
11722
11765
  * ```ts
11723
- * const result: TableType = await db.table.where({ key: 'value' }).take();
11766
+ * const taken: TableType = await db.table.where({ key: 'value' }).take();
11724
11767
  * ```
11768
+ *
11769
+ * Makes no effect if the query previously has `get`, `pluck`, `exec`.
11770
+ *
11771
+ * Changes `getOptional` to `get`.
11725
11772
  */
11726
11773
  take() {
11727
11774
  return _queryTake(_clone(this));
11728
11775
  }
11729
11776
  /**
11730
- * Takes a single record, adds `LIMIT 1`.
11731
- * Returns `undefined` when not found.
11777
+ * Use `takeOptional` to "take" a single record. It adds `LIMIT 1`, returns `undefined` when not found.
11732
11778
  *
11733
11779
  * ```ts
11734
- * const result: TableType | undefined = await db.table
11780
+ * const takenOptional: TableType | undefined = await db.table
11735
11781
  * .where({ key: 'value' })
11736
11782
  * .takeOptional();
11737
11783
  * ```
11784
+ *
11785
+ * Makes no effect if the query previously has `getOptional`, `pluck`, `exec`.
11786
+ *
11787
+ * Changes `get` to `getOptional`.
11738
11788
  */
11739
11789
  takeOptional() {
11740
11790
  return _queryTakeOptional(_clone(this));