pqb 0.0.7 → 0.0.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 +22 -17
- package/dist/index.esm.js +74 -23
- package/dist/index.esm.js.map +1 -1
- package/dist/index.js +77 -22
- package/dist/index.js.map +1 -1
- package/package.json +1 -1
- package/src/columnSchema/columnsSchema.ts +4 -0
- package/src/errors.ts +12 -2
- package/src/queryMethods/select.ts +3 -0
- package/src/queryMethods/where.test.ts +14 -2
- package/src/queryMethods/where.ts +3 -0
- package/src/relations.ts +1 -1
- package/src/sql/aggregate.ts +1 -4
- package/src/sql/having.ts +2 -8
- package/src/sql/insert.ts +2 -8
- package/src/sql/join.ts +19 -15
- package/src/sql/select.ts +36 -15
- package/src/sql/types.ts +6 -2
- package/src/sql/where.ts +9 -15
package/dist/index.js
CHANGED
|
@@ -1933,6 +1933,8 @@ class ArrayOfColumnsObjects extends ColumnType {
|
|
|
1933
1933
|
this.operators = Operators.any;
|
|
1934
1934
|
}
|
|
1935
1935
|
}
|
|
1936
|
+
class PluckResultColumnType extends ColumnType {
|
|
1937
|
+
}
|
|
1936
1938
|
class TableSchema {
|
|
1937
1939
|
constructor(shape) {
|
|
1938
1940
|
this.shape = shape;
|
|
@@ -2005,21 +2007,28 @@ const processJoinItem = (model, query, values, args, quotedAs) => {
|
|
|
2005
2007
|
const [first] = args;
|
|
2006
2008
|
if (typeof first === "string") {
|
|
2007
2009
|
if (first in model.relations) {
|
|
2008
|
-
const {
|
|
2009
|
-
|
|
2010
|
-
|
|
2011
|
-
|
|
2010
|
+
const {
|
|
2011
|
+
key,
|
|
2012
|
+
query: toQuery,
|
|
2013
|
+
joinQuery: joinQuery2
|
|
2014
|
+
} = model.relations[first];
|
|
2015
|
+
const joinedQuery = joinQuery2(model, toQuery);
|
|
2016
|
+
const joinedQueryData = joinedQuery.query;
|
|
2017
|
+
const table = typeof joinedQueryData.from === "string" ? joinedQueryData.from : joinedQuery.table;
|
|
2018
|
+
let target3 = quoteSchemaAndTable(joinedQueryData.schema, table);
|
|
2019
|
+
const as = joinedQueryData.as || key;
|
|
2012
2020
|
if (as !== table) {
|
|
2013
2021
|
target3 += ` AS ${q(as)}`;
|
|
2014
2022
|
}
|
|
2015
2023
|
const queryData = {
|
|
2024
|
+
as: joinedQuery.query.as,
|
|
2016
2025
|
and: [],
|
|
2017
2026
|
or: []
|
|
2018
2027
|
};
|
|
2019
|
-
if (
|
|
2020
|
-
queryData.and.push(...
|
|
2021
|
-
if (
|
|
2022
|
-
queryData.or.push(...
|
|
2028
|
+
if (joinedQueryData.and)
|
|
2029
|
+
queryData.and.push(...joinedQueryData.and);
|
|
2030
|
+
if (joinedQueryData.or)
|
|
2031
|
+
queryData.or.push(...joinedQueryData.or);
|
|
2023
2032
|
const arg = (_a = args[1]) == null ? void 0 : _a.call(
|
|
2024
2033
|
args,
|
|
2025
2034
|
new model.onQueryBuilder({ table: model.table, query }, args[0])
|
|
@@ -2032,7 +2041,7 @@ const processJoinItem = (model, query, values, args, quotedAs) => {
|
|
|
2032
2041
|
}
|
|
2033
2042
|
const joinAs2 = q(as);
|
|
2034
2043
|
const onConditions = whereToSql(
|
|
2035
|
-
|
|
2044
|
+
joinedQuery,
|
|
2036
2045
|
queryData,
|
|
2037
2046
|
values,
|
|
2038
2047
|
quotedAs,
|
|
@@ -2328,9 +2337,9 @@ const whereHandlers = {
|
|
|
2328
2337
|
const item = value;
|
|
2329
2338
|
const leftColumn = quoteFullColumn(
|
|
2330
2339
|
item.on[0],
|
|
2331
|
-
|
|
2340
|
+
getJoinItemSource(item.joinTo)
|
|
2332
2341
|
);
|
|
2333
|
-
const joinTo =
|
|
2342
|
+
const joinTo = getJoinItemSource(item.joinFrom);
|
|
2334
2343
|
const [op, rightColumn] = item.on.length === 2 ? ["=", quoteFullColumn(item.on[1], joinTo)] : [item.on[1], quoteFullColumn(item.on[2], joinTo)];
|
|
2335
2344
|
ands.push(`${prefix}${leftColumn} ${op} ${rightColumn}`);
|
|
2336
2345
|
}
|
|
@@ -2356,6 +2365,9 @@ const whereHandlers = {
|
|
|
2356
2365
|
});
|
|
2357
2366
|
}
|
|
2358
2367
|
};
|
|
2368
|
+
const getJoinItemSource = (joinItem) => {
|
|
2369
|
+
return typeof joinItem === "string" ? q(joinItem) : q(getQueryAs(joinItem));
|
|
2370
|
+
};
|
|
2359
2371
|
const pushIn = (ands, prefix, quotedAs, values, arg) => {
|
|
2360
2372
|
let value;
|
|
2361
2373
|
if (Array.isArray(arg.values)) {
|
|
@@ -2438,6 +2450,23 @@ const aggregateToSql = (model, values, item, quotedAs) => {
|
|
|
2438
2450
|
const relationQueryKey = Symbol("relationQuery");
|
|
2439
2451
|
const isRequiredRelationKey = Symbol("isRequiredRelation");
|
|
2440
2452
|
|
|
2453
|
+
class PormError extends Error {
|
|
2454
|
+
}
|
|
2455
|
+
class NotFoundError extends PormError {
|
|
2456
|
+
constructor(message = "Record is not found") {
|
|
2457
|
+
super(message);
|
|
2458
|
+
}
|
|
2459
|
+
}
|
|
2460
|
+
class MoreThanOneRowError extends PormError {
|
|
2461
|
+
}
|
|
2462
|
+
class PormInternalError extends Error {
|
|
2463
|
+
}
|
|
2464
|
+
class UnhandledTypeError extends PormInternalError {
|
|
2465
|
+
constructor(value) {
|
|
2466
|
+
super(`Unhandled type: ${JSON.stringify(value)} received`);
|
|
2467
|
+
}
|
|
2468
|
+
}
|
|
2469
|
+
|
|
2441
2470
|
const jsonColumnOrMethodToSql = (column, values, quotedAs) => {
|
|
2442
2471
|
return typeof column === "string" ? quoteFullColumn(column, quotedAs) : jsonToSql(column, values, quotedAs);
|
|
2443
2472
|
};
|
|
@@ -2482,7 +2511,7 @@ const selectToSql = (model, query, values, quotedAs) => {
|
|
|
2482
2511
|
if (query.select) {
|
|
2483
2512
|
const list = [];
|
|
2484
2513
|
query.select.forEach((item) => {
|
|
2485
|
-
var _a2, _b;
|
|
2514
|
+
var _a2, _b, _c;
|
|
2486
2515
|
if (typeof item === "string") {
|
|
2487
2516
|
list.push(
|
|
2488
2517
|
item === "*" ? ((_a2 = query.join) == null ? void 0 : _a2.length) ? `${quotedAs}.*` : "*" : quoteFullColumn(item, quotedAs)
|
|
@@ -2492,8 +2521,35 @@ const selectToSql = (model, query, values, quotedAs) => {
|
|
|
2492
2521
|
const as = q(getQueryAs(relationQuery));
|
|
2493
2522
|
relationQuery._as(relationQuery.query[relationQueryKey]);
|
|
2494
2523
|
const { returnType } = relationQuery.query;
|
|
2495
|
-
|
|
2496
|
-
|
|
2524
|
+
switch (returnType) {
|
|
2525
|
+
case "all":
|
|
2526
|
+
case "one":
|
|
2527
|
+
case "oneOrThrow":
|
|
2528
|
+
relationQuery = relationQuery._json();
|
|
2529
|
+
break;
|
|
2530
|
+
case "pluck": {
|
|
2531
|
+
const first = (_c = relationQuery.query.select) == null ? void 0 : _c[0];
|
|
2532
|
+
if (!first)
|
|
2533
|
+
throw new PormInternalError(`Nothing was selected for pluck`);
|
|
2534
|
+
const selection = selectToSql(
|
|
2535
|
+
relationQuery.__model,
|
|
2536
|
+
relationQuery.query,
|
|
2537
|
+
values,
|
|
2538
|
+
q(getQueryAs(relationQuery))
|
|
2539
|
+
);
|
|
2540
|
+
relationQuery.query.select = [
|
|
2541
|
+
raw(`COALESCE(json_agg(${selection}), '[]')`)
|
|
2542
|
+
];
|
|
2543
|
+
break;
|
|
2544
|
+
}
|
|
2545
|
+
case "rows":
|
|
2546
|
+
case "value":
|
|
2547
|
+
case "valueOrThrow":
|
|
2548
|
+
case "rowCount":
|
|
2549
|
+
case "void":
|
|
2550
|
+
break;
|
|
2551
|
+
default:
|
|
2552
|
+
throw new UnhandledTypeError(returnType);
|
|
2497
2553
|
}
|
|
2498
2554
|
list.push(`(${relationQuery.toSql(values).text}) AS ${as}`);
|
|
2499
2555
|
} else {
|
|
@@ -3100,14 +3156,6 @@ const setQueryObjectValue = (q, object, key, value) => {
|
|
|
3100
3156
|
return q;
|
|
3101
3157
|
};
|
|
3102
3158
|
|
|
3103
|
-
class NotFoundError extends Error {
|
|
3104
|
-
constructor(message = "Record is not found") {
|
|
3105
|
-
super(message);
|
|
3106
|
-
}
|
|
3107
|
-
}
|
|
3108
|
-
class MoreThanOneRowError extends Error {
|
|
3109
|
-
}
|
|
3110
|
-
|
|
3111
3159
|
const addParserForRawExpression = (q, key, raw) => {
|
|
3112
3160
|
var _a;
|
|
3113
3161
|
const parser = (_a = raw.__column) == null ? void 0 : _a.parseFn;
|
|
@@ -3936,6 +3984,9 @@ class WhereQueryBuilder extends Where {
|
|
|
3936
3984
|
this.relations = {};
|
|
3937
3985
|
this.withData = {};
|
|
3938
3986
|
this.__model = this;
|
|
3987
|
+
if (tableAlias) {
|
|
3988
|
+
this.query.as = tableAlias;
|
|
3989
|
+
}
|
|
3939
3990
|
}
|
|
3940
3991
|
clone() {
|
|
3941
3992
|
const cloned = Object.create(this.__model);
|
|
@@ -5505,8 +5556,11 @@ exports.OnConflictQueryBuilder = OnConflictQueryBuilder;
|
|
|
5505
5556
|
exports.OnQueryBuilder = OnQueryBuilder;
|
|
5506
5557
|
exports.Operators = Operators;
|
|
5507
5558
|
exports.PathColumn = PathColumn;
|
|
5559
|
+
exports.PluckResultColumnType = PluckResultColumnType;
|
|
5508
5560
|
exports.PointColumn = PointColumn;
|
|
5509
5561
|
exports.PolygonColumn = PolygonColumn;
|
|
5562
|
+
exports.PormError = PormError;
|
|
5563
|
+
exports.PormInternalError = PormInternalError;
|
|
5510
5564
|
exports.QueryCallbacks = QueryCallbacks;
|
|
5511
5565
|
exports.QueryGet = QueryGet;
|
|
5512
5566
|
exports.QueryLog = QueryLog;
|
|
@@ -5530,6 +5584,7 @@ exports.TransactionAdapter = TransactionAdapter;
|
|
|
5530
5584
|
exports.TsQueryColumn = TsQueryColumn;
|
|
5531
5585
|
exports.TsVectorColumn = TsVectorColumn;
|
|
5532
5586
|
exports.UUIDColumn = UUIDColumn;
|
|
5587
|
+
exports.UnhandledTypeError = UnhandledTypeError;
|
|
5533
5588
|
exports.Union = Union;
|
|
5534
5589
|
exports.Update = Update;
|
|
5535
5590
|
exports.VarCharColumn = VarCharColumn;
|