@budibase/backend-core 2.27.4 → 2.27.5

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.
Files changed (46) hide show
  1. package/dist/index.js +1399 -160
  2. package/dist/index.js.map +4 -4
  3. package/dist/index.js.meta.json +1 -1
  4. package/dist/package.json +5 -4
  5. package/dist/plugins.js.meta.json +1 -1
  6. package/dist/src/constants/db.d.ts +6 -0
  7. package/dist/src/constants/db.js +7 -1
  8. package/dist/src/constants/db.js.map +1 -1
  9. package/dist/src/environment.d.ts +2 -0
  10. package/dist/src/environment.js +2 -0
  11. package/dist/src/environment.js.map +1 -1
  12. package/dist/src/index.d.ts +1 -0
  13. package/dist/src/index.js +2 -1
  14. package/dist/src/index.js.map +1 -1
  15. package/dist/src/sql/designDoc.d.ts +2 -0
  16. package/dist/src/sql/designDoc.js +20 -0
  17. package/dist/src/sql/designDoc.js.map +1 -0
  18. package/dist/src/sql/index.d.ts +4 -0
  19. package/dist/src/sql/index.js +36 -0
  20. package/dist/src/sql/index.js.map +1 -0
  21. package/dist/src/sql/sql.d.ts +21 -0
  22. package/dist/src/sql/sql.js +752 -0
  23. package/dist/src/sql/sql.js.map +1 -0
  24. package/dist/src/sql/sqlStatements.d.ts +14 -0
  25. package/dist/src/sql/sqlStatements.js +60 -0
  26. package/dist/src/sql/sqlStatements.js.map +1 -0
  27. package/dist/src/sql/sqlTable.d.ts +13 -0
  28. package/dist/src/sql/sqlTable.js +231 -0
  29. package/dist/src/sql/sqlTable.js.map +1 -0
  30. package/dist/src/sql/utils.d.ts +22 -0
  31. package/dist/src/sql/utils.js +133 -0
  32. package/dist/src/sql/utils.js.map +1 -0
  33. package/dist/tests/core/utilities/mocks/licenses.d.ts +1 -0
  34. package/dist/tests/core/utilities/mocks/licenses.js +5 -1
  35. package/dist/tests/core/utilities/mocks/licenses.js.map +1 -1
  36. package/package.json +5 -4
  37. package/src/constants/db.ts +6 -0
  38. package/src/environment.ts +3 -0
  39. package/src/index.ts +1 -0
  40. package/src/sql/designDoc.ts +17 -0
  41. package/src/sql/index.ts +5 -0
  42. package/src/sql/sql.ts +852 -0
  43. package/src/sql/sqlStatements.ts +79 -0
  44. package/src/sql/sqlTable.ts +289 -0
  45. package/src/sql/utils.ts +134 -0
  46. package/tests/core/utilities/mocks/licenses.ts +4 -0
@@ -0,0 +1,752 @@
1
+ "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || function (mod) {
19
+ if (mod && mod.__esModule) return mod;
20
+ var result = {};
21
+ if (mod != null) for (var k in mod) if (k !== "default" && Object.prototype.hasOwnProperty.call(mod, k)) __createBinding(result, mod, k);
22
+ __setModuleDefault(result, mod);
23
+ return result;
24
+ };
25
+ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
26
+ function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
27
+ return new (P || (P = Promise))(function (resolve, reject) {
28
+ function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
29
+ function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
30
+ function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
31
+ step((generator = generator.apply(thisArg, _arguments || [])).next());
32
+ });
33
+ };
34
+ var __importDefault = (this && this.__importDefault) || function (mod) {
35
+ return (mod && mod.__esModule) ? mod : { "default": mod };
36
+ };
37
+ Object.defineProperty(exports, "__esModule", { value: true });
38
+ const knex_1 = require("knex");
39
+ const dbCore = __importStar(require("../db"));
40
+ const utils_1 = require("./utils");
41
+ const sqlStatements_1 = require("./sqlStatements");
42
+ const sqlTable_1 = __importDefault(require("./sqlTable"));
43
+ const types_1 = require("@budibase/types");
44
+ const environment_1 = __importDefault(require("../environment"));
45
+ const shared_core_1 = require("@budibase/shared-core");
46
+ const envLimit = environment_1.default.SQL_MAX_ROWS
47
+ ? parseInt(environment_1.default.SQL_MAX_ROWS)
48
+ : null;
49
+ const BASE_LIMIT = envLimit || 5000;
50
+ // these are invalid dates sent by the client, need to convert them to a real max date
51
+ const MIN_ISO_DATE = "0000-00-00T00:00:00.000Z";
52
+ const MAX_ISO_DATE = "9999-00-00T00:00:00.000Z";
53
+ function likeKey(client, key) {
54
+ let start, end;
55
+ switch (client) {
56
+ case types_1.SqlClient.MY_SQL:
57
+ start = end = "`";
58
+ break;
59
+ case types_1.SqlClient.SQL_LITE:
60
+ case types_1.SqlClient.ORACLE:
61
+ case types_1.SqlClient.POSTGRES:
62
+ start = end = '"';
63
+ break;
64
+ case types_1.SqlClient.MS_SQL:
65
+ start = "[";
66
+ end = "]";
67
+ break;
68
+ default:
69
+ throw new Error("Unknown client generating like key");
70
+ }
71
+ const parts = key.split(".");
72
+ key = parts.map(part => `${start}${part}${end}`).join(".");
73
+ return key;
74
+ }
75
+ function parse(input) {
76
+ if (Array.isArray(input)) {
77
+ return JSON.stringify(input);
78
+ }
79
+ if (input == undefined) {
80
+ return null;
81
+ }
82
+ if (typeof input !== "string") {
83
+ return input;
84
+ }
85
+ if (input === MAX_ISO_DATE || input === MIN_ISO_DATE) {
86
+ return null;
87
+ }
88
+ if ((0, utils_1.isIsoDateString)(input)) {
89
+ return new Date(input.trim());
90
+ }
91
+ return input;
92
+ }
93
+ function parseBody(body) {
94
+ for (let [key, value] of Object.entries(body)) {
95
+ body[key] = parse(value);
96
+ }
97
+ return body;
98
+ }
99
+ function parseFilters(filters) {
100
+ if (!filters) {
101
+ return {};
102
+ }
103
+ for (let [key, value] of Object.entries(filters)) {
104
+ let parsed;
105
+ if (typeof value === "object") {
106
+ parsed = parseFilters(value);
107
+ }
108
+ else {
109
+ parsed = parse(value);
110
+ }
111
+ // @ts-ignore
112
+ filters[key] = parsed;
113
+ }
114
+ return filters;
115
+ }
116
+ function generateSelectStatement(json, knex) {
117
+ var _a;
118
+ const { resource, meta } = json;
119
+ if (!resource) {
120
+ return "*";
121
+ }
122
+ const schema = (_a = meta === null || meta === void 0 ? void 0 : meta.table) === null || _a === void 0 ? void 0 : _a.schema;
123
+ return resource.fields.map(field => {
124
+ const fieldNames = field.split(/\./g);
125
+ const tableName = fieldNames[0];
126
+ const columnName = fieldNames[1];
127
+ const columnSchema = schema === null || schema === void 0 ? void 0 : schema[columnName];
128
+ if (columnSchema && knex.client.config.client === types_1.SqlClient.POSTGRES) {
129
+ const externalType = schema[columnName].externalType;
130
+ if (externalType === null || externalType === void 0 ? void 0 : externalType.includes("money")) {
131
+ return knex.raw(`"${tableName}"."${columnName}"::money::numeric as "${field}"`);
132
+ }
133
+ }
134
+ if (knex.client.config.client === types_1.SqlClient.MS_SQL &&
135
+ (columnSchema === null || columnSchema === void 0 ? void 0 : columnSchema.type) === types_1.FieldType.DATETIME &&
136
+ columnSchema.timeOnly) {
137
+ // Time gets returned as timestamp from mssql, not matching the expected HH:mm format
138
+ return knex.raw(`CONVERT(varchar, ${field}, 108) as "${field}"`);
139
+ }
140
+ return `${field} as ${field}`;
141
+ });
142
+ }
143
+ function getTableName(table) {
144
+ // SQS uses the table ID rather than the table name
145
+ if ((table === null || table === void 0 ? void 0 : table.sourceType) === types_1.TableSourceType.INTERNAL ||
146
+ (table === null || table === void 0 ? void 0 : table.sourceId) === types_1.INTERNAL_TABLE_SOURCE_ID) {
147
+ return table === null || table === void 0 ? void 0 : table._id;
148
+ }
149
+ else {
150
+ return table === null || table === void 0 ? void 0 : table.name;
151
+ }
152
+ }
153
+ function convertBooleans(query) {
154
+ if (Array.isArray(query)) {
155
+ return query.map((q) => convertBooleans(q));
156
+ }
157
+ else {
158
+ if (query.bindings) {
159
+ query.bindings = query.bindings.map(binding => {
160
+ if (typeof binding === "boolean") {
161
+ return binding ? 1 : 0;
162
+ }
163
+ return binding;
164
+ });
165
+ }
166
+ }
167
+ return query;
168
+ }
169
+ class InternalBuilder {
170
+ constructor(client) {
171
+ this.client = client;
172
+ }
173
+ // right now we only do filters on the specific table being queried
174
+ addFilters(query, filters, table, opts) {
175
+ var _a;
176
+ if (!filters) {
177
+ return query;
178
+ }
179
+ filters = parseFilters(filters);
180
+ // if all or specified in filters, then everything is an or
181
+ const allOr = filters.allOr;
182
+ const sqlStatements = new sqlStatements_1.SqlStatements(this.client, table, { allOr });
183
+ const tableName = this.client === types_1.SqlClient.SQL_LITE ? table._id : table.name;
184
+ function getTableAlias(name) {
185
+ var _a;
186
+ const alias = (_a = opts.aliases) === null || _a === void 0 ? void 0 : _a[name];
187
+ return alias || name;
188
+ }
189
+ function iterate(structure, fn) {
190
+ for (let [key, value] of Object.entries(structure)) {
191
+ const updatedKey = dbCore.removeKeyNumbering(key);
192
+ const isRelationshipField = updatedKey.includes(".");
193
+ if (!opts.relationship && !isRelationshipField) {
194
+ const alias = getTableAlias(tableName);
195
+ fn(alias ? `${alias}.${updatedKey}` : updatedKey, value);
196
+ }
197
+ if (opts.relationship && isRelationshipField) {
198
+ const [filterTableName, property] = updatedKey.split(".");
199
+ const alias = getTableAlias(filterTableName);
200
+ fn(alias ? `${alias}.${property}` : property, value);
201
+ }
202
+ }
203
+ }
204
+ const like = (key, value) => {
205
+ const fuzzyOr = filters === null || filters === void 0 ? void 0 : filters.fuzzyOr;
206
+ const fnc = fuzzyOr || allOr ? "orWhere" : "where";
207
+ // postgres supports ilike, nothing else does
208
+ if (this.client === types_1.SqlClient.POSTGRES) {
209
+ query = query[fnc](key, "ilike", `%${value}%`);
210
+ }
211
+ else {
212
+ const rawFnc = `${fnc}Raw`;
213
+ // @ts-ignore
214
+ query = query[rawFnc](`LOWER(${likeKey(this.client, key)}) LIKE ?`, [
215
+ `%${value.toLowerCase()}%`,
216
+ ]);
217
+ }
218
+ };
219
+ const contains = (mode, any = false) => {
220
+ const rawFnc = allOr ? "orWhereRaw" : "whereRaw";
221
+ const not = mode === (filters === null || filters === void 0 ? void 0 : filters.notContains) ? "NOT " : "";
222
+ function stringifyArray(value, quoteStyle = '"') {
223
+ for (let i in value) {
224
+ if (typeof value[i] === "string") {
225
+ value[i] = `${quoteStyle}${value[i]}${quoteStyle}`;
226
+ }
227
+ }
228
+ return `[${value.join(",")}]`;
229
+ }
230
+ if (this.client === types_1.SqlClient.POSTGRES) {
231
+ iterate(mode, (key, value) => {
232
+ const wrap = any ? "" : "'";
233
+ const op = any ? "\\?| array" : "@>";
234
+ const fieldNames = key.split(/\./g);
235
+ const table = fieldNames[0];
236
+ const col = fieldNames[1];
237
+ query = query[rawFnc](`${not}COALESCE("${table}"."${col}"::jsonb ${op} ${wrap}${stringifyArray(value, any ? "'" : '"')}${wrap}, FALSE)`);
238
+ });
239
+ }
240
+ else if (this.client === types_1.SqlClient.MY_SQL) {
241
+ const jsonFnc = any ? "JSON_OVERLAPS" : "JSON_CONTAINS";
242
+ iterate(mode, (key, value) => {
243
+ query = query[rawFnc](`${not}COALESCE(${jsonFnc}(${key}, '${stringifyArray(value)}'), FALSE)`);
244
+ });
245
+ }
246
+ else {
247
+ const andOr = mode === (filters === null || filters === void 0 ? void 0 : filters.containsAny) ? " OR " : " AND ";
248
+ iterate(mode, (key, value) => {
249
+ let statement = "";
250
+ for (let i in value) {
251
+ if (typeof value[i] === "string") {
252
+ value[i] = `%"${value[i].toLowerCase()}"%`;
253
+ }
254
+ else {
255
+ value[i] = `%${value[i]}%`;
256
+ }
257
+ statement +=
258
+ (statement ? andOr : "") +
259
+ `COALESCE(LOWER(${likeKey(this.client, key)}), '') LIKE ?`;
260
+ }
261
+ if (statement === "") {
262
+ return;
263
+ }
264
+ // @ts-ignore
265
+ query = query[rawFnc](`${not}(${statement})`, value);
266
+ });
267
+ }
268
+ };
269
+ if (filters.oneOf) {
270
+ iterate(filters.oneOf, (key, array) => {
271
+ const fnc = allOr ? "orWhereIn" : "whereIn";
272
+ query = query[fnc](key, Array.isArray(array) ? array : [array]);
273
+ });
274
+ }
275
+ if (filters.string) {
276
+ iterate(filters.string, (key, value) => {
277
+ const fnc = allOr ? "orWhere" : "where";
278
+ // postgres supports ilike, nothing else does
279
+ if (this.client === types_1.SqlClient.POSTGRES) {
280
+ query = query[fnc](key, "ilike", `${value}%`);
281
+ }
282
+ else {
283
+ const rawFnc = `${fnc}Raw`;
284
+ // @ts-ignore
285
+ query = query[rawFnc](`LOWER(${likeKey(this.client, key)}) LIKE ?`, [
286
+ `${value.toLowerCase()}%`,
287
+ ]);
288
+ }
289
+ });
290
+ }
291
+ if (filters.fuzzy) {
292
+ iterate(filters.fuzzy, like);
293
+ }
294
+ if (filters.range) {
295
+ iterate(filters.range, (key, value) => {
296
+ const isEmptyObject = (val) => {
297
+ return (val &&
298
+ Object.keys(val).length === 0 &&
299
+ Object.getPrototypeOf(val) === Object.prototype);
300
+ };
301
+ if (isEmptyObject(value.low)) {
302
+ value.low = "";
303
+ }
304
+ if (isEmptyObject(value.high)) {
305
+ value.high = "";
306
+ }
307
+ const lowValid = (0, utils_1.isValidFilter)(value.low), highValid = (0, utils_1.isValidFilter)(value.high);
308
+ if (lowValid && highValid) {
309
+ query = sqlStatements.between(query, key, value.low, value.high);
310
+ }
311
+ else if (lowValid) {
312
+ query = sqlStatements.lte(query, key, value.low);
313
+ }
314
+ else if (highValid) {
315
+ query = sqlStatements.gte(query, key, value.high);
316
+ }
317
+ });
318
+ }
319
+ if (filters.equal) {
320
+ iterate(filters.equal, (key, value) => {
321
+ const fnc = allOr ? "orWhereRaw" : "whereRaw";
322
+ if (this.client === types_1.SqlClient.MS_SQL) {
323
+ query = query[fnc](`CASE WHEN ${likeKey(this.client, key)} = ? THEN 1 ELSE 0 END = 1`, [value]);
324
+ }
325
+ else {
326
+ query = query[fnc](`COALESCE(${likeKey(this.client, key)} = ?, FALSE)`, [value]);
327
+ }
328
+ });
329
+ }
330
+ if (filters.notEqual) {
331
+ iterate(filters.notEqual, (key, value) => {
332
+ const fnc = allOr ? "orWhereRaw" : "whereRaw";
333
+ if (this.client === types_1.SqlClient.MS_SQL) {
334
+ query = query[fnc](`CASE WHEN ${likeKey(this.client, key)} = ? THEN 1 ELSE 0 END = 0`, [value]);
335
+ }
336
+ else {
337
+ query = query[fnc](`COALESCE(${likeKey(this.client, key)} != ?, TRUE)`, [value]);
338
+ }
339
+ });
340
+ }
341
+ if (filters.empty) {
342
+ iterate(filters.empty, key => {
343
+ const fnc = allOr ? "orWhereNull" : "whereNull";
344
+ query = query[fnc](key);
345
+ });
346
+ }
347
+ if (filters.notEmpty) {
348
+ iterate(filters.notEmpty, key => {
349
+ const fnc = allOr ? "orWhereNotNull" : "whereNotNull";
350
+ query = query[fnc](key);
351
+ });
352
+ }
353
+ if (filters.contains) {
354
+ contains(filters.contains);
355
+ }
356
+ if (filters.notContains) {
357
+ contains(filters.notContains);
358
+ }
359
+ if (filters.containsAny) {
360
+ contains(filters.containsAny, true);
361
+ }
362
+ // when searching internal tables make sure long looking for rows
363
+ if (filters.documentType && !(0, utils_1.isExternalTable)(table)) {
364
+ const tableRef = ((_a = opts === null || opts === void 0 ? void 0 : opts.aliases) === null || _a === void 0 ? void 0 : _a[table._id]) || table._id;
365
+ // has to be its own option, must always be AND onto the search
366
+ query.andWhereLike(`${tableRef}._id`, `${(0, types_1.prefixed)(filters.documentType)}%`);
367
+ }
368
+ return query;
369
+ }
370
+ addSorting(query, json) {
371
+ let { sort, paginate } = json;
372
+ const table = json.meta.table;
373
+ const tableName = getTableName(table);
374
+ const aliases = json.tableAliases;
375
+ const aliased = tableName && (aliases === null || aliases === void 0 ? void 0 : aliases[tableName]) ? aliases[tableName] : table === null || table === void 0 ? void 0 : table.name;
376
+ if (sort && Object.keys(sort || {}).length > 0) {
377
+ for (let [key, value] of Object.entries(sort)) {
378
+ const direction = value.direction === types_1.SortDirection.ASCENDING ? "asc" : "desc";
379
+ let nulls;
380
+ if (this.client === types_1.SqlClient.POSTGRES) {
381
+ // All other clients already sort this as expected by default, and adding this to the rest of the clients is causing issues
382
+ nulls = value.direction === types_1.SortDirection.ASCENDING ? "first" : "last";
383
+ }
384
+ query = query.orderBy(`${aliased}.${key}`, direction, nulls);
385
+ }
386
+ }
387
+ else if (this.client === types_1.SqlClient.MS_SQL && (paginate === null || paginate === void 0 ? void 0 : paginate.limit)) {
388
+ // @ts-ignore
389
+ query = query.orderBy(`${aliased}.${table === null || table === void 0 ? void 0 : table.primary[0]}`);
390
+ }
391
+ return query;
392
+ }
393
+ tableNameWithSchema(tableName, opts) {
394
+ let withSchema = (opts === null || opts === void 0 ? void 0 : opts.schema) ? `${opts.schema}.${tableName}` : tableName;
395
+ if (opts === null || opts === void 0 ? void 0 : opts.alias) {
396
+ withSchema += ` as ${opts.alias}`;
397
+ }
398
+ return withSchema;
399
+ }
400
+ addRelationships(query, fromTable, relationships, schema, aliases) {
401
+ if (!relationships) {
402
+ return query;
403
+ }
404
+ const tableSets = {};
405
+ // aggregate into table sets (all the same to tables)
406
+ for (let relationship of relationships) {
407
+ const keyObj = {
408
+ toTable: relationship.tableName,
409
+ throughTable: undefined,
410
+ };
411
+ if (relationship.through) {
412
+ keyObj.throughTable = relationship.through;
413
+ }
414
+ const key = JSON.stringify(keyObj);
415
+ if (tableSets[key]) {
416
+ tableSets[key].push(relationship);
417
+ }
418
+ else {
419
+ tableSets[key] = [relationship];
420
+ }
421
+ }
422
+ for (let [key, relationships] of Object.entries(tableSets)) {
423
+ const { toTable, throughTable } = JSON.parse(key);
424
+ const toAlias = (aliases === null || aliases === void 0 ? void 0 : aliases[toTable]) || toTable, throughAlias = (aliases === null || aliases === void 0 ? void 0 : aliases[throughTable]) || throughTable, fromAlias = (aliases === null || aliases === void 0 ? void 0 : aliases[fromTable]) || fromTable;
425
+ let toTableWithSchema = this.tableNameWithSchema(toTable, {
426
+ alias: toAlias,
427
+ schema,
428
+ });
429
+ let throughTableWithSchema = this.tableNameWithSchema(throughTable, {
430
+ alias: throughAlias,
431
+ schema,
432
+ });
433
+ if (!throughTable) {
434
+ // @ts-ignore
435
+ query = query.leftJoin(toTableWithSchema, function () {
436
+ for (let relationship of relationships) {
437
+ const from = relationship.from, to = relationship.to;
438
+ // @ts-ignore
439
+ this.orOn(`${fromAlias}.${from}`, "=", `${toAlias}.${to}`);
440
+ }
441
+ });
442
+ }
443
+ else {
444
+ query = query
445
+ // @ts-ignore
446
+ .leftJoin(throughTableWithSchema, function () {
447
+ for (let relationship of relationships) {
448
+ const fromPrimary = relationship.fromPrimary;
449
+ const from = relationship.from;
450
+ // @ts-ignore
451
+ this.orOn(`${fromAlias}.${fromPrimary}`, "=", `${throughAlias}.${from}`);
452
+ }
453
+ })
454
+ .leftJoin(toTableWithSchema, function () {
455
+ for (let relationship of relationships) {
456
+ const toPrimary = relationship.toPrimary;
457
+ const to = relationship.to;
458
+ // @ts-ignore
459
+ this.orOn(`${toAlias}.${toPrimary}`, `${throughAlias}.${to}`);
460
+ }
461
+ });
462
+ }
463
+ }
464
+ return query.limit(BASE_LIMIT);
465
+ }
466
+ knexWithAlias(knex, endpoint, aliases) {
467
+ const tableName = endpoint.entityId;
468
+ const tableAlias = aliases === null || aliases === void 0 ? void 0 : aliases[tableName];
469
+ const query = knex(this.tableNameWithSchema(tableName, {
470
+ alias: tableAlias,
471
+ schema: endpoint.schema,
472
+ }));
473
+ return query;
474
+ }
475
+ create(knex, json, opts) {
476
+ const { endpoint, body } = json;
477
+ let query = this.knexWithAlias(knex, endpoint);
478
+ const parsedBody = parseBody(body);
479
+ // make sure no null values in body for creation
480
+ for (let [key, value] of Object.entries(parsedBody)) {
481
+ if (value == null) {
482
+ delete parsedBody[key];
483
+ }
484
+ }
485
+ // mysql can't use returning
486
+ if (opts.disableReturning) {
487
+ return query.insert(parsedBody);
488
+ }
489
+ else {
490
+ return query.insert(parsedBody).returning("*");
491
+ }
492
+ }
493
+ bulkCreate(knex, json) {
494
+ const { endpoint, body } = json;
495
+ let query = this.knexWithAlias(knex, endpoint);
496
+ if (!Array.isArray(body)) {
497
+ return query;
498
+ }
499
+ const parsedBody = body.map(row => parseBody(row));
500
+ return query.insert(parsedBody);
501
+ }
502
+ read(knex, json, limit) {
503
+ let { endpoint, resource, filters, paginate, relationships, tableAliases } = json;
504
+ const tableName = endpoint.entityId;
505
+ // select all if not specified
506
+ if (!resource) {
507
+ resource = { fields: [] };
508
+ }
509
+ let selectStatement = "*";
510
+ // handle select
511
+ if (resource.fields && resource.fields.length > 0) {
512
+ // select the resources as the format "table.columnName" - this is what is provided
513
+ // by the resource builder further up
514
+ selectStatement = generateSelectStatement(json, knex);
515
+ }
516
+ let foundLimit = limit || BASE_LIMIT;
517
+ // handle pagination
518
+ let foundOffset = null;
519
+ if (paginate && paginate.page && paginate.limit) {
520
+ // @ts-ignore
521
+ const page = paginate.page <= 1 ? 0 : paginate.page - 1;
522
+ const offset = page * paginate.limit;
523
+ foundLimit = paginate.limit;
524
+ foundOffset = offset;
525
+ }
526
+ else if (paginate && paginate.limit) {
527
+ foundLimit = paginate.limit;
528
+ }
529
+ // start building the query
530
+ let query = this.knexWithAlias(knex, endpoint, tableAliases);
531
+ query = query.limit(foundLimit);
532
+ if (foundOffset) {
533
+ query = query.offset(foundOffset);
534
+ }
535
+ query = this.addFilters(query, filters, json.meta.table, {
536
+ aliases: tableAliases,
537
+ });
538
+ // add sorting to pre-query
539
+ query = this.addSorting(query, json);
540
+ const alias = (tableAliases === null || tableAliases === void 0 ? void 0 : tableAliases[tableName]) || tableName;
541
+ let preQuery = knex({
542
+ [alias]: query,
543
+ }).select(selectStatement);
544
+ // have to add after as well (this breaks MS-SQL)
545
+ if (this.client !== types_1.SqlClient.MS_SQL) {
546
+ preQuery = this.addSorting(preQuery, json);
547
+ }
548
+ // handle joins
549
+ query = this.addRelationships(preQuery, tableName, relationships, endpoint.schema, tableAliases);
550
+ return this.addFilters(query, filters, json.meta.table, {
551
+ relationship: true,
552
+ aliases: tableAliases,
553
+ });
554
+ }
555
+ update(knex, json, opts) {
556
+ const { endpoint, body, filters, tableAliases } = json;
557
+ let query = this.knexWithAlias(knex, endpoint, tableAliases);
558
+ const parsedBody = parseBody(body);
559
+ query = this.addFilters(query, filters, json.meta.table, {
560
+ aliases: tableAliases,
561
+ });
562
+ // mysql can't use returning
563
+ if (opts.disableReturning) {
564
+ return query.update(parsedBody);
565
+ }
566
+ else {
567
+ return query.update(parsedBody).returning("*");
568
+ }
569
+ }
570
+ delete(knex, json, opts) {
571
+ const { endpoint, filters, tableAliases } = json;
572
+ let query = this.knexWithAlias(knex, endpoint, tableAliases);
573
+ query = this.addFilters(query, filters, json.meta.table, {
574
+ aliases: tableAliases,
575
+ });
576
+ // mysql can't use returning
577
+ if (opts.disableReturning) {
578
+ return query.delete();
579
+ }
580
+ else {
581
+ return query.delete().returning(generateSelectStatement(json, knex));
582
+ }
583
+ }
584
+ }
585
+ class SqlQueryBuilder extends sqlTable_1.default {
586
+ // pass through client to get flavour of SQL
587
+ constructor(client, limit = BASE_LIMIT) {
588
+ super(client);
589
+ this.limit = limit;
590
+ }
591
+ /**
592
+ * @param json The JSON query DSL which is to be converted to SQL.
593
+ * @param opts extra options which are to be passed into the query builder, e.g. disableReturning
594
+ * which for the sake of mySQL stops adding the returning statement to inserts, updates and deletes.
595
+ * @return the query ready to be passed to the driver.
596
+ */
597
+ _query(json, opts = {}) {
598
+ const sqlClient = this.getSqlClient();
599
+ const config = {
600
+ client: sqlClient,
601
+ };
602
+ if (sqlClient === types_1.SqlClient.SQL_LITE) {
603
+ config.useNullAsDefault = true;
604
+ }
605
+ const client = (0, knex_1.knex)(config);
606
+ let query;
607
+ const builder = new InternalBuilder(sqlClient);
608
+ switch (this._operation(json)) {
609
+ case types_1.Operation.CREATE:
610
+ query = builder.create(client, json, opts);
611
+ break;
612
+ case types_1.Operation.READ:
613
+ query = builder.read(client, json, this.limit);
614
+ break;
615
+ case types_1.Operation.UPDATE:
616
+ query = builder.update(client, json, opts);
617
+ break;
618
+ case types_1.Operation.DELETE:
619
+ query = builder.delete(client, json, opts);
620
+ break;
621
+ case types_1.Operation.BULK_CREATE:
622
+ query = builder.bulkCreate(client, json);
623
+ break;
624
+ case types_1.Operation.CREATE_TABLE:
625
+ case types_1.Operation.UPDATE_TABLE:
626
+ case types_1.Operation.DELETE_TABLE:
627
+ return this._tableQuery(json);
628
+ default:
629
+ throw `Operation type is not supported by SQL query builder`;
630
+ }
631
+ if (opts === null || opts === void 0 ? void 0 : opts.disableBindings) {
632
+ return { sql: query.toString() };
633
+ }
634
+ else {
635
+ let native = (0, utils_1.getNativeSql)(query);
636
+ if (sqlClient === types_1.SqlClient.SQL_LITE) {
637
+ native = convertBooleans(native);
638
+ }
639
+ return native;
640
+ }
641
+ }
642
+ getReturningRow(queryFn, json) {
643
+ var _a;
644
+ return __awaiter(this, void 0, void 0, function* () {
645
+ if (!json.extra || !json.extra.idFilter) {
646
+ return {};
647
+ }
648
+ const input = this._query({
649
+ endpoint: Object.assign(Object.assign({}, json.endpoint), { operation: types_1.Operation.READ }),
650
+ resource: {
651
+ fields: [],
652
+ },
653
+ filters: (_a = json.extra) === null || _a === void 0 ? void 0 : _a.idFilter,
654
+ paginate: {
655
+ limit: 1,
656
+ },
657
+ meta: json.meta,
658
+ });
659
+ return queryFn(input, types_1.Operation.READ);
660
+ });
661
+ }
662
+ // when creating if an ID has been inserted need to make sure
663
+ // the id filter is enriched with it before trying to retrieve the row
664
+ checkLookupKeys(id, json) {
665
+ var _a;
666
+ if (!id || !json.meta.table || !json.meta.table.primary) {
667
+ return json;
668
+ }
669
+ const primaryKey = (_a = json.meta.table.primary) === null || _a === void 0 ? void 0 : _a[0];
670
+ json.extra = {
671
+ idFilter: {
672
+ equal: {
673
+ [primaryKey]: id,
674
+ },
675
+ },
676
+ };
677
+ return json;
678
+ }
679
+ // this function recreates the returning functionality of postgres
680
+ queryWithReturning(json, queryFn, processFn = (result) => result) {
681
+ return __awaiter(this, void 0, void 0, function* () {
682
+ const sqlClient = this.getSqlClient();
683
+ const operation = this._operation(json);
684
+ const input = this._query(json, { disableReturning: true });
685
+ if (Array.isArray(input)) {
686
+ const responses = [];
687
+ for (let query of input) {
688
+ responses.push(yield queryFn(query, operation));
689
+ }
690
+ return responses;
691
+ }
692
+ let row;
693
+ // need to manage returning, a feature mySQL can't do
694
+ if (operation === types_1.Operation.DELETE) {
695
+ row = processFn(yield this.getReturningRow(queryFn, json));
696
+ }
697
+ const response = yield queryFn(input, operation);
698
+ const results = processFn(response);
699
+ // same as delete, manage returning
700
+ if (operation === types_1.Operation.CREATE || operation === types_1.Operation.UPDATE) {
701
+ let id;
702
+ if (sqlClient === types_1.SqlClient.MS_SQL) {
703
+ id = results === null || results === void 0 ? void 0 : results[0].id;
704
+ }
705
+ else if (sqlClient === types_1.SqlClient.MY_SQL) {
706
+ id = results === null || results === void 0 ? void 0 : results.insertId;
707
+ }
708
+ row = processFn(yield this.getReturningRow(queryFn, this.checkLookupKeys(id, json)));
709
+ }
710
+ if (operation !== types_1.Operation.READ) {
711
+ return row;
712
+ }
713
+ return results.length ? results : [{ [operation.toLowerCase()]: true }];
714
+ });
715
+ }
716
+ convertJsonStringColumns(table, results, aliases) {
717
+ const tableName = getTableName(table);
718
+ for (const [name, field] of Object.entries(table.schema)) {
719
+ if (!this._isJsonColumn(field)) {
720
+ continue;
721
+ }
722
+ const aliasedTableName = (tableName && (aliases === null || aliases === void 0 ? void 0 : aliases[tableName])) || tableName;
723
+ const fullName = `${aliasedTableName}.${name}`;
724
+ for (let row of results) {
725
+ if (typeof row[fullName] === "string") {
726
+ row[fullName] = JSON.parse(row[fullName]);
727
+ }
728
+ if (typeof row[name] === "string") {
729
+ row[name] = JSON.parse(row[name]);
730
+ }
731
+ }
732
+ }
733
+ return results;
734
+ }
735
+ _isJsonColumn(field) {
736
+ return (types_1.JsonTypes.includes(field.type) &&
737
+ !shared_core_1.helpers.schema.isDeprecatedSingleUserColumn(field));
738
+ }
739
+ log(query, values) {
740
+ if (!environment_1.default.SQL_LOGGING_ENABLE) {
741
+ return;
742
+ }
743
+ const sqlClient = this.getSqlClient();
744
+ let string = `[SQL] [${sqlClient.toUpperCase()}] query="${query}"`;
745
+ if (values) {
746
+ string += ` values="${values.join(", ")}"`;
747
+ }
748
+ console.log(string);
749
+ }
750
+ }
751
+ exports.default = SqlQueryBuilder;
752
+ //# sourceMappingURL=sql.js.map