pqb 0.31.0 → 0.31.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
@@ -2994,6 +2994,7 @@ if (process.versions.bun) {
2994
2994
  };
2995
2995
  }
2996
2996
  Object.defineProperty(Then.prototype, "then", {
2997
+ configurable: true,
2997
2998
  get: getThen,
2998
2999
  set(value) {
2999
3000
  Object.defineProperty(this, "then", {
@@ -4403,12 +4404,6 @@ const mergeColumnsSql = (quotedColumns2) => {
4403
4404
  };
4404
4405
  const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
4405
4406
  const arr = row.map((value) => {
4406
- if (typeof value === "function") {
4407
- value = resolveSubQueryCallback(
4408
- q,
4409
- value
4410
- );
4411
- }
4412
4407
  if (value && typeof value === "object") {
4413
4408
  if (value instanceof Expression) {
4414
4409
  return value.toSQL(ctx, quotedAs);
@@ -4711,6 +4706,7 @@ const toSQL = (table, options) => {
4711
4706
  return !(options == null ? void 0 : options.clearCache) && table.q[toSQLCacheKey] || (table.q[toSQLCacheKey] = makeSQL(table, options));
4712
4707
  };
4713
4708
  const makeSQL = (table, options) => {
4709
+ var _a;
4714
4710
  const query = table.q;
4715
4711
  const sql = [];
4716
4712
  const values = (options == null ? void 0 : options.values) || [];
@@ -4724,24 +4720,21 @@ const makeSQL = (table, options) => {
4724
4720
  pushWithSql(ctx, query.with);
4725
4721
  }
4726
4722
  if (query.type) {
4723
+ const tableName = (_a = table.table) != null ? _a : query.as;
4724
+ if (!tableName)
4725
+ throw new Error(`Table is missing for ${query.type}`);
4727
4726
  if (query.type === "truncate") {
4728
- if (!table.table)
4729
- throw new Error("Table is missing for truncate");
4730
- pushTruncateSql(ctx, table.table, query);
4727
+ pushTruncateSql(ctx, tableName, query);
4731
4728
  return { text: sql.join(" "), values };
4732
4729
  }
4733
4730
  if (query.type === "columnInfo") {
4734
- if (!table.table)
4735
- throw new Error("Table is missing for truncate");
4736
4731
  pushColumnInfoSql(ctx, table, query);
4737
4732
  return { text: sql.join(" "), values };
4738
4733
  }
4739
- if (!table.table)
4740
- throw new Error(`Table is missing for ${query.type}`);
4741
- const quotedAs2 = `"${query.as || table.table}"`;
4734
+ const quotedAs2 = `"${query.as || tableName}"`;
4742
4735
  if (query.type === "insert") {
4743
4736
  return {
4744
- hookSelect: pushInsertSql(ctx, table, query, `"${table.table}"`),
4737
+ hookSelect: pushInsertSql(ctx, table, query, `"${tableName}"`),
4745
4738
  text: sql.join(" "),
4746
4739
  values
4747
4740
  };
@@ -6153,9 +6146,17 @@ const processCreateItem = (q, item, rowIndex, ctx, encoders) => {
6153
6146
  item,
6154
6147
  rowIndex
6155
6148
  );
6156
- } else if (!ctx.columns.has(key) && (shape[key] && !shape[key].data.computed || shape === anyShape)) {
6157
- ctx.columns.set(key, ctx.columns.size);
6158
- encoders[key] = (_c = shape[key]) == null ? void 0 : _c.encodeFn;
6149
+ } else {
6150
+ if (typeof item[key] === "function") {
6151
+ item[key] = resolveSubQueryCallback(
6152
+ q,
6153
+ item[key]
6154
+ );
6155
+ }
6156
+ if (!ctx.columns.has(key) && (shape[key] && !shape[key].data.computed || shape === anyShape)) {
6157
+ ctx.columns.set(key, ctx.columns.size);
6158
+ encoders[key] = (_c = shape[key]) == null ? void 0 : _c.encodeFn;
6159
+ }
6159
6160
  }
6160
6161
  }
6161
6162
  };
@@ -6361,6 +6362,22 @@ class Create {
6361
6362
  * });
6362
6363
  * ```
6363
6364
  *
6365
+ * `create` and `insert` can be used in {@link WithMethods.with} expressions:
6366
+ *
6367
+ * ```ts
6368
+ * db.$queryBuilder
6369
+ * // create a record in one table
6370
+ * .with('a', db.table.select('id').create(data))
6371
+ * // create a record in other table using the first table record id
6372
+ * .with('b', (q) =>
6373
+ * db.otherTable.select('id').create({
6374
+ * ...otherData,
6375
+ * aId: () => q.from('a').get('id'),
6376
+ * }),
6377
+ * )
6378
+ * .from('b');
6379
+ * ```
6380
+ *
6364
6381
  * @param data - data for the record, may have values, raw SQL, queries, relation operations.
6365
6382
  */
6366
6383
  create(data) {
@@ -6589,18 +6606,18 @@ class Create {
6589
6606
  *
6590
6607
  * // single column:
6591
6608
  * // (this requires a composite primary key or unique index, see below)
6592
- * db.table.create(data).onConfict('email').merge();
6609
+ * db.table.create(data).onConflict('email').merge();
6593
6610
  *
6594
6611
  * // array of columns:
6595
- * db.table.create(data).onConfict(['email', 'name']).merge();
6612
+ * db.table.create(data).onConflict(['email', 'name']).merge();
6596
6613
  *
6597
6614
  * // constraint name
6598
- * db.table.create(data).onConfict({ constraint: 'unique_index_name' }).merge();
6615
+ * db.table.create(data).onConflict({ constraint: 'unique_index_name' }).merge();
6599
6616
  *
6600
6617
  * // raw SQL expression:
6601
6618
  * db.table
6602
6619
  * .create(data)
6603
- * .onConfict(sql`(email) where condition`)
6620
+ * .onConflict(sql`(email) where condition`)
6604
6621
  * .merge();
6605
6622
  * ```
6606
6623
  *
@@ -6869,6 +6886,19 @@ class Delete {
6869
6886
  * // delete all users who have corresponding profile records:
6870
6887
  * db.table.join(Profile, 'profile.userId', 'user.id').all().delete();
6871
6888
  * ```
6889
+ *
6890
+ * `delete` can be used in {@link WithMethods.with} expressions:
6891
+ *
6892
+ * ```ts
6893
+ * db.$queryBuilder
6894
+ * // delete a record in one table
6895
+ * .with('a', db.table.find(1).select('id').delete())
6896
+ * // delete a record in other table using the first table record id
6897
+ * .with('b', (q) =>
6898
+ * db.otherTable.select('id').whereIn('aId', q.from('a').pluck('id')).delete(),
6899
+ * )
6900
+ * .from('b');
6901
+ * ```
6872
6902
  */
6873
6903
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
6874
6904
  delete(..._args) {
@@ -9531,7 +9561,24 @@ class Update {
9531
9561
  * })
9532
9562
  * ```
9533
9563
  *
9534
- * It is not supported because query inside `WITH` cannot reference the table in `UPDATE`.
9564
+ * `update` can be used in {@link WithMethods.with} expressions:
9565
+ *
9566
+ * ```ts
9567
+ * db.$queryBuilder
9568
+ * // update record in one table
9569
+ * .with('a', db.table.find(1).select('id').update(data))
9570
+ * // update record in other table using the first table record id
9571
+ * .with('b', (q) =>
9572
+ * db.otherTable
9573
+ * .find(1)
9574
+ * .select('id')
9575
+ * .update({
9576
+ * ...otherData,
9577
+ * aId: () => q.from('a').get('id'),
9578
+ * }),
9579
+ * )
9580
+ * .from('b');
9581
+ * ```
9535
9582
  *
9536
9583
  * ### null, undefined, unknown columns
9537
9584
  *