pqb 0.41.2 → 0.41.4

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
@@ -3404,20 +3404,18 @@ class Transaction {
3404
3404
  sql.text = `BEGIN${options.level ? ` ISOLATION LEVEL ${options.level}` : ""}${options.readOnly !== void 0 ? ` READ ${options.readOnly ? "ONLY" : "WRITE"}` : ""}${options.deferrable !== void 0 ? ` ${options.deferrable ? "" : "NOT "}DEFERRABLE` : ""}`;
3405
3405
  if (log)
3406
3406
  logData = log.beforeQuery(sql);
3407
- try {
3408
- const result = await this.q.adapter.transaction(sql, callback);
3409
- if (log)
3410
- log.afterQuery(commitSql$1, logData);
3411
- await runAfterCommit(
3412
- trx.afterCommit,
3413
- result
3414
- );
3415
- return result;
3416
- } catch (err) {
3407
+ const result = await this.q.adapter.transaction(sql, callback).catch((err) => {
3417
3408
  if (log)
3418
3409
  log.afterQuery(rollbackSql$1, logData);
3419
3410
  throw err;
3420
- }
3411
+ });
3412
+ if (log)
3413
+ log.afterQuery(commitSql$1, logData);
3414
+ await runAfterCommit(
3415
+ trx.afterCommit,
3416
+ result
3417
+ );
3418
+ return result;
3421
3419
  } else {
3422
3420
  try {
3423
3421
  sql.text = `SAVEPOINT "${transactionId}"`;
@@ -4316,172 +4314,175 @@ const addParsersForSelectJoined = (q, arg, as = arg) => {
4316
4314
  );
4317
4315
  }
4318
4316
  };
4319
- const addParserForSelectItem = (q, as, key, arg) => {
4320
- var _a, _b, _c, _d;
4317
+ const addParserForSelectItem = (q, as, key, arg, joinQuery) => {
4318
+ var _a, _b, _c, _d, _e;
4321
4319
  if (typeof arg === "object" || typeof arg === "function") {
4322
- if (isExpression(arg)) {
4323
- addParserForRawExpression(q, key, arg);
4324
- } else {
4325
- const { q: query } = arg;
4326
- if (query.batchParsers) {
4327
- const batchParsers = (_b = (_a = q.q).batchParsers) != null ? _b : _a.batchParsers = [];
4328
- for (const bp of query.batchParsers) {
4329
- batchParsers.push({ path: [key, ...bp.path], fn: bp.fn });
4330
- }
4331
- }
4332
- if (query.hookSelect || query.parsers || query.transform) {
4333
- const batchParsers = (_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = [];
4334
- batchParsers.push({
4335
- path: [key],
4336
- fn: (path, queryResult) => {
4337
- var _a2, _b2, _c2;
4338
- const { rows } = queryResult;
4339
- const originalReturnType = query.returnType || "all";
4340
- let returnType = originalReturnType;
4341
- const { hookSelect } = query;
4342
- const batches = [];
4343
- const last = path.length;
4344
- if (returnType === "value" || returnType === "valueOrThrow") {
4345
- if (hookSelect) {
4346
- batches.push = (item) => {
4347
- if (!(key in item)) {
4348
- returnType = returnType === "value" ? "one" : "oneOrThrow";
4349
- }
4350
- batches.push = Array.prototype.push;
4351
- return batches.push(item);
4352
- };
4353
- }
4320
+ const { q: query } = arg;
4321
+ if (query.batchParsers) {
4322
+ const batchParsers = (_b = (_a = q.q).batchParsers) != null ? _b : _a.batchParsers = [];
4323
+ for (const bp of query.batchParsers) {
4324
+ batchParsers.push({ path: [key, ...bp.path], fn: bp.fn });
4325
+ }
4326
+ }
4327
+ if (query.hookSelect || query.parsers || query.transform) {
4328
+ const batchParsers = (_d = (_c = q.q).batchParsers) != null ? _d : _c.batchParsers = [];
4329
+ batchParsers.push({
4330
+ path: [key],
4331
+ fn: (path, queryResult) => {
4332
+ var _a2, _b2, _c2;
4333
+ const { rows } = queryResult;
4334
+ const originalReturnType = query.returnType || "all";
4335
+ let returnType = originalReturnType;
4336
+ const { hookSelect } = query;
4337
+ const batches = [];
4338
+ const last = path.length;
4339
+ if (returnType === "value" || returnType === "valueOrThrow") {
4340
+ if (hookSelect) {
4341
+ batches.push = (item) => {
4342
+ if (!(key in item)) {
4343
+ returnType = returnType === "value" ? "one" : "oneOrThrow";
4344
+ }
4345
+ batches.push = Array.prototype.push;
4346
+ return batches.push(item);
4347
+ };
4354
4348
  }
4355
- collectNestedSelectBatches(batches, rows, path, last);
4356
- switch (returnType) {
4357
- case "all": {
4358
- const { parsers } = query;
4359
- if (parsers) {
4360
- for (const { data } of batches) {
4361
- for (const one of data) {
4362
- parseRecord(parsers, one);
4363
- }
4349
+ }
4350
+ collectNestedSelectBatches(batches, rows, path, last);
4351
+ switch (returnType) {
4352
+ case "all": {
4353
+ const { parsers } = query;
4354
+ if (parsers) {
4355
+ for (const { data } of batches) {
4356
+ for (const one of data) {
4357
+ parseRecord(parsers, one);
4364
4358
  }
4365
4359
  }
4366
- break;
4367
4360
  }
4368
- case "one":
4369
- case "oneOrThrow": {
4370
- const { parsers } = query;
4371
- if (parsers) {
4372
- if (returnType === "one") {
4373
- for (const { data } of batches) {
4374
- if (data)
4375
- parseRecord(parsers, data);
4376
- }
4377
- } else {
4378
- for (const { data } of batches) {
4379
- if (!data)
4380
- throw new NotFoundError(arg);
4361
+ break;
4362
+ }
4363
+ case "one":
4364
+ case "oneOrThrow": {
4365
+ const { parsers } = query;
4366
+ if (parsers) {
4367
+ if (returnType === "one") {
4368
+ for (const { data } of batches) {
4369
+ if (data)
4381
4370
  parseRecord(parsers, data);
4382
- }
4383
4371
  }
4384
- } else if (returnType !== "one") {
4372
+ } else {
4385
4373
  for (const { data } of batches) {
4386
4374
  if (!data)
4387
4375
  throw new NotFoundError(arg);
4376
+ parseRecord(parsers, data);
4388
4377
  }
4389
4378
  }
4390
- if (hookSelect) {
4391
- for (const batch of batches) {
4392
- batch.data = [batch.data];
4393
- }
4379
+ } else if (returnType !== "one") {
4380
+ for (const { data } of batches) {
4381
+ if (!data)
4382
+ throw new NotFoundError(arg);
4394
4383
  }
4395
- break;
4396
4384
  }
4397
- case "pluck": {
4398
- const parse = (_a2 = query.parsers) == null ? void 0 : _a2.pluck;
4399
- if (parse) {
4400
- for (const { data } of batches) {
4401
- for (let i = 0; i < data.length; i++) {
4402
- data[i] = parse(data[i]);
4403
- }
4385
+ if (hookSelect) {
4386
+ for (const batch of batches) {
4387
+ batch.data = [batch.data];
4388
+ }
4389
+ }
4390
+ break;
4391
+ }
4392
+ case "pluck": {
4393
+ const parse = (_a2 = query.parsers) == null ? void 0 : _a2.pluck;
4394
+ if (parse) {
4395
+ for (const { data } of batches) {
4396
+ for (let i = 0; i < data.length; i++) {
4397
+ data[i] = parse(data[i]);
4404
4398
  }
4405
4399
  }
4406
- break;
4407
4400
  }
4408
- case "value":
4409
- case "valueOrThrow": {
4410
- const parse = (_b2 = query.parsers) == null ? void 0 : _b2[getValueKey];
4411
- if (parse) {
4412
- if (returnType === "value") {
4413
- for (const item of batches) {
4414
- item.parent[item.key] = item.data = item.data === void 0 ? arg.q.notFoundDefault : parse(item.data);
4415
- }
4416
- } else {
4417
- for (const item of batches) {
4418
- if (item.data === void 0)
4419
- throw new NotFoundError(arg);
4420
- item.parent[item.key] = item.data = parse(item.data);
4421
- }
4401
+ break;
4402
+ }
4403
+ case "value":
4404
+ case "valueOrThrow": {
4405
+ const parse = (_b2 = query.parsers) == null ? void 0 : _b2[getValueKey];
4406
+ if (parse) {
4407
+ if (returnType === "value") {
4408
+ for (const item of batches) {
4409
+ item.parent[item.key] = item.data = item.data === void 0 ? query.notFoundDefault : parse(item.data);
4422
4410
  }
4423
- } else if (returnType !== "value") {
4424
- for (const { data } of batches) {
4425
- if (data === void 0)
4411
+ } else {
4412
+ for (const item of batches) {
4413
+ if (item.data === void 0)
4426
4414
  throw new NotFoundError(arg);
4415
+ item.parent[item.key] = item.data = parse(item.data);
4427
4416
  }
4428
4417
  }
4429
- if (hookSelect) {
4430
- for (const batch of batches) {
4431
- batch.data = [batch.data];
4432
- }
4418
+ } else if (returnType !== "value") {
4419
+ for (const { data } of batches) {
4420
+ if (data === void 0)
4421
+ throw new NotFoundError(arg);
4433
4422
  }
4434
- break;
4435
4423
  }
4436
- }
4437
- if (hookSelect) {
4438
- let tempColumns;
4439
- let renames;
4440
- for (const column of hookSelect.keys()) {
4441
- const as2 = hookSelect.get(column).as;
4442
- if (as2)
4443
- (renames != null ? renames : renames = {})[column] = as2;
4444
- (_c2 = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _c2.add(as2 || column);
4424
+ if (hookSelect) {
4425
+ for (const batch of batches) {
4426
+ batch.data = [batch.data];
4427
+ }
4445
4428
  }
4446
- if (renames) {
4447
- for (const { data } of batches) {
4448
- for (const record of data) {
4449
- if (record) {
4450
- for (const a in renames) {
4451
- const value = record[renames[a]];
4452
- record[renames[a]] = record[a];
4453
- record[a] = value;
4454
- }
4429
+ break;
4430
+ }
4431
+ }
4432
+ if (hookSelect) {
4433
+ let tempColumns;
4434
+ let renames;
4435
+ for (const column of hookSelect.keys()) {
4436
+ const as2 = hookSelect.get(column).as;
4437
+ if (as2)
4438
+ (renames != null ? renames : renames = {})[column] = as2;
4439
+ (_c2 = tempColumns != null ? tempColumns : tempColumns = /* @__PURE__ */ new Set()) == null ? void 0 : _c2.add(as2 || column);
4440
+ }
4441
+ if (renames) {
4442
+ for (const { data } of batches) {
4443
+ for (const record of data) {
4444
+ if (record) {
4445
+ for (const a in renames) {
4446
+ const value = record[renames[a]];
4447
+ record[renames[a]] = record[a];
4448
+ record[a] = value;
4455
4449
  }
4456
4450
  }
4457
4451
  }
4458
4452
  }
4459
- if (query.selectedComputeds) {
4460
- const maybePromise = processComputedBatches(
4461
- query,
4462
- batches,
4463
- originalReturnType,
4464
- returnType,
4465
- tempColumns,
4466
- renames,
4467
- key
4468
- );
4469
- if (maybePromise)
4470
- return maybePromise;
4471
- }
4472
- finalizeNestedHookSelect(
4453
+ }
4454
+ if (query.selectedComputeds) {
4455
+ const maybePromise = processComputedBatches(
4456
+ query,
4473
4457
  batches,
4474
4458
  originalReturnType,
4459
+ returnType,
4475
4460
  tempColumns,
4476
4461
  renames,
4477
4462
  key
4478
4463
  );
4464
+ if (maybePromise)
4465
+ return maybePromise;
4479
4466
  }
4480
- applyBatchTransforms(query, batches);
4481
- return;
4467
+ finalizeNestedHookSelect(
4468
+ batches,
4469
+ originalReturnType,
4470
+ tempColumns,
4471
+ renames,
4472
+ key
4473
+ );
4482
4474
  }
4483
- });
4484
- }
4475
+ applyBatchTransforms(query, batches);
4476
+ return;
4477
+ }
4478
+ });
4479
+ }
4480
+ if (!joinQuery && ((_e = arg.q) == null ? void 0 : _e.subQuery) && arg.q.expr) {
4481
+ arg = arg.q.expr;
4482
+ }
4483
+ if (isExpression(arg)) {
4484
+ addParserForRawExpression(q, key, arg);
4485
+ return arg;
4485
4486
  }
4486
4487
  return arg;
4487
4488
  }
@@ -4516,7 +4517,7 @@ const collectNestedSelectBatches = (batches, rows, path, last) => {
4516
4517
  };
4517
4518
  const emptyArrSQL = new RawSQL("'[]'");
4518
4519
  const processSelectArg = (q, as, arg, columnAs) => {
4519
- var _a, _b;
4520
+ var _a;
4520
4521
  if (typeof arg === "string") {
4521
4522
  return setParserForSelectedString(q, arg, as, columnAs);
4522
4523
  }
@@ -4524,13 +4525,16 @@ const processSelectArg = (q, as, arg, columnAs) => {
4524
4525
  let aliases;
4525
4526
  for (const key in arg) {
4526
4527
  let value = arg[key];
4528
+ let joinQuery;
4527
4529
  if (typeof value === "function") {
4528
4530
  value = resolveSubQueryCallback(q, value);
4529
4531
  if (isQueryNone(value)) {
4530
4532
  if (value.q.innerJoinLateral) {
4531
4533
  return false;
4532
4534
  }
4533
- } else if (!isExpression(value) && value.joinQuery) {
4535
+ }
4536
+ if (!isExpression(value) && value.joinQuery) {
4537
+ joinQuery = true;
4534
4538
  value = value.joinQuery(value, q);
4535
4539
  let query;
4536
4540
  const returnType = value.q.returnType;
@@ -4568,8 +4572,6 @@ const processSelectArg = (q, as, arg, columnAs) => {
4568
4572
  (q2) => q2,
4569
4573
  key
4570
4574
  );
4571
- } else if (((_b = value.q) == null ? void 0 : _b.subQuery) && value.q.expr) {
4572
- value = value.q.expr;
4573
4575
  }
4574
4576
  }
4575
4577
  if (aliases)
@@ -4578,7 +4580,8 @@ const processSelectArg = (q, as, arg, columnAs) => {
4578
4580
  q,
4579
4581
  as,
4580
4582
  key,
4581
- value
4583
+ value,
4584
+ joinQuery
4582
4585
  );
4583
4586
  }
4584
4587
  return { selectAs };