pqb 0.67.0 → 0.67.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.d.ts +229 -201
- package/dist/index.js +98 -56
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +98 -56
- package/dist/index.mjs.map +1 -1
- package/dist/internal.d.ts +9490 -2
- package/dist/internal.js +17 -0
- package/dist/internal.js.map +1 -1
- package/dist/internal.mjs +17 -1
- package/dist/internal.mjs.map +1 -1
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -3501,6 +3501,11 @@ var NestedSqlSessionError = class extends OrchidOrmInternalError {
|
|
|
3501
3501
|
super(query, "Cannot nest SQL session scopes. Outer scope already has role or setConfig defined.");
|
|
3502
3502
|
}
|
|
3503
3503
|
};
|
|
3504
|
+
var CannotMutateReadOnlyTableError = class extends OrchidOrmInternalError {
|
|
3505
|
+
constructor(query) {
|
|
3506
|
+
super(query, "Cannot mutate a readonly table.");
|
|
3507
|
+
}
|
|
3508
|
+
};
|
|
3504
3509
|
const escape = (value, migration, nested) => {
|
|
3505
3510
|
const type = typeof value;
|
|
3506
3511
|
if (type === "number" || type === "bigint") return String(value);
|
|
@@ -5183,6 +5188,9 @@ const throwIfNoWhere = (q, method) => {
|
|
|
5183
5188
|
const throwIfJoinLateral = (q, method) => {
|
|
5184
5189
|
if (q.q.join?.some((x) => Array.isArray(x) || "s" in x.args && x.args.s)) throw new OrchidOrmInternalError(q, `Cannot join a complex query in ${method}`);
|
|
5185
5190
|
};
|
|
5191
|
+
const throwIfReadOnly = (query) => {
|
|
5192
|
+
if (query.internal.readOnly) throw new CannotMutateReadOnlyTableError(query);
|
|
5193
|
+
};
|
|
5186
5194
|
const throwOnReadOnlyUpdate = (query, column, key) => {
|
|
5187
5195
|
if (column.data.appReadOnly || column.data.readOnly) throw new OrchidOrmInternalError(query, "Trying to update a readonly column", { column: key });
|
|
5188
5196
|
};
|
|
@@ -6763,17 +6771,27 @@ const _get = (query, returnType, arg) => {
|
|
|
6763
6771
|
if (q.returning) q.returning = void 0;
|
|
6764
6772
|
q.returnType = returnType;
|
|
6765
6773
|
let type;
|
|
6766
|
-
|
|
6767
|
-
|
|
6768
|
-
|
|
6774
|
+
let value = arg;
|
|
6775
|
+
if (typeof value === "function") {
|
|
6776
|
+
const item = processSelectAsArg(query, getQueryAs(query), "value", value);
|
|
6777
|
+
if (item !== false) value = item;
|
|
6778
|
+
}
|
|
6779
|
+
if (typeof value === "string") {
|
|
6780
|
+
const joinedAs = q.valuesJoinedAs?.[value];
|
|
6781
|
+
type = joinedAs ? q.joinedShapes?.[joinedAs]?.value : _getSelectableColumn(query, value);
|
|
6769
6782
|
q.getColumn = type;
|
|
6770
|
-
const selected = setParserForSelectedString(query, joinedAs ? joinedAs + "." +
|
|
6783
|
+
const selected = setParserForSelectedString(query, joinedAs ? joinedAs + "." + value : value, getQueryAs(query), getValueKey);
|
|
6771
6784
|
q.select = selected ? [q.expr = new SelectItemExpression(query, selected, type)] : void 0;
|
|
6772
|
-
} else {
|
|
6773
|
-
type =
|
|
6785
|
+
} else if (isExpression(value)) {
|
|
6786
|
+
type = value.result.value;
|
|
6774
6787
|
q.getColumn = type;
|
|
6775
|
-
addParserForRawExpression(query, getValueKey,
|
|
6776
|
-
q.select = [q.expr =
|
|
6788
|
+
addParserForRawExpression(query, getValueKey, value);
|
|
6789
|
+
q.select = [q.expr = value];
|
|
6790
|
+
} else {
|
|
6791
|
+
const selected = value;
|
|
6792
|
+
q.getColumn = selected.q.getColumn;
|
|
6793
|
+
if (q.getColumn) addColumnParserToQuery(q, getValueKey, q.getColumn);
|
|
6794
|
+
q.select = selected ? [{ selectAs: { value: selected } }] : void 0;
|
|
6777
6795
|
}
|
|
6778
6796
|
return setQueryOperators(query, type?.operators || Operators.any);
|
|
6779
6797
|
};
|
|
@@ -8470,6 +8488,7 @@ var AggregateMethods = class {
|
|
|
8470
8488
|
* @param data - optionally passed custom data when creating a single record.
|
|
8471
8489
|
*/
|
|
8472
8490
|
const insertFrom = (query, from, many, queryMany, data) => {
|
|
8491
|
+
throwIfReadOnly(query);
|
|
8473
8492
|
const ctx = createCtx();
|
|
8474
8493
|
const obj = data && (Array.isArray(data) ? handleManyData(query, data, ctx) : handleOneData(query, data, ctx));
|
|
8475
8494
|
return insert(query, {
|
|
@@ -8925,17 +8944,21 @@ const insert = (self, { columns, insertFrom, values }, many, queryMany) => {
|
|
|
8925
8944
|
return self;
|
|
8926
8945
|
};
|
|
8927
8946
|
const _queryCreate = (q, data) => {
|
|
8947
|
+
throwIfReadOnly(q);
|
|
8928
8948
|
createSelect(q);
|
|
8929
8949
|
return _queryInsert(q, data);
|
|
8930
8950
|
};
|
|
8931
8951
|
const _queryInsert = (query, data) => {
|
|
8952
|
+
throwIfReadOnly(query);
|
|
8932
8953
|
return insert(query, handleOneData(query, data, createCtx()));
|
|
8933
8954
|
};
|
|
8934
8955
|
const _queryCreateMany = (q, data) => {
|
|
8956
|
+
throwIfReadOnly(q);
|
|
8935
8957
|
createSelect(q);
|
|
8936
8958
|
return _queryInsertMany(q, data);
|
|
8937
8959
|
};
|
|
8938
8960
|
const _queryInsertMany = (q, data) => {
|
|
8961
|
+
throwIfReadOnly(q);
|
|
8939
8962
|
let result = insert(q, handleManyData(q, data, createCtx()), true);
|
|
8940
8963
|
if (!data.length) result = result.none();
|
|
8941
8964
|
return result;
|
|
@@ -10888,44 +10911,49 @@ const collectNestedSelectBatches = (batches, rows, path, last) => {
|
|
|
10888
10911
|
};
|
|
10889
10912
|
const emptyArrSQL = new RawSql("'[]'");
|
|
10890
10913
|
const processSelectArg = (q, as, arg, columnAs) => {
|
|
10891
|
-
const query = q;
|
|
10892
10914
|
if (typeof arg === "string") return setParserForSelectedString(q, arg, as, columnAs);
|
|
10893
10915
|
const selectAs = {};
|
|
10894
10916
|
for (const key in arg) {
|
|
10895
|
-
|
|
10896
|
-
|
|
10897
|
-
|
|
10898
|
-
|
|
10899
|
-
|
|
10900
|
-
|
|
10901
|
-
|
|
10902
|
-
|
|
10903
|
-
|
|
10904
|
-
|
|
10905
|
-
|
|
10906
|
-
|
|
10907
|
-
|
|
10908
|
-
|
|
10909
|
-
|
|
10910
|
-
|
|
10911
|
-
|
|
10912
|
-
|
|
10913
|
-
|
|
10914
|
-
|
|
10915
|
-
|
|
10916
|
-
|
|
10917
|
-
|
|
10918
|
-
|
|
10919
|
-
|
|
10920
|
-
|
|
10921
|
-
|
|
10922
|
-
|
|
10923
|
-
value
|
|
10917
|
+
const item = processSelectAsArg(q, as, key, arg[key]);
|
|
10918
|
+
if (item === false) return false;
|
|
10919
|
+
selectAs[key] = item;
|
|
10920
|
+
}
|
|
10921
|
+
return { selectAs };
|
|
10922
|
+
};
|
|
10923
|
+
const processSelectAsArg = (q, as, key, arg) => {
|
|
10924
|
+
const query = q;
|
|
10925
|
+
let value = arg;
|
|
10926
|
+
let joinQuery;
|
|
10927
|
+
if (typeof value === "function") {
|
|
10928
|
+
value = resolveSubQueryCallback(q, value);
|
|
10929
|
+
if (isQueryNone(value)) {
|
|
10930
|
+
if (value.q.innerJoinLateral) return false;
|
|
10931
|
+
}
|
|
10932
|
+
if (!isExpression(value)) {
|
|
10933
|
+
if (isRelationQuery(value) && value.q.subQuery !== 1) {
|
|
10934
|
+
joinQuery = true;
|
|
10935
|
+
setSelectRelation(query.q);
|
|
10936
|
+
value = value.joinQuery(value, q);
|
|
10937
|
+
let subQuery;
|
|
10938
|
+
const { returnType, innerJoinLateral } = value.q;
|
|
10939
|
+
if (!returnType || returnType === "all") {
|
|
10940
|
+
subQuery = value.json(false);
|
|
10941
|
+
if (!innerJoinLateral) value.q.coalesceValue = emptyArrSQL;
|
|
10942
|
+
} else if (returnType === "pluck") {
|
|
10943
|
+
subQuery = value.q.select ? value.wrap(cloneQueryBaseUnscoped(value)).jsonAgg(value.q.select[0]) : value.json(false);
|
|
10944
|
+
value.q.coalesceValue = emptyArrSQL;
|
|
10945
|
+
} else if (returnType === "value" || returnType === "valueOrThrow") if (value.q.select) {
|
|
10946
|
+
if (typeof value.q.select[0] === "string") value.q.select[0] = { selectAs: { r: value.q.select[0] } };
|
|
10947
|
+
subQuery = value;
|
|
10948
|
+
} else subQuery = value.json(false);
|
|
10949
|
+
else subQuery = value;
|
|
10950
|
+
const as = _joinLateral(q, innerJoinLateral || query.q.returnType === "valueOrThrow" ? "JOIN" : "LEFT JOIN", subQuery, key, innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow");
|
|
10951
|
+
if (as) value.q.joinedForSelect = _copyQueryAliasToQuery(value, q, as);
|
|
10924
10952
|
}
|
|
10953
|
+
value = prepareSubQueryForSql(q, value);
|
|
10925
10954
|
}
|
|
10926
|
-
selectAs[key] = addParserForSelectItem(q, as, key, value, key, joinQuery);
|
|
10927
10955
|
}
|
|
10928
|
-
return
|
|
10956
|
+
return addParserForSelectItem(query, as, key, value, key, joinQuery);
|
|
10929
10957
|
};
|
|
10930
10958
|
const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
|
|
10931
10959
|
const { q } = query;
|
|
@@ -11279,6 +11307,7 @@ var QueryGet = class {
|
|
|
11279
11307
|
}
|
|
11280
11308
|
};
|
|
11281
11309
|
const _queryDelete = (query) => {
|
|
11310
|
+
throwIfReadOnly(query);
|
|
11282
11311
|
const q = query.q;
|
|
11283
11312
|
if (!q.select) {
|
|
11284
11313
|
if (q.returnType === "oneOrThrow" || q.returnType === "valueOrThrow") q.throwOnNotFound = true;
|
|
@@ -11462,6 +11491,7 @@ var RefExpression = class extends Expression {
|
|
|
11462
11491
|
};
|
|
11463
11492
|
const _queryUpdateMany = (self, primaryKeys, data, strict) => {
|
|
11464
11493
|
const query = self;
|
|
11494
|
+
throwIfReadOnly(query);
|
|
11465
11495
|
const { q } = query;
|
|
11466
11496
|
const { shape } = q;
|
|
11467
11497
|
q.type = "update";
|
|
@@ -11481,6 +11511,7 @@ const _queryUpdateMany = (self, primaryKeys, data, strict) => {
|
|
|
11481
11511
|
return query;
|
|
11482
11512
|
};
|
|
11483
11513
|
const _queryChangeCounter = (self, op, data) => {
|
|
11514
|
+
throwIfReadOnly(self);
|
|
11484
11515
|
const q = self.q;
|
|
11485
11516
|
q.type = "update";
|
|
11486
11517
|
if (!q.select) {
|
|
@@ -11513,6 +11544,7 @@ const _queryChangeCounter = (self, op, data) => {
|
|
|
11513
11544
|
};
|
|
11514
11545
|
const _queryUpdate = (updateSelf, arg) => {
|
|
11515
11546
|
const query = updateSelf;
|
|
11547
|
+
throwIfReadOnly(query);
|
|
11516
11548
|
const { q } = query;
|
|
11517
11549
|
q.type = "update";
|
|
11518
11550
|
const set = { ...arg };
|
|
@@ -11840,6 +11872,7 @@ var QueryUpdate = class {
|
|
|
11840
11872
|
*/
|
|
11841
11873
|
updateFrom(arg, ...args) {
|
|
11842
11874
|
const q = _clone(this);
|
|
11875
|
+
throwIfReadOnly(q);
|
|
11843
11876
|
const joinArgs = _joinReturningArgs(q, true, arg, args, true);
|
|
11844
11877
|
if (!joinArgs) return _queryNone(q);
|
|
11845
11878
|
joinArgs.u = true;
|
|
@@ -12831,6 +12864,28 @@ var Having = class {
|
|
|
12831
12864
|
return pushQueryValueImmutable(_clone(this), "having", args);
|
|
12832
12865
|
}
|
|
12833
12866
|
};
|
|
12867
|
+
var QueryPluck = class {
|
|
12868
|
+
/**
|
|
12869
|
+
* `.pluck` returns a single array of a single selected column values:
|
|
12870
|
+
*
|
|
12871
|
+
* ```ts
|
|
12872
|
+
* const ids = await db.table.select('id').pluck();
|
|
12873
|
+
* // ids are an array of all users' id like [1, 2, 3]
|
|
12874
|
+
* ```
|
|
12875
|
+
* @param select - column name or a raw SQL
|
|
12876
|
+
*/
|
|
12877
|
+
pluck(select) {
|
|
12878
|
+
const q = _clone(this);
|
|
12879
|
+
q.q.returnType = "pluck";
|
|
12880
|
+
let selected;
|
|
12881
|
+
if (typeof select === "function") {
|
|
12882
|
+
const item = processSelectAsArg(q, q.q.as || q.table, "pluck", select);
|
|
12883
|
+
if (item !== false) selected = isExpression(item) ? item : { selectAs: { pluck: item } };
|
|
12884
|
+
} else selected = addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
|
|
12885
|
+
q.q.select = selected ? [selected] : void 0;
|
|
12886
|
+
return q;
|
|
12887
|
+
}
|
|
12888
|
+
};
|
|
12834
12889
|
var QueryMap = class {
|
|
12835
12890
|
/**
|
|
12836
12891
|
* Use `map` to transform individual records of a query result.
|
|
@@ -13086,6 +13141,7 @@ var QueryTruncate = class {
|
|
|
13086
13141
|
* @param options - truncate options, may have `cascade: true` and `restartIdentity: true`
|
|
13087
13142
|
*/
|
|
13088
13143
|
truncate(options) {
|
|
13144
|
+
throwIfReadOnly(this);
|
|
13089
13145
|
const query = Object.create(_clone(this));
|
|
13090
13146
|
query.toSQL = () => makeTruncateSql(query, options);
|
|
13091
13147
|
return _queryExec(query);
|
|
@@ -13185,22 +13241,6 @@ var QueryMethods = class {
|
|
|
13185
13241
|
return _queryRows(_clone(this));
|
|
13186
13242
|
}
|
|
13187
13243
|
/**
|
|
13188
|
-
* `.pluck` returns a single array of a single selected column values:
|
|
13189
|
-
*
|
|
13190
|
-
* ```ts
|
|
13191
|
-
* const ids = await db.table.select('id').pluck();
|
|
13192
|
-
* // ids are an array of all users' id like [1, 2, 3]
|
|
13193
|
-
* ```
|
|
13194
|
-
* @param select - column name or a raw SQL
|
|
13195
|
-
*/
|
|
13196
|
-
pluck(select) {
|
|
13197
|
-
const q = _clone(this);
|
|
13198
|
-
q.q.returnType = "pluck";
|
|
13199
|
-
const selected = addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
|
|
13200
|
-
q.q.select = selected ? [selected] : void 0;
|
|
13201
|
-
return q;
|
|
13202
|
-
}
|
|
13203
|
-
/**
|
|
13204
13244
|
* `.exec` won't parse the response at all, and returns undefined:
|
|
13205
13245
|
*
|
|
13206
13246
|
* ```ts
|
|
@@ -13655,6 +13695,7 @@ applyMixins(QueryMethods, [
|
|
|
13655
13695
|
QueryOrCreate,
|
|
13656
13696
|
QueryHooks,
|
|
13657
13697
|
QueryGet,
|
|
13698
|
+
QueryPluck,
|
|
13658
13699
|
MergeQueryMethods,
|
|
13659
13700
|
QuerySql,
|
|
13660
13701
|
QueryTransform,
|
|
@@ -13764,6 +13805,7 @@ var Db = class extends QueryMethods {
|
|
|
13764
13805
|
snakeCase: options.snakeCase,
|
|
13765
13806
|
noPrimaryKey: options.noPrimaryKey === "ignore",
|
|
13766
13807
|
comment: options.comment,
|
|
13808
|
+
readOnly: options.readOnly,
|
|
13767
13809
|
nowSQL: options.nowSQL,
|
|
13768
13810
|
tableData,
|
|
13769
13811
|
selectAllCount
|