pqb 0.40.7 → 0.40.9
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.d.ts +133 -11
- package/dist/index.js +79 -51
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +79 -51
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -803,8 +803,8 @@ const columnCode = (type, ctx, key, code, data = type.data, skip) => {
|
|
|
803
803
|
addCode(code, part);
|
|
804
804
|
}
|
|
805
805
|
}
|
|
806
|
-
if (data.
|
|
807
|
-
addCode(code, ".
|
|
806
|
+
if (data.explicitSelect)
|
|
807
|
+
addCode(code, ".select(false)");
|
|
808
808
|
if (data.isNullable)
|
|
809
809
|
addCode(code, ".nullable()");
|
|
810
810
|
if (type.encodeFn && type.encodeFn !== (skip == null ? void 0 : skip.encodeFn))
|
|
@@ -2356,7 +2356,7 @@ const getArgQueryTarget = (ctx, first, joinSubQuery, cloned) => {
|
|
|
2356
2356
|
}
|
|
2357
2357
|
};
|
|
2358
2358
|
const subJoinToSql = (ctx, jq, innerAs, outerAs, cloned) => {
|
|
2359
|
-
if (!jq.q.select && jq.
|
|
2359
|
+
if (!jq.q.select && jq.q.selectAllColumns) {
|
|
2360
2360
|
if (!cloned)
|
|
2361
2361
|
jq = jq.clone();
|
|
2362
2362
|
jq.q.select = [new RawSQL(`${innerAs}.*`)];
|
|
@@ -2693,7 +2693,7 @@ const _join = (query, require2, type, first, args) => {
|
|
|
2693
2693
|
);
|
|
2694
2694
|
if (joinKey && "s" in joinArgs && joinArgs.s) {
|
|
2695
2695
|
const j = "j" in joinArgs ? (_b = joinArgs.r) != null ? _b : joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
|
|
2696
|
-
if (j.q.select || !j.
|
|
2696
|
+
if (j.q.select || !j.q.selectAllColumns) {
|
|
2697
2697
|
const shape2 = getShapeFromSelect(j, true);
|
|
2698
2698
|
setQueryObjectValue(
|
|
2699
2699
|
query,
|
|
@@ -3395,6 +3395,32 @@ class Transaction {
|
|
|
3395
3395
|
}
|
|
3396
3396
|
}
|
|
3397
3397
|
}
|
|
3398
|
+
/**
|
|
3399
|
+
* Use the `$ensureTransaction` when you want to ensure the sequence of queries is running in a transaction, but there is no need for Postgres [savepoints](https://www.postgresql.org/docs/current/sql-savepoint.html).
|
|
3400
|
+
*
|
|
3401
|
+
* ```ts
|
|
3402
|
+
* async function updateUserBalance(userId: string, amount: number) {
|
|
3403
|
+
* await db.$ensureTransaction(async () => {
|
|
3404
|
+
* await db.transfer.create({ userId, amount })
|
|
3405
|
+
* await db.user.find(userId).increment({ balance: amount })
|
|
3406
|
+
* })
|
|
3407
|
+
* }
|
|
3408
|
+
*
|
|
3409
|
+
* async function saveDeposit(userId: string, deposit: { ... }) {
|
|
3410
|
+
* await db.$ensureTransaction(async () => {
|
|
3411
|
+
* await db.deposit.create(deposit)
|
|
3412
|
+
* // transaction in updateUserBalance won't be started
|
|
3413
|
+
* await updateUserBalance(userId, deposit.amount)
|
|
3414
|
+
* })
|
|
3415
|
+
* }
|
|
3416
|
+
* ```
|
|
3417
|
+
*/
|
|
3418
|
+
ensureTransaction(cb) {
|
|
3419
|
+
const trx = this.internal.transactionStorage.getStore();
|
|
3420
|
+
if (trx)
|
|
3421
|
+
return cb();
|
|
3422
|
+
return Transaction.prototype.transaction.call(this, cb);
|
|
3423
|
+
}
|
|
3398
3424
|
}
|
|
3399
3425
|
const runAfterCommit = async (afterCommit, result) => {
|
|
3400
3426
|
if (afterCommit) {
|
|
@@ -4687,7 +4713,7 @@ class SelectItemExpression extends Expression {
|
|
|
4687
4713
|
}
|
|
4688
4714
|
// `makeSQL` acts similarly to how select args are handled
|
|
4689
4715
|
makeSQL(ctx, quotedAs) {
|
|
4690
|
-
return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.
|
|
4716
|
+
return typeof this.item === "string" ? this.item === "*" ? selectAllSql(this.q, quotedAs) : columnToSql(ctx, this.q, this.q.shape, this.item, quotedAs, true) : this.item.toSQL(ctx, quotedAs);
|
|
4691
4717
|
}
|
|
4692
4718
|
}
|
|
4693
4719
|
|
|
@@ -4821,12 +4847,14 @@ function queryFrom(self, arg) {
|
|
|
4821
4847
|
data.batchParsers = q.q.batchParsers;
|
|
4822
4848
|
}
|
|
4823
4849
|
data.from = arg;
|
|
4850
|
+
data.selectAllColumns = data.selectAllKeys = void 0;
|
|
4824
4851
|
return self;
|
|
4825
4852
|
}
|
|
4826
4853
|
function queryFromSql(self, args) {
|
|
4827
4854
|
const data = self.q;
|
|
4828
4855
|
data.as || (data.as = "t");
|
|
4829
4856
|
data.from = sqlQueryArgsToExpression(args);
|
|
4857
|
+
data.selectAllColumns = data.selectAllKeys = void 0;
|
|
4830
4858
|
return self;
|
|
4831
4859
|
}
|
|
4832
4860
|
class FromMethods {
|
|
@@ -4941,11 +4969,11 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
|
|
|
4941
4969
|
if (item === "*") {
|
|
4942
4970
|
if (hookSelect) {
|
|
4943
4971
|
selected != null ? selected : selected = {};
|
|
4944
|
-
for (const key in
|
|
4972
|
+
for (const key in query.selectAllKeys || query.shape) {
|
|
4945
4973
|
selected[key] = quotedAs;
|
|
4946
4974
|
}
|
|
4947
4975
|
}
|
|
4948
|
-
sql = selectAllSql(
|
|
4976
|
+
sql = selectAllSql(query, quotedAs);
|
|
4949
4977
|
} else {
|
|
4950
4978
|
const index = item.indexOf(".");
|
|
4951
4979
|
if (index !== -1) {
|
|
@@ -5047,15 +5075,15 @@ const selectToSql = (ctx, table, query, quotedAs, hookSelect = query.hookSelect)
|
|
|
5047
5075
|
list.push(sql);
|
|
5048
5076
|
}
|
|
5049
5077
|
}
|
|
5050
|
-
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(
|
|
5078
|
+
return list.length ? list.join(", ") : query.select ? "" : selectAllSql(query, quotedAs);
|
|
5051
5079
|
};
|
|
5052
5080
|
function selectedObjectToSQL(ctx, quotedAs, item) {
|
|
5053
5081
|
const sql = item.toSQL(ctx, quotedAs);
|
|
5054
5082
|
return ctx.aliasValue ? `${sql} r` : sql;
|
|
5055
5083
|
}
|
|
5056
|
-
const selectAllSql = (
|
|
5084
|
+
const selectAllSql = (query, quotedAs) => {
|
|
5057
5085
|
var _a, _b, _c;
|
|
5058
|
-
return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b =
|
|
5086
|
+
return ((_a = query.join) == null ? void 0 : _a.length) ? ((_b = query.selectAllColumns) == null ? void 0 : _b.map((item) => `${quotedAs}.${item}`).join(", ")) || `${quotedAs}.*` : ((_c = query.selectAllColumns) == null ? void 0 : _c.join(", ")) || "*";
|
|
5059
5087
|
};
|
|
5060
5088
|
const pushSubQuerySql = (ctx, query, as, list, quotedAs) => {
|
|
5061
5089
|
var _a, _b, _c;
|
|
@@ -5249,7 +5277,7 @@ const pushWithSql = (ctx, items) => {
|
|
|
5249
5277
|
|
|
5250
5278
|
const checkIfASimpleQuery = (q) => {
|
|
5251
5279
|
var _a, _b;
|
|
5252
|
-
if (q.q.returnType && q.q.returnType !== "all" || q.
|
|
5280
|
+
if (q.q.returnType && q.q.returnType !== "all" || q.q.selectAllColumns || ((_a = q.q.and) == null ? void 0 : _a.length) || ((_b = q.q.or) == null ? void 0 : _b.length) || q.q.scopes)
|
|
5253
5281
|
return false;
|
|
5254
5282
|
const keys = Object.keys(q.q);
|
|
5255
5283
|
return !keys.some((key) => queryKeysOfNotSimpleQuery.includes(key));
|
|
@@ -5277,7 +5305,7 @@ const pushFromAndAs = (ctx, table, data, quotedAs) => {
|
|
|
5277
5305
|
const from = getFrom(ctx, table, data, quotedAs);
|
|
5278
5306
|
sql += from;
|
|
5279
5307
|
if (data.as && quotedAs && quotedAs !== from) {
|
|
5280
|
-
sql += `
|
|
5308
|
+
sql += ` ${quotedAs}`;
|
|
5281
5309
|
}
|
|
5282
5310
|
for (const as in data.sources) {
|
|
5283
5311
|
const source = data.sources[as];
|
|
@@ -9411,16 +9439,17 @@ var __spreadValues$5 = (a, b) => {
|
|
|
9411
9439
|
}
|
|
9412
9440
|
return a;
|
|
9413
9441
|
};
|
|
9414
|
-
const mergableObjects =
|
|
9415
|
-
shape
|
|
9416
|
-
withShapes
|
|
9417
|
-
parsers
|
|
9418
|
-
defaults
|
|
9419
|
-
joinedShapes
|
|
9420
|
-
joinedParsers
|
|
9421
|
-
joinedBatchParsers
|
|
9422
|
-
selectedComputeds
|
|
9423
|
-
|
|
9442
|
+
const mergableObjects = /* @__PURE__ */ new Set([
|
|
9443
|
+
"shape",
|
|
9444
|
+
"withShapes",
|
|
9445
|
+
"parsers",
|
|
9446
|
+
"defaults",
|
|
9447
|
+
"joinedShapes",
|
|
9448
|
+
"joinedParsers",
|
|
9449
|
+
"joinedBatchParsers",
|
|
9450
|
+
"selectedComputeds"
|
|
9451
|
+
]);
|
|
9452
|
+
const dontMergeArrays = /* @__PURE__ */ new Set(["selectAllColumns", "selectAllKeys"]);
|
|
9424
9453
|
class MergeQueryMethods {
|
|
9425
9454
|
merge(q) {
|
|
9426
9455
|
const query = this.clone();
|
|
@@ -9436,8 +9465,10 @@ class MergeQueryMethods {
|
|
|
9436
9465
|
break;
|
|
9437
9466
|
case "object":
|
|
9438
9467
|
if (Array.isArray(value)) {
|
|
9439
|
-
|
|
9440
|
-
|
|
9468
|
+
if (!dontMergeArrays.has(key)) {
|
|
9469
|
+
a[key] = a[key] ? [...a[key], ...value] : value;
|
|
9470
|
+
}
|
|
9471
|
+
} else if (mergableObjects.has(key)) {
|
|
9441
9472
|
a[key] = a[key] ? __spreadValues$5(__spreadValues$5({}, a[key]), value) : value;
|
|
9442
9473
|
} else if (key === "union") {
|
|
9443
9474
|
a[key] = a[key] ? {
|
|
@@ -11357,7 +11388,11 @@ class QueryUpsertOrCreate {
|
|
|
11357
11388
|
if (!isObjectEmpty(updateData)) {
|
|
11358
11389
|
_queryUpdate(q, updateData);
|
|
11359
11390
|
}
|
|
11360
|
-
|
|
11391
|
+
const c = orCreate(q, data.create, updateData, mergeData);
|
|
11392
|
+
if (!c.q.select) {
|
|
11393
|
+
c.q.returnType = "void";
|
|
11394
|
+
}
|
|
11395
|
+
return c;
|
|
11361
11396
|
}
|
|
11362
11397
|
/**
|
|
11363
11398
|
* `orCreate` creates a record only if it was not found by conditions.
|
|
@@ -12658,7 +12693,7 @@ class Db {
|
|
|
12658
12693
|
const parsers = {};
|
|
12659
12694
|
let hasParsers = false;
|
|
12660
12695
|
let modifyQuery = void 0;
|
|
12661
|
-
let
|
|
12696
|
+
let prepareSelectAll = false;
|
|
12662
12697
|
const { snakeCase } = options;
|
|
12663
12698
|
for (const key in shape) {
|
|
12664
12699
|
const column = shape[key];
|
|
@@ -12668,14 +12703,17 @@ class Db {
|
|
|
12668
12703
|
parsers[key] = column.parseFn;
|
|
12669
12704
|
}
|
|
12670
12705
|
if (column.data.name) {
|
|
12671
|
-
|
|
12706
|
+
prepareSelectAll = true;
|
|
12672
12707
|
} else if (snakeCase) {
|
|
12673
12708
|
const snakeName = toSnakeCase(key);
|
|
12674
12709
|
if (snakeName !== key) {
|
|
12675
|
-
|
|
12710
|
+
prepareSelectAll = true;
|
|
12676
12711
|
column.data.name = snakeName;
|
|
12677
12712
|
}
|
|
12678
12713
|
}
|
|
12714
|
+
if (column.data.explicitSelect) {
|
|
12715
|
+
prepareSelectAll = true;
|
|
12716
|
+
}
|
|
12679
12717
|
const { modifyQuery: mq } = column.data;
|
|
12680
12718
|
if (mq) {
|
|
12681
12719
|
modifyQuery = pushOrNewArray(modifyQuery, (q) => mq(q, column));
|
|
@@ -12695,17 +12733,6 @@ class Db {
|
|
|
12695
12733
|
}
|
|
12696
12734
|
}
|
|
12697
12735
|
}
|
|
12698
|
-
if (hasCustomName) {
|
|
12699
|
-
const list = [];
|
|
12700
|
-
for (const key in shape) {
|
|
12701
|
-
const column = shape[key];
|
|
12702
|
-
list.push(
|
|
12703
|
-
column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
|
|
12704
|
-
);
|
|
12705
|
-
}
|
|
12706
|
-
this.internal.columnsForSelectAll = list;
|
|
12707
|
-
this.internal.columnsKeysForSelectAll = __spreadValues({}, shape);
|
|
12708
|
-
}
|
|
12709
12736
|
this.q = {
|
|
12710
12737
|
adapter,
|
|
12711
12738
|
shape,
|
|
@@ -12738,21 +12765,22 @@ class Db {
|
|
|
12738
12765
|
const columns = Object.keys(
|
|
12739
12766
|
shape
|
|
12740
12767
|
);
|
|
12741
|
-
const { toSQL } = this;
|
|
12742
12768
|
this.columns = columns;
|
|
12743
|
-
this.defaultSelectColumns = columns.filter(
|
|
12744
|
-
(column) => !shape[column].data.isHidden
|
|
12745
|
-
);
|
|
12746
12769
|
if (options.computed)
|
|
12747
12770
|
applyComputedColumns(this, options.computed);
|
|
12748
|
-
|
|
12749
|
-
|
|
12750
|
-
const
|
|
12751
|
-
|
|
12752
|
-
|
|
12753
|
-
|
|
12754
|
-
|
|
12755
|
-
|
|
12771
|
+
if (prepareSelectAll) {
|
|
12772
|
+
const list = [];
|
|
12773
|
+
for (const key in shape) {
|
|
12774
|
+
const column = shape[key];
|
|
12775
|
+
if (!column.data.explicitSelect) {
|
|
12776
|
+
list.push(
|
|
12777
|
+
column.data.name ? `"${column.data.name}" AS "${key}"` : `"${key}"`
|
|
12778
|
+
);
|
|
12779
|
+
}
|
|
12780
|
+
}
|
|
12781
|
+
this.q.selectAllColumns = list;
|
|
12782
|
+
this.q.selectAllKeys = __spreadValues({}, shape);
|
|
12783
|
+
}
|
|
12756
12784
|
if (modifyQuery) {
|
|
12757
12785
|
for (const cb of modifyQuery) {
|
|
12758
12786
|
cb(this);
|