pqb 0.33.0 → 0.33.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.js CHANGED
@@ -3365,9 +3365,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
3365
3365
  result = filterResult(q, returnType, queryResult, hookSelect, result);
3366
3366
  }
3367
3367
  if (query.transform) {
3368
- for (const fn of query.transform) {
3369
- result = fn(result);
3370
- }
3368
+ result = orchidCore.applyTransforms(returnType, query.transform, result);
3371
3369
  }
3372
3370
  return resolve == null ? void 0 : resolve(result);
3373
3371
  } catch (err) {
@@ -3585,10 +3583,8 @@ const addParserForSelectItem = (q, as, key, arg) => {
3585
3583
  orchidCore.setParserToQuery(q.q, key, (item) => {
3586
3584
  const t = query.returnType || "all";
3587
3585
  subQueryResult.rows = t === "value" || t === "valueOrThrow" ? [[item]] : t === "one" || t === "oneOrThrow" ? [item] : item;
3588
- return orchidCore.applyTransforms(
3589
- query.transform,
3590
- query.handleResult(arg, t, subQueryResult, true)
3591
- );
3586
+ const result = query.handleResult(arg, t, subQueryResult, true);
3587
+ return query.transform ? orchidCore.applyTransforms(t, query.transform, result) : result;
3592
3588
  });
3593
3589
  }
3594
3590
  if (query.returnType === "valueOrThrow" || query.returnType === "oneOrThrow") {
@@ -6713,7 +6709,10 @@ class Create {
6713
6709
  * @param args - object with columns list and raw SQL for values
6714
6710
  */
6715
6711
  createRaw(...args) {
6716
- return _queryCreateRaw(this.clone(), args);
6712
+ return _queryCreateRaw(
6713
+ this.clone(),
6714
+ args
6715
+ );
6717
6716
  }
6718
6717
  /**
6719
6718
  * Works exactly as {@link createRaw}, except that it returns inserted row count by default.
@@ -6721,7 +6720,10 @@ class Create {
6721
6720
  * @param args - object with columns list and raw SQL for values
6722
6721
  */
6723
6722
  insertRaw(...args) {
6724
- return _queryInsertRaw(this.clone(), args);
6723
+ return _queryInsertRaw(
6724
+ this.clone(),
6725
+ args
6726
+ );
6725
6727
  }
6726
6728
  /**
6727
6729
  * `createManyRaw` and `insertManyRaw` are for creating many record with raw SQL expressions.
@@ -6742,7 +6744,10 @@ class Create {
6742
6744
  * @param args - object with columns list and array of raw SQL for values
6743
6745
  */
6744
6746
  createManyRaw(...args) {
6745
- return _queryCreateManyRaw(this.clone(), args);
6747
+ return _queryCreateManyRaw(
6748
+ this.clone(),
6749
+ args
6750
+ );
6746
6751
  }
6747
6752
  /**
6748
6753
  * Works exactly as {@link createManyRaw}, except that it returns inserted row count by default.
@@ -6750,7 +6755,10 @@ class Create {
6750
6755
  * @param args - object with columns list and array of raw SQL for values
6751
6756
  */
6752
6757
  insertManyRaw(...args) {
6753
- return _queryInsertManyRaw(this.clone(), args);
6758
+ return _queryInsertManyRaw(
6759
+ this.clone(),
6760
+ args
6761
+ );
6754
6762
  }
6755
6763
  /**
6756
6764
  * These methods are for creating a single record, for batch creating see {@link createManyFrom}.
@@ -6792,7 +6800,11 @@ class Create {
6792
6800
  * @param data - additionally you can set some columns
6793
6801
  */
6794
6802
  createFrom(query, data) {
6795
- return _queryCreateFrom(this.clone(), query, data);
6803
+ return _queryCreateFrom(
6804
+ this.clone(),
6805
+ query,
6806
+ data
6807
+ );
6796
6808
  }
6797
6809
  /**
6798
6810
  * Works exactly as {@link createFrom}, except that it returns inserted row count by default.
@@ -6801,7 +6813,11 @@ class Create {
6801
6813
  * @param data - additionally you can set some columns
6802
6814
  */
6803
6815
  insertFrom(query, data) {
6804
- return _queryInsertFrom(this.clone(), query, data);
6816
+ return _queryInsertFrom(
6817
+ this.clone(),
6818
+ query,
6819
+ data
6820
+ );
6805
6821
  }
6806
6822
  /**
6807
6823
  * Similar to `createFrom`, but intended to create many records.
@@ -6817,7 +6833,10 @@ class Create {
6817
6833
  * @param query - query to create new records from
6818
6834
  */
6819
6835
  createManyFrom(query) {
6820
- return _queryCreateManyFrom(this.clone(), query);
6836
+ return _queryCreateManyFrom(
6837
+ this.clone(),
6838
+ query
6839
+ );
6821
6840
  }
6822
6841
  /**
6823
6842
  * Works exactly as {@link createManyFrom}, except that it returns inserted row count by default.
@@ -6825,7 +6844,10 @@ class Create {
6825
6844
  * @param query - query to create new records from
6826
6845
  */
6827
6846
  insertManyFrom(query) {
6828
- return _queryInsertManyFrom(this.clone(), query);
6847
+ return _queryInsertManyFrom(
6848
+ this.clone(),
6849
+ query
6850
+ );
6829
6851
  }
6830
6852
  /**
6831
6853
  * `defaults` allows setting values that will be used later in `create`.
@@ -6850,7 +6872,10 @@ class Create {
6850
6872
  * @param data - default values for `create` and `createMany` which will follow `defaults`
6851
6873
  */
6852
6874
  defaults(data) {
6853
- return _queryDefaults(this.clone(), data);
6875
+ return _queryDefaults(
6876
+ this.clone(),
6877
+ data
6878
+ );
6854
6879
  }
6855
6880
  /**
6856
6881
  * By default, violating unique constraint will cause the creative query to throw,
@@ -7312,9 +7337,7 @@ class Having {
7312
7337
  return pushQueryValue(
7313
7338
  q,
7314
7339
  "having",
7315
- args.map(
7316
- (arg) => arg(q).q.expr
7317
- )
7340
+ args.map((arg) => arg(q).q.expr)
7318
7341
  );
7319
7342
  }
7320
7343
  /**
@@ -10364,6 +10387,8 @@ class TransformMethods {
10364
10387
  *
10365
10388
  * `transform` method should be called in the last order, other methods can't be chained after calling it.
10366
10389
  *
10390
+ * It is meant to transform the whole result of a query, for transforming individual records consider using {@link QueryMap.map}.
10391
+ *
10367
10392
  * The [hooks](/guide/hooks.html) that are going to run after the query will receive the query result **before** transferring.
10368
10393
  *
10369
10394
  * Consider the following example of a cursor-based pagination by `id`:
@@ -10415,6 +10440,52 @@ class TransformMethods {
10415
10440
  }
10416
10441
  }
10417
10442
 
10443
+ class QueryMap {
10444
+ /**
10445
+ * Use `map` to transform individual records of a query result.
10446
+ *
10447
+ * It accepts a single record and should return a single transformed record.
10448
+ *
10449
+ * For transforming the whole result of a query, consider using [transform](#transform) instead.
10450
+ *
10451
+ * The [hooks](/guide/hooks) that are going to run after the query will receive the query result **before** transformation.
10452
+ *
10453
+ * ```ts
10454
+ * // add a `titleLength` to every post
10455
+ * const posts = await db.post.limit(10).map((post) => ({
10456
+ * ...post,
10457
+ * titleLength: post.title.length,
10458
+ * }));
10459
+ *
10460
+ * posts[0].titleLength; // number
10461
+ *
10462
+ * // using the exact same `map` function to transform a single post
10463
+ * const singlePost = await db.post.find(id).map((post) => ({
10464
+ * ...post,
10465
+ * titleLength: post.title.length,
10466
+ * }));
10467
+ *
10468
+ * singlePost.titleLength; // number
10469
+ *
10470
+ * // can be used in sub-queries
10471
+ * const postsWithComments = await db.post.select('title', {
10472
+ * comments: (q) =>
10473
+ * q.comments.map((comment) => ({
10474
+ * ...comment,
10475
+ * truncatedContent: comment.content.slice(0, 100),
10476
+ * })),
10477
+ * });
10478
+ *
10479
+ * postsWithComments[0].comments[0].truncatedContent; // string
10480
+ * ```
10481
+ *
10482
+ * @param fn - function to transform an individual record
10483
+ */
10484
+ map(fn) {
10485
+ return pushQueryValue(this.clone(), "transform", { map: fn });
10486
+ }
10487
+ }
10488
+
10418
10489
  class ScopeMethods {
10419
10490
  /**
10420
10491
  * See {@link ScopeMethods}
@@ -11396,6 +11467,7 @@ orchidCore.applyMixins(QueryMethods, [
11396
11467
  MergeQueryMethods,
11397
11468
  SqlMethod,
11398
11469
  TransformMethods,
11470
+ QueryMap,
11399
11471
  ScopeMethods,
11400
11472
  SoftDeleteMethods,
11401
11473
  ExpressionMethods