pqb 0.30.6 → 0.31.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.js CHANGED
@@ -2379,25 +2379,14 @@ const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
2379
2379
  }).text}) ${outerAs || innerAs}`;
2380
2380
  };
2381
2381
  const processArgs = (args, ctx, query, joinAs, joinShape, quotedAs) => {
2382
- if (args.length === 1) {
2383
- return getObjectOrRawConditions(
2384
- ctx,
2385
- query,
2386
- args[0],
2387
- quotedAs,
2388
- joinAs,
2389
- joinShape
2390
- );
2391
- } else {
2392
- return getConditionsFor3Or4LengthItem(
2393
- ctx,
2394
- query,
2395
- joinAs,
2396
- quotedAs,
2397
- args,
2398
- joinShape
2399
- );
2400
- }
2382
+ return args.length === 1 ? getObjectOrRawConditions(ctx, query, args[0], quotedAs, joinAs, joinShape) : getConditionsFor3Or4LengthItem(
2383
+ ctx,
2384
+ query,
2385
+ joinAs,
2386
+ quotedAs,
2387
+ args,
2388
+ joinShape
2389
+ );
2401
2390
  };
2402
2391
  const getConditionsFor3Or4LengthItem = (ctx, query, target, quotedAs, args, joinShape) => {
2403
2392
  const [leftColumn, opOrRightColumn, maybeRightColumn] = args;
@@ -2550,7 +2539,9 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2550
2539
  );
2551
2540
  return { w: first, r, s: joinSubQuery || getIsJoinSubQuery(r) };
2552
2541
  }
2553
- } else if (typeof args[0] === "function") {
2542
+ }
2543
+ const args0 = args.length ? args[0] : orchidCore.returnArg;
2544
+ if (typeof args0 === "function") {
2554
2545
  const q = first;
2555
2546
  if (q.joinQueryAfterCallback) {
2556
2547
  let base = q.baseQuery;
@@ -2568,7 +2559,7 @@ const processJoinArgs = (joinTo, first, args, joinSubQuery) => {
2568
2559
  const joinedShapes = __spreadProps$4(__spreadValues$c({}, joinTo.q.joinedShapes), {
2569
2560
  [joinTo.q.as || joinTo.table]: joinTo.shape
2570
2561
  });
2571
- const r = args[0](
2562
+ const r = args0(
2572
2563
  makeJoinQueryBuilder(
2573
2564
  q,
2574
2565
  q.q.joinedShapes ? __spreadValues$c(__spreadValues$c({}, q.q.joinedShapes), joinedShapes) : joinedShapes,
@@ -3685,15 +3676,16 @@ class AsMethods {
3685
3676
  }
3686
3677
 
3687
3678
  function queryFrom(self, arg) {
3688
- var _a;
3679
+ var _a, _b;
3689
3680
  const data = self.q;
3690
3681
  if (typeof arg === "string") {
3691
3682
  data.as || (data.as = arg);
3683
+ data.shape = (_a = data.withShapes) == null ? void 0 : _a[arg];
3692
3684
  } else if (orchidCore.isExpression(arg)) {
3693
3685
  data.as || (data.as = "t");
3694
3686
  } else if (Array.isArray(arg)) {
3695
3687
  const { shape } = data;
3696
- const parsers = (_a = data.parsers) != null ? _a : data.parsers = {};
3688
+ const parsers = (_b = data.parsers) != null ? _b : data.parsers = {};
3697
3689
  for (const item of arg) {
3698
3690
  if (typeof item === "string") {
3699
3691
  const withShape = data.withShapes[item];
@@ -3723,7 +3715,7 @@ function queryFromSql(self, args) {
3723
3715
  data.from = sqlQueryArgsToExpression(args);
3724
3716
  return self;
3725
3717
  }
3726
- class From {
3718
+ class FromMethods {
3727
3719
  /**
3728
3720
  * Set the `FROM` value, by default the table name is used.
3729
3721
  *
@@ -4080,20 +4072,21 @@ const havingToSql = (ctx, query, quotedAs) => {
4080
4072
  ).join(" AND ");
4081
4073
  };
4082
4074
 
4083
- const pushWithSql = (ctx, withData) => {
4084
- if (!withData.length)
4075
+ const pushWithSql = (ctx, items) => {
4076
+ if (!items.length)
4085
4077
  return;
4086
4078
  ctx.sql.push(
4087
4079
  "WITH",
4088
- withData.map((withItem) => {
4089
- const [name, options, query] = withItem;
4080
+ items.map((item) => {
4081
+ var _a;
4090
4082
  let inner;
4091
- if (orchidCore.isExpression(query)) {
4092
- inner = query.toSQL(ctx, `"${name}"`);
4083
+ if (item.q) {
4084
+ inner = makeSQL(item.q, ctx).text;
4093
4085
  } else {
4094
- inner = makeSQL(query, ctx).text;
4086
+ inner = item.s.toSQL(ctx, `"${item.n}"`);
4095
4087
  }
4096
- return `${options.recursive ? "RECURSIVE " : ""}"${name}"${options.columns ? `(${options.columns.map((x) => `"${x}"`).join(", ")})` : ""} AS ${options.materialized ? "MATERIALIZED " : options.notMaterialized ? "NOT MATERIALIZED " : ""}(${inner})`;
4088
+ const o = (_a = item.o) != null ? _a : orchidCore.emptyObject;
4089
+ return `${o.recursive ? "RECURSIVE " : ""}"${item.n}"${o.columns ? `(${o.columns.map((x) => `"${x}"`).join(", ")})` : ""} AS ${o.materialized ? "MATERIALIZED " : o.notMaterialized ? "NOT MATERIALIZED " : ""}(${inner})`;
4097
4090
  }).join(", ")
4098
4091
  );
4099
4092
  };
@@ -4775,54 +4768,50 @@ const makeSQL = (table, options) => {
4775
4768
  }
4776
4769
  }
4777
4770
  const quotedAs = (query.as || table.table) && `"${query.as || table.table}"`;
4778
- sql.push("SELECT");
4779
- if (query.distinct) {
4780
- pushDistinctSql(ctx, table, query.distinct, quotedAs);
4781
- }
4782
- pushSelectSql(ctx, table, query, quotedAs);
4783
- if (table.table || query.from) {
4784
- pushFromAndAs(ctx, table, query, quotedAs);
4785
- }
4786
- if (query.join) {
4787
- pushJoinSql(
4788
- ctx,
4789
- table,
4790
- query,
4791
- quotedAs
4792
- );
4793
- }
4794
- if (query.and || query.or) {
4795
- pushWhereStatementSql(ctx, table, query, quotedAs);
4796
- }
4797
- if (query.group) {
4798
- const group = query.group.map(
4799
- (item) => orchidCore.isExpression(item) ? item.toSQL(ctx, quotedAs) : columnToSql(ctx, table.q, table.q.shape, item, quotedAs)
4800
- );
4801
- sql.push(`GROUP BY ${group.join(", ")}`);
4802
- }
4803
- if (query.having)
4804
- pushHavingSql(ctx, query, quotedAs);
4805
- if (query.window) {
4806
- const window = [];
4807
- query.window.forEach((item) => {
4808
- for (const key in item) {
4809
- window.push(
4810
- `"${key}" AS ${windowToSql(ctx, query, item[key], quotedAs)}`
4811
- );
4812
- }
4813
- });
4814
- sql.push(`WINDOW ${window.join(", ")}`);
4815
- }
4816
4771
  if (query.union) {
4817
- for (const item of query.union) {
4818
- let itemSql;
4819
- if (orchidCore.isExpression(item.arg)) {
4820
- itemSql = item.arg.toSQL(ctx, quotedAs);
4821
- } else {
4822
- const argSql = makeSQL(item.arg, { values });
4823
- itemSql = argSql.text;
4824
- }
4825
- sql.push(`${item.kind} ${item.wrap ? `(${itemSql})` : itemSql}`);
4772
+ sql.push(`(${makeSQL(query.union.b, { values }).text})`);
4773
+ for (const u of query.union.u) {
4774
+ const itemSql = orchidCore.isExpression(u.a) ? u.a.toSQL(ctx, quotedAs) : makeSQL(u.a, { values }).text;
4775
+ sql.push(`${u.k} (${itemSql})`);
4776
+ }
4777
+ } else {
4778
+ sql.push("SELECT");
4779
+ if (query.distinct) {
4780
+ pushDistinctSql(ctx, table, query.distinct, quotedAs);
4781
+ }
4782
+ pushSelectSql(ctx, table, query, quotedAs);
4783
+ if (table.table || query.from) {
4784
+ pushFromAndAs(ctx, table, query, quotedAs);
4785
+ }
4786
+ if (query.join) {
4787
+ pushJoinSql(
4788
+ ctx,
4789
+ table,
4790
+ query,
4791
+ quotedAs
4792
+ );
4793
+ }
4794
+ if (query.and || query.or) {
4795
+ pushWhereStatementSql(ctx, table, query, quotedAs);
4796
+ }
4797
+ if (query.group) {
4798
+ const group = query.group.map(
4799
+ (item) => orchidCore.isExpression(item) ? item.toSQL(ctx, quotedAs) : columnToSql(ctx, table.q, table.q.shape, item, quotedAs)
4800
+ );
4801
+ sql.push(`GROUP BY ${group.join(", ")}`);
4802
+ }
4803
+ if (query.having)
4804
+ pushHavingSql(ctx, query, quotedAs);
4805
+ if (query.window) {
4806
+ const window = [];
4807
+ query.window.forEach((item) => {
4808
+ for (const key in item) {
4809
+ window.push(
4810
+ `"${key}" AS ${windowToSql(ctx, query, item[key], quotedAs)}`
4811
+ );
4812
+ }
4813
+ });
4814
+ sql.push(`WINDOW ${window.join(", ")}`);
4826
4815
  }
4827
4816
  }
4828
4817
  if (query.order) {
@@ -4906,7 +4895,7 @@ const cloneQuery = (q) => {
4906
4895
  if (q.window)
4907
4896
  q.window = q.window.slice(0);
4908
4897
  if (q.union)
4909
- q.union = q.union.slice(0);
4898
+ q.union = { b: q.union.b, u: q.union.u.slice(0) };
4910
4899
  if (q.order)
4911
4900
  q.order = q.order.slice(0);
4912
4901
  } else if (q.type === "insert") {
@@ -6366,7 +6355,7 @@ class Create {
6366
6355
  *
6367
6356
  * await db.table.create({
6368
6357
  * // raw SQL
6369
- * column1: sql`'John' || ' ' || 'Doe'`,
6358
+ * column1: (q) => q.sql`'John' || ' ' || 'Doe'`,
6370
6359
  *
6371
6360
  * // query that returns a single value
6372
6361
  * // returning multiple values will result in Postgres error
@@ -8304,6 +8293,11 @@ class MergeQueryMethods {
8304
8293
  a[key] = a[key] ? [...a[key], ...value] : value;
8305
8294
  } else if (mergableObjects[key]) {
8306
8295
  a[key] = a[key] ? __spreadValues$5(__spreadValues$5({}, a[key]), value) : value;
8296
+ } else if (key === "union") {
8297
+ a[key] = a[key] ? {
8298
+ b: a[key].b,
8299
+ u: [...a[key].u, ...value.u]
8300
+ } : value;
8307
8301
  } else {
8308
8302
  a[key] = value;
8309
8303
  }
@@ -8317,196 +8311,225 @@ class MergeQueryMethods {
8317
8311
  }
8318
8312
  }
8319
8313
 
8320
- var __defProp$4 = Object.defineProperty;
8321
- var __defProps$1 = Object.defineProperties;
8322
- var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
8323
- var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
8324
- var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
8325
- var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
8326
- var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8327
- var __spreadValues$4 = (a, b) => {
8328
- for (var prop in b || (b = {}))
8329
- if (__hasOwnProp$4.call(b, prop))
8330
- __defNormalProp$4(a, prop, b[prop]);
8331
- if (__getOwnPropSymbols$4)
8332
- for (var prop of __getOwnPropSymbols$4(b)) {
8333
- if (__propIsEnum$4.call(b, prop))
8334
- __defNormalProp$4(a, prop, b[prop]);
8335
- }
8336
- return a;
8314
+ const _queryUnion = (base, args, k) => {
8315
+ const q = base.baseQuery.clone();
8316
+ const u = args.map(
8317
+ (a) => ({
8318
+ a: typeof a === "function" ? a(q) : a,
8319
+ k
8320
+ })
8321
+ );
8322
+ const union = q.q.union = base.q.union;
8323
+ if (union) {
8324
+ union.u.push(...u);
8325
+ } else {
8326
+ q.q.union = {
8327
+ b: base,
8328
+ u
8329
+ };
8330
+ }
8331
+ return q;
8337
8332
  };
8338
- var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
8339
- class With {
8333
+ class Union {
8340
8334
  /**
8341
- * Add Common Table Expression (CTE) to the query.
8335
+ * Creates a union query, takes one or more queries or SQL expressions.
8342
8336
  *
8343
8337
  * ```ts
8344
- * import { columnTypes } from 'orchid-orm';
8345
- * import { NumberColumn } from './number';
8346
- *
8347
- * // .with optionally accepts such options:
8348
- * type WithOptions = {
8349
- * // list of columns returned by this WITH statement
8350
- * // by default all columns from provided column shape will be included
8351
- * // true is for default behavior
8352
- * columns?: string[] | boolean;
8353
- *
8354
- * // Adds RECURSIVE keyword:
8355
- * recursive?: true;
8356
- *
8357
- * // Adds MATERIALIZED keyword:
8358
- * materialized?: true;
8359
- *
8360
- * // Adds NOT MATERIALIZED keyword:
8361
- * notMaterialized?: true;
8362
- * };
8363
- *
8364
- * // accepts columns shape and a raw expression:
8365
- * db.table.with(
8366
- * 'alias',
8367
- * {
8368
- * id: columnTypes.integer(),
8369
- * name: columnTypes.text(3, 100),
8370
- * },
8371
- * sql`SELECT id, name FROM "someTable"`,
8372
- * );
8373
- *
8374
- * // accepts query:
8375
- * db.table.with('alias', db.table.all());
8376
- *
8377
- * // accepts a callback for a query builder:
8378
- * db.table.with('alias', (qb) =>
8379
- * qb.select({ one: sql`1`.type((t) => t.integer()) }),
8380
- * );
8381
- *
8382
- * // All mentioned forms can accept options as a second argument:
8383
- * db.table.with(
8384
- * 'alias',
8385
- * {
8386
- * recursive: true,
8387
- * materialized: true,
8388
- * },
8389
- * rawOrQueryOrCallback,
8390
- * );
8338
+ * // The first query of the union
8339
+ * db.one
8340
+ * .select('id', 'name')
8341
+ * // add two more queries to the union
8342
+ * .union(
8343
+ * db.two.select('id', 'name'),
8344
+ * (q = q.sql`SELECT id, name FROM "thirdTable"`),
8345
+ * )
8346
+ * // sub-sequent `union` is equivalent to passing multiple queries into a single `union`
8347
+ * .union(db.three.select('id', 'name'));
8391
8348
  * ```
8392
8349
  *
8393
- * Defined `WITH` table can be used in `.from` or `.join` with all the type safeness:
8350
+ * `order`, `limit`, `offset` are special, it matters if you place them **before** or **after** the `union`, it also have a meaning to place them before and after.
8394
8351
  *
8395
8352
  * ```ts
8396
- * db.table.with('alias', db.table.all()).from('alias').select('alias.id');
8397
- *
8398
- * db.table
8399
- * .with('alias', db.table.all())
8400
- * .join('alias', 'alias.id', 'user.id')
8401
- * .select('alias.id');
8353
+ * // order, limit, offset are applied ONLY to 'one'
8354
+ * db.one
8355
+ * .order('x')
8356
+ * .limit(1)
8357
+ * .offset(1)
8358
+ * // 'two' also has order, limit, and offset
8359
+ * .unionAll(db.two.order('y').limit(2).offset(2))
8360
+ * // sets order, limit, offset for all records
8361
+ * .order('z')
8362
+ * .limit(3)
8363
+ * .offset(3);
8402
8364
  * ```
8403
8365
  *
8404
- * @param args - first argument is an alias for this CTE, other arguments can be column shape, query object, or raw SQL.
8405
- */
8406
- with(...args) {
8407
- const q = this.clone();
8408
- let options = args.length === 3 && !orchidCore.isExpression(args[2]) || args.length === 4 ? args[1] : void 0;
8409
- const last = args[args.length - 1];
8410
- const query = typeof last === "function" ? last(q.queryBuilder) : last;
8411
- const shape = args.length === 4 ? args[2] : orchidCore.isExpression(query) ? args[1] : query.q.shape;
8412
- if ((options == null ? void 0 : options.columns) === true) {
8413
- options = __spreadProps$1(__spreadValues$4({}, options), {
8414
- columns: Object.keys(shape)
8415
- });
8416
- }
8417
- pushQueryValue(q, "with", [args[0], options || orchidCore.emptyObject, query]);
8418
- return setQueryObjectValue(q, "withShapes", args[0], shape);
8419
- }
8420
- }
8421
-
8422
- class Union {
8423
- /**
8424
- * Creates a union query, taking an array or a list of callbacks, builders, or raw statements to build the union statement, with optional boolean `wrap`.
8425
- * If the `wrap` parameter is true, the queries will be individually wrapped in parentheses.
8366
+ * Equivalent SQL:
8426
8367
  *
8427
- * ```ts
8428
- * SomeTable.select('id', 'name').union(
8429
- * [
8430
- * OtherTable.select('id', 'name'),
8431
- * sql`SELECT id, name FROM "thirdTable"`,
8432
- * ],
8433
- * true, // optional wrap parameter
8434
- * );
8368
+ * ```sql
8369
+ * -- both union parts have their own order, limit, offset
8370
+ * ( SELECT * FROM one ORDER x ASC LIMIT 1 OFFSET 1 )
8371
+ * UNION ALL
8372
+ * ( SELECT * FROM two ORDER y ASC LIMIT 2 OFFSET 2 )
8373
+ * -- order, limit, offset of the whole query
8374
+ * ORDER BY z ASC LIMIT 3 OFFSET 3
8435
8375
  * ```
8436
8376
  *
8437
- * @param args - array of queries or raw SQLs
8438
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8377
+ * All the listed methods have the same signature, they are only different by SQL keyword:
8378
+ *
8379
+ * - `union` - union of all queries, performs deduplication
8380
+ * - `unionAll` - `union` that allows duplicated rows
8381
+ * - `intersect` - get only rows that are present in all queries
8382
+ * - `intersectAll` - `intersect` that allows duplicated rows
8383
+ * - `except` - get only rows that are in the first query but not in the second
8384
+ * - `exceptAll` - `except` that allows duplicated rows
8385
+ *
8386
+ * @param args - array of queries or SQL expressions
8439
8387
  */
8440
- union(args, wrap) {
8441
- return pushQueryArray(
8388
+ union(...args) {
8389
+ return _queryUnion(
8442
8390
  this.clone(),
8443
- "union",
8444
- args.map((arg) => ({ arg, kind: "UNION", wrap }))
8391
+ args,
8392
+ "UNION"
8445
8393
  );
8446
8394
  }
8447
8395
  /**
8448
- * Same as `union`, but allows duplicated rows.
8396
+ * Same as {@link union}, but allows duplicated rows.
8449
8397
  *
8450
- * @param args - array of queries or raw SQLs
8451
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8398
+ * @param args - array of queries or SQL expressions
8452
8399
  */
8453
- unionAll(args, wrap) {
8454
- return pushQueryArray(
8400
+ unionAll(...args) {
8401
+ return _queryUnion(
8455
8402
  this.clone(),
8456
- "union",
8457
- args.map((arg) => ({ arg, kind: "UNION ALL", wrap }))
8403
+ args,
8404
+ "UNION ALL"
8458
8405
  );
8459
8406
  }
8460
8407
  /**
8461
- * Same as `union`, but uses a `INTERSECT` SQL keyword instead
8408
+ * Same as {@link union}, but uses a `INTERSECT` SQL keyword instead
8462
8409
  *
8463
- * @param args - array of queries or raw SQLs
8464
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8410
+ * @param args - array of queries or SQL expressions
8465
8411
  */
8466
- intersect(args, wrap) {
8467
- return pushQueryArray(
8412
+ intersect(...args) {
8413
+ return _queryUnion(
8468
8414
  this.clone(),
8469
- "union",
8470
- args.map((arg) => ({ arg, kind: "INTERSECT", wrap }))
8415
+ args,
8416
+ "INTERSECT"
8471
8417
  );
8472
8418
  }
8473
8419
  /**
8474
- * Same as `intersect`, but allows duplicated rows.
8420
+ * Same as {@link intersect}, but allows duplicated rows.
8475
8421
  *
8476
- * @param args - array of queries or raw SQLs
8477
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8422
+ * @param args - array of queries or SQL expressions
8478
8423
  */
8479
- intersectAll(args, wrap) {
8480
- return pushQueryArray(
8424
+ intersectAll(...args) {
8425
+ return _queryUnion(
8481
8426
  this.clone(),
8482
- "union",
8483
- args.map((arg) => ({ arg, kind: "INTERSECT ALL", wrap }))
8427
+ args,
8428
+ "INTERSECT ALL"
8484
8429
  );
8485
8430
  }
8486
8431
  /**
8487
- * Same as `union`, but uses an `EXCEPT` SQL keyword instead
8432
+ * Same as {@link union}, but uses an `EXCEPT` SQL keyword instead
8488
8433
  *
8489
- * @param args - array of queries or raw SQLs
8490
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8434
+ * @param args - array of queries or SQL expressions
8491
8435
  */
8492
- except(args, wrap) {
8493
- return pushQueryArray(
8436
+ except(...args) {
8437
+ return _queryUnion(
8494
8438
  this.clone(),
8495
- "union",
8496
- args.map((arg) => ({ arg, kind: "EXCEPT", wrap }))
8439
+ args,
8440
+ "EXCEPT"
8497
8441
  );
8498
8442
  }
8499
8443
  /**
8500
- * Same as `except`, but allows duplicated rows.
8444
+ * Same as {@link except}, but allows duplicated rows.
8501
8445
  *
8502
- * @param args - array of queries or raw SQLs
8503
- * @param wrap - provide `true` if you want the queries to be wrapped into parentheses
8446
+ * @param args - array of queries or SQL expressions
8504
8447
  */
8505
- exceptAll(args, wrap) {
8506
- return pushQueryArray(
8448
+ exceptAll(...args) {
8449
+ return _queryUnion(
8507
8450
  this.clone(),
8508
- "union",
8509
- args.map((arg) => ({ arg, kind: "EXCEPT ALL", wrap }))
8451
+ args,
8452
+ "EXCEPT ALL"
8453
+ );
8454
+ }
8455
+ }
8456
+
8457
+ var __defProp$4 = Object.defineProperty;
8458
+ var __defProps$1 = Object.defineProperties;
8459
+ var __getOwnPropDescs$1 = Object.getOwnPropertyDescriptors;
8460
+ var __getOwnPropSymbols$4 = Object.getOwnPropertySymbols;
8461
+ var __hasOwnProp$4 = Object.prototype.hasOwnProperty;
8462
+ var __propIsEnum$4 = Object.prototype.propertyIsEnumerable;
8463
+ var __defNormalProp$4 = (obj, key, value) => key in obj ? __defProp$4(obj, key, { enumerable: true, configurable: true, writable: true, value }) : obj[key] = value;
8464
+ var __spreadValues$4 = (a, b) => {
8465
+ for (var prop in b || (b = {}))
8466
+ if (__hasOwnProp$4.call(b, prop))
8467
+ __defNormalProp$4(a, prop, b[prop]);
8468
+ if (__getOwnPropSymbols$4)
8469
+ for (var prop of __getOwnPropSymbols$4(b)) {
8470
+ if (__propIsEnum$4.call(b, prop))
8471
+ __defNormalProp$4(a, prop, b[prop]);
8472
+ }
8473
+ return a;
8474
+ };
8475
+ var __spreadProps$1 = (a, b) => __defProps$1(a, __getOwnPropDescs$1(b));
8476
+ class WithMethods {
8477
+ with(name, second, third) {
8478
+ const q = this.clone();
8479
+ let [options, queryArg] = third ? [second, third] : [void 0, second];
8480
+ let query;
8481
+ if (typeof queryArg === "function") {
8482
+ const arg = q.queryBuilder.clone();
8483
+ arg.q.withShapes = q.q.withShapes;
8484
+ query = queryArg(arg);
8485
+ } else {
8486
+ query = queryArg;
8487
+ }
8488
+ if ((options == null ? void 0 : options.columns) === true) {
8489
+ options = __spreadProps$1(__spreadValues$4({}, options), {
8490
+ columns: Object.keys(query.shape)
8491
+ });
8492
+ }
8493
+ pushQueryValue(q, "with", { n: name, o: options, q: query });
8494
+ const shape = getShapeFromSelect(query, true);
8495
+ return setQueryObjectValue(q, "withShapes", name, shape);
8496
+ }
8497
+ withRecursive(name, ...args) {
8498
+ var _a, _b, _c;
8499
+ const q = this.clone();
8500
+ let [options, baseFn, recursiveFn] = args.length === 2 ? [{}, args[0], args[1]] : args;
8501
+ const arg = q.queryBuilder.clone();
8502
+ arg.q.withShapes = q.q.withShapes;
8503
+ let query = typeof baseFn === "function" ? baseFn(arg) : baseFn;
8504
+ const shape = ((_b = (_a = arg.q).withShapes) != null ? _b : _a.withShapes = {})[name] = getShapeFromSelect(
8505
+ query,
8506
+ true
8507
+ );
8508
+ const recursive = recursiveFn(arg);
8509
+ query = _queryUnion(query, [recursive], (_c = options.union) != null ? _c : "UNION ALL");
8510
+ options.recursive = true;
8511
+ if (options.columns === true) {
8512
+ options = __spreadProps$1(__spreadValues$4({}, options), {
8513
+ columns: Object.keys(shape)
8514
+ });
8515
+ }
8516
+ pushQueryValue(q, "with", { n: name, o: options, q: query });
8517
+ return setQueryObjectValue(q, "withShapes", name, shape);
8518
+ }
8519
+ // eslint-disable-next-line @typescript-eslint/no-explicit-any
8520
+ withSql(name, ...args) {
8521
+ const q = this.clone();
8522
+ const [options, shape, sql] = args.length === 2 ? [void 0, args[0], args[1]] : args;
8523
+ pushQueryValue(q, "with", {
8524
+ n: name,
8525
+ o: options,
8526
+ s: sql(q)
8527
+ });
8528
+ return setQueryObjectValue(
8529
+ q,
8530
+ "withShapes",
8531
+ name,
8532
+ shape(this.columnTypes)
8510
8533
  );
8511
8534
  }
8512
8535
  }
@@ -9340,11 +9363,10 @@ const _queryUpdate = (query, arg) => {
9340
9363
  "q",
9341
9364
  "withShapes"
9342
9365
  );
9343
- pushQueryValue(query, "with", [
9344
- as,
9345
- orchidCore.emptyObject,
9346
- value
9347
- ]);
9366
+ pushQueryValue(query, "with", {
9367
+ n: as,
9368
+ q: value
9369
+ });
9348
9370
  set[key] = new RawSQL(`(SELECT * FROM "${as}")`);
9349
9371
  }
9350
9372
  } else {
@@ -9442,7 +9464,7 @@ class Update {
9442
9464
  * column1: 123,
9443
9465
  *
9444
9466
  * // use raw SQL to update the column
9445
- * column2: sql`2 + 2`,
9467
+ * column2: (q) => q.sql`2 + 2`,
9446
9468
  *
9447
9469
  * // use query that returns a single value
9448
9470
  * // returning multiple values will result in Postgres error
@@ -9556,7 +9578,7 @@ class Update {
9556
9578
  );
9557
9579
  }
9558
9580
  /**
9559
- * `updateRaw` is for updating records with raw expression.
9581
+ * `updateSql` is for updating records with raw expression.
9560
9582
  *
9561
9583
  * The behavior is the same as a regular `update` method has:
9562
9584
  * `find` or `where` must precede calling this method,
@@ -9567,14 +9589,14 @@ class Update {
9567
9589
  * const value = 'new name';
9568
9590
  *
9569
9591
  * // update with SQL template string
9570
- * const updatedCount = await db.table.find(1).updateRaw`name = ${value}`;
9592
+ * const updatedCount = await db.table.find(1).updateSql`name = ${value}`;
9571
9593
  *
9572
9594
  * // or update with `sql` function:
9573
- * await db.table.find(1).updateRaw(sql`name = ${value}`);
9595
+ * await db.table.find(1).updateSql(sql`name = ${value}`);
9574
9596
  * ```
9575
9597
  * @param args - raw SQL via a template string or by using a `sql` method
9576
9598
  */
9577
- updateRaw(...args) {
9599
+ updateSql(...args) {
9578
9600
  return _queryUpdateRaw(
9579
9601
  this.clone(),
9580
9602
  sqlQueryArgsToExpression(args)
@@ -11149,10 +11171,7 @@ class QueryMethods {
11149
11171
  * pets: (q) => q.pets.join().none(),
11150
11172
  * });
11151
11173
  *
11152
- * await db.user.join(
11153
- * (q) => q.pets.none(),
11154
- * (q) => q,
11155
- * );
11174
+ * await db.user.join((q) => q.pets.none());
11156
11175
  *
11157
11176
  * await db.user.join('pets', (q) => q.none());
11158
11177
  * ```
@@ -11308,10 +11327,10 @@ orchidCore.applyMixins(QueryMethods, [
11308
11327
  AsMethods,
11309
11328
  AggregateMethods,
11310
11329
  Select,
11311
- From,
11330
+ FromMethods,
11312
11331
  Join,
11313
11332
  OnMethods,
11314
- With,
11333
+ WithMethods,
11315
11334
  Union,
11316
11335
  JsonModifiers,
11317
11336
  JsonMethods,
@@ -11945,7 +11964,7 @@ exports.EnumColumn = EnumColumn;
11945
11964
  exports.ExpressionMethods = ExpressionMethods;
11946
11965
  exports.FnExpression = FnExpression;
11947
11966
  exports.For = For;
11948
- exports.From = From;
11967
+ exports.FromMethods = FromMethods;
11949
11968
  exports.Having = Having;
11950
11969
  exports.InetColumn = InetColumn;
11951
11970
  exports.IntegerBaseColumn = IntegerBaseColumn;
@@ -12012,7 +12031,7 @@ exports.Update = Update;
12012
12031
  exports.VarCharColumn = VarCharColumn;
12013
12032
  exports.VirtualColumn = VirtualColumn;
12014
12033
  exports.Where = Where;
12015
- exports.With = With;
12034
+ exports.WithMethods = WithMethods;
12016
12035
  exports.XMLColumn = XMLColumn;
12017
12036
  exports._initQueryBuilder = _initQueryBuilder;
12018
12037
  exports._queryAfterSaveCommit = _queryAfterSaveCommit;
@@ -12060,6 +12079,7 @@ exports._queryRows = _queryRows;
12060
12079
  exports._querySelect = _querySelect;
12061
12080
  exports._queryTake = _queryTake;
12062
12081
  exports._queryTakeOptional = _queryTakeOptional;
12082
+ exports._queryUnion = _queryUnion;
12063
12083
  exports._queryUpdate = _queryUpdate;
12064
12084
  exports._queryUpdateOrThrow = _queryUpdateOrThrow;
12065
12085
  exports._queryUpdateRaw = _queryUpdateRaw;