@budibase/backend-core 2.29.28 → 2.29.29
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.js +428 -308
- package/dist/index.js.map +4 -4
- package/dist/index.js.meta.json +1 -1
- package/dist/package.json +4 -4
- package/dist/src/sql/sql.js +388 -237
- package/dist/src/sql/sql.js.map +1 -1
- package/dist/src/sql/utils.js.map +1 -1
- package/package.json +4 -4
- package/src/sql/sql.ts +453 -290
- package/src/sql/utils.ts +1 -0
- package/dist/src/sql/sqlStatements.d.ts +0 -16
- package/dist/src/sql/sqlStatements.js +0 -66
- package/dist/src/sql/sqlStatements.js.map +0 -1
- package/src/sql/sqlStatements.ts +0 -87
package/dist/index.js
CHANGED
|
@@ -15440,7 +15440,7 @@ var require_cookie = __commonJS({
|
|
|
15440
15440
|
c.value = cookieValue;
|
|
15441
15441
|
return c;
|
|
15442
15442
|
}
|
|
15443
|
-
function
|
|
15443
|
+
function parse(str, options2) {
|
|
15444
15444
|
if (!options2 || typeof options2 !== "object") {
|
|
15445
15445
|
options2 = {};
|
|
15446
15446
|
}
|
|
@@ -15622,7 +15622,7 @@ var require_cookie = __commonJS({
|
|
|
15622
15622
|
});
|
|
15623
15623
|
}
|
|
15624
15624
|
Cookie2.cookiesCreated = 0;
|
|
15625
|
-
Cookie2.parse =
|
|
15625
|
+
Cookie2.parse = parse;
|
|
15626
15626
|
Cookie2.fromJSON = fromJSON;
|
|
15627
15627
|
Cookie2.prototype.key = "";
|
|
15628
15628
|
Cookie2.prototype.value = "";
|
|
@@ -16179,7 +16179,7 @@ var require_cookie = __commonJS({
|
|
|
16179
16179
|
exports.MemoryCookieStore = MemoryCookieStore;
|
|
16180
16180
|
exports.parseDate = parseDate;
|
|
16181
16181
|
exports.formatDate = formatDate;
|
|
16182
|
-
exports.parse =
|
|
16182
|
+
exports.parse = parse;
|
|
16183
16183
|
exports.fromJSON = fromJSON;
|
|
16184
16184
|
exports.domainMatch = domainMatch;
|
|
16185
16185
|
exports.defaultPath = defaultPath;
|
|
@@ -16381,7 +16381,7 @@ var require_aws_sign2 = __commonJS({
|
|
|
16381
16381
|
"../../node_modules/aws-sign2/index.js"(exports, module2) {
|
|
16382
16382
|
"use strict";
|
|
16383
16383
|
var crypto2 = require("crypto");
|
|
16384
|
-
var
|
|
16384
|
+
var parse = require("url").parse;
|
|
16385
16385
|
var keys2 = [
|
|
16386
16386
|
"acl",
|
|
16387
16387
|
"location",
|
|
@@ -16447,7 +16447,7 @@ var require_aws_sign2 = __commonJS({
|
|
|
16447
16447
|
}
|
|
16448
16448
|
module2.exports.canonicalizeHeaders = canonicalizeHeaders;
|
|
16449
16449
|
function canonicalizeResource(resource) {
|
|
16450
|
-
var url =
|
|
16450
|
+
var url = parse(resource, true), path3 = url.pathname, buf = [];
|
|
16451
16451
|
Object.keys(url.query).forEach(function(key) {
|
|
16452
16452
|
if (!~keys2.indexOf(key))
|
|
16453
16453
|
return;
|
|
@@ -41274,11 +41274,11 @@ var require_lib7 = __commonJS({
|
|
|
41274
41274
|
"../../node_modules/request/node_modules/qs/lib/index.js"(exports, module2) {
|
|
41275
41275
|
"use strict";
|
|
41276
41276
|
var stringify = require_stringify2();
|
|
41277
|
-
var
|
|
41277
|
+
var parse = require_parse();
|
|
41278
41278
|
var formats2 = require_formats();
|
|
41279
41279
|
module2.exports = {
|
|
41280
41280
|
formats: formats2,
|
|
41281
|
-
parse
|
|
41281
|
+
parse,
|
|
41282
41282
|
stringify
|
|
41283
41283
|
};
|
|
41284
41284
|
}
|
|
@@ -41870,7 +41870,7 @@ var require_uri_all = __commonJS({
|
|
|
41870
41870
|
}
|
|
41871
41871
|
var URI_PARSE = /^(?:([^:\/?#]+):)?(?:\/\/((?:([^\/?#@]*)@)?(\[[^\/?#\]]+\]|[^\/?#:]*)(?:\:(\d*))?))?([^?#]*)(?:\?([^#]*))?(?:#((?:.|\n|\r)*))?/i;
|
|
41872
41872
|
var NO_MATCH_IS_UNDEFINED = "".match(/(){0}/)[1] === void 0;
|
|
41873
|
-
function
|
|
41873
|
+
function parse(uriString) {
|
|
41874
41874
|
var options2 = arguments.length > 1 && arguments[1] !== void 0 ? arguments[1] : {};
|
|
41875
41875
|
var components = {};
|
|
41876
41876
|
var protocol = options2.iri !== false ? IRI_PROTOCOL : URI_PROTOCOL;
|
|
@@ -42041,8 +42041,8 @@ var require_uri_all = __commonJS({
|
|
|
42041
42041
|
var skipNormalization = arguments[3];
|
|
42042
42042
|
var target = {};
|
|
42043
42043
|
if (!skipNormalization) {
|
|
42044
|
-
base3 =
|
|
42045
|
-
relative =
|
|
42044
|
+
base3 = parse(serialize(base3, options2), options2);
|
|
42045
|
+
relative = parse(serialize(relative, options2), options2);
|
|
42046
42046
|
}
|
|
42047
42047
|
options2 = options2 || {};
|
|
42048
42048
|
if (!options2.tolerant && relative.scheme) {
|
|
@@ -42093,24 +42093,24 @@ var require_uri_all = __commonJS({
|
|
|
42093
42093
|
}
|
|
42094
42094
|
function resolve(baseURI, relativeURI, options2) {
|
|
42095
42095
|
var schemelessOptions = assign({ scheme: "null" }, options2);
|
|
42096
|
-
return serialize(resolveComponents(
|
|
42096
|
+
return serialize(resolveComponents(parse(baseURI, schemelessOptions), parse(relativeURI, schemelessOptions), schemelessOptions, true), schemelessOptions);
|
|
42097
42097
|
}
|
|
42098
42098
|
function normalize(uri, options2) {
|
|
42099
42099
|
if (typeof uri === "string") {
|
|
42100
|
-
uri = serialize(
|
|
42100
|
+
uri = serialize(parse(uri, options2), options2);
|
|
42101
42101
|
} else if (typeOf(uri) === "object") {
|
|
42102
|
-
uri =
|
|
42102
|
+
uri = parse(serialize(uri, options2), options2);
|
|
42103
42103
|
}
|
|
42104
42104
|
return uri;
|
|
42105
42105
|
}
|
|
42106
42106
|
function equal(uriA, uriB, options2) {
|
|
42107
42107
|
if (typeof uriA === "string") {
|
|
42108
|
-
uriA = serialize(
|
|
42108
|
+
uriA = serialize(parse(uriA, options2), options2);
|
|
42109
42109
|
} else if (typeOf(uriA) === "object") {
|
|
42110
42110
|
uriA = serialize(uriA, options2);
|
|
42111
42111
|
}
|
|
42112
42112
|
if (typeof uriB === "string") {
|
|
42113
|
-
uriB = serialize(
|
|
42113
|
+
uriB = serialize(parse(uriB, options2), options2);
|
|
42114
42114
|
} else if (typeOf(uriB) === "object") {
|
|
42115
42115
|
uriB = serialize(uriB, options2);
|
|
42116
42116
|
}
|
|
@@ -42125,7 +42125,7 @@ var require_uri_all = __commonJS({
|
|
|
42125
42125
|
var handler = {
|
|
42126
42126
|
scheme: "http",
|
|
42127
42127
|
domainHost: true,
|
|
42128
|
-
parse: function
|
|
42128
|
+
parse: function parse2(components, options2) {
|
|
42129
42129
|
if (!components.host) {
|
|
42130
42130
|
components.error = components.error || "HTTP URIs must have a host.";
|
|
42131
42131
|
}
|
|
@@ -42154,7 +42154,7 @@ var require_uri_all = __commonJS({
|
|
|
42154
42154
|
var handler$2 = {
|
|
42155
42155
|
scheme: "ws",
|
|
42156
42156
|
domainHost: true,
|
|
42157
|
-
parse: function
|
|
42157
|
+
parse: function parse2(components, options2) {
|
|
42158
42158
|
var wsComponents = components;
|
|
42159
42159
|
wsComponents.secure = isSecure(wsComponents);
|
|
42160
42160
|
wsComponents.resourceName = (wsComponents.path || "/") + (wsComponents.query ? "?" + wsComponents.query : "");
|
|
@@ -42330,7 +42330,7 @@ var require_uri_all = __commonJS({
|
|
|
42330
42330
|
var UUID = /^[0-9A-Fa-f]{8}(?:\-[0-9A-Fa-f]{4}){3}\-[0-9A-Fa-f]{12}$/;
|
|
42331
42331
|
var handler$6 = {
|
|
42332
42332
|
scheme: "urn:uuid",
|
|
42333
|
-
parse: function
|
|
42333
|
+
parse: function parse2(urnComponents, options2) {
|
|
42334
42334
|
var uuidComponents = urnComponents;
|
|
42335
42335
|
uuidComponents.uuid = uuidComponents.nss;
|
|
42336
42336
|
uuidComponents.nss = void 0;
|
|
@@ -42355,7 +42355,7 @@ var require_uri_all = __commonJS({
|
|
|
42355
42355
|
exports2.SCHEMES = SCHEMES;
|
|
42356
42356
|
exports2.pctEncChar = pctEncChar;
|
|
42357
42357
|
exports2.pctDecChars = pctDecChars;
|
|
42358
|
-
exports2.parse =
|
|
42358
|
+
exports2.parse = parse;
|
|
42359
42359
|
exports2.removeDotSegments = removeDotSegments;
|
|
42360
42360
|
exports2.serialize = serialize;
|
|
42361
42361
|
exports2.resolveComponents = resolveComponents;
|
|
@@ -54462,6 +54462,15 @@ var DatasourceFeature = /* @__PURE__ */ ((DatasourceFeature2) => {
|
|
|
54462
54462
|
})(DatasourceFeature || {});
|
|
54463
54463
|
|
|
54464
54464
|
// ../types/src/sdk/search.ts
|
|
54465
|
+
var BasicOperator = /* @__PURE__ */ ((BasicOperator2) => {
|
|
54466
|
+
BasicOperator2["EQUAL"] = "equal";
|
|
54467
|
+
BasicOperator2["NOT_EQUAL"] = "notEqual";
|
|
54468
|
+
BasicOperator2["EMPTY"] = "empty";
|
|
54469
|
+
BasicOperator2["NOT_EMPTY"] = "notEmpty";
|
|
54470
|
+
BasicOperator2["FUZZY"] = "fuzzy";
|
|
54471
|
+
BasicOperator2["STRING"] = "string";
|
|
54472
|
+
return BasicOperator2;
|
|
54473
|
+
})(BasicOperator || {});
|
|
54465
54474
|
var ArrayOperator = /* @__PURE__ */ ((ArrayOperator2) => {
|
|
54466
54475
|
ArrayOperator2["CONTAINS"] = "contains";
|
|
54467
54476
|
ArrayOperator2["NOT_CONTAINS"] = "notContains";
|
|
@@ -54469,6 +54478,10 @@ var ArrayOperator = /* @__PURE__ */ ((ArrayOperator2) => {
|
|
|
54469
54478
|
ArrayOperator2["ONE_OF"] = "oneOf";
|
|
54470
54479
|
return ArrayOperator2;
|
|
54471
54480
|
})(ArrayOperator || {});
|
|
54481
|
+
var RangeOperator = /* @__PURE__ */ ((RangeOperator2) => {
|
|
54482
|
+
RangeOperator2["RANGE"] = "range";
|
|
54483
|
+
return RangeOperator2;
|
|
54484
|
+
})(RangeOperator || {});
|
|
54472
54485
|
|
|
54473
54486
|
// ../types/src/sdk/db.ts
|
|
54474
54487
|
var isDocument = (doc) => {
|
|
@@ -54988,6 +55001,8 @@ function validate(cronExpression) {
|
|
|
54988
55001
|
// ../shared-core/src/helpers/schema.ts
|
|
54989
55002
|
var schema_exports = {};
|
|
54990
55003
|
__export(schema_exports, {
|
|
55004
|
+
decodeNonAscii: () => decodeNonAscii,
|
|
55005
|
+
encodeNonAscii: () => encodeNonAscii,
|
|
54991
55006
|
isDeprecatedSingleUserColumn: () => isDeprecatedSingleUserColumn,
|
|
54992
55007
|
isRequired: () => isRequired
|
|
54993
55008
|
});
|
|
@@ -54999,6 +55014,17 @@ function isRequired(constraints) {
|
|
|
54999
55014
|
const isRequired2 = !!constraints && (typeof constraints.presence !== "boolean" && constraints.presence?.allowEmpty === false || constraints.presence === true);
|
|
55000
55015
|
return isRequired2;
|
|
55001
55016
|
}
|
|
55017
|
+
function encodeNonAscii(str) {
|
|
55018
|
+
return str.split("").map((char) => {
|
|
55019
|
+
return char.charCodeAt(0) > 127 ? "\\u" + char.charCodeAt(0).toString(16).padStart(4, "0") : char;
|
|
55020
|
+
}).join("");
|
|
55021
|
+
}
|
|
55022
|
+
function decodeNonAscii(str) {
|
|
55023
|
+
return str.replace(
|
|
55024
|
+
/\\u([0-9a-fA-F]{4})/g,
|
|
55025
|
+
(match, p1) => String.fromCharCode(parseInt(p1, 16))
|
|
55026
|
+
);
|
|
55027
|
+
}
|
|
55002
55028
|
|
|
55003
55029
|
// ../shared-core/src/filters.ts
|
|
55004
55030
|
var import_lodash = require("lodash");
|
|
@@ -55102,7 +55128,7 @@ var getKeyNumbering = (key) => {
|
|
|
55102
55128
|
}
|
|
55103
55129
|
};
|
|
55104
55130
|
var ColumnSplitter = class {
|
|
55105
|
-
constructor(tables) {
|
|
55131
|
+
constructor(tables, opts) {
|
|
55106
55132
|
this.tableNames = tables.map((table) => table.name);
|
|
55107
55133
|
this.tableIds = tables.map((table) => table._id);
|
|
55108
55134
|
this.relationshipColumnNames = tables.flatMap(
|
|
@@ -55111,11 +55137,28 @@ var ColumnSplitter = class {
|
|
|
55111
55137
|
)
|
|
55112
55138
|
);
|
|
55113
55139
|
this.relationships = this.tableNames.concat(this.tableIds).concat(this.relationshipColumnNames).sort((a, b) => b.length - a.length);
|
|
55140
|
+
if (opts?.aliases) {
|
|
55141
|
+
this.aliases = {};
|
|
55142
|
+
for (const [key, value] of Object.entries(opts.aliases)) {
|
|
55143
|
+
this.aliases[value] = key;
|
|
55144
|
+
}
|
|
55145
|
+
}
|
|
55146
|
+
this.columnPrefix = opts?.columnPrefix;
|
|
55114
55147
|
}
|
|
55115
55148
|
run(key) {
|
|
55116
55149
|
let { prefix, key: splitKey } = getKeyNumbering(key);
|
|
55150
|
+
let tableName = void 0;
|
|
55151
|
+
if (this.aliases) {
|
|
55152
|
+
for (const possibleAlias of Object.keys(this.aliases || {})) {
|
|
55153
|
+
const withDot = `${possibleAlias}.`;
|
|
55154
|
+
if (splitKey.startsWith(withDot)) {
|
|
55155
|
+
tableName = this.aliases[possibleAlias];
|
|
55156
|
+
splitKey = splitKey.slice(withDot.length);
|
|
55157
|
+
}
|
|
55158
|
+
}
|
|
55159
|
+
}
|
|
55117
55160
|
let relationship;
|
|
55118
|
-
for (
|
|
55161
|
+
for (const possibleRelationship of this.relationships) {
|
|
55119
55162
|
const withDot = `${possibleRelationship}.`;
|
|
55120
55163
|
if (splitKey.startsWith(withDot)) {
|
|
55121
55164
|
const finalKeyParts = splitKey.split(withDot);
|
|
@@ -55125,7 +55168,13 @@ var ColumnSplitter = class {
|
|
|
55125
55168
|
break;
|
|
55126
55169
|
}
|
|
55127
55170
|
}
|
|
55171
|
+
if (this.columnPrefix) {
|
|
55172
|
+
if (splitKey.startsWith(this.columnPrefix)) {
|
|
55173
|
+
splitKey = decodeNonAscii(splitKey.slice(this.columnPrefix.length));
|
|
55174
|
+
}
|
|
55175
|
+
}
|
|
55128
55176
|
return {
|
|
55177
|
+
tableName,
|
|
55129
55178
|
numberPrefix: prefix,
|
|
55130
55179
|
relationshipPrefix: relationship,
|
|
55131
55180
|
column: splitKey
|
|
@@ -55466,7 +55515,7 @@ var sort = (docs, sort2, sortOrder, sortType = "string" /* STRING */) => {
|
|
|
55466
55515
|
if (!sort2 || !sortOrder || !sortType) {
|
|
55467
55516
|
return docs;
|
|
55468
55517
|
}
|
|
55469
|
-
const
|
|
55518
|
+
const parse = (x) => {
|
|
55470
55519
|
if (x == null) {
|
|
55471
55520
|
return x;
|
|
55472
55521
|
}
|
|
@@ -55476,8 +55525,8 @@ var sort = (docs, sort2, sortOrder, sortType = "string" /* STRING */) => {
|
|
|
55476
55525
|
return parseFloat(x);
|
|
55477
55526
|
};
|
|
55478
55527
|
return docs.slice().sort((a, b) => {
|
|
55479
|
-
const colA =
|
|
55480
|
-
const colB =
|
|
55528
|
+
const colA = parse(a[sort2]);
|
|
55529
|
+
const colB = parse(b[sort2]);
|
|
55481
55530
|
const result = colB == null || colA > colB ? 1 : -1;
|
|
55482
55531
|
if (sortOrder.toLowerCase() === "descending") {
|
|
55483
55532
|
return result * -1;
|
|
@@ -67432,62 +67481,6 @@ __export(sql_exports, {
|
|
|
67432
67481
|
// src/sql/sql.ts
|
|
67433
67482
|
var import_knex2 = require("knex");
|
|
67434
67483
|
|
|
67435
|
-
// src/sql/sqlStatements.ts
|
|
67436
|
-
var SqlStatements = class {
|
|
67437
|
-
constructor(client, table, { allOr, columnPrefix } = {}) {
|
|
67438
|
-
this.client = client;
|
|
67439
|
-
this.table = table;
|
|
67440
|
-
this.allOr = allOr;
|
|
67441
|
-
this.columnPrefix = columnPrefix;
|
|
67442
|
-
}
|
|
67443
|
-
getField(key) {
|
|
67444
|
-
const fieldName = key.split(".")[1];
|
|
67445
|
-
let found = this.table.schema[fieldName];
|
|
67446
|
-
if (!found && this.columnPrefix) {
|
|
67447
|
-
const prefixRemovedFieldName = fieldName.replace(this.columnPrefix, "");
|
|
67448
|
-
found = this.table.schema[prefixRemovedFieldName];
|
|
67449
|
-
}
|
|
67450
|
-
return found;
|
|
67451
|
-
}
|
|
67452
|
-
between(query, key, low, high) {
|
|
67453
|
-
const field = this.getField(key);
|
|
67454
|
-
if (field?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
67455
|
-
query = query.whereRaw(
|
|
67456
|
-
`CAST(${key} AS INTEGER) BETWEEN CAST(? AS INTEGER) AND CAST(? AS INTEGER)`,
|
|
67457
|
-
[low, high]
|
|
67458
|
-
);
|
|
67459
|
-
} else {
|
|
67460
|
-
const fnc = this.allOr ? "orWhereBetween" : "whereBetween";
|
|
67461
|
-
query = query[fnc](key, [low, high]);
|
|
67462
|
-
}
|
|
67463
|
-
return query;
|
|
67464
|
-
}
|
|
67465
|
-
lte(query, key, low) {
|
|
67466
|
-
const field = this.getField(key);
|
|
67467
|
-
if (field?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
67468
|
-
query = query.whereRaw(`CAST(${key} AS INTEGER) >= CAST(? AS INTEGER)`, [
|
|
67469
|
-
low
|
|
67470
|
-
]);
|
|
67471
|
-
} else {
|
|
67472
|
-
const fnc = this.allOr ? "orWhere" : "where";
|
|
67473
|
-
query = query[fnc](key, ">=", low);
|
|
67474
|
-
}
|
|
67475
|
-
return query;
|
|
67476
|
-
}
|
|
67477
|
-
gte(query, key, high) {
|
|
67478
|
-
const field = this.getField(key);
|
|
67479
|
-
if (field?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
67480
|
-
query = query.whereRaw(`CAST(${key} AS INTEGER) <= CAST(? AS INTEGER)`, [
|
|
67481
|
-
high
|
|
67482
|
-
]);
|
|
67483
|
-
} else {
|
|
67484
|
-
const fnc = this.allOr ? "orWhere" : "where";
|
|
67485
|
-
query = query[fnc](key, "<=", high);
|
|
67486
|
-
}
|
|
67487
|
-
return query;
|
|
67488
|
-
}
|
|
67489
|
-
};
|
|
67490
|
-
|
|
67491
67484
|
// src/sql/sqlTable.ts
|
|
67492
67485
|
var import_knex = require("knex");
|
|
67493
67486
|
function isIgnoredType(type) {
|
|
@@ -67704,109 +67697,6 @@ function getBaseLimit() {
|
|
|
67704
67697
|
const envLimit = environment_default.SQL_MAX_ROWS ? parseInt(environment_default.SQL_MAX_ROWS) : null;
|
|
67705
67698
|
return envLimit || 5e3;
|
|
67706
67699
|
}
|
|
67707
|
-
function quote(client, str) {
|
|
67708
|
-
switch (client) {
|
|
67709
|
-
case "sqlite3" /* SQL_LITE */:
|
|
67710
|
-
case "oracledb" /* ORACLE */:
|
|
67711
|
-
case "pg" /* POSTGRES */:
|
|
67712
|
-
return `"${str}"`;
|
|
67713
|
-
case "mssql" /* MS_SQL */:
|
|
67714
|
-
return `[${str}]`;
|
|
67715
|
-
case "mysql2" /* MY_SQL */:
|
|
67716
|
-
return `\`${str}\``;
|
|
67717
|
-
}
|
|
67718
|
-
}
|
|
67719
|
-
function quotedIdentifier(client, key) {
|
|
67720
|
-
return key.split(".").map((part) => quote(client, part)).join(".");
|
|
67721
|
-
}
|
|
67722
|
-
function parse(input) {
|
|
67723
|
-
if (Array.isArray(input)) {
|
|
67724
|
-
return JSON.stringify(input);
|
|
67725
|
-
}
|
|
67726
|
-
if (input == void 0) {
|
|
67727
|
-
return null;
|
|
67728
|
-
}
|
|
67729
|
-
if (typeof input !== "string") {
|
|
67730
|
-
return input;
|
|
67731
|
-
}
|
|
67732
|
-
if (isInvalidISODateString(input)) {
|
|
67733
|
-
return null;
|
|
67734
|
-
}
|
|
67735
|
-
if (isValidISODateString(input)) {
|
|
67736
|
-
return new Date(input.trim());
|
|
67737
|
-
}
|
|
67738
|
-
return input;
|
|
67739
|
-
}
|
|
67740
|
-
function parseBody(body2) {
|
|
67741
|
-
for (let [key, value] of Object.entries(body2)) {
|
|
67742
|
-
body2[key] = parse(value);
|
|
67743
|
-
}
|
|
67744
|
-
return body2;
|
|
67745
|
-
}
|
|
67746
|
-
function parseFilters(filters) {
|
|
67747
|
-
if (!filters) {
|
|
67748
|
-
return {};
|
|
67749
|
-
}
|
|
67750
|
-
for (let [key, value] of Object.entries(filters)) {
|
|
67751
|
-
let parsed;
|
|
67752
|
-
if (typeof value === "object") {
|
|
67753
|
-
parsed = parseFilters(value);
|
|
67754
|
-
} else {
|
|
67755
|
-
parsed = parse(value);
|
|
67756
|
-
}
|
|
67757
|
-
filters[key] = parsed;
|
|
67758
|
-
}
|
|
67759
|
-
return filters;
|
|
67760
|
-
}
|
|
67761
|
-
function generateSelectStatement(json, knex3) {
|
|
67762
|
-
const { resource, meta } = json;
|
|
67763
|
-
const client = knex3.client.config.client;
|
|
67764
|
-
if (!resource || !resource.fields || resource.fields.length === 0) {
|
|
67765
|
-
return "*";
|
|
67766
|
-
}
|
|
67767
|
-
const schema = meta.table.schema;
|
|
67768
|
-
return resource.fields.map((field) => {
|
|
67769
|
-
const parts = field.split(/\./g);
|
|
67770
|
-
let table = void 0;
|
|
67771
|
-
let column = void 0;
|
|
67772
|
-
if (parts.length === 1) {
|
|
67773
|
-
column = parts[0];
|
|
67774
|
-
}
|
|
67775
|
-
if (parts.length === 2) {
|
|
67776
|
-
table = parts[0];
|
|
67777
|
-
column = parts[1];
|
|
67778
|
-
}
|
|
67779
|
-
if (parts.length > 2) {
|
|
67780
|
-
table = parts[0];
|
|
67781
|
-
column = parts.slice(1).join(".");
|
|
67782
|
-
}
|
|
67783
|
-
if (!column) {
|
|
67784
|
-
throw new Error(`Invalid field name: ${field}`);
|
|
67785
|
-
}
|
|
67786
|
-
const columnSchema = schema[column];
|
|
67787
|
-
if (client === "pg" /* POSTGRES */ && columnSchema?.externalType?.includes("money")) {
|
|
67788
|
-
return knex3.raw(
|
|
67789
|
-
`${quotedIdentifier(
|
|
67790
|
-
client,
|
|
67791
|
-
[table, column].join(".")
|
|
67792
|
-
)}::money::numeric as ${quote(client, field)}`
|
|
67793
|
-
);
|
|
67794
|
-
}
|
|
67795
|
-
if (client === "mssql" /* MS_SQL */ && columnSchema?.type === "datetime" /* DATETIME */ && columnSchema.timeOnly) {
|
|
67796
|
-
return knex3.raw(`CONVERT(varchar, ${field}, 108) as "${field}"`);
|
|
67797
|
-
}
|
|
67798
|
-
if (table) {
|
|
67799
|
-
return knex3.raw(
|
|
67800
|
-
`${quote(client, table)}.${quote(client, column)} as ${quote(
|
|
67801
|
-
client,
|
|
67802
|
-
field
|
|
67803
|
-
)}`
|
|
67804
|
-
);
|
|
67805
|
-
} else {
|
|
67806
|
-
return knex3.raw(`${quote(client, field)} as ${quote(client, field)}`);
|
|
67807
|
-
}
|
|
67808
|
-
});
|
|
67809
|
-
}
|
|
67810
67700
|
function getTableName(table) {
|
|
67811
67701
|
if (table?.sourceType === "internal" /* INTERNAL */ || table?.sourceId === INTERNAL_TABLE_SOURCE_ID) {
|
|
67812
67702
|
return table?._id;
|
|
@@ -67830,23 +67720,203 @@ function convertBooleans(query) {
|
|
|
67830
67720
|
return query;
|
|
67831
67721
|
}
|
|
67832
67722
|
var InternalBuilder = class {
|
|
67833
|
-
constructor(client) {
|
|
67723
|
+
constructor(client, knex3, query) {
|
|
67834
67724
|
this.client = client;
|
|
67725
|
+
this.query = query;
|
|
67726
|
+
this.knex = knex3;
|
|
67727
|
+
this.splitter = new filters_exports.ColumnSplitter([this.table], {
|
|
67728
|
+
aliases: this.query.tableAliases,
|
|
67729
|
+
columnPrefix: this.query.meta.columnPrefix
|
|
67730
|
+
});
|
|
67731
|
+
}
|
|
67732
|
+
get table() {
|
|
67733
|
+
return this.query.meta.table;
|
|
67734
|
+
}
|
|
67735
|
+
getFieldSchema(key) {
|
|
67736
|
+
const { column } = this.splitter.run(key);
|
|
67737
|
+
return this.table.schema[column];
|
|
67738
|
+
}
|
|
67739
|
+
// Takes a string like foo and returns a quoted string like [foo] for SQL Server
|
|
67740
|
+
// and "foo" for Postgres.
|
|
67741
|
+
quote(str) {
|
|
67742
|
+
switch (this.client) {
|
|
67743
|
+
case "sqlite3" /* SQL_LITE */:
|
|
67744
|
+
case "oracledb" /* ORACLE */:
|
|
67745
|
+
case "pg" /* POSTGRES */:
|
|
67746
|
+
return `"${str}"`;
|
|
67747
|
+
case "mssql" /* MS_SQL */:
|
|
67748
|
+
return `[${str}]`;
|
|
67749
|
+
case "mysql2" /* MY_SQL */:
|
|
67750
|
+
return `\`${str}\``;
|
|
67751
|
+
}
|
|
67752
|
+
}
|
|
67753
|
+
// Takes a string like a.b.c and returns a quoted identifier like [a].[b].[c]
|
|
67754
|
+
// for SQL Server and `a`.`b`.`c` for MySQL.
|
|
67755
|
+
quotedIdentifier(key) {
|
|
67756
|
+
return key.split(".").map((part) => this.quote(part)).join(".");
|
|
67757
|
+
}
|
|
67758
|
+
generateSelectStatement() {
|
|
67759
|
+
const { resource, meta } = this.query;
|
|
67760
|
+
if (!resource || !resource.fields || resource.fields.length === 0) {
|
|
67761
|
+
return "*";
|
|
67762
|
+
}
|
|
67763
|
+
const schema = meta.table.schema;
|
|
67764
|
+
return resource.fields.map((field) => {
|
|
67765
|
+
const parts = field.split(/\./g);
|
|
67766
|
+
let table = void 0;
|
|
67767
|
+
let column = void 0;
|
|
67768
|
+
if (parts.length === 1) {
|
|
67769
|
+
column = parts[0];
|
|
67770
|
+
}
|
|
67771
|
+
if (parts.length === 2) {
|
|
67772
|
+
table = parts[0];
|
|
67773
|
+
column = parts[1];
|
|
67774
|
+
}
|
|
67775
|
+
if (parts.length > 2) {
|
|
67776
|
+
table = parts[0];
|
|
67777
|
+
column = parts.slice(1).join(".");
|
|
67778
|
+
}
|
|
67779
|
+
if (!column) {
|
|
67780
|
+
throw new Error(`Invalid field name: ${field}`);
|
|
67781
|
+
}
|
|
67782
|
+
const columnSchema = schema[column];
|
|
67783
|
+
if (this.client === "pg" /* POSTGRES */ && columnSchema?.externalType?.includes("money")) {
|
|
67784
|
+
return this.knex.raw(
|
|
67785
|
+
`${this.quotedIdentifier(
|
|
67786
|
+
[table, column].join(".")
|
|
67787
|
+
)}::money::numeric as ${this.quote(field)}`
|
|
67788
|
+
);
|
|
67789
|
+
}
|
|
67790
|
+
if (this.client === "mssql" /* MS_SQL */ && columnSchema?.type === "datetime" /* DATETIME */ && columnSchema.timeOnly) {
|
|
67791
|
+
return this.knex.raw(`CONVERT(varchar, ${field}, 108) as "${field}"`);
|
|
67792
|
+
}
|
|
67793
|
+
if (table) {
|
|
67794
|
+
return this.knex.raw(
|
|
67795
|
+
`${this.quote(table)}.${this.quote(column)} as ${this.quote(field)}`
|
|
67796
|
+
);
|
|
67797
|
+
} else {
|
|
67798
|
+
return this.knex.raw(`${this.quote(field)} as ${this.quote(field)}`);
|
|
67799
|
+
}
|
|
67800
|
+
});
|
|
67801
|
+
}
|
|
67802
|
+
// OracleDB can't use character-large-objects (CLOBs) in WHERE clauses,
|
|
67803
|
+
// so when we use them we need to wrap them in to_char(). This function
|
|
67804
|
+
// converts a field name to the appropriate identifier.
|
|
67805
|
+
convertClobs(field) {
|
|
67806
|
+
const parts = field.split(".");
|
|
67807
|
+
const col = parts.pop();
|
|
67808
|
+
const schema = this.table.schema[col];
|
|
67809
|
+
let identifier = this.quotedIdentifier(field);
|
|
67810
|
+
if (schema.type === "string" /* STRING */ || schema.type === "longform" /* LONGFORM */ || schema.type === "bb_reference_single" /* BB_REFERENCE_SINGLE */ || schema.type === "bb_reference" /* BB_REFERENCE */ || schema.type === "options" /* OPTIONS */ || schema.type === "barcodeqr" /* BARCODEQR */) {
|
|
67811
|
+
identifier = `to_char(${identifier})`;
|
|
67812
|
+
}
|
|
67813
|
+
return identifier;
|
|
67814
|
+
}
|
|
67815
|
+
parse(input, schema) {
|
|
67816
|
+
if (Array.isArray(input)) {
|
|
67817
|
+
return JSON.stringify(input);
|
|
67818
|
+
}
|
|
67819
|
+
if (input == void 0) {
|
|
67820
|
+
return null;
|
|
67821
|
+
}
|
|
67822
|
+
if (this.client === "oracledb" /* ORACLE */ && schema.type === "datetime" /* DATETIME */ && schema.timeOnly) {
|
|
67823
|
+
if (input instanceof Date) {
|
|
67824
|
+
const hours = input.getHours().toString().padStart(2, "0");
|
|
67825
|
+
const minutes = input.getMinutes().toString().padStart(2, "0");
|
|
67826
|
+
const seconds = input.getSeconds().toString().padStart(2, "0");
|
|
67827
|
+
return `${hours}:${minutes}:${seconds}`;
|
|
67828
|
+
}
|
|
67829
|
+
if (typeof input === "string") {
|
|
67830
|
+
return /* @__PURE__ */ new Date(`1970-01-01T${input}Z`);
|
|
67831
|
+
}
|
|
67832
|
+
}
|
|
67833
|
+
if (typeof input === "string") {
|
|
67834
|
+
if (isInvalidISODateString(input)) {
|
|
67835
|
+
return null;
|
|
67836
|
+
}
|
|
67837
|
+
if (isValidISODateString(input)) {
|
|
67838
|
+
return new Date(input.trim());
|
|
67839
|
+
}
|
|
67840
|
+
}
|
|
67841
|
+
return input;
|
|
67842
|
+
}
|
|
67843
|
+
parseBody(body2) {
|
|
67844
|
+
for (let [key, value] of Object.entries(body2)) {
|
|
67845
|
+
const { column } = this.splitter.run(key);
|
|
67846
|
+
const schema = this.table.schema[column];
|
|
67847
|
+
if (!schema) {
|
|
67848
|
+
continue;
|
|
67849
|
+
}
|
|
67850
|
+
body2[key] = this.parse(value, schema);
|
|
67851
|
+
}
|
|
67852
|
+
return body2;
|
|
67853
|
+
}
|
|
67854
|
+
parseFilters(filters) {
|
|
67855
|
+
for (const op of Object.values(BasicOperator)) {
|
|
67856
|
+
const filter = filters[op];
|
|
67857
|
+
if (!filter) {
|
|
67858
|
+
continue;
|
|
67859
|
+
}
|
|
67860
|
+
for (const key of Object.keys(filter)) {
|
|
67861
|
+
if (Array.isArray(filter[key])) {
|
|
67862
|
+
filter[key] = JSON.stringify(filter[key]);
|
|
67863
|
+
continue;
|
|
67864
|
+
}
|
|
67865
|
+
const { column } = this.splitter.run(key);
|
|
67866
|
+
const schema = this.table.schema[column];
|
|
67867
|
+
if (!schema) {
|
|
67868
|
+
continue;
|
|
67869
|
+
}
|
|
67870
|
+
filter[key] = this.parse(filter[key], schema);
|
|
67871
|
+
}
|
|
67872
|
+
}
|
|
67873
|
+
for (const op of Object.values(ArrayOperator)) {
|
|
67874
|
+
const filter = filters[op];
|
|
67875
|
+
if (!filter) {
|
|
67876
|
+
continue;
|
|
67877
|
+
}
|
|
67878
|
+
for (const key of Object.keys(filter)) {
|
|
67879
|
+
const { column } = this.splitter.run(key);
|
|
67880
|
+
const schema = this.table.schema[column];
|
|
67881
|
+
if (!schema) {
|
|
67882
|
+
continue;
|
|
67883
|
+
}
|
|
67884
|
+
filter[key] = filter[key].map((v) => this.parse(v, schema));
|
|
67885
|
+
}
|
|
67886
|
+
}
|
|
67887
|
+
for (const op of Object.values(RangeOperator)) {
|
|
67888
|
+
const filter = filters[op];
|
|
67889
|
+
if (!filter) {
|
|
67890
|
+
continue;
|
|
67891
|
+
}
|
|
67892
|
+
for (const key of Object.keys(filter)) {
|
|
67893
|
+
const { column } = this.splitter.run(key);
|
|
67894
|
+
const schema = this.table.schema[column];
|
|
67895
|
+
if (!schema) {
|
|
67896
|
+
continue;
|
|
67897
|
+
}
|
|
67898
|
+
const value = filter[key];
|
|
67899
|
+
if ("low" in value) {
|
|
67900
|
+
value.low = this.parse(value.low, schema);
|
|
67901
|
+
}
|
|
67902
|
+
if ("high" in value) {
|
|
67903
|
+
value.high = this.parse(value.high, schema);
|
|
67904
|
+
}
|
|
67905
|
+
}
|
|
67906
|
+
}
|
|
67907
|
+
return filters;
|
|
67835
67908
|
}
|
|
67836
67909
|
// right now we only do filters on the specific table being queried
|
|
67837
|
-
addFilters(query, filters,
|
|
67910
|
+
addFilters(query, filters, opts) {
|
|
67838
67911
|
if (!filters) {
|
|
67839
67912
|
return query;
|
|
67840
67913
|
}
|
|
67841
|
-
filters = parseFilters(filters);
|
|
67914
|
+
filters = this.parseFilters(filters);
|
|
67915
|
+
const aliases = this.query.tableAliases;
|
|
67842
67916
|
const allOr = filters.allOr;
|
|
67843
|
-
const
|
|
67844
|
-
allOr,
|
|
67845
|
-
columnPrefix: opts.columnPrefix
|
|
67846
|
-
});
|
|
67847
|
-
const tableName = this.client === "sqlite3" /* SQL_LITE */ ? table._id : table.name;
|
|
67917
|
+
const tableName = this.client === "sqlite3" /* SQL_LITE */ ? this.table._id : this.table.name;
|
|
67848
67918
|
function getTableAlias(name) {
|
|
67849
|
-
const alias =
|
|
67919
|
+
const alias = aliases?.[name];
|
|
67850
67920
|
return alias || name;
|
|
67851
67921
|
}
|
|
67852
67922
|
function iterate(structure, fn, complexKeyFn) {
|
|
@@ -67863,10 +67933,10 @@ var InternalBuilder = class {
|
|
|
67863
67933
|
),
|
|
67864
67934
|
castedTypeValue.values
|
|
67865
67935
|
);
|
|
67866
|
-
} else if (!opts
|
|
67936
|
+
} else if (!opts?.relationship && !isRelationshipField) {
|
|
67867
67937
|
const alias = getTableAlias(tableName);
|
|
67868
67938
|
fn(alias ? `${alias}.${updatedKey}` : updatedKey, value);
|
|
67869
|
-
} else if (opts
|
|
67939
|
+
} else if (opts?.relationship && isRelationshipField) {
|
|
67870
67940
|
const [filterTableName, property] = updatedKey.split(".");
|
|
67871
67941
|
const alias = getTableAlias(filterTableName);
|
|
67872
67942
|
fn(alias ? `${alias}.${property}` : property, value);
|
|
@@ -67880,10 +67950,9 @@ var InternalBuilder = class {
|
|
|
67880
67950
|
query = query[fnc](key, "ilike", `%${value}%`);
|
|
67881
67951
|
} else {
|
|
67882
67952
|
const rawFnc = `${fnc}Raw`;
|
|
67883
|
-
query = query[rawFnc](
|
|
67884
|
-
|
|
67885
|
-
|
|
67886
|
-
);
|
|
67953
|
+
query = query[rawFnc](`LOWER(${this.quotedIdentifier(key)}) LIKE ?`, [
|
|
67954
|
+
`%${value.toLowerCase()}%`
|
|
67955
|
+
]);
|
|
67887
67956
|
}
|
|
67888
67957
|
};
|
|
67889
67958
|
const contains = (mode, any = false) => {
|
|
@@ -67902,10 +67971,10 @@ var InternalBuilder = class {
|
|
|
67902
67971
|
const wrap = any ? "" : "'";
|
|
67903
67972
|
const op = any ? "\\?| array" : "@>";
|
|
67904
67973
|
const fieldNames = key.split(/\./g);
|
|
67905
|
-
const
|
|
67974
|
+
const table = fieldNames[0];
|
|
67906
67975
|
const col = fieldNames[1];
|
|
67907
67976
|
query = query[rawFnc](
|
|
67908
|
-
`${not}COALESCE("${
|
|
67977
|
+
`${not}COALESCE("${table}"."${col}"::jsonb ${op} ${wrap}${stringifyArray(
|
|
67909
67978
|
value,
|
|
67910
67979
|
any ? "'" : '"'
|
|
67911
67980
|
)}${wrap}, FALSE)`
|
|
@@ -67924,21 +67993,26 @@ var InternalBuilder = class {
|
|
|
67924
67993
|
const andOr = mode === filters?.containsAny ? " OR " : " AND ";
|
|
67925
67994
|
iterate(mode, (key, value) => {
|
|
67926
67995
|
let statement = "";
|
|
67996
|
+
const identifier = this.quotedIdentifier(key);
|
|
67927
67997
|
for (let i in value) {
|
|
67928
67998
|
if (typeof value[i] === "string") {
|
|
67929
67999
|
value[i] = `%"${value[i].toLowerCase()}"%`;
|
|
67930
68000
|
} else {
|
|
67931
68001
|
value[i] = `%${value[i]}%`;
|
|
67932
68002
|
}
|
|
67933
|
-
statement +=
|
|
67934
|
-
this.client,
|
|
67935
|
-
key
|
|
67936
|
-
)}), '') LIKE ?`;
|
|
68003
|
+
statement += `${statement ? andOr : ""}COALESCE(LOWER(${identifier}), '') LIKE ?`;
|
|
67937
68004
|
}
|
|
67938
68005
|
if (statement === "") {
|
|
67939
68006
|
return;
|
|
67940
68007
|
}
|
|
67941
|
-
|
|
68008
|
+
if (not) {
|
|
68009
|
+
query = query[rawFnc](
|
|
68010
|
+
`(NOT (${statement}) OR ${identifier} IS NULL)`,
|
|
68011
|
+
value
|
|
68012
|
+
);
|
|
68013
|
+
} else {
|
|
68014
|
+
query = query[rawFnc](statement, value);
|
|
68015
|
+
}
|
|
67942
68016
|
});
|
|
67943
68017
|
}
|
|
67944
68018
|
};
|
|
@@ -67947,10 +68021,23 @@ var InternalBuilder = class {
|
|
|
67947
68021
|
iterate(
|
|
67948
68022
|
filters.oneOf,
|
|
67949
68023
|
(key, array) => {
|
|
67950
|
-
|
|
68024
|
+
if (this.client === "oracledb" /* ORACLE */) {
|
|
68025
|
+
key = this.convertClobs(key);
|
|
68026
|
+
array = Array.isArray(array) ? array : [array];
|
|
68027
|
+
const binding = new Array(array.length).fill("?").join(",");
|
|
68028
|
+
query = query.whereRaw(`${key} IN (${binding})`, array);
|
|
68029
|
+
} else {
|
|
68030
|
+
query = query[fnc](key, Array.isArray(array) ? array : [array]);
|
|
68031
|
+
}
|
|
67951
68032
|
},
|
|
67952
68033
|
(key, array) => {
|
|
67953
|
-
|
|
68034
|
+
if (this.client === "oracledb" /* ORACLE */) {
|
|
68035
|
+
const keyStr = `(${key.map((k) => this.convertClobs(k)).join(",")})`;
|
|
68036
|
+
const binding = `(${array.map((a) => `(${new Array(a.length).fill("?").join(",")})`).join(",")})`;
|
|
68037
|
+
query = query.whereRaw(`${keyStr} IN ${binding}`, array.flat());
|
|
68038
|
+
} else {
|
|
68039
|
+
query = query[fnc](key, Array.isArray(array) ? array : [array]);
|
|
68040
|
+
}
|
|
67954
68041
|
}
|
|
67955
68042
|
);
|
|
67956
68043
|
}
|
|
@@ -67961,10 +68048,9 @@ var InternalBuilder = class {
|
|
|
67961
68048
|
query = query[fnc](key, "ilike", `${value}%`);
|
|
67962
68049
|
} else {
|
|
67963
68050
|
const rawFnc = `${fnc}Raw`;
|
|
67964
|
-
query = query[rawFnc](
|
|
67965
|
-
|
|
67966
|
-
|
|
67967
|
-
);
|
|
68051
|
+
query = query[rawFnc](`LOWER(${this.quotedIdentifier(key)}) LIKE ?`, [
|
|
68052
|
+
`${value.toLowerCase()}%`
|
|
68053
|
+
]);
|
|
67968
68054
|
}
|
|
67969
68055
|
});
|
|
67970
68056
|
}
|
|
@@ -67983,12 +68069,40 @@ var InternalBuilder = class {
|
|
|
67983
68069
|
value.high = "";
|
|
67984
68070
|
}
|
|
67985
68071
|
const lowValid = isValidFilter(value.low), highValid = isValidFilter(value.high);
|
|
68072
|
+
const schema = this.getFieldSchema(key);
|
|
68073
|
+
if (this.client === "oracledb" /* ORACLE */) {
|
|
68074
|
+
key = this.knex.raw(this.convertClobs(key));
|
|
68075
|
+
}
|
|
67986
68076
|
if (lowValid && highValid) {
|
|
67987
|
-
|
|
68077
|
+
if (schema?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
68078
|
+
query = query.whereRaw(
|
|
68079
|
+
`CAST(${key} AS INTEGER) BETWEEN CAST(? AS INTEGER) AND CAST(? AS INTEGER)`,
|
|
68080
|
+
[value.low, value.high]
|
|
68081
|
+
);
|
|
68082
|
+
} else {
|
|
68083
|
+
const fnc = allOr ? "orWhereBetween" : "whereBetween";
|
|
68084
|
+
query = query[fnc](key, [value.low, value.high]);
|
|
68085
|
+
}
|
|
67988
68086
|
} else if (lowValid) {
|
|
67989
|
-
|
|
68087
|
+
if (schema?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
68088
|
+
query = query.whereRaw(
|
|
68089
|
+
`CAST(${key} AS INTEGER) >= CAST(? AS INTEGER)`,
|
|
68090
|
+
[value.low]
|
|
68091
|
+
);
|
|
68092
|
+
} else {
|
|
68093
|
+
const fnc = allOr ? "orWhere" : "where";
|
|
68094
|
+
query = query[fnc](key, ">=", value.low);
|
|
68095
|
+
}
|
|
67990
68096
|
} else if (highValid) {
|
|
67991
|
-
|
|
68097
|
+
if (schema?.type === "bigint" /* BIGINT */ && this.client === "sqlite3" /* SQL_LITE */) {
|
|
68098
|
+
query = query.whereRaw(
|
|
68099
|
+
`CAST(${key} AS INTEGER) <= CAST(? AS INTEGER)`,
|
|
68100
|
+
[value.high]
|
|
68101
|
+
);
|
|
68102
|
+
} else {
|
|
68103
|
+
const fnc = allOr ? "orWhere" : "where";
|
|
68104
|
+
query = query[fnc](key, "<=", value.high);
|
|
68105
|
+
}
|
|
67992
68106
|
}
|
|
67993
68107
|
});
|
|
67994
68108
|
}
|
|
@@ -67997,20 +68111,18 @@ var InternalBuilder = class {
|
|
|
67997
68111
|
const fnc = allOr ? "orWhereRaw" : "whereRaw";
|
|
67998
68112
|
if (this.client === "mssql" /* MS_SQL */) {
|
|
67999
68113
|
query = query[fnc](
|
|
68000
|
-
`CASE WHEN ${quotedIdentifier(
|
|
68001
|
-
this.client,
|
|
68002
|
-
key
|
|
68003
|
-
)} = ? THEN 1 ELSE 0 END = 1`,
|
|
68114
|
+
`CASE WHEN ${this.quotedIdentifier(key)} = ? THEN 1 ELSE 0 END = 1`,
|
|
68004
68115
|
[value]
|
|
68005
68116
|
);
|
|
68006
68117
|
} else if (this.client === "oracledb" /* ORACLE */) {
|
|
68118
|
+
const identifier = this.convertClobs(key);
|
|
68007
68119
|
query = query[fnc](
|
|
68008
|
-
`
|
|
68120
|
+
`(${identifier} IS NOT NULL AND ${identifier} = ?)`,
|
|
68009
68121
|
[value]
|
|
68010
68122
|
);
|
|
68011
68123
|
} else {
|
|
68012
68124
|
query = query[fnc](
|
|
68013
|
-
`COALESCE(${quotedIdentifier(
|
|
68125
|
+
`COALESCE(${this.quotedIdentifier(key)} = ?, FALSE)`,
|
|
68014
68126
|
[value]
|
|
68015
68127
|
);
|
|
68016
68128
|
}
|
|
@@ -68021,20 +68133,18 @@ var InternalBuilder = class {
|
|
|
68021
68133
|
const fnc = allOr ? "orWhereRaw" : "whereRaw";
|
|
68022
68134
|
if (this.client === "mssql" /* MS_SQL */) {
|
|
68023
68135
|
query = query[fnc](
|
|
68024
|
-
`CASE WHEN ${quotedIdentifier(
|
|
68025
|
-
this.client,
|
|
68026
|
-
key
|
|
68027
|
-
)} = ? THEN 1 ELSE 0 END = 0`,
|
|
68136
|
+
`CASE WHEN ${this.quotedIdentifier(key)} = ? THEN 1 ELSE 0 END = 0`,
|
|
68028
68137
|
[value]
|
|
68029
68138
|
);
|
|
68030
68139
|
} else if (this.client === "oracledb" /* ORACLE */) {
|
|
68140
|
+
const identifier = this.convertClobs(key);
|
|
68031
68141
|
query = query[fnc](
|
|
68032
|
-
`
|
|
68142
|
+
`(${identifier} IS NOT NULL AND ${identifier} != ?) OR ${identifier} IS NULL`,
|
|
68033
68143
|
[value]
|
|
68034
68144
|
);
|
|
68035
68145
|
} else {
|
|
68036
68146
|
query = query[fnc](
|
|
68037
|
-
`COALESCE(${quotedIdentifier(
|
|
68147
|
+
`COALESCE(${this.quotedIdentifier(key)} != ?, TRUE)`,
|
|
68038
68148
|
[value]
|
|
68039
68149
|
);
|
|
68040
68150
|
}
|
|
@@ -68061,8 +68171,8 @@ var InternalBuilder = class {
|
|
|
68061
68171
|
if (filters.containsAny) {
|
|
68062
68172
|
contains(filters.containsAny, true);
|
|
68063
68173
|
}
|
|
68064
|
-
const tableRef =
|
|
68065
|
-
if (filters.documentType && !isExternalTable(table) && tableRef) {
|
|
68174
|
+
const tableRef = aliases?.[this.table._id] || this.table._id;
|
|
68175
|
+
if (filters.documentType && !isExternalTable(this.table) && tableRef) {
|
|
68066
68176
|
query.andWhereLike(
|
|
68067
68177
|
`${tableRef}._id`,
|
|
68068
68178
|
`${prefixed(filters.documentType)}%`
|
|
@@ -68070,34 +68180,39 @@ var InternalBuilder = class {
|
|
|
68070
68180
|
}
|
|
68071
68181
|
return query;
|
|
68072
68182
|
}
|
|
68073
|
-
addDistinctCount(query
|
|
68074
|
-
const
|
|
68075
|
-
const
|
|
68076
|
-
const
|
|
68077
|
-
const aliased = table.name && aliases?.[table.name] ? aliases[table.name] : table.name;
|
|
68183
|
+
addDistinctCount(query) {
|
|
68184
|
+
const primary = this.table.primary;
|
|
68185
|
+
const aliases = this.query.tableAliases;
|
|
68186
|
+
const aliased = this.table.name && aliases?.[this.table.name] ? aliases[this.table.name] : this.table.name;
|
|
68078
68187
|
if (!primary) {
|
|
68079
68188
|
throw new Error("SQL counting requires primary key to be supplied");
|
|
68080
68189
|
}
|
|
68081
68190
|
return query.countDistinct(`${aliased}.${primary[0]} as total`);
|
|
68082
68191
|
}
|
|
68083
|
-
addSorting(query
|
|
68084
|
-
let { sort: sort2 } =
|
|
68085
|
-
const
|
|
68086
|
-
const
|
|
68087
|
-
const
|
|
68088
|
-
const
|
|
68089
|
-
const aliased = tableName && aliases?.[tableName] ? aliases[tableName] : table?.name;
|
|
68192
|
+
addSorting(query) {
|
|
68193
|
+
let { sort: sort2 } = this.query;
|
|
68194
|
+
const primaryKey = this.table.primary;
|
|
68195
|
+
const tableName = getTableName(this.table);
|
|
68196
|
+
const aliases = this.query.tableAliases;
|
|
68197
|
+
const aliased = tableName && aliases?.[tableName] ? aliases[tableName] : this.table?.name;
|
|
68090
68198
|
if (!Array.isArray(primaryKey)) {
|
|
68091
68199
|
throw new Error("Sorting requires primary key to be specified for table");
|
|
68092
68200
|
}
|
|
68093
68201
|
if (sort2 && Object.keys(sort2 || {}).length > 0) {
|
|
68094
68202
|
for (let [key, value] of Object.entries(sort2)) {
|
|
68095
68203
|
const direction = value.direction === "ascending" /* ASCENDING */ ? "asc" : "desc";
|
|
68096
|
-
let nulls;
|
|
68097
|
-
if (this.client === "pg" /* POSTGRES */) {
|
|
68204
|
+
let nulls = void 0;
|
|
68205
|
+
if (this.client === "pg" /* POSTGRES */ || this.client === "oracledb" /* ORACLE */) {
|
|
68098
68206
|
nulls = value.direction === "ascending" /* ASCENDING */ ? "first" : "last";
|
|
68099
68207
|
}
|
|
68100
|
-
|
|
68208
|
+
let composite = `${aliased}.${key}`;
|
|
68209
|
+
if (this.client === "oracledb" /* ORACLE */) {
|
|
68210
|
+
query = query.orderByRaw(
|
|
68211
|
+
`${this.convertClobs(composite)} ${direction} nulls ${nulls}`
|
|
68212
|
+
);
|
|
68213
|
+
} else {
|
|
68214
|
+
query = query.orderBy(composite, direction, nulls);
|
|
68215
|
+
}
|
|
68101
68216
|
}
|
|
68102
68217
|
}
|
|
68103
68218
|
if (!sort2 || sort2[primaryKey[0]] === void 0) {
|
|
@@ -68172,23 +68287,41 @@ var InternalBuilder = class {
|
|
|
68172
68287
|
}
|
|
68173
68288
|
return query;
|
|
68174
68289
|
}
|
|
68175
|
-
|
|
68176
|
-
|
|
68177
|
-
|
|
68178
|
-
|
|
68179
|
-
|
|
68180
|
-
|
|
68181
|
-
|
|
68290
|
+
qualifiedKnex(opts) {
|
|
68291
|
+
let alias = this.query.tableAliases?.[this.query.endpoint.entityId];
|
|
68292
|
+
if (opts?.alias === false) {
|
|
68293
|
+
alias = void 0;
|
|
68294
|
+
} else if (typeof opts?.alias === "string") {
|
|
68295
|
+
alias = opts.alias;
|
|
68296
|
+
}
|
|
68297
|
+
return this.knex(
|
|
68298
|
+
this.tableNameWithSchema(this.query.endpoint.entityId, {
|
|
68299
|
+
alias,
|
|
68300
|
+
schema: this.query.endpoint.schema
|
|
68182
68301
|
})
|
|
68183
68302
|
);
|
|
68184
68303
|
}
|
|
68185
|
-
create(
|
|
68186
|
-
const {
|
|
68187
|
-
let query = this.
|
|
68188
|
-
const parsedBody = parseBody(body2);
|
|
68189
|
-
|
|
68190
|
-
|
|
68191
|
-
|
|
68304
|
+
create(opts) {
|
|
68305
|
+
const { body: body2 } = this.query;
|
|
68306
|
+
let query = this.qualifiedKnex({ alias: false });
|
|
68307
|
+
const parsedBody = this.parseBody(body2);
|
|
68308
|
+
if (this.client === "oracledb" /* ORACLE */) {
|
|
68309
|
+
for (const [column, schema] of Object.entries(
|
|
68310
|
+
this.query.meta.table.schema
|
|
68311
|
+
)) {
|
|
68312
|
+
if (schema.constraints?.presence === true || schema.type === "formula" /* FORMULA */ || schema.type === "auto" /* AUTO */ || schema.type === "link" /* LINK */) {
|
|
68313
|
+
continue;
|
|
68314
|
+
}
|
|
68315
|
+
const value = parsedBody[column];
|
|
68316
|
+
if (value == null) {
|
|
68317
|
+
parsedBody[column] = null;
|
|
68318
|
+
}
|
|
68319
|
+
}
|
|
68320
|
+
} else {
|
|
68321
|
+
for (let [key, value] of Object.entries(parsedBody)) {
|
|
68322
|
+
if (value == null) {
|
|
68323
|
+
delete parsedBody[key];
|
|
68324
|
+
}
|
|
68192
68325
|
}
|
|
68193
68326
|
}
|
|
68194
68327
|
if (opts.disableReturning) {
|
|
@@ -68197,40 +68330,40 @@ var InternalBuilder = class {
|
|
|
68197
68330
|
return query.insert(parsedBody).returning("*");
|
|
68198
68331
|
}
|
|
68199
68332
|
}
|
|
68200
|
-
bulkCreate(
|
|
68201
|
-
const {
|
|
68202
|
-
let query = this.
|
|
68333
|
+
bulkCreate() {
|
|
68334
|
+
const { body: body2 } = this.query;
|
|
68335
|
+
let query = this.qualifiedKnex({ alias: false });
|
|
68203
68336
|
if (!Array.isArray(body2)) {
|
|
68204
68337
|
return query;
|
|
68205
68338
|
}
|
|
68206
|
-
const parsedBody = body2.map((row) => parseBody(row));
|
|
68339
|
+
const parsedBody = body2.map((row) => this.parseBody(row));
|
|
68207
68340
|
return query.insert(parsedBody);
|
|
68208
68341
|
}
|
|
68209
|
-
bulkUpsert(
|
|
68210
|
-
const {
|
|
68211
|
-
let query = this.
|
|
68342
|
+
bulkUpsert() {
|
|
68343
|
+
const { body: body2 } = this.query;
|
|
68344
|
+
let query = this.qualifiedKnex({ alias: false });
|
|
68212
68345
|
if (!Array.isArray(body2)) {
|
|
68213
68346
|
return query;
|
|
68214
68347
|
}
|
|
68215
|
-
const parsedBody = body2.map((row) => parseBody(row));
|
|
68348
|
+
const parsedBody = body2.map((row) => this.parseBody(row));
|
|
68216
68349
|
if (this.client === "pg" /* POSTGRES */ || this.client === "sqlite3" /* SQL_LITE */ || this.client === "mysql2" /* MY_SQL */) {
|
|
68217
|
-
const primary =
|
|
68350
|
+
const primary = this.table.primary;
|
|
68218
68351
|
if (!primary) {
|
|
68219
68352
|
throw new Error("Primary key is required for upsert");
|
|
68220
68353
|
}
|
|
68221
68354
|
const ret = query.insert(parsedBody).onConflict(primary).merge();
|
|
68222
68355
|
return ret;
|
|
68223
|
-
} else if (this.client === "mssql" /* MS_SQL */) {
|
|
68356
|
+
} else if (this.client === "mssql" /* MS_SQL */ || this.client === "oracledb" /* ORACLE */) {
|
|
68224
68357
|
return query.insert(parsedBody);
|
|
68225
68358
|
}
|
|
68226
68359
|
return query.upsert(parsedBody);
|
|
68227
68360
|
}
|
|
68228
|
-
read(
|
|
68229
|
-
let { endpoint, filters, paginate, relationships, tableAliases } =
|
|
68361
|
+
read(opts = {}) {
|
|
68362
|
+
let { endpoint, filters, paginate, relationships, tableAliases } = this.query;
|
|
68230
68363
|
const { limits } = opts;
|
|
68231
68364
|
const counting = endpoint.operation === "COUNT" /* COUNT */;
|
|
68232
68365
|
const tableName = endpoint.entityId;
|
|
68233
|
-
let query = this.
|
|
68366
|
+
let query = this.qualifiedKnex();
|
|
68234
68367
|
let foundOffset = null;
|
|
68235
68368
|
let foundLimit = limits?.query || limits?.base;
|
|
68236
68369
|
if (paginate && paginate.page && paginate.limit) {
|
|
@@ -68251,23 +68384,20 @@ var InternalBuilder = class {
|
|
|
68251
68384
|
if (foundOffset != null) {
|
|
68252
68385
|
query = query.offset(foundOffset);
|
|
68253
68386
|
}
|
|
68254
|
-
query = this.addSorting(query
|
|
68387
|
+
query = this.addSorting(query);
|
|
68255
68388
|
}
|
|
68256
|
-
query = this.addFilters(query, filters
|
|
68257
|
-
columnPrefix: json.meta.columnPrefix,
|
|
68258
|
-
aliases: tableAliases
|
|
68259
|
-
});
|
|
68389
|
+
query = this.addFilters(query, filters);
|
|
68260
68390
|
const alias = tableAliases?.[tableName] || tableName;
|
|
68261
|
-
let preQuery =
|
|
68391
|
+
let preQuery = this.knex({
|
|
68262
68392
|
// the typescript definition for the knex constructor doesn't support this
|
|
68263
68393
|
// syntax, but it is the only way to alias a pre-query result as part of
|
|
68264
68394
|
// a query - there is an alias dictionary type, but it assumes it can only
|
|
68265
68395
|
// be a table name, not a pre-query
|
|
68266
68396
|
[alias]: query
|
|
68267
68397
|
});
|
|
68268
|
-
preQuery = !counting ? preQuery.select(generateSelectStatement(
|
|
68398
|
+
preQuery = !counting ? preQuery.select(this.generateSelectStatement()) : this.addDistinctCount(preQuery);
|
|
68269
68399
|
if (this.client !== "mssql" /* MS_SQL */ && !counting) {
|
|
68270
|
-
preQuery = this.addSorting(preQuery
|
|
68400
|
+
preQuery = this.addSorting(preQuery);
|
|
68271
68401
|
}
|
|
68272
68402
|
query = this.addRelationships(
|
|
68273
68403
|
preQuery,
|
|
@@ -68279,37 +68409,27 @@ var InternalBuilder = class {
|
|
|
68279
68409
|
if (limits?.base) {
|
|
68280
68410
|
query = query.limit(limits.base);
|
|
68281
68411
|
}
|
|
68282
|
-
return this.addFilters(query, filters,
|
|
68283
|
-
columnPrefix: json.meta.columnPrefix,
|
|
68284
|
-
relationship: true,
|
|
68285
|
-
aliases: tableAliases
|
|
68286
|
-
});
|
|
68412
|
+
return this.addFilters(query, filters, { relationship: true });
|
|
68287
68413
|
}
|
|
68288
|
-
update(
|
|
68289
|
-
const {
|
|
68290
|
-
let query = this.
|
|
68291
|
-
const parsedBody = parseBody(body2);
|
|
68292
|
-
query = this.addFilters(query, filters
|
|
68293
|
-
columnPrefix: json.meta.columnPrefix,
|
|
68294
|
-
aliases: tableAliases
|
|
68295
|
-
});
|
|
68414
|
+
update(opts) {
|
|
68415
|
+
const { body: body2, filters } = this.query;
|
|
68416
|
+
let query = this.qualifiedKnex();
|
|
68417
|
+
const parsedBody = this.parseBody(body2);
|
|
68418
|
+
query = this.addFilters(query, filters);
|
|
68296
68419
|
if (opts.disableReturning) {
|
|
68297
68420
|
return query.update(parsedBody);
|
|
68298
68421
|
} else {
|
|
68299
68422
|
return query.update(parsedBody).returning("*");
|
|
68300
68423
|
}
|
|
68301
68424
|
}
|
|
68302
|
-
delete(
|
|
68303
|
-
const {
|
|
68304
|
-
let query = this.
|
|
68305
|
-
query = this.addFilters(query, filters
|
|
68306
|
-
columnPrefix: json.meta.columnPrefix,
|
|
68307
|
-
aliases: tableAliases
|
|
68308
|
-
});
|
|
68425
|
+
delete(opts) {
|
|
68426
|
+
const { filters } = this.query;
|
|
68427
|
+
let query = this.qualifiedKnex();
|
|
68428
|
+
query = this.addFilters(query, filters);
|
|
68309
68429
|
if (opts.disableReturning) {
|
|
68310
68430
|
return query.delete();
|
|
68311
68431
|
} else {
|
|
68312
|
-
return query.delete().returning(generateSelectStatement(
|
|
68432
|
+
return query.delete().returning(this.generateSelectStatement());
|
|
68313
68433
|
}
|
|
68314
68434
|
}
|
|
68315
68435
|
};
|
|
@@ -68342,18 +68462,18 @@ var SqlQueryBuilder = class extends sqlTable_default {
|
|
|
68342
68462
|
const config = {
|
|
68343
68463
|
client: sqlClient
|
|
68344
68464
|
};
|
|
68345
|
-
if (sqlClient === "sqlite3" /* SQL_LITE */) {
|
|
68465
|
+
if (sqlClient === "sqlite3" /* SQL_LITE */ || sqlClient === "oracledb" /* ORACLE */) {
|
|
68346
68466
|
config.useNullAsDefault = true;
|
|
68347
68467
|
}
|
|
68348
68468
|
const client = (0, import_knex2.knex)(config);
|
|
68349
68469
|
let query;
|
|
68350
|
-
const builder = new InternalBuilder(sqlClient);
|
|
68470
|
+
const builder = new InternalBuilder(sqlClient, client, json);
|
|
68351
68471
|
switch (this._operation(json)) {
|
|
68352
68472
|
case "CREATE" /* CREATE */:
|
|
68353
|
-
query = builder.create(
|
|
68473
|
+
query = builder.create(opts);
|
|
68354
68474
|
break;
|
|
68355
68475
|
case "READ" /* READ */:
|
|
68356
|
-
query = builder.read(
|
|
68476
|
+
query = builder.read({
|
|
68357
68477
|
limits: {
|
|
68358
68478
|
query: this.limit,
|
|
68359
68479
|
base: getBaseLimit()
|
|
@@ -68361,19 +68481,19 @@ var SqlQueryBuilder = class extends sqlTable_default {
|
|
|
68361
68481
|
});
|
|
68362
68482
|
break;
|
|
68363
68483
|
case "COUNT" /* COUNT */:
|
|
68364
|
-
query = builder.read(
|
|
68484
|
+
query = builder.read();
|
|
68365
68485
|
break;
|
|
68366
68486
|
case "UPDATE" /* UPDATE */:
|
|
68367
|
-
query = builder.update(
|
|
68487
|
+
query = builder.update(opts);
|
|
68368
68488
|
break;
|
|
68369
68489
|
case "DELETE" /* DELETE */:
|
|
68370
|
-
query = builder.delete(
|
|
68490
|
+
query = builder.delete(opts);
|
|
68371
68491
|
break;
|
|
68372
68492
|
case "BULK_CREATE" /* BULK_CREATE */:
|
|
68373
|
-
query = builder.bulkCreate(
|
|
68493
|
+
query = builder.bulkCreate();
|
|
68374
68494
|
break;
|
|
68375
68495
|
case "BULK_UPSERT" /* BULK_UPSERT */:
|
|
68376
|
-
query = builder.bulkUpsert(
|
|
68496
|
+
query = builder.bulkUpsert();
|
|
68377
68497
|
break;
|
|
68378
68498
|
case "CREATE_TABLE" /* CREATE_TABLE */:
|
|
68379
68499
|
case "UPDATE_TABLE" /* UPDATE_TABLE */:
|