pqb 0.45.4 → 0.46.0
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 +45 -27
- package/dist/index.js +106 -29
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +106 -29
- package/dist/index.mjs.map +1 -1
- package/package.json +2 -2
package/dist/index.mjs
CHANGED
|
@@ -1104,8 +1104,35 @@ const text = {
|
|
|
1104
1104
|
};
|
|
1105
1105
|
const encodeJsonPath = (ctx, path) => addValue(ctx.values, `{${Array.isArray(path) ? path.join(", ") : path}}`);
|
|
1106
1106
|
const jsonPathQueryOp = (key, [path, options], ctx) => `jsonb_path_query_first(${key}, ${addValue(ctx.values, path)}${options?.vars ? `, ${addValue(ctx.values, JSON.stringify(options.vars))}${options.silent ? ", true" : ""}` : options?.silent ? ", NULL, true" : ""})`;
|
|
1107
|
+
const quoteJsonValue = (arg, ctx, quotedAs, IN) => {
|
|
1108
|
+
if (arg && typeof arg === "object") {
|
|
1109
|
+
if (IN && Array.isArray(arg)) {
|
|
1110
|
+
return `(${arg.map((value) => addValue(ctx.values, JSON.stringify(value)) + "::jsonb").join(", ")})`;
|
|
1111
|
+
}
|
|
1112
|
+
if (isExpression(arg)) {
|
|
1113
|
+
return "to_jsonb(" + arg.toSQL(ctx, quotedAs) + ")";
|
|
1114
|
+
}
|
|
1115
|
+
if ("toSQL" in arg) {
|
|
1116
|
+
return `to_jsonb((${getSqlText(
|
|
1117
|
+
arg.toSQL({ values: ctx.values })
|
|
1118
|
+
)}))`;
|
|
1119
|
+
}
|
|
1120
|
+
}
|
|
1121
|
+
return addValue(ctx.values, JSON.stringify(arg)) + "::jsonb";
|
|
1122
|
+
};
|
|
1107
1123
|
const json = {
|
|
1108
|
-
|
|
1124
|
+
equals: make(
|
|
1125
|
+
(key, value, ctx, quotedAs) => value === null ? `nullif(${key}, 'null'::jsonb) IS NULL` : `${key} = ${quoteJsonValue(value, ctx, quotedAs)}`
|
|
1126
|
+
),
|
|
1127
|
+
not: make(
|
|
1128
|
+
(key, value, ctx, quotedAs) => value === null ? `nullif(${key}, 'null'::jsonb) IS NOT NULL` : `${key} != ${quoteJsonValue(value, ctx, quotedAs)}`
|
|
1129
|
+
),
|
|
1130
|
+
in: make(
|
|
1131
|
+
(key, value, ctx, quotedAs) => `${key} IN ${quoteJsonValue(value, ctx, quotedAs, true)}`
|
|
1132
|
+
),
|
|
1133
|
+
notIn: make(
|
|
1134
|
+
(key, value, ctx, quotedAs) => `NOT ${key} IN ${quoteJsonValue(value, ctx, quotedAs, true)}`
|
|
1135
|
+
),
|
|
1109
1136
|
jsonPathQueryFirst: Object.assign(
|
|
1110
1137
|
function(path, options) {
|
|
1111
1138
|
const { q, columnTypes } = this;
|
|
@@ -1919,13 +1946,39 @@ const _queryAll = (q) => {
|
|
|
1919
1946
|
q.q.all = true;
|
|
1920
1947
|
return q;
|
|
1921
1948
|
};
|
|
1922
|
-
const _queryTake = (
|
|
1923
|
-
q
|
|
1924
|
-
|
|
1949
|
+
const _queryTake = (query) => {
|
|
1950
|
+
const q = query.q;
|
|
1951
|
+
switch (q.returnType) {
|
|
1952
|
+
case "valueOrThrow":
|
|
1953
|
+
case "pluck":
|
|
1954
|
+
case "void":
|
|
1955
|
+
break;
|
|
1956
|
+
case "value": {
|
|
1957
|
+
q.returnType = "valueOrThrow";
|
|
1958
|
+
break;
|
|
1959
|
+
}
|
|
1960
|
+
default: {
|
|
1961
|
+
q.returnType = "oneOrThrow";
|
|
1962
|
+
}
|
|
1963
|
+
}
|
|
1964
|
+
return query;
|
|
1925
1965
|
};
|
|
1926
|
-
const _queryTakeOptional = (
|
|
1927
|
-
q
|
|
1928
|
-
|
|
1966
|
+
const _queryTakeOptional = (query) => {
|
|
1967
|
+
const q = query.q;
|
|
1968
|
+
switch (q.returnType) {
|
|
1969
|
+
case "value":
|
|
1970
|
+
case "pluck":
|
|
1971
|
+
case "void":
|
|
1972
|
+
break;
|
|
1973
|
+
case "valueOrThrow": {
|
|
1974
|
+
q.returnType = "value";
|
|
1975
|
+
break;
|
|
1976
|
+
}
|
|
1977
|
+
default: {
|
|
1978
|
+
q.returnType = "one";
|
|
1979
|
+
}
|
|
1980
|
+
}
|
|
1981
|
+
return query;
|
|
1929
1982
|
};
|
|
1930
1983
|
const _queryExec = (q) => {
|
|
1931
1984
|
q.q.returnType = "void";
|
|
@@ -2109,16 +2162,27 @@ const pushDistinctSql = (ctx, table, distinct, quotedAs) => {
|
|
|
2109
2162
|
}
|
|
2110
2163
|
};
|
|
2111
2164
|
|
|
2165
|
+
const noneResult = (q, queryData, type) => {
|
|
2166
|
+
if (!type || type === "all" || type === "rows" || type === "pluck") {
|
|
2167
|
+
return [];
|
|
2168
|
+
} else if (type === "one" || type === "value" || type === "void") {
|
|
2169
|
+
return queryData.notFoundDefault;
|
|
2170
|
+
} else if (type === "valueOrThrow" && queryData.returning) {
|
|
2171
|
+
return 0;
|
|
2172
|
+
} else {
|
|
2173
|
+
throw new NotFoundError(q);
|
|
2174
|
+
}
|
|
2175
|
+
};
|
|
2112
2176
|
const noneMethods = {
|
|
2113
|
-
// `then` resolves or rejects based on return type of the query.
|
|
2177
|
+
// `then` resolves or rejects based on a return type of the query.
|
|
2114
2178
|
// It is `async` so it returns a chainable Promise.
|
|
2115
2179
|
async then(resolve, reject) {
|
|
2116
|
-
|
|
2117
|
-
|
|
2118
|
-
resolve?.(
|
|
2119
|
-
|
|
2120
|
-
|
|
2121
|
-
|
|
2180
|
+
try {
|
|
2181
|
+
const result = noneResult(this, this.q, this.q.returnType);
|
|
2182
|
+
resolve?.(result);
|
|
2183
|
+
} catch (err) {
|
|
2184
|
+
reject?.(err);
|
|
2185
|
+
}
|
|
2122
2186
|
},
|
|
2123
2187
|
// `catch` returns a Promise, so it is chainable with then/catch.
|
|
2124
2188
|
catch: () => new Promise(noop)
|
|
@@ -2127,6 +2191,11 @@ const _queryNone = (q) => {
|
|
|
2127
2191
|
if (isQueryNone(q)) return q;
|
|
2128
2192
|
q = extendQuery(q, noneMethods);
|
|
2129
2193
|
pushQueryValueImmutable(q, "and", new RawSQL("false"));
|
|
2194
|
+
pushQueryValueImmutable(
|
|
2195
|
+
q,
|
|
2196
|
+
"transform",
|
|
2197
|
+
(_, queryData) => noneResult(q, queryData, queryData.returnType)
|
|
2198
|
+
);
|
|
2130
2199
|
return q;
|
|
2131
2200
|
};
|
|
2132
2201
|
const isQueryNone = (q) => q.then === noneMethods.then;
|
|
@@ -2184,7 +2253,9 @@ const _join = (query, require, type, first, args) => {
|
|
|
2184
2253
|
args,
|
|
2185
2254
|
joinSubQuery
|
|
2186
2255
|
);
|
|
2187
|
-
if (
|
|
2256
|
+
if (require && "r" in joinArgs && isQueryNone(joinArgs.r)) {
|
|
2257
|
+
return _queryNone(query);
|
|
2258
|
+
} else if (joinKey && "s" in joinArgs && joinArgs.s) {
|
|
2188
2259
|
const j = "j" in joinArgs ? joinArgs.r ?? joinArgs.j : "r" in joinArgs ? joinArgs.r : joinArgs.q;
|
|
2189
2260
|
const jq = j.q;
|
|
2190
2261
|
if (jq.select || !jq.selectAllColumns) {
|
|
@@ -2211,8 +2282,6 @@ const _join = (query, require, type, first, args) => {
|
|
|
2211
2282
|
computeds
|
|
2212
2283
|
);
|
|
2213
2284
|
}
|
|
2214
|
-
} else if (require && "r" in joinArgs && isQueryNone(joinArgs.r)) {
|
|
2215
|
-
return _queryNone(query);
|
|
2216
2285
|
} else {
|
|
2217
2286
|
addAllShapesAndParsers(
|
|
2218
2287
|
query,
|
|
@@ -2912,12 +2981,13 @@ const runAfterCommit = async (afterCommit, result) => {
|
|
|
2912
2981
|
}
|
|
2913
2982
|
};
|
|
2914
2983
|
|
|
2915
|
-
const applyBatchTransforms = (
|
|
2916
|
-
if (
|
|
2984
|
+
const applyBatchTransforms = (q, batches) => {
|
|
2985
|
+
if (q.transform) {
|
|
2917
2986
|
for (const item of batches) {
|
|
2918
2987
|
item.parent[item.key] = applyTransforms(
|
|
2919
|
-
|
|
2920
|
-
|
|
2988
|
+
q,
|
|
2989
|
+
q.returnType,
|
|
2990
|
+
q.transform,
|
|
2921
2991
|
item.data
|
|
2922
2992
|
);
|
|
2923
2993
|
}
|
|
@@ -3360,7 +3430,7 @@ const then = async (q, adapter, trx, beforeHooks, afterHooks, afterCommitHooks,
|
|
|
3360
3430
|
);
|
|
3361
3431
|
}
|
|
3362
3432
|
if (query.transform) {
|
|
3363
|
-
result = applyTransforms(returnType, query.transform, result);
|
|
3433
|
+
result = applyTransforms(query, returnType, query.transform, result);
|
|
3364
3434
|
}
|
|
3365
3435
|
return resolve?.(result);
|
|
3366
3436
|
} catch (err) {
|
|
@@ -5917,7 +5987,8 @@ const skipQueryKeysForSubQuery = {
|
|
|
5917
5987
|
returnsOne: true,
|
|
5918
5988
|
aliases: true,
|
|
5919
5989
|
sqlCache: true,
|
|
5920
|
-
defaults: true
|
|
5990
|
+
defaults: true,
|
|
5991
|
+
transform: true
|
|
5921
5992
|
};
|
|
5922
5993
|
const getIsJoinSubQuery = (query) => {
|
|
5923
5994
|
const {
|
|
@@ -11689,25 +11760,31 @@ class QueryMethods {
|
|
|
11689
11760
|
return _queryAll(_clone(this));
|
|
11690
11761
|
}
|
|
11691
11762
|
/**
|
|
11692
|
-
*
|
|
11693
|
-
* Throws when not found.
|
|
11763
|
+
* Use `take` to "take" a single record. It adds `LIMIT 1`, throws a `NotFoundError` when not found.
|
|
11694
11764
|
*
|
|
11695
11765
|
* ```ts
|
|
11696
|
-
* const
|
|
11766
|
+
* const taken: TableType = await db.table.where({ key: 'value' }).take();
|
|
11697
11767
|
* ```
|
|
11768
|
+
*
|
|
11769
|
+
* Makes no effect if the query previously has `get`, `pluck`, `exec`.
|
|
11770
|
+
*
|
|
11771
|
+
* Changes `getOptional` to `get`.
|
|
11698
11772
|
*/
|
|
11699
11773
|
take() {
|
|
11700
11774
|
return _queryTake(_clone(this));
|
|
11701
11775
|
}
|
|
11702
11776
|
/**
|
|
11703
|
-
*
|
|
11704
|
-
* Returns `undefined` when not found.
|
|
11777
|
+
* Use `takeOptional` to "take" a single record. It adds `LIMIT 1`, returns `undefined` when not found.
|
|
11705
11778
|
*
|
|
11706
11779
|
* ```ts
|
|
11707
|
-
* const
|
|
11780
|
+
* const takenOptional: TableType | undefined = await db.table
|
|
11708
11781
|
* .where({ key: 'value' })
|
|
11709
11782
|
* .takeOptional();
|
|
11710
11783
|
* ```
|
|
11784
|
+
*
|
|
11785
|
+
* Makes no effect if the query previously has `getOptional`, `pluck`, `exec`.
|
|
11786
|
+
*
|
|
11787
|
+
* Changes `get` to `getOptional`.
|
|
11711
11788
|
*/
|
|
11712
11789
|
takeOptional() {
|
|
11713
11790
|
return _queryTakeOptional(_clone(this));
|