@budibase/server 0.9.184 → 0.9.185-alpha.11
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/Dockerfile +1 -1
- package/__mocks__/mssql.ts +1 -1
- package/__mocks__/pg.ts +3 -0
- package/builder/assets/bb-space-man.5be51a45.svg +290 -0
- package/builder/assets/index.053c6f7a.js +1321 -0
- package/builder/assets/index.4a7c5636.css +1 -0
- package/builder/assets/vendor.470042a4.js +152 -0
- package/builder/assets/vendor.5e1fe644.css +1 -0
- package/builder/index.html +4 -4
- package/coverage/clover.xml +1357 -959
- package/coverage/coverage-final.json +91 -87
- package/coverage/lcov-report/api/controllers/analytics.js.html +2 -2
- package/coverage/lcov-report/api/controllers/apikeys.js.html +2 -2
- package/coverage/lcov-report/api/controllers/application.js.html +65 -14
- package/coverage/lcov-report/api/controllers/auth.js.html +19 -10
- package/coverage/lcov-report/api/controllers/automation.js.html +3 -3
- package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
- package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
- package/coverage/lcov-report/api/controllers/component.js.html +4 -4
- package/coverage/lcov-report/api/controllers/datasource.js.html +147 -27
- package/coverage/lcov-report/api/controllers/dev.js.html +16 -7
- package/coverage/lcov-report/api/controllers/hosting.js.html +10 -10
- package/coverage/lcov-report/api/controllers/index.html +90 -90
- package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
- package/coverage/lcov-report/api/controllers/layout.js.html +3 -3
- package/coverage/lcov-report/api/controllers/metadata.js.html +3 -3
- package/coverage/lcov-report/api/controllers/permission.js.html +3 -3
- package/coverage/lcov-report/api/controllers/query.js.html +53 -209
- package/coverage/lcov-report/api/controllers/role.js.html +9 -9
- package/coverage/lcov-report/api/controllers/routing.js.html +3 -3
- package/coverage/lcov-report/api/controllers/row/external.js.html +12 -6
- package/coverage/lcov-report/api/controllers/row/index.html +26 -26
- package/coverage/lcov-report/api/controllers/row/index.js.html +2 -2
- package/coverage/lcov-report/api/controllers/row/internal.js.html +9 -9
- package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +2 -2
- package/coverage/lcov-report/api/controllers/row/utils.js.html +21 -30
- package/coverage/lcov-report/api/controllers/screen.js.html +2 -2
- package/coverage/lcov-report/api/controllers/script.js.html +3 -3
- package/coverage/lcov-report/api/controllers/table/external.js.html +980 -0
- package/coverage/lcov-report/api/controllers/table/index.html +56 -26
- package/coverage/lcov-report/api/controllers/table/index.js.html +80 -350
- package/coverage/lcov-report/api/controllers/table/internal.js.html +545 -0
- package/coverage/lcov-report/api/controllers/table/utils.js.html +172 -49
- package/coverage/lcov-report/api/controllers/templates.js.html +4 -4
- package/coverage/lcov-report/api/controllers/user.js.html +390 -33
- package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
- package/coverage/lcov-report/api/controllers/view/index.html +9 -9
- package/coverage/lcov-report/api/controllers/view/index.js.html +5 -5
- package/coverage/lcov-report/api/controllers/view/utils.js.html +11 -11
- package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +3 -3
- package/coverage/lcov-report/api/controllers/webhook.js.html +4 -4
- package/coverage/lcov-report/api/index.html +1 -1
- package/coverage/lcov-report/api/index.js.html +1 -1
- package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
- package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
- package/coverage/lcov-report/api/routes/application.js.html +1 -1
- package/coverage/lcov-report/api/routes/auth.js.html +1 -1
- package/coverage/lcov-report/api/routes/automation.js.html +1 -1
- package/coverage/lcov-report/api/routes/backup.js.html +1 -1
- package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
- package/coverage/lcov-report/api/routes/component.js.html +1 -1
- package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
- package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
- package/coverage/lcov-report/api/routes/dev.js.html +2 -2
- package/coverage/lcov-report/api/routes/hosting.js.html +1 -1
- package/coverage/lcov-report/api/routes/index.html +4 -4
- package/coverage/lcov-report/api/routes/index.js.html +1 -1
- package/coverage/lcov-report/api/routes/integration.js.html +1 -1
- package/coverage/lcov-report/api/routes/layout.js.html +1 -1
- package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
- package/coverage/lcov-report/api/routes/permission.js.html +1 -1
- package/coverage/lcov-report/api/routes/query.js.html +5 -2
- package/coverage/lcov-report/api/routes/role.js.html +1 -1
- package/coverage/lcov-report/api/routes/routing.js.html +1 -1
- package/coverage/lcov-report/api/routes/row.js.html +1 -1
- package/coverage/lcov-report/api/routes/screen.js.html +1 -1
- package/coverage/lcov-report/api/routes/script.js.html +1 -1
- package/coverage/lcov-report/api/routes/static.js.html +2 -2
- package/coverage/lcov-report/api/routes/table.js.html +35 -2
- package/coverage/lcov-report/api/routes/templates.js.html +1 -1
- package/coverage/lcov-report/api/routes/user.js.html +17 -2
- package/coverage/lcov-report/api/routes/view.js.html +1 -1
- package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
- package/coverage/lcov-report/automations/actions.js.html +2 -2
- package/coverage/lcov-report/automations/automationUtils.js.html +3 -3
- package/coverage/lcov-report/automations/bullboard.js.html +71 -20
- package/coverage/lcov-report/automations/index.html +33 -48
- package/coverage/lcov-report/automations/index.js.html +2 -2
- package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
- package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
- package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
- package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
- package/coverage/lcov-report/automations/steps/discord.js.html +3 -3
- package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
- package/coverage/lcov-report/automations/steps/executeScript.js.html +3 -3
- package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
- package/coverage/lcov-report/automations/steps/index.html +14 -14
- package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
- package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +3 -3
- package/coverage/lcov-report/automations/steps/queryRows.js.html +3 -3
- package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
- package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
- package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
- package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
- package/coverage/lcov-report/automations/steps/utils.js.html +7 -7
- package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
- package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
- package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
- package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
- package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
- package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
- package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
- package/coverage/lcov-report/automations/triggers.js.html +22 -10
- package/coverage/lcov-report/automations/utils.js.html +42 -12
- package/coverage/lcov-report/constants/index.html +5 -5
- package/coverage/lcov-report/constants/index.js.html +49 -4
- package/coverage/lcov-report/constants/layouts.js.html +1 -1
- package/coverage/lcov-report/constants/screens.js.html +1 -1
- package/coverage/lcov-report/db/client.js.html +2 -2
- package/coverage/lcov-report/db/inMemoryView.js.html +2 -2
- package/coverage/lcov-report/db/index.html +2 -2
- package/coverage/lcov-report/db/index.js.html +1 -1
- package/coverage/lcov-report/db/linkedRows/LinkController.js.html +6 -6
- package/coverage/lcov-report/db/linkedRows/index.html +10 -10
- package/coverage/lcov-report/db/linkedRows/index.js.html +5 -5
- package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +6 -6
- package/coverage/lcov-report/db/newid.js.html +1 -1
- package/coverage/lcov-report/db/utils.js.html +1 -1
- package/coverage/lcov-report/events/AutomationEmitter.js.html +6 -6
- package/coverage/lcov-report/events/index.html +6 -6
- package/coverage/lcov-report/events/index.js.html +1 -1
- package/coverage/lcov-report/events/utils.js.html +5 -5
- package/coverage/lcov-report/index.html +98 -83
- package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
- package/coverage/lcov-report/integrations/tests/index.html +1 -1
- package/coverage/lcov-report/middleware/appInfo.js.html +2 -2
- package/coverage/lcov-report/middleware/authorized.js.html +11 -8
- package/coverage/lcov-report/middleware/builder.js.html +16 -7
- package/coverage/lcov-report/middleware/currentapp.js.html +6 -6
- package/coverage/lcov-report/middleware/index.html +23 -23
- package/coverage/lcov-report/middleware/joi-validator.js.html +2 -2
- package/coverage/lcov-report/middleware/resourceId.js.html +18 -18
- package/coverage/lcov-report/middleware/selfhost.js.html +2 -2
- package/coverage/lcov-report/middleware/usageQuota.js.html +4 -4
- package/coverage/lcov-report/middleware/utils.js.html +1 -1
- package/coverage/lcov-report/{automations/thread.js.html → threads/automation.js.html} +83 -32
- package/coverage/lcov-report/threads/index.html +141 -0
- package/coverage/lcov-report/threads/index.js.html +260 -0
- package/coverage/lcov-report/threads/query.js.html +269 -0
- package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
- package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
- package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
- package/coverage/lcov-report/utilities/csvParser.js.html +144 -15
- package/coverage/lcov-report/utilities/global.js.html +26 -11
- package/coverage/lcov-report/utilities/index.html +49 -49
- package/coverage/lcov-report/utilities/index.js.html +65 -17
- package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +30 -9
- package/coverage/lcov-report/utilities/queue/index.html +14 -14
- package/coverage/lcov-report/utilities/routing/index.html +1 -1
- package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
- package/coverage/lcov-report/utilities/rowProcessor/index.html +4 -4
- package/coverage/lcov-report/utilities/rowProcessor/index.js.html +2 -2
- package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +3 -3
- package/coverage/lcov-report/utilities/scriptRunner.js.html +16 -7
- package/coverage/lcov-report/utilities/security.js.html +3 -3
- package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
- package/coverage/lcov-report/utilities/users.js.html +1 -1
- package/coverage/lcov-report/utilities/workerRequests.js.html +5 -5
- package/coverage/lcov.info +2301 -1599
- package/dist/api/controllers/application.js +16 -2
- package/dist/api/controllers/auth.js +3 -3
- package/dist/api/controllers/datasource.js +47 -16
- package/dist/api/controllers/deploy/index.js +2 -0
- package/dist/api/controllers/dev.js +4 -1
- package/dist/api/controllers/query.js +30 -58
- package/dist/api/controllers/row/ExternalRequest.js +37 -14
- package/dist/api/controllers/row/external.js +1 -0
- package/dist/api/controllers/row/utils.js +6 -12
- package/dist/api/controllers/static/index.js +1 -1
- package/dist/api/controllers/table/external.js +242 -0
- package/dist/api/controllers/table/index.js +49 -123
- package/dist/api/controllers/table/internal.js +150 -0
- package/dist/api/controllers/table/utils.js +62 -28
- package/dist/api/controllers/user.js +123 -11
- package/dist/api/routes/query.js +1 -1
- package/dist/api/routes/table.js +7 -1
- package/dist/api/routes/user.js +2 -1
- package/dist/app.js +2 -11
- package/dist/automations/bullboard.js +37 -10
- package/dist/automations/triggers.js +7 -3
- package/dist/automations/utils.js +11 -2
- package/dist/constants/index.js +13 -0
- package/dist/definitions/datasource.js +4 -0
- package/dist/integrations/base/IntegrationBase.js +2 -0
- package/dist/integrations/base/datasourcePlus.js +2 -0
- package/dist/integrations/base/sql.js +334 -180
- package/dist/integrations/base/sqlTable.js +143 -0
- package/dist/integrations/base/utils.js +30 -0
- package/dist/integrations/microsoftSqlServer.js +117 -9
- package/dist/integrations/mysql.js +14 -84
- package/dist/integrations/postgres.js +35 -28
- package/dist/integrations/utils.js +56 -9
- package/dist/middleware/authorized.js +2 -1
- package/dist/middleware/builder.js +4 -1
- package/dist/package.json +7 -6
- package/dist/tests/utilities/TestConfiguration.js +2 -1
- package/dist/{automations/thread.js → threads/automation.js} +23 -7
- package/dist/threads/index.js +58 -0
- package/dist/threads/query.js +61 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utilities/csvParser.js +46 -5
- package/dist/utilities/global.js +6 -2
- package/dist/utilities/index.js +18 -2
- package/dist/utilities/queue/inMemoryQueue.js +8 -0
- package/dist/utilities/scriptRunner.js +4 -1
- package/package.json +8 -7
- package/scripts/integrations/mssql/data/Dockerfile +9 -0
- package/scripts/integrations/mssql/data/entrypoint.sh +24 -0
- package/scripts/integrations/mssql/data/setup.sql +54 -0
- package/scripts/integrations/mssql/docker-compose.yaml +12 -0
- package/scripts/integrations/mssql/reset.sh +3 -0
- package/scripts/likeCypress.ts +33 -0
- package/src/api/controllers/application.js +19 -2
- package/src/api/controllers/auth.js +3 -0
- package/src/api/controllers/datasource.js +56 -16
- package/src/api/controllers/deploy/index.js +2 -0
- package/src/api/controllers/dev.js +4 -1
- package/src/api/controllers/query.js +29 -81
- package/src/api/controllers/row/ExternalRequest.ts +47 -16
- package/src/api/controllers/row/external.js +2 -0
- package/src/api/controllers/row/utils.js +8 -11
- package/src/api/controllers/static/index.js +1 -1
- package/src/api/controllers/table/external.js +300 -0
- package/src/api/controllers/table/index.js +47 -137
- package/src/api/controllers/table/internal.js +155 -0
- package/src/api/controllers/table/utils.js +74 -33
- package/src/api/controllers/user.js +132 -13
- package/src/api/routes/query.js +1 -0
- package/src/api/routes/table.js +11 -0
- package/src/api/routes/tests/datasource.spec.js +2 -2
- package/src/api/routes/tests/misc.spec.js +5 -1
- package/src/api/routes/tests/query.spec.js +0 -1
- package/src/api/routes/user.js +5 -0
- package/src/app.ts +2 -10
- package/src/automations/bullboard.js +28 -11
- package/src/automations/tests/automation.spec.js +3 -4
- package/src/automations/triggers.js +7 -3
- package/src/automations/utils.js +12 -2
- package/src/constants/index.js +15 -0
- package/src/definitions/common.ts +7 -1
- package/src/definitions/datasource.ts +9 -3
- package/src/integrations/airtable.ts +2 -1
- package/src/integrations/arangodb.ts +2 -1
- package/src/integrations/base/IntegrationBase.ts +6 -0
- package/src/integrations/base/datasourcePlus.ts +9 -0
- package/src/integrations/base/sql.ts +359 -222
- package/src/integrations/base/sqlTable.ts +180 -0
- package/src/integrations/base/utils.ts +22 -0
- package/src/integrations/couchdb.ts +2 -1
- package/src/integrations/dynamodb.ts +2 -1
- package/src/integrations/elasticsearch.ts +2 -1
- package/src/integrations/microsoftSqlServer.ts +145 -13
- package/src/integrations/mongodb.ts +2 -1
- package/src/integrations/mysql.ts +20 -94
- package/src/integrations/postgres.ts +45 -35
- package/src/integrations/rest.ts +2 -1
- package/src/integrations/s3.ts +2 -1
- package/src/integrations/tests/microsoftSqlServer.spec.js +22 -15
- package/src/integrations/tests/postgres.spec.js +4 -0
- package/src/integrations/utils.ts +60 -8
- package/src/middleware/authorized.js +2 -1
- package/src/middleware/builder.js +4 -1
- package/src/tests/utilities/TestConfiguration.js +2 -1
- package/src/{automations/thread.js → threads/automation.js} +24 -7
- package/src/threads/index.js +60 -0
- package/src/threads/query.js +63 -0
- package/src/utilities/csvParser.js +48 -5
- package/src/utilities/global.js +7 -2
- package/src/utilities/index.js +18 -2
- package/src/utilities/queue/inMemoryQueue.js +7 -0
- package/src/utilities/scriptRunner.js +4 -1
- package/src/utilities/tests/__snapshots__/csvParser.spec.js.snap +0 -6
- package/src/utilities/tests/csvParser.spec.js +3 -0
- package/tsconfig.json +1 -1
- package/builder/assets/index.34964222.js +0 -201
- package/builder/assets/index.7469e1e4.css +0 -29
- package/builder/assets/vendor.278a305c.js +0 -9
- package/builder/assets/vendor.d75d5eb7.css +0 -1
|
@@ -1,210 +1,282 @@
|
|
|
1
1
|
"use strict";
|
|
2
|
+
var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, generator) {
|
|
3
|
+
function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
|
|
4
|
+
return new (P || (P = Promise))(function (resolve, reject) {
|
|
5
|
+
function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
|
|
6
|
+
function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
|
|
7
|
+
function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
|
|
8
|
+
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
|
+
});
|
|
10
|
+
};
|
|
11
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
12
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
13
|
+
};
|
|
2
14
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
15
|
const knex_1 = require("knex");
|
|
4
|
-
const BASE_LIMIT = 5000;
|
|
5
16
|
const datasource_1 = require("../../definitions/datasource");
|
|
6
17
|
const utils_1 = require("../utils");
|
|
7
|
-
|
|
8
|
-
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
}
|
|
16
|
-
return body;
|
|
17
|
-
}
|
|
18
|
-
// right now we only do filters on the specific table being queried
|
|
19
|
-
function addFilters(tableName, query, filters) {
|
|
20
|
-
function iterate(structure, fn) {
|
|
21
|
-
for (let [key, value] of Object.entries(structure)) {
|
|
22
|
-
fn(`${tableName}.${key}`, value);
|
|
23
|
-
}
|
|
24
|
-
}
|
|
25
|
-
if (!filters) {
|
|
26
|
-
return query;
|
|
27
|
-
}
|
|
28
|
-
// if all or specified in filters, then everything is an or
|
|
29
|
-
const allOr = filters.allOr;
|
|
30
|
-
if (filters.oneOf) {
|
|
31
|
-
iterate(filters.oneOf, (key, array) => {
|
|
32
|
-
const fnc = allOr ? "orWhereIn" : "whereIn";
|
|
33
|
-
query = query[fnc](key, array);
|
|
34
|
-
});
|
|
35
|
-
}
|
|
36
|
-
if (filters.string) {
|
|
37
|
-
iterate(filters.string, (key, value) => {
|
|
38
|
-
const fnc = allOr ? "orWhere" : "where";
|
|
39
|
-
query = query[fnc](key, "ilike", `${value}%`);
|
|
40
|
-
});
|
|
41
|
-
}
|
|
42
|
-
if (filters.fuzzy) {
|
|
43
|
-
iterate(filters.fuzzy, (key, value) => {
|
|
44
|
-
const fnc = allOr ? "orWhere" : "where";
|
|
45
|
-
query = query[fnc](key, "ilike", `%${value}%`);
|
|
46
|
-
});
|
|
18
|
+
const sqlTable_1 = __importDefault(require("./sqlTable"));
|
|
19
|
+
const BASE_LIMIT = 5000;
|
|
20
|
+
// these are invalid dates sent by the client, need to convert them to a real max date
|
|
21
|
+
const MIN_ISO_DATE = "0000-00-00T00:00:00.000Z";
|
|
22
|
+
const MAX_ISO_DATE = "9999-00-00T00:00:00.000Z";
|
|
23
|
+
function parse(input) {
|
|
24
|
+
if (Array.isArray(input)) {
|
|
25
|
+
return JSON.stringify(input);
|
|
47
26
|
}
|
|
48
|
-
if (
|
|
49
|
-
|
|
50
|
-
if (!value.high || !value.low) {
|
|
51
|
-
return;
|
|
52
|
-
}
|
|
53
|
-
const fnc = allOr ? "orWhereBetween" : "whereBetween";
|
|
54
|
-
query = query[fnc](key, [value.low, value.high]);
|
|
55
|
-
});
|
|
27
|
+
if (typeof input !== "string") {
|
|
28
|
+
return input;
|
|
56
29
|
}
|
|
57
|
-
if (
|
|
58
|
-
|
|
59
|
-
const fnc = allOr ? "orWhere" : "where";
|
|
60
|
-
query = query[fnc]({ [key]: value });
|
|
61
|
-
});
|
|
30
|
+
if (input === MAX_ISO_DATE) {
|
|
31
|
+
return new Date(8640000000000000);
|
|
62
32
|
}
|
|
63
|
-
if (
|
|
64
|
-
|
|
65
|
-
const fnc = allOr ? "orWhereNot" : "whereNot";
|
|
66
|
-
query = query[fnc]({ [key]: value });
|
|
67
|
-
});
|
|
33
|
+
if (input === MIN_ISO_DATE) {
|
|
34
|
+
return new Date(-8640000000000000);
|
|
68
35
|
}
|
|
69
|
-
if (
|
|
70
|
-
|
|
71
|
-
const fnc = allOr ? "orWhereNull" : "whereNull";
|
|
72
|
-
query = query[fnc](key);
|
|
73
|
-
});
|
|
36
|
+
if (utils_1.isIsoDateString(input)) {
|
|
37
|
+
return new Date(input);
|
|
74
38
|
}
|
|
75
|
-
|
|
76
|
-
iterate(filters.notEmpty, key => {
|
|
77
|
-
const fnc = allOr ? "orWhereNotNull" : "whereNotNull";
|
|
78
|
-
query = query[fnc](key);
|
|
79
|
-
});
|
|
80
|
-
}
|
|
81
|
-
return query;
|
|
39
|
+
return input;
|
|
82
40
|
}
|
|
83
|
-
function
|
|
84
|
-
|
|
85
|
-
|
|
41
|
+
function parseBody(body) {
|
|
42
|
+
for (let [key, value] of Object.entries(body)) {
|
|
43
|
+
body[key] = parse(value);
|
|
86
44
|
}
|
|
87
|
-
|
|
88
|
-
|
|
89
|
-
|
|
90
|
-
|
|
91
|
-
|
|
45
|
+
return body;
|
|
46
|
+
}
|
|
47
|
+
function parseFilters(filters) {
|
|
48
|
+
for (let [key, value] of Object.entries(filters)) {
|
|
49
|
+
let parsed;
|
|
50
|
+
if (typeof value === "object") {
|
|
51
|
+
parsed = parseFilters(value);
|
|
92
52
|
}
|
|
93
53
|
else {
|
|
94
|
-
|
|
95
|
-
const fromPrimary = relationship.fromPrimary;
|
|
96
|
-
const toPrimary = relationship.toPrimary;
|
|
97
|
-
query = query
|
|
98
|
-
// @ts-ignore
|
|
99
|
-
.leftJoin(throughTable, `${fromTable}.${fromPrimary}`, `${throughTable}.${from}`)
|
|
100
|
-
.leftJoin(toTable, `${toTable}.${toPrimary}`, `${throughTable}.${to}`);
|
|
54
|
+
parsed = parse(value);
|
|
101
55
|
}
|
|
56
|
+
// @ts-ignore
|
|
57
|
+
filters[key] = parsed;
|
|
102
58
|
}
|
|
103
|
-
return
|
|
59
|
+
return filters;
|
|
104
60
|
}
|
|
105
|
-
|
|
106
|
-
|
|
107
|
-
|
|
108
|
-
const parsedBody = parseBody(body);
|
|
109
|
-
// make sure no null values in body for creation
|
|
110
|
-
for (let [key, value] of Object.entries(parsedBody)) {
|
|
111
|
-
if (value == null) {
|
|
112
|
-
delete parsedBody[key];
|
|
113
|
-
}
|
|
114
|
-
}
|
|
115
|
-
// mysql can't use returning
|
|
116
|
-
if (opts.disableReturning) {
|
|
117
|
-
return query.insert(parsedBody);
|
|
118
|
-
}
|
|
119
|
-
else {
|
|
120
|
-
return query.insert(parsedBody).returning("*");
|
|
61
|
+
class InternalBuilder {
|
|
62
|
+
constructor(client) {
|
|
63
|
+
this.client = client;
|
|
121
64
|
}
|
|
122
|
-
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
|
|
129
|
-
|
|
130
|
-
|
|
131
|
-
|
|
132
|
-
|
|
133
|
-
//
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
145
|
-
|
|
65
|
+
// right now we only do filters on the specific table being queried
|
|
66
|
+
addFilters(tableName, query, filters) {
|
|
67
|
+
function iterate(structure, fn) {
|
|
68
|
+
for (let [key, value] of Object.entries(structure)) {
|
|
69
|
+
fn(`${tableName}.${key}`, value);
|
|
70
|
+
}
|
|
71
|
+
}
|
|
72
|
+
if (!filters) {
|
|
73
|
+
return query;
|
|
74
|
+
}
|
|
75
|
+
filters = parseFilters(filters);
|
|
76
|
+
// if all or specified in filters, then everything is an or
|
|
77
|
+
const allOr = filters.allOr;
|
|
78
|
+
if (filters.oneOf) {
|
|
79
|
+
iterate(filters.oneOf, (key, array) => {
|
|
80
|
+
const fnc = allOr ? "orWhereIn" : "whereIn";
|
|
81
|
+
query = query[fnc](key, array);
|
|
82
|
+
});
|
|
83
|
+
}
|
|
84
|
+
if (filters.string) {
|
|
85
|
+
iterate(filters.string, (key, value) => {
|
|
86
|
+
const fnc = allOr ? "orWhere" : "where";
|
|
87
|
+
// postgres supports ilike, nothing else does
|
|
88
|
+
if (this.client === utils_1.SqlClients.POSTGRES) {
|
|
89
|
+
query = query[fnc](key, "ilike", `${value}%`);
|
|
90
|
+
}
|
|
91
|
+
else {
|
|
92
|
+
const rawFnc = `${fnc}Raw`;
|
|
93
|
+
// @ts-ignore
|
|
94
|
+
query = query[rawFnc](`LOWER(${key}) LIKE ?`, [`${value}%`]);
|
|
95
|
+
}
|
|
96
|
+
});
|
|
97
|
+
}
|
|
98
|
+
if (filters.fuzzy) {
|
|
99
|
+
iterate(filters.fuzzy, (key, value) => {
|
|
100
|
+
const fnc = allOr ? "orWhere" : "where";
|
|
101
|
+
// postgres supports ilike, nothing else does
|
|
102
|
+
if (this.client === utils_1.SqlClients.POSTGRES) {
|
|
103
|
+
query = query[fnc](key, "ilike", `%${value}%`);
|
|
104
|
+
}
|
|
105
|
+
else {
|
|
106
|
+
const rawFnc = `${fnc}Raw`;
|
|
107
|
+
// @ts-ignore
|
|
108
|
+
query = query[rawFnc](`LOWER(${key}) LIKE ?`, [`%${value}%`]);
|
|
109
|
+
}
|
|
110
|
+
});
|
|
111
|
+
}
|
|
112
|
+
if (filters.range) {
|
|
113
|
+
iterate(filters.range, (key, value) => {
|
|
114
|
+
if (!value.high || !value.low) {
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
const fnc = allOr ? "orWhereBetween" : "whereBetween";
|
|
118
|
+
query = query[fnc](key, [value.low, value.high]);
|
|
119
|
+
});
|
|
120
|
+
}
|
|
121
|
+
if (filters.equal) {
|
|
122
|
+
iterate(filters.equal, (key, value) => {
|
|
123
|
+
const fnc = allOr ? "orWhere" : "where";
|
|
124
|
+
query = query[fnc]({ [key]: value });
|
|
125
|
+
});
|
|
126
|
+
}
|
|
127
|
+
if (filters.notEqual) {
|
|
128
|
+
iterate(filters.notEqual, (key, value) => {
|
|
129
|
+
const fnc = allOr ? "orWhereNot" : "whereNot";
|
|
130
|
+
query = query[fnc]({ [key]: value });
|
|
131
|
+
});
|
|
132
|
+
}
|
|
133
|
+
if (filters.empty) {
|
|
134
|
+
iterate(filters.empty, key => {
|
|
135
|
+
const fnc = allOr ? "orWhereNull" : "whereNull";
|
|
136
|
+
query = query[fnc](key);
|
|
137
|
+
});
|
|
138
|
+
}
|
|
139
|
+
if (filters.notEmpty) {
|
|
140
|
+
iterate(filters.notEmpty, key => {
|
|
141
|
+
const fnc = allOr ? "orWhereNotNull" : "whereNotNull";
|
|
142
|
+
query = query[fnc](key);
|
|
143
|
+
});
|
|
144
|
+
}
|
|
145
|
+
return query;
|
|
146
146
|
}
|
|
147
|
-
|
|
148
|
-
|
|
147
|
+
addRelationships(knex, query, fields, fromTable, relationships) {
|
|
148
|
+
if (!relationships) {
|
|
149
|
+
return query;
|
|
150
|
+
}
|
|
151
|
+
for (let relationship of relationships) {
|
|
152
|
+
const from = relationship.from, to = relationship.to, toTable = relationship.tableName;
|
|
153
|
+
if (!relationship.through) {
|
|
154
|
+
// @ts-ignore
|
|
155
|
+
query = query.leftJoin(toTable, `${fromTable}.${from}`, `${toTable}.${to}`);
|
|
156
|
+
}
|
|
157
|
+
else {
|
|
158
|
+
const throughTable = relationship.through;
|
|
159
|
+
const fromPrimary = relationship.fromPrimary;
|
|
160
|
+
const toPrimary = relationship.toPrimary;
|
|
161
|
+
query = query
|
|
162
|
+
// @ts-ignore
|
|
163
|
+
.leftJoin(throughTable, `${fromTable}.${fromPrimary}`, `${throughTable}.${from}`)
|
|
164
|
+
.leftJoin(toTable, `${toTable}.${toPrimary}`, `${throughTable}.${to}`);
|
|
165
|
+
}
|
|
166
|
+
}
|
|
167
|
+
return query.limit(BASE_LIMIT);
|
|
149
168
|
}
|
|
150
|
-
|
|
151
|
-
|
|
152
|
-
|
|
153
|
-
|
|
169
|
+
create(knex, json, opts) {
|
|
170
|
+
const { endpoint, body } = json;
|
|
171
|
+
let query = knex(endpoint.entityId);
|
|
172
|
+
const parsedBody = parseBody(body);
|
|
173
|
+
// make sure no null values in body for creation
|
|
174
|
+
for (let [key, value] of Object.entries(parsedBody)) {
|
|
175
|
+
if (value == null) {
|
|
176
|
+
delete parsedBody[key];
|
|
177
|
+
}
|
|
178
|
+
}
|
|
179
|
+
// mysql can't use returning
|
|
180
|
+
if (opts.disableReturning) {
|
|
181
|
+
return query.insert(parsedBody);
|
|
182
|
+
}
|
|
183
|
+
else {
|
|
184
|
+
return query.insert(parsedBody).returning("*");
|
|
185
|
+
}
|
|
154
186
|
}
|
|
155
|
-
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
187
|
+
bulkCreate(knex, json) {
|
|
188
|
+
const { endpoint, body } = json;
|
|
189
|
+
let query = knex(endpoint.entityId);
|
|
190
|
+
if (!Array.isArray(body)) {
|
|
191
|
+
return query;
|
|
159
192
|
}
|
|
193
|
+
const parsedBody = body.map(row => parseBody(row));
|
|
194
|
+
return query.insert(parsedBody);
|
|
160
195
|
}
|
|
161
|
-
|
|
162
|
-
|
|
163
|
-
|
|
196
|
+
read(knex, json, limit) {
|
|
197
|
+
var _a, _b;
|
|
198
|
+
let { endpoint, resource, filters, sort, paginate, relationships } = json;
|
|
199
|
+
const tableName = endpoint.entityId;
|
|
200
|
+
// select all if not specified
|
|
201
|
+
if (!resource) {
|
|
202
|
+
resource = { fields: [] };
|
|
203
|
+
}
|
|
204
|
+
let selectStatement = "*";
|
|
205
|
+
// handle select
|
|
206
|
+
if (resource.fields && resource.fields.length > 0) {
|
|
207
|
+
// select the resources as the format "table.columnName" - this is what is provided
|
|
208
|
+
// by the resource builder further up
|
|
209
|
+
selectStatement = resource.fields.map(field => `${field} as ${field}`);
|
|
210
|
+
}
|
|
211
|
+
let foundLimit = limit || BASE_LIMIT;
|
|
212
|
+
// handle pagination
|
|
213
|
+
let foundOffset = null;
|
|
214
|
+
if (paginate && paginate.page && paginate.limit) {
|
|
215
|
+
// @ts-ignore
|
|
216
|
+
const page = paginate.page <= 1 ? 0 : paginate.page - 1;
|
|
217
|
+
const offset = page * paginate.limit;
|
|
218
|
+
foundLimit = paginate.limit;
|
|
219
|
+
foundOffset = offset;
|
|
220
|
+
}
|
|
221
|
+
else if (paginate && paginate.limit) {
|
|
222
|
+
foundLimit = paginate.limit;
|
|
223
|
+
}
|
|
224
|
+
// start building the query
|
|
225
|
+
let query = knex(tableName).limit(foundLimit);
|
|
226
|
+
if (foundOffset) {
|
|
227
|
+
query = query.offset(foundOffset);
|
|
228
|
+
}
|
|
229
|
+
if (sort) {
|
|
230
|
+
for (let [key, value] of Object.entries(sort)) {
|
|
231
|
+
const direction = value === datasource_1.SortDirection.ASCENDING ? "asc" : "desc";
|
|
232
|
+
query = query.orderBy(key, direction);
|
|
233
|
+
}
|
|
234
|
+
}
|
|
235
|
+
if (this.client === utils_1.SqlClients.MS_SQL && !sort && (paginate === null || paginate === void 0 ? void 0 : paginate.limit)) {
|
|
236
|
+
// @ts-ignore
|
|
237
|
+
query = query.orderBy((_b = (_a = json.meta) === null || _a === void 0 ? void 0 : _a.table) === null || _b === void 0 ? void 0 : _b.primary[0]);
|
|
238
|
+
}
|
|
239
|
+
query = this.addFilters(tableName, query, filters);
|
|
164
240
|
// @ts-ignore
|
|
165
|
-
|
|
166
|
-
|
|
167
|
-
|
|
168
|
-
|
|
169
|
-
|
|
170
|
-
|
|
171
|
-
const { endpoint, body, filters } = json;
|
|
172
|
-
let query = knex(endpoint.entityId);
|
|
173
|
-
const parsedBody = parseBody(body);
|
|
174
|
-
query = addFilters(endpoint.entityId, query, filters);
|
|
175
|
-
// mysql can't use returning
|
|
176
|
-
if (opts.disableReturning) {
|
|
177
|
-
return query.update(parsedBody);
|
|
178
|
-
}
|
|
179
|
-
else {
|
|
180
|
-
return query.update(parsedBody).returning("*");
|
|
241
|
+
let preQuery = knex({
|
|
242
|
+
// @ts-ignore
|
|
243
|
+
[tableName]: query,
|
|
244
|
+
}).select(selectStatement);
|
|
245
|
+
// handle joins
|
|
246
|
+
return this.addRelationships(knex, preQuery, selectStatement, tableName, relationships);
|
|
181
247
|
}
|
|
182
|
-
|
|
183
|
-
|
|
184
|
-
|
|
185
|
-
|
|
186
|
-
|
|
187
|
-
|
|
188
|
-
|
|
189
|
-
|
|
248
|
+
update(knex, json, opts) {
|
|
249
|
+
const { endpoint, body, filters } = json;
|
|
250
|
+
let query = knex(endpoint.entityId);
|
|
251
|
+
const parsedBody = parseBody(body);
|
|
252
|
+
query = this.addFilters(endpoint.entityId, query, filters);
|
|
253
|
+
// mysql can't use returning
|
|
254
|
+
if (opts.disableReturning) {
|
|
255
|
+
return query.update(parsedBody);
|
|
256
|
+
}
|
|
257
|
+
else {
|
|
258
|
+
return query.update(parsedBody).returning("*");
|
|
259
|
+
}
|
|
190
260
|
}
|
|
191
|
-
|
|
192
|
-
|
|
261
|
+
delete(knex, json, opts) {
|
|
262
|
+
const { endpoint, filters } = json;
|
|
263
|
+
let query = knex(endpoint.entityId);
|
|
264
|
+
query = this.addFilters(endpoint.entityId, query, filters);
|
|
265
|
+
// mysql can't use returning
|
|
266
|
+
if (opts.disableReturning) {
|
|
267
|
+
return query.delete();
|
|
268
|
+
}
|
|
269
|
+
else {
|
|
270
|
+
return query.delete().returning("*");
|
|
271
|
+
}
|
|
193
272
|
}
|
|
194
273
|
}
|
|
195
|
-
class SqlQueryBuilder {
|
|
274
|
+
class SqlQueryBuilder extends sqlTable_1.default {
|
|
196
275
|
// pass through client to get flavour of SQL
|
|
197
276
|
constructor(client, limit = BASE_LIMIT) {
|
|
198
|
-
|
|
277
|
+
super(client);
|
|
199
278
|
this.limit = limit;
|
|
200
279
|
}
|
|
201
|
-
/**
|
|
202
|
-
* @param json the input JSON structure from which an SQL query will be built.
|
|
203
|
-
* @return {string} the operation that was found in the JSON.
|
|
204
|
-
*/
|
|
205
|
-
_operation(json) {
|
|
206
|
-
return json.endpoint.operation;
|
|
207
|
-
}
|
|
208
280
|
/**
|
|
209
281
|
* @param json The JSON query DSL which is to be converted to SQL.
|
|
210
282
|
* @param opts extra options which are to be passed into the query builder, e.g. disableReturning
|
|
@@ -212,26 +284,108 @@ class SqlQueryBuilder {
|
|
|
212
284
|
* @return {{ sql: string, bindings: object }} the query ready to be passed to the driver.
|
|
213
285
|
*/
|
|
214
286
|
_query(json, opts = {}) {
|
|
215
|
-
const
|
|
287
|
+
const sqlClient = this.getSqlClient();
|
|
288
|
+
const client = knex_1.knex({ client: sqlClient });
|
|
216
289
|
let query;
|
|
290
|
+
const builder = new InternalBuilder(sqlClient);
|
|
217
291
|
switch (this._operation(json)) {
|
|
218
292
|
case datasource_1.Operation.CREATE:
|
|
219
|
-
query =
|
|
293
|
+
query = builder.create(client, json, opts);
|
|
220
294
|
break;
|
|
221
295
|
case datasource_1.Operation.READ:
|
|
222
|
-
query =
|
|
296
|
+
query = builder.read(client, json, this.limit);
|
|
223
297
|
break;
|
|
224
298
|
case datasource_1.Operation.UPDATE:
|
|
225
|
-
query =
|
|
299
|
+
query = builder.update(client, json, opts);
|
|
226
300
|
break;
|
|
227
301
|
case datasource_1.Operation.DELETE:
|
|
228
|
-
query =
|
|
302
|
+
query = builder.delete(client, json, opts);
|
|
303
|
+
break;
|
|
304
|
+
case datasource_1.Operation.BULK_CREATE:
|
|
305
|
+
query = builder.bulkCreate(client, json);
|
|
229
306
|
break;
|
|
307
|
+
case datasource_1.Operation.CREATE_TABLE:
|
|
308
|
+
case datasource_1.Operation.UPDATE_TABLE:
|
|
309
|
+
case datasource_1.Operation.DELETE_TABLE:
|
|
310
|
+
return this._tableQuery(json);
|
|
230
311
|
default:
|
|
231
312
|
throw `Operation type is not supported by SQL query builder`;
|
|
232
313
|
}
|
|
233
314
|
// @ts-ignore
|
|
234
315
|
return query.toSQL().toNative();
|
|
235
316
|
}
|
|
317
|
+
getReturningRow(queryFn, json) {
|
|
318
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
319
|
+
if (!json.extra || !json.extra.idFilter) {
|
|
320
|
+
return {};
|
|
321
|
+
}
|
|
322
|
+
const input = this._query({
|
|
323
|
+
endpoint: Object.assign(Object.assign({}, json.endpoint), { operation: datasource_1.Operation.READ }),
|
|
324
|
+
resource: {
|
|
325
|
+
fields: [],
|
|
326
|
+
},
|
|
327
|
+
filters: json.extra.idFilter,
|
|
328
|
+
paginate: {
|
|
329
|
+
limit: 1,
|
|
330
|
+
},
|
|
331
|
+
meta: json.meta,
|
|
332
|
+
});
|
|
333
|
+
return queryFn(input, datasource_1.Operation.READ);
|
|
334
|
+
});
|
|
335
|
+
}
|
|
336
|
+
// when creating if an ID has been inserted need to make sure
|
|
337
|
+
// the id filter is enriched with it before trying to retrieve the row
|
|
338
|
+
checkLookupKeys(id, json) {
|
|
339
|
+
var _a, _b;
|
|
340
|
+
if (!id || !((_a = json.meta) === null || _a === void 0 ? void 0 : _a.table) || !json.meta.table.primary) {
|
|
341
|
+
return json;
|
|
342
|
+
}
|
|
343
|
+
const primaryKey = (_b = json.meta.table.primary) === null || _b === void 0 ? void 0 : _b[0];
|
|
344
|
+
json.extra = {
|
|
345
|
+
idFilter: {
|
|
346
|
+
equal: {
|
|
347
|
+
[primaryKey]: id,
|
|
348
|
+
},
|
|
349
|
+
},
|
|
350
|
+
};
|
|
351
|
+
return json;
|
|
352
|
+
}
|
|
353
|
+
// this function recreates the returning functionality of postgres
|
|
354
|
+
queryWithReturning(json, queryFn, processFn = (result) => result) {
|
|
355
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
356
|
+
const sqlClient = this.getSqlClient();
|
|
357
|
+
const operation = this._operation(json);
|
|
358
|
+
const input = this._query(json, { disableReturning: true });
|
|
359
|
+
if (Array.isArray(input)) {
|
|
360
|
+
const responses = [];
|
|
361
|
+
for (let query of input) {
|
|
362
|
+
responses.push(yield queryFn(query, operation));
|
|
363
|
+
}
|
|
364
|
+
return responses;
|
|
365
|
+
}
|
|
366
|
+
let row;
|
|
367
|
+
// need to manage returning, a feature mySQL can't do
|
|
368
|
+
if (operation === datasource_1.Operation.DELETE) {
|
|
369
|
+
row = processFn(yield this.getReturningRow(queryFn, json));
|
|
370
|
+
}
|
|
371
|
+
const response = yield queryFn(input, operation);
|
|
372
|
+
const results = processFn(response);
|
|
373
|
+
// same as delete, manage returning
|
|
374
|
+
if (operation === datasource_1.Operation.CREATE || operation === datasource_1.Operation.UPDATE) {
|
|
375
|
+
let id;
|
|
376
|
+
if (sqlClient === utils_1.SqlClients.MS_SQL) {
|
|
377
|
+
id = results === null || results === void 0 ? void 0 : results[0].id;
|
|
378
|
+
}
|
|
379
|
+
else if (sqlClient === utils_1.SqlClients.MY_SQL) {
|
|
380
|
+
id = results === null || results === void 0 ? void 0 : results.insertId;
|
|
381
|
+
}
|
|
382
|
+
row = processFn(yield this.getReturningRow(queryFn, this.checkLookupKeys(id, json)));
|
|
383
|
+
}
|
|
384
|
+
if (operation !== datasource_1.Operation.READ) {
|
|
385
|
+
return row;
|
|
386
|
+
}
|
|
387
|
+
return results.length ? results : [{ [operation.toLowerCase()]: true }];
|
|
388
|
+
});
|
|
389
|
+
}
|
|
236
390
|
}
|
|
237
391
|
module.exports = SqlQueryBuilder;
|