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.mjs
CHANGED
|
@@ -3478,6 +3478,11 @@ var NestedSqlSessionError = class extends OrchidOrmInternalError {
|
|
|
3478
3478
|
super(query, "Cannot nest SQL session scopes. Outer scope already has role or setConfig defined.");
|
|
3479
3479
|
}
|
|
3480
3480
|
};
|
|
3481
|
+
var CannotMutateReadOnlyTableError = class extends OrchidOrmInternalError {
|
|
3482
|
+
constructor(query) {
|
|
3483
|
+
super(query, "Cannot mutate a readonly table.");
|
|
3484
|
+
}
|
|
3485
|
+
};
|
|
3481
3486
|
const escape = (value, migration, nested) => {
|
|
3482
3487
|
const type = typeof value;
|
|
3483
3488
|
if (type === "number" || type === "bigint") return String(value);
|
|
@@ -5160,6 +5165,9 @@ const throwIfNoWhere = (q, method) => {
|
|
|
5160
5165
|
const throwIfJoinLateral = (q, method) => {
|
|
5161
5166
|
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}`);
|
|
5162
5167
|
};
|
|
5168
|
+
const throwIfReadOnly = (query) => {
|
|
5169
|
+
if (query.internal.readOnly) throw new CannotMutateReadOnlyTableError(query);
|
|
5170
|
+
};
|
|
5163
5171
|
const throwOnReadOnlyUpdate = (query, column, key) => {
|
|
5164
5172
|
if (column.data.appReadOnly || column.data.readOnly) throw new OrchidOrmInternalError(query, "Trying to update a readonly column", { column: key });
|
|
5165
5173
|
};
|
|
@@ -6740,17 +6748,27 @@ const _get = (query, returnType, arg) => {
|
|
|
6740
6748
|
if (q.returning) q.returning = void 0;
|
|
6741
6749
|
q.returnType = returnType;
|
|
6742
6750
|
let type;
|
|
6743
|
-
|
|
6744
|
-
|
|
6745
|
-
|
|
6751
|
+
let value = arg;
|
|
6752
|
+
if (typeof value === "function") {
|
|
6753
|
+
const item = processSelectAsArg(query, getQueryAs(query), "value", value);
|
|
6754
|
+
if (item !== false) value = item;
|
|
6755
|
+
}
|
|
6756
|
+
if (typeof value === "string") {
|
|
6757
|
+
const joinedAs = q.valuesJoinedAs?.[value];
|
|
6758
|
+
type = joinedAs ? q.joinedShapes?.[joinedAs]?.value : _getSelectableColumn(query, value);
|
|
6746
6759
|
q.getColumn = type;
|
|
6747
|
-
const selected = setParserForSelectedString(query, joinedAs ? joinedAs + "." +
|
|
6760
|
+
const selected = setParserForSelectedString(query, joinedAs ? joinedAs + "." + value : value, getQueryAs(query), getValueKey);
|
|
6748
6761
|
q.select = selected ? [q.expr = new SelectItemExpression(query, selected, type)] : void 0;
|
|
6749
|
-
} else {
|
|
6750
|
-
type =
|
|
6762
|
+
} else if (isExpression(value)) {
|
|
6763
|
+
type = value.result.value;
|
|
6751
6764
|
q.getColumn = type;
|
|
6752
|
-
addParserForRawExpression(query, getValueKey,
|
|
6753
|
-
q.select = [q.expr =
|
|
6765
|
+
addParserForRawExpression(query, getValueKey, value);
|
|
6766
|
+
q.select = [q.expr = value];
|
|
6767
|
+
} else {
|
|
6768
|
+
const selected = value;
|
|
6769
|
+
q.getColumn = selected.q.getColumn;
|
|
6770
|
+
if (q.getColumn) addColumnParserToQuery(q, getValueKey, q.getColumn);
|
|
6771
|
+
q.select = selected ? [{ selectAs: { value: selected } }] : void 0;
|
|
6754
6772
|
}
|
|
6755
6773
|
return setQueryOperators(query, type?.operators || Operators.any);
|
|
6756
6774
|
};
|
|
@@ -8447,6 +8465,7 @@ var AggregateMethods = class {
|
|
|
8447
8465
|
* @param data - optionally passed custom data when creating a single record.
|
|
8448
8466
|
*/
|
|
8449
8467
|
const insertFrom = (query, from, many, queryMany, data) => {
|
|
8468
|
+
throwIfReadOnly(query);
|
|
8450
8469
|
const ctx = createCtx();
|
|
8451
8470
|
const obj = data && (Array.isArray(data) ? handleManyData(query, data, ctx) : handleOneData(query, data, ctx));
|
|
8452
8471
|
return insert(query, {
|
|
@@ -8902,17 +8921,21 @@ const insert = (self, { columns, insertFrom, values }, many, queryMany) => {
|
|
|
8902
8921
|
return self;
|
|
8903
8922
|
};
|
|
8904
8923
|
const _queryCreate = (q, data) => {
|
|
8924
|
+
throwIfReadOnly(q);
|
|
8905
8925
|
createSelect(q);
|
|
8906
8926
|
return _queryInsert(q, data);
|
|
8907
8927
|
};
|
|
8908
8928
|
const _queryInsert = (query, data) => {
|
|
8929
|
+
throwIfReadOnly(query);
|
|
8909
8930
|
return insert(query, handleOneData(query, data, createCtx()));
|
|
8910
8931
|
};
|
|
8911
8932
|
const _queryCreateMany = (q, data) => {
|
|
8933
|
+
throwIfReadOnly(q);
|
|
8912
8934
|
createSelect(q);
|
|
8913
8935
|
return _queryInsertMany(q, data);
|
|
8914
8936
|
};
|
|
8915
8937
|
const _queryInsertMany = (q, data) => {
|
|
8938
|
+
throwIfReadOnly(q);
|
|
8916
8939
|
let result = insert(q, handleManyData(q, data, createCtx()), true);
|
|
8917
8940
|
if (!data.length) result = result.none();
|
|
8918
8941
|
return result;
|
|
@@ -10865,44 +10888,49 @@ const collectNestedSelectBatches = (batches, rows, path, last) => {
|
|
|
10865
10888
|
};
|
|
10866
10889
|
const emptyArrSQL = new RawSql("'[]'");
|
|
10867
10890
|
const processSelectArg = (q, as, arg, columnAs) => {
|
|
10868
|
-
const query = q;
|
|
10869
10891
|
if (typeof arg === "string") return setParserForSelectedString(q, arg, as, columnAs);
|
|
10870
10892
|
const selectAs = {};
|
|
10871
10893
|
for (const key in arg) {
|
|
10872
|
-
|
|
10873
|
-
|
|
10874
|
-
|
|
10875
|
-
|
|
10876
|
-
|
|
10877
|
-
|
|
10878
|
-
|
|
10879
|
-
|
|
10880
|
-
|
|
10881
|
-
|
|
10882
|
-
|
|
10883
|
-
|
|
10884
|
-
|
|
10885
|
-
|
|
10886
|
-
|
|
10887
|
-
|
|
10888
|
-
|
|
10889
|
-
|
|
10890
|
-
|
|
10891
|
-
|
|
10892
|
-
|
|
10893
|
-
|
|
10894
|
-
|
|
10895
|
-
|
|
10896
|
-
|
|
10897
|
-
|
|
10898
|
-
|
|
10899
|
-
|
|
10900
|
-
value
|
|
10894
|
+
const item = processSelectAsArg(q, as, key, arg[key]);
|
|
10895
|
+
if (item === false) return false;
|
|
10896
|
+
selectAs[key] = item;
|
|
10897
|
+
}
|
|
10898
|
+
return { selectAs };
|
|
10899
|
+
};
|
|
10900
|
+
const processSelectAsArg = (q, as, key, arg) => {
|
|
10901
|
+
const query = q;
|
|
10902
|
+
let value = arg;
|
|
10903
|
+
let joinQuery;
|
|
10904
|
+
if (typeof value === "function") {
|
|
10905
|
+
value = resolveSubQueryCallback(q, value);
|
|
10906
|
+
if (isQueryNone(value)) {
|
|
10907
|
+
if (value.q.innerJoinLateral) return false;
|
|
10908
|
+
}
|
|
10909
|
+
if (!isExpression(value)) {
|
|
10910
|
+
if (isRelationQuery(value) && value.q.subQuery !== 1) {
|
|
10911
|
+
joinQuery = true;
|
|
10912
|
+
setSelectRelation(query.q);
|
|
10913
|
+
value = value.joinQuery(value, q);
|
|
10914
|
+
let subQuery;
|
|
10915
|
+
const { returnType, innerJoinLateral } = value.q;
|
|
10916
|
+
if (!returnType || returnType === "all") {
|
|
10917
|
+
subQuery = value.json(false);
|
|
10918
|
+
if (!innerJoinLateral) value.q.coalesceValue = emptyArrSQL;
|
|
10919
|
+
} else if (returnType === "pluck") {
|
|
10920
|
+
subQuery = value.q.select ? value.wrap(cloneQueryBaseUnscoped(value)).jsonAgg(value.q.select[0]) : value.json(false);
|
|
10921
|
+
value.q.coalesceValue = emptyArrSQL;
|
|
10922
|
+
} else if (returnType === "value" || returnType === "valueOrThrow") if (value.q.select) {
|
|
10923
|
+
if (typeof value.q.select[0] === "string") value.q.select[0] = { selectAs: { r: value.q.select[0] } };
|
|
10924
|
+
subQuery = value;
|
|
10925
|
+
} else subQuery = value.json(false);
|
|
10926
|
+
else subQuery = value;
|
|
10927
|
+
const as = _joinLateral(q, innerJoinLateral || query.q.returnType === "valueOrThrow" ? "JOIN" : "LEFT JOIN", subQuery, key, innerJoinLateral && returnType !== "one" && returnType !== "oneOrThrow");
|
|
10928
|
+
if (as) value.q.joinedForSelect = _copyQueryAliasToQuery(value, q, as);
|
|
10901
10929
|
}
|
|
10930
|
+
value = prepareSubQueryForSql(q, value);
|
|
10902
10931
|
}
|
|
10903
|
-
selectAs[key] = addParserForSelectItem(q, as, key, value, key, joinQuery);
|
|
10904
10932
|
}
|
|
10905
|
-
return
|
|
10933
|
+
return addParserForSelectItem(query, as, key, value, key, joinQuery);
|
|
10906
10934
|
};
|
|
10907
10935
|
const setParserForSelectedString = (query, arg, as, columnAs, columnAlias) => {
|
|
10908
10936
|
const { q } = query;
|
|
@@ -11256,6 +11284,7 @@ var QueryGet = class {
|
|
|
11256
11284
|
}
|
|
11257
11285
|
};
|
|
11258
11286
|
const _queryDelete = (query) => {
|
|
11287
|
+
throwIfReadOnly(query);
|
|
11259
11288
|
const q = query.q;
|
|
11260
11289
|
if (!q.select) {
|
|
11261
11290
|
if (q.returnType === "oneOrThrow" || q.returnType === "valueOrThrow") q.throwOnNotFound = true;
|
|
@@ -11439,6 +11468,7 @@ var RefExpression = class extends Expression {
|
|
|
11439
11468
|
};
|
|
11440
11469
|
const _queryUpdateMany = (self, primaryKeys, data, strict) => {
|
|
11441
11470
|
const query = self;
|
|
11471
|
+
throwIfReadOnly(query);
|
|
11442
11472
|
const { q } = query;
|
|
11443
11473
|
const { shape } = q;
|
|
11444
11474
|
q.type = "update";
|
|
@@ -11458,6 +11488,7 @@ const _queryUpdateMany = (self, primaryKeys, data, strict) => {
|
|
|
11458
11488
|
return query;
|
|
11459
11489
|
};
|
|
11460
11490
|
const _queryChangeCounter = (self, op, data) => {
|
|
11491
|
+
throwIfReadOnly(self);
|
|
11461
11492
|
const q = self.q;
|
|
11462
11493
|
q.type = "update";
|
|
11463
11494
|
if (!q.select) {
|
|
@@ -11490,6 +11521,7 @@ const _queryChangeCounter = (self, op, data) => {
|
|
|
11490
11521
|
};
|
|
11491
11522
|
const _queryUpdate = (updateSelf, arg) => {
|
|
11492
11523
|
const query = updateSelf;
|
|
11524
|
+
throwIfReadOnly(query);
|
|
11493
11525
|
const { q } = query;
|
|
11494
11526
|
q.type = "update";
|
|
11495
11527
|
const set = { ...arg };
|
|
@@ -11817,6 +11849,7 @@ var QueryUpdate = class {
|
|
|
11817
11849
|
*/
|
|
11818
11850
|
updateFrom(arg, ...args) {
|
|
11819
11851
|
const q = _clone(this);
|
|
11852
|
+
throwIfReadOnly(q);
|
|
11820
11853
|
const joinArgs = _joinReturningArgs(q, true, arg, args, true);
|
|
11821
11854
|
if (!joinArgs) return _queryNone(q);
|
|
11822
11855
|
joinArgs.u = true;
|
|
@@ -12808,6 +12841,28 @@ var Having = class {
|
|
|
12808
12841
|
return pushQueryValueImmutable(_clone(this), "having", args);
|
|
12809
12842
|
}
|
|
12810
12843
|
};
|
|
12844
|
+
var QueryPluck = class {
|
|
12845
|
+
/**
|
|
12846
|
+
* `.pluck` returns a single array of a single selected column values:
|
|
12847
|
+
*
|
|
12848
|
+
* ```ts
|
|
12849
|
+
* const ids = await db.table.select('id').pluck();
|
|
12850
|
+
* // ids are an array of all users' id like [1, 2, 3]
|
|
12851
|
+
* ```
|
|
12852
|
+
* @param select - column name or a raw SQL
|
|
12853
|
+
*/
|
|
12854
|
+
pluck(select) {
|
|
12855
|
+
const q = _clone(this);
|
|
12856
|
+
q.q.returnType = "pluck";
|
|
12857
|
+
let selected;
|
|
12858
|
+
if (typeof select === "function") {
|
|
12859
|
+
const item = processSelectAsArg(q, q.q.as || q.table, "pluck", select);
|
|
12860
|
+
if (item !== false) selected = isExpression(item) ? item : { selectAs: { pluck: item } };
|
|
12861
|
+
} else selected = addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
|
|
12862
|
+
q.q.select = selected ? [selected] : void 0;
|
|
12863
|
+
return q;
|
|
12864
|
+
}
|
|
12865
|
+
};
|
|
12811
12866
|
var QueryMap = class {
|
|
12812
12867
|
/**
|
|
12813
12868
|
* Use `map` to transform individual records of a query result.
|
|
@@ -13063,6 +13118,7 @@ var QueryTruncate = class {
|
|
|
13063
13118
|
* @param options - truncate options, may have `cascade: true` and `restartIdentity: true`
|
|
13064
13119
|
*/
|
|
13065
13120
|
truncate(options) {
|
|
13121
|
+
throwIfReadOnly(this);
|
|
13066
13122
|
const query = Object.create(_clone(this));
|
|
13067
13123
|
query.toSQL = () => makeTruncateSql(query, options);
|
|
13068
13124
|
return _queryExec(query);
|
|
@@ -13162,22 +13218,6 @@ var QueryMethods = class {
|
|
|
13162
13218
|
return _queryRows(_clone(this));
|
|
13163
13219
|
}
|
|
13164
13220
|
/**
|
|
13165
|
-
* `.pluck` returns a single array of a single selected column values:
|
|
13166
|
-
*
|
|
13167
|
-
* ```ts
|
|
13168
|
-
* const ids = await db.table.select('id').pluck();
|
|
13169
|
-
* // ids are an array of all users' id like [1, 2, 3]
|
|
13170
|
-
* ```
|
|
13171
|
-
* @param select - column name or a raw SQL
|
|
13172
|
-
*/
|
|
13173
|
-
pluck(select) {
|
|
13174
|
-
const q = _clone(this);
|
|
13175
|
-
q.q.returnType = "pluck";
|
|
13176
|
-
const selected = addParserForSelectItem(q, q.q.as || q.table, "pluck", select);
|
|
13177
|
-
q.q.select = selected ? [selected] : void 0;
|
|
13178
|
-
return q;
|
|
13179
|
-
}
|
|
13180
|
-
/**
|
|
13181
13221
|
* `.exec` won't parse the response at all, and returns undefined:
|
|
13182
13222
|
*
|
|
13183
13223
|
* ```ts
|
|
@@ -13632,6 +13672,7 @@ applyMixins(QueryMethods, [
|
|
|
13632
13672
|
QueryOrCreate,
|
|
13633
13673
|
QueryHooks,
|
|
13634
13674
|
QueryGet,
|
|
13675
|
+
QueryPluck,
|
|
13635
13676
|
MergeQueryMethods,
|
|
13636
13677
|
QuerySql,
|
|
13637
13678
|
QueryTransform,
|
|
@@ -13741,6 +13782,7 @@ var Db = class extends QueryMethods {
|
|
|
13741
13782
|
snakeCase: options.snakeCase,
|
|
13742
13783
|
noPrimaryKey: options.noPrimaryKey === "ignore",
|
|
13743
13784
|
comment: options.comment,
|
|
13785
|
+
readOnly: options.readOnly,
|
|
13744
13786
|
nowSQL: options.nowSQL,
|
|
13745
13787
|
tableData,
|
|
13746
13788
|
selectAllCount
|