pqb 0.33.0 → 0.33.2

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
@@ -1,4 +1,4 @@
1
- import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, quoteObjectKey, toArray, singleQuote, addCode, singleQuoteArray, objectHasValues, columnDefaultArgumentToCode, columnErrorMessagesToCode, getValueKey, addValue, isExpression, joinTruthy, numberDataToCode, stringDataToCode, getDefaultLanguage, dateDataToCode, pushOrNewArrayToObject, returnArg as returnArg$1, noop, arrayDataToCode, emptyArray, callWithThis, setParserToQuery, applyTransforms, isRawSQL, pushOrNewArray, setDefaultNowFn, setDefaultLanguage, makeTimestampsHelpers, setCurrentColumnName, setAdapterConnectRetry, isObjectEmpty, ValExpression, applyMixins, toSnakeCase, snakeCaseKey } from 'orchid-core';
1
+ import { ExpressionTypeMethod, Expression, RawSQLBase, emptyObject, isTemplateLiteralArgs, ColumnTypeBase, setColumnData, pushColumnData, quoteObjectKey, toArray, singleQuote, addCode, singleQuoteArray, objectHasValues, columnDefaultArgumentToCode, columnErrorMessagesToCode, getValueKey, addValue, isExpression, joinTruthy, numberDataToCode, stringDataToCode, getDefaultLanguage, dateDataToCode, pushOrNewArrayToObject, returnArg as returnArg$1, noop, arrayDataToCode, emptyArray, callWithThis, applyTransforms, setParserToQuery, isRawSQL, pushOrNewArray, setDefaultNowFn, setDefaultLanguage, makeTimestampsHelpers, setCurrentColumnName, setAdapterConnectRetry, isObjectEmpty, ValExpression, applyMixins, toSnakeCase, snakeCaseKey } from 'orchid-core';
2
2
  import pg from 'pg';
3
3
  import { inspect } from 'node:util';
4
4
  import { AsyncLocalStorage } from 'node:async_hooks';
@@ -3363,9 +3363,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3363
3363
  result = filterResult(q, returnType, queryResult, hookSelect, result);
3364
3364
  }
3365
3365
  if (query.transform) {
3366
- for (const fn of query.transform) {
3367
- result = fn(result);
3368
- }
3366
+ result = applyTransforms(returnType, query.transform, result);
3369
3367
  }
3370
3368
  return resolve == null ? void 0 : resolve(result);
3371
3369
  } catch (err) {
@@ -3583,10 +3581,8 @@ const addParserForSelectItem = (q, as, key, arg) => {
3583
3581
  setParserToQuery(q.q, key, (item) => {
3584
3582
  const t = query.returnType || "all";
3585
3583
  subQueryResult.rows = t === "value" || t === "valueOrThrow" ? [[item]] : t === "one" || t === "oneOrThrow" ? [item] : item;
3586
- return applyTransforms(
3587
- query.transform,
3588
- query.handleResult(arg, t, subQueryResult, true)
3589
- );
3584
+ const result = query.handleResult(arg, t, subQueryResult, true);
3585
+ return query.transform ? applyTransforms(t, query.transform, result) : result;
3590
3586
  });
3591
3587
  }
3592
3588
  if (query.returnType === "valueOrThrow" || query.returnType === "oneOrThrow") {
@@ -6711,7 +6707,10 @@ class Create {
6711
6707
  * @param args - object with columns list and raw SQL for values
6712
6708
  */
6713
6709
  createRaw(...args) {
6714
- return _queryCreateRaw(this.clone(), args);
6710
+ return _queryCreateRaw(
6711
+ this.clone(),
6712
+ args
6713
+ );
6715
6714
  }
6716
6715
  /**
6717
6716
  * Works exactly as {@link createRaw}, except that it returns inserted row count by default.
@@ -6719,7 +6718,10 @@ class Create {
6719
6718
  * @param args - object with columns list and raw SQL for values
6720
6719
  */
6721
6720
  insertRaw(...args) {
6722
- return _queryInsertRaw(this.clone(), args);
6721
+ return _queryInsertRaw(
6722
+ this.clone(),
6723
+ args
6724
+ );
6723
6725
  }
6724
6726
  /**
6725
6727
  * `createManyRaw` and `insertManyRaw` are for creating many record with raw SQL expressions.
@@ -6740,7 +6742,10 @@ class Create {
6740
6742
  * @param args - object with columns list and array of raw SQL for values
6741
6743
  */
6742
6744
  createManyRaw(...args) {
6743
- return _queryCreateManyRaw(this.clone(), args);
6745
+ return _queryCreateManyRaw(
6746
+ this.clone(),
6747
+ args
6748
+ );
6744
6749
  }
6745
6750
  /**
6746
6751
  * Works exactly as {@link createManyRaw}, except that it returns inserted row count by default.
@@ -6748,7 +6753,10 @@ class Create {
6748
6753
  * @param args - object with columns list and array of raw SQL for values
6749
6754
  */
6750
6755
  insertManyRaw(...args) {
6751
- return _queryInsertManyRaw(this.clone(), args);
6756
+ return _queryInsertManyRaw(
6757
+ this.clone(),
6758
+ args
6759
+ );
6752
6760
  }
6753
6761
  /**
6754
6762
  * These methods are for creating a single record, for batch creating see {@link createManyFrom}.
@@ -6790,7 +6798,11 @@ class Create {
6790
6798
  * @param data - additionally you can set some columns
6791
6799
  */
6792
6800
  createFrom(query, data) {
6793
- return _queryCreateFrom(this.clone(), query, data);
6801
+ return _queryCreateFrom(
6802
+ this.clone(),
6803
+ query,
6804
+ data
6805
+ );
6794
6806
  }
6795
6807
  /**
6796
6808
  * Works exactly as {@link createFrom}, except that it returns inserted row count by default.
@@ -6799,7 +6811,11 @@ class Create {
6799
6811
  * @param data - additionally you can set some columns
6800
6812
  */
6801
6813
  insertFrom(query, data) {
6802
- return _queryInsertFrom(this.clone(), query, data);
6814
+ return _queryInsertFrom(
6815
+ this.clone(),
6816
+ query,
6817
+ data
6818
+ );
6803
6819
  }
6804
6820
  /**
6805
6821
  * Similar to `createFrom`, but intended to create many records.
@@ -6815,7 +6831,10 @@ class Create {
6815
6831
  * @param query - query to create new records from
6816
6832
  */
6817
6833
  createManyFrom(query) {
6818
- return _queryCreateManyFrom(this.clone(), query);
6834
+ return _queryCreateManyFrom(
6835
+ this.clone(),
6836
+ query
6837
+ );
6819
6838
  }
6820
6839
  /**
6821
6840
  * Works exactly as {@link createManyFrom}, except that it returns inserted row count by default.
@@ -6823,7 +6842,10 @@ class Create {
6823
6842
  * @param query - query to create new records from
6824
6843
  */
6825
6844
  insertManyFrom(query) {
6826
- return _queryInsertManyFrom(this.clone(), query);
6845
+ return _queryInsertManyFrom(
6846
+ this.clone(),
6847
+ query
6848
+ );
6827
6849
  }
6828
6850
  /**
6829
6851
  * `defaults` allows setting values that will be used later in `create`.
@@ -6848,7 +6870,10 @@ class Create {
6848
6870
  * @param data - default values for `create` and `createMany` which will follow `defaults`
6849
6871
  */
6850
6872
  defaults(data) {
6851
- return _queryDefaults(this.clone(), data);
6873
+ return _queryDefaults(
6874
+ this.clone(),
6875
+ data
6876
+ );
6852
6877
  }
6853
6878
  /**
6854
6879
  * By default, violating unique constraint will cause the creative query to throw,
@@ -7310,9 +7335,7 @@ class Having {
7310
7335
  return pushQueryValue(
7311
7336
  q,
7312
7337
  "having",
7313
- args.map(
7314
- (arg) => arg(q).q.expr
7315
- )
7338
+ args.map((arg) => arg(q).q.expr)
7316
7339
  );
7317
7340
  }
7318
7341
  /**
@@ -10362,6 +10385,8 @@ class TransformMethods {
10362
10385
  *
10363
10386
  * `transform` method should be called in the last order, other methods can't be chained after calling it.
10364
10387
  *
10388
+ * It is meant to transform the whole result of a query, for transforming individual records consider using {@link QueryMap.map}.
10389
+ *
10365
10390
  * The [hooks](/guide/hooks.html) that are going to run after the query will receive the query result **before** transferring.
10366
10391
  *
10367
10392
  * Consider the following example of a cursor-based pagination by `id`:
@@ -10413,6 +10438,52 @@ class TransformMethods {
10413
10438
  }
10414
10439
  }
10415
10440
 
10441
+ class QueryMap {
10442
+ /**
10443
+ * Use `map` to transform individual records of a query result.
10444
+ *
10445
+ * It accepts a single record and should return a single transformed record.
10446
+ *
10447
+ * For transforming the whole result of a query, consider using [transform](#transform) instead.
10448
+ *
10449
+ * The [hooks](/guide/hooks) that are going to run after the query will receive the query result **before** transformation.
10450
+ *
10451
+ * ```ts
10452
+ * // add a `titleLength` to every post
10453
+ * const posts = await db.post.limit(10).map((post) => ({
10454
+ * ...post,
10455
+ * titleLength: post.title.length,
10456
+ * }));
10457
+ *
10458
+ * posts[0].titleLength; // number
10459
+ *
10460
+ * // using the exact same `map` function to transform a single post
10461
+ * const singlePost = await db.post.find(id).map((post) => ({
10462
+ * ...post,
10463
+ * titleLength: post.title.length,
10464
+ * }));
10465
+ *
10466
+ * singlePost.titleLength; // number
10467
+ *
10468
+ * // can be used in sub-queries
10469
+ * const postsWithComments = await db.post.select('title', {
10470
+ * comments: (q) =>
10471
+ * q.comments.map((comment) => ({
10472
+ * ...comment,
10473
+ * truncatedContent: comment.content.slice(0, 100),
10474
+ * })),
10475
+ * });
10476
+ *
10477
+ * postsWithComments[0].comments[0].truncatedContent; // string
10478
+ * ```
10479
+ *
10480
+ * @param fn - function to transform an individual record
10481
+ */
10482
+ map(fn) {
10483
+ return pushQueryValue(this.clone(), "transform", { map: fn });
10484
+ }
10485
+ }
10486
+
10416
10487
  class ScopeMethods {
10417
10488
  /**
10418
10489
  * See {@link ScopeMethods}
@@ -11394,6 +11465,7 @@ applyMixins(QueryMethods, [
11394
11465
  MergeQueryMethods,
11395
11466
  SqlMethod,
11396
11467
  TransformMethods,
11468
+ QueryMap,
11397
11469
  ScopeMethods,
11398
11470
  SoftDeleteMethods,
11399
11471
  ExpressionMethods