pqb 0.31.0 → 0.31.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.mjs CHANGED
@@ -4403,12 +4403,6 @@ const mergeColumnsSql = (quotedColumns2) => {
4403
4403
  };
4404
4404
  const encodeRow = (ctx, q, QueryClass, row, runtimeDefaults, quotedAs) => {
4405
4405
  const arr = row.map((value) => {
4406
- if (typeof value === "function") {
4407
- value = resolveSubQueryCallback(
4408
- q,
4409
- value
4410
- );
4411
- }
4412
4406
  if (value && typeof value === "object") {
4413
4407
  if (value instanceof Expression) {
4414
4408
  return value.toSQL(ctx, quotedAs);
@@ -4711,6 +4705,7 @@ const toSQL = (table, options) => {
4711
4705
  return !(options == null ? void 0 : options.clearCache) && table.q[toSQLCacheKey] || (table.q[toSQLCacheKey] = makeSQL(table, options));
4712
4706
  };
4713
4707
  const makeSQL = (table, options) => {
4708
+ var _a;
4714
4709
  const query = table.q;
4715
4710
  const sql = [];
4716
4711
  const values = (options == null ? void 0 : options.values) || [];
@@ -4724,24 +4719,21 @@ const makeSQL = (table, options) => {
4724
4719
  pushWithSql(ctx, query.with);
4725
4720
  }
4726
4721
  if (query.type) {
4722
+ const tableName = (_a = table.table) != null ? _a : query.as;
4723
+ if (!tableName)
4724
+ throw new Error(`Table is missing for ${query.type}`);
4727
4725
  if (query.type === "truncate") {
4728
- if (!table.table)
4729
- throw new Error("Table is missing for truncate");
4730
- pushTruncateSql(ctx, table.table, query);
4726
+ pushTruncateSql(ctx, tableName, query);
4731
4727
  return { text: sql.join(" "), values };
4732
4728
  }
4733
4729
  if (query.type === "columnInfo") {
4734
- if (!table.table)
4735
- throw new Error("Table is missing for truncate");
4736
4730
  pushColumnInfoSql(ctx, table, query);
4737
4731
  return { text: sql.join(" "), values };
4738
4732
  }
4739
- if (!table.table)
4740
- throw new Error(`Table is missing for ${query.type}`);
4741
- const quotedAs2 = `"${query.as || table.table}"`;
4733
+ const quotedAs2 = `"${query.as || tableName}"`;
4742
4734
  if (query.type === "insert") {
4743
4735
  return {
4744
- hookSelect: pushInsertSql(ctx, table, query, `"${table.table}"`),
4736
+ hookSelect: pushInsertSql(ctx, table, query, `"${tableName}"`),
4745
4737
  text: sql.join(" "),
4746
4738
  values
4747
4739
  };
@@ -6153,9 +6145,17 @@ const processCreateItem = (q, item, rowIndex, ctx, encoders) => {
6153
6145
  item,
6154
6146
  rowIndex
6155
6147
  );
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;
6148
+ } else {
6149
+ if (typeof item[key] === "function") {
6150
+ item[key] = resolveSubQueryCallback(
6151
+ q,
6152
+ item[key]
6153
+ );
6154
+ }
6155
+ if (!ctx.columns.has(key) && (shape[key] && !shape[key].data.computed || shape === anyShape)) {
6156
+ ctx.columns.set(key, ctx.columns.size);
6157
+ encoders[key] = (_c = shape[key]) == null ? void 0 : _c.encodeFn;
6158
+ }
6159
6159
  }
6160
6160
  }
6161
6161
  };
@@ -6361,6 +6361,22 @@ class Create {
6361
6361
  * });
6362
6362
  * ```
6363
6363
  *
6364
+ * `create` and `insert` can be used in {@link WithMethods.with} expressions:
6365
+ *
6366
+ * ```ts
6367
+ * db.$queryBuilder
6368
+ * // create a record in one table
6369
+ * .with('a', db.table.select('id').create(data))
6370
+ * // create a record in other table using the first table record id
6371
+ * .with('b', (q) =>
6372
+ * db.otherTable.select('id').create({
6373
+ * ...otherData,
6374
+ * aId: () => q.from('a').get('id'),
6375
+ * }),
6376
+ * )
6377
+ * .from('b');
6378
+ * ```
6379
+ *
6364
6380
  * @param data - data for the record, may have values, raw SQL, queries, relation operations.
6365
6381
  */
6366
6382
  create(data) {
@@ -6869,6 +6885,19 @@ class Delete {
6869
6885
  * // delete all users who have corresponding profile records:
6870
6886
  * db.table.join(Profile, 'profile.userId', 'user.id').all().delete();
6871
6887
  * ```
6888
+ *
6889
+ * `delete` can be used in {@link WithMethods.with} expressions:
6890
+ *
6891
+ * ```ts
6892
+ * db.$queryBuilder
6893
+ * // delete a record in one table
6894
+ * .with('a', db.table.find(1).select('id').delete())
6895
+ * // delete a record in other table using the first table record id
6896
+ * .with('b', (q) =>
6897
+ * db.otherTable.select('id').whereIn('aId', q.from('a').pluck('id')).delete(),
6898
+ * )
6899
+ * .from('b');
6900
+ * ```
6872
6901
  */
6873
6902
  // eslint-disable-next-line @typescript-eslint/no-unused-vars
6874
6903
  delete(..._args) {
@@ -9531,7 +9560,24 @@ class Update {
9531
9560
  * })
9532
9561
  * ```
9533
9562
  *
9534
- * It is not supported because query inside `WITH` cannot reference the table in `UPDATE`.
9563
+ * `update` can be used in {@link WithMethods.with} expressions:
9564
+ *
9565
+ * ```ts
9566
+ * db.$queryBuilder
9567
+ * // update record in one table
9568
+ * .with('a', db.table.find(1).select('id').update(data))
9569
+ * // update record in other table using the first table record id
9570
+ * .with('b', (q) =>
9571
+ * db.otherTable
9572
+ * .find(1)
9573
+ * .select('id')
9574
+ * .update({
9575
+ * ...otherData,
9576
+ * aId: () => q.from('a').get('id'),
9577
+ * }),
9578
+ * )
9579
+ * .from('b');
9580
+ * ```
9535
9581
  *
9536
9582
  * ### null, undefined, unknown columns
9537
9583
  *