@budibase/server 1.0.26 → 1.0.27-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/__mocks__/node-fetch.ts +21 -0
- package/builder/assets/bb-spaceship.8f6e81e1.svg +227 -0
- package/builder/assets/{index.4fb3f887.css → index.0dce895b.css} +1 -1
- package/builder/assets/index.b98dd7ca.js +1348 -0
- package/builder/assets/{vendor.470042a4.js → vendor.f9faf6ab.js} +88 -88
- package/builder/index.html +3 -3
- package/coverage/clover.xml +1364 -1121
- package/coverage/coverage-final.json +37 -36
- package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
- package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
- package/coverage/lcov-report/api/controllers/application.js.html +59 -59
- package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
- package/coverage/lcov-report/api/controllers/automation.js.html +22 -10
- 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 +170 -68
- package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
- package/coverage/lcov-report/api/controllers/hosting.js.html +1 -1
- package/coverage/lcov-report/api/controllers/index.html +39 -54
- package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
- package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
- package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
- package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
- package/coverage/lcov-report/api/controllers/query/index.html +126 -0
- package/coverage/lcov-report/api/controllers/{query.js.html → query/index.js.html} +225 -114
- package/coverage/lcov-report/api/controllers/query/validation.js.html +206 -0
- package/coverage/lcov-report/api/controllers/role.js.html +1 -1
- package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
- package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
- package/coverage/lcov-report/api/controllers/row/index.html +22 -22
- package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
- package/coverage/lcov-report/api/controllers/row/internal.js.html +38 -11
- package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +7 -4
- package/coverage/lcov-report/api/controllers/row/utils.js.html +29 -20
- package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
- package/coverage/lcov-report/api/controllers/script.js.html +1 -1
- package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
- package/coverage/lcov-report/api/controllers/table/index.html +1 -1
- package/coverage/lcov-report/api/controllers/table/index.js.html +1 -1
- package/coverage/lcov-report/api/controllers/table/internal.js.html +1 -1
- package/coverage/lcov-report/api/controllers/table/utils.js.html +1 -1
- package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
- package/coverage/lcov-report/api/controllers/user.js.html +108 -9
- package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
- package/coverage/lcov-report/api/controllers/view/index.html +1 -1
- package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
- package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
- package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
- package/coverage/lcov-report/api/controllers/webhook.js.html +18 -6
- package/coverage/lcov-report/api/index.html +1 -1
- package/coverage/lcov-report/api/index.js.html +5 -5
- 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 +1 -1
- package/coverage/lcov-report/api/routes/hosting.js.html +1 -1
- package/coverage/lcov-report/api/routes/index.html +9 -9
- 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 +19 -85
- 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 +1 -1
- package/coverage/lcov-report/api/routes/table.js.html +1 -1
- package/coverage/lcov-report/api/routes/templates.js.html +1 -1
- package/coverage/lcov-report/api/routes/user.js.html +32 -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 +1 -1
- package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
- package/coverage/lcov-report/automations/bullboard.js.html +1 -1
- package/coverage/lcov-report/automations/index.html +20 -20
- package/coverage/lcov-report/automations/index.js.html +1 -1
- 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 +1 -1
- package/coverage/lcov-report/automations/steps/executeQuery.js.html +31 -7
- package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
- package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
- package/coverage/lcov-report/automations/steps/index.html +11 -11
- package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
- package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +5 -8
- package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
- 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 +1 -1
- package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
- package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
- package/coverage/lcov-report/automations/triggers.js.html +1 -1
- package/coverage/lcov-report/automations/utils.js.html +48 -15
- package/coverage/lcov-report/constants/index.html +1 -1
- package/coverage/lcov-report/constants/index.js.html +22 -22
- 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 +18 -18
- package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
- package/coverage/lcov-report/db/index.html +15 -15
- package/coverage/lcov-report/db/index.js.html +3 -3
- package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
- package/coverage/lcov-report/db/linkedRows/index.html +1 -1
- package/coverage/lcov-report/db/linkedRows/index.js.html +1 -1
- package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
- package/coverage/lcov-report/db/newid.js.html +4 -4
- package/coverage/lcov-report/db/utils.js.html +107 -80
- package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
- package/coverage/lcov-report/events/index.html +1 -1
- package/coverage/lcov-report/events/index.js.html +1 -1
- package/coverage/lcov-report/events/utils.js.html +1 -1
- package/coverage/lcov-report/index.html +99 -84
- 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 +1 -1
- package/coverage/lcov-report/middleware/authorized.js.html +10 -10
- package/coverage/lcov-report/middleware/builder.js.html +8 -8
- package/coverage/lcov-report/middleware/currentapp.js.html +28 -28
- package/coverage/lcov-report/middleware/index.html +1 -1
- package/coverage/lcov-report/middleware/joi-validator.js.html +11 -11
- package/coverage/lcov-report/middleware/resourceId.js.html +18 -18
- package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
- package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
- package/coverage/lcov-report/middleware/utils.js.html +2 -2
- package/coverage/lcov-report/threads/automation.js.html +1 -1
- package/coverage/lcov-report/threads/index.html +31 -31
- package/coverage/lcov-report/threads/index.js.html +12 -12
- package/coverage/lcov-report/threads/query.js.html +396 -69
- package/coverage/lcov-report/threads/utils.js.html +358 -13
- 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 +1 -1
- package/coverage/lcov-report/utilities/global.js.html +13 -13
- package/coverage/lcov-report/utilities/index.html +19 -19
- package/coverage/lcov-report/utilities/index.js.html +69 -36
- package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +2 -2
- package/coverage/lcov-report/utilities/queue/index.html +1 -1
- 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 +19 -19
- package/coverage/lcov-report/utilities/rowProcessor/index.js.html +235 -10
- package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +1 -1
- package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
- package/coverage/lcov-report/utilities/security.js.html +2 -2
- 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 +19 -19
- package/coverage/lcov.info +2294 -1874
- package/dist/api/controllers/automation.js +7 -5
- package/dist/api/controllers/datasource.js +29 -0
- package/dist/api/controllers/query/import/index.js +79 -0
- package/dist/api/controllers/query/import/sources/base/index.js +64 -0
- package/dist/api/controllers/query/import/sources/base/openapi.js +42 -0
- package/dist/api/controllers/query/import/sources/curl.js +96 -0
- package/dist/api/controllers/query/import/sources/openapi2.js +142 -0
- package/dist/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.json +253 -0
- package/dist/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.json +1054 -0
- package/dist/api/controllers/{query.js → query/index.js} +86 -52
- package/dist/api/controllers/query/validation.js +38 -0
- package/dist/api/controllers/row/internal.js +14 -6
- package/dist/api/controllers/row/internalSearch.js +2 -1
- package/dist/api/controllers/row/utils.js +13 -6
- package/dist/api/controllers/user.js +37 -1
- package/dist/api/controllers/webhook.js +4 -0
- package/dist/api/routes/query.js +5 -33
- package/dist/api/routes/user.js +3 -1
- package/dist/automations/steps/executeQuery.js +8 -1
- package/dist/automations/steps/outgoingWebhook.js +2 -3
- package/dist/automations/utils.js +10 -0
- package/dist/db/utils.js +8 -0
- package/dist/definitions/datasource.js +6 -1
- package/dist/integrations/base/sqlTable.js +1 -1
- package/dist/integrations/rest.js +207 -117
- package/dist/package.json +11 -4
- package/dist/tests/utilities/TestConfiguration.js +47 -0
- package/dist/threads/query.js +148 -40
- package/dist/threads/utils.js +105 -0
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/dist/utilities/fileSystem/utilities.js +2 -1
- package/dist/utilities/index.js +10 -0
- package/dist/utilities/rowProcessor/index.js +76 -0
- package/package.json +12 -5
- package/src/api/controllers/automation.js +8 -4
- package/src/api/controllers/datasource.js +34 -0
- package/src/api/controllers/query/import/index.ts +85 -0
- package/src/api/controllers/query/import/sources/base/index.ts +87 -0
- package/src/api/controllers/query/import/sources/base/openapi.ts +24 -0
- package/src/api/controllers/query/import/sources/curl.ts +102 -0
- package/src/api/controllers/query/import/sources/openapi2.ts +163 -0
- package/src/api/controllers/query/import/sources/tests/curl/curl.spec.js +103 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/delete.txt +1 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/empty-body.txt +2 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/get.txt +1 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/headers.txt +3 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/patch.txt +2 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/path.txt +1 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/post.txt +2 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/put.txt +2 -0
- package/src/api/controllers/query/import/sources/tests/curl/data/query.txt +1 -0
- package/src/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.json +253 -0
- package/src/api/controllers/query/import/sources/tests/openapi2/data/crud/crud.yaml +160 -0
- package/src/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.json +1054 -0
- package/src/api/controllers/query/import/sources/tests/openapi2/data/petstore/petstore.yaml +711 -0
- package/src/api/controllers/query/import/sources/tests/openapi2/openapi2.spec.js +238 -0
- package/src/api/controllers/query/import/tests/index.spec.js +109 -0
- package/src/api/controllers/query/index.js +195 -0
- package/src/api/controllers/query/validation.js +42 -0
- package/src/api/controllers/row/internal.js +14 -5
- package/src/api/controllers/row/internalSearch.js +2 -1
- package/src/api/controllers/row/utils.js +11 -8
- package/src/api/controllers/user.js +33 -0
- package/src/api/controllers/webhook.js +4 -0
- package/src/api/routes/query.js +13 -35
- package/src/api/routes/tests/datasource.spec.js +45 -0
- package/src/api/routes/tests/query.spec.js +69 -0
- package/src/api/routes/user.js +10 -0
- package/src/automations/steps/executeQuery.js +9 -1
- package/src/automations/steps/outgoingWebhook.js +2 -3
- package/src/automations/utils.js +11 -0
- package/src/db/utils.js +9 -0
- package/src/definitions/common.ts +2 -16
- package/src/definitions/datasource.ts +83 -1
- package/src/integrations/base/IntegrationBase.ts +4 -4
- package/src/integrations/base/sqlTable.ts +1 -1
- package/src/integrations/oracle.ts +1 -1
- package/src/integrations/rest.ts +230 -137
- package/src/integrations/tests/rest.spec.js +167 -19
- package/src/tests/utilities/TestConfiguration.js +57 -0
- package/src/threads/query.js +149 -40
- package/src/threads/utils.js +115 -0
- package/src/utilities/fileSystem/utilities.js +2 -0
- package/src/utilities/index.js +11 -0
- package/src/utilities/rowProcessor/index.js +75 -0
- package/builder/assets/index.8adef83f.js +0 -1316
- package/src/api/controllers/query.js +0 -158
package/dist/threads/query.js
CHANGED
|
@@ -8,51 +8,159 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
|
|
|
8
8
|
step((generator = generator.apply(thisArg, _arguments || [])).next());
|
|
9
9
|
});
|
|
10
10
|
};
|
|
11
|
-
require("./utils")
|
|
11
|
+
const threadUtils = require("./utils");
|
|
12
|
+
threadUtils.threadSetup();
|
|
12
13
|
const ScriptRunner = require("../utilities/scriptRunner");
|
|
13
14
|
const { integrations } = require("../integrations");
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
15
|
+
const { processStringSync } = require("@budibase/string-templates");
|
|
16
|
+
const CouchDB = require("../db");
|
|
17
|
+
class QueryRunner {
|
|
18
|
+
constructor(input, flags = { noRecursiveQuery: false }) {
|
|
19
|
+
this.appId = input.appId;
|
|
20
|
+
this.datasource = input.datasource;
|
|
21
|
+
this.queryVerb = input.queryVerb;
|
|
22
|
+
this.fields = input.fields;
|
|
23
|
+
this.parameters = input.parameters;
|
|
24
|
+
this.transformer = input.transformer;
|
|
25
|
+
this.queryId = input.queryId;
|
|
26
|
+
this.noRecursiveQuery = flags.noRecursiveQuery;
|
|
27
|
+
this.cachedVariables = [];
|
|
28
|
+
// allows the response from a query to be stored throughout this
|
|
29
|
+
// execution so that if it needs to be re-used for another variable
|
|
30
|
+
// it can be
|
|
31
|
+
this.queryResponse = {};
|
|
32
|
+
this.hasRerun = false;
|
|
33
|
+
}
|
|
34
|
+
execute() {
|
|
35
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
36
|
+
let { datasource, fields, queryVerb, transformer } = this;
|
|
37
|
+
// pre-query, make sure datasource variables are added to parameters
|
|
38
|
+
const parameters = yield this.addDatasourceVariables();
|
|
39
|
+
const query = threadUtils.enrichQueryFields(fields, parameters);
|
|
40
|
+
const Integration = integrations[datasource.source];
|
|
41
|
+
if (!Integration) {
|
|
42
|
+
throw "Integration type does not exist.";
|
|
43
|
+
}
|
|
44
|
+
const integration = new Integration(datasource.config);
|
|
45
|
+
let output = threadUtils.formatResponse(yield integration[queryVerb](query));
|
|
46
|
+
let rows = output, info = undefined, extra = undefined;
|
|
47
|
+
if (threadUtils.hasExtraData(output)) {
|
|
48
|
+
rows = output.data;
|
|
49
|
+
info = output.info;
|
|
50
|
+
extra = output.extra;
|
|
51
|
+
}
|
|
52
|
+
// transform as required
|
|
53
|
+
if (transformer) {
|
|
54
|
+
const runner = new ScriptRunner(transformer, {
|
|
55
|
+
data: rows,
|
|
56
|
+
params: parameters,
|
|
57
|
+
});
|
|
58
|
+
rows = runner.execute();
|
|
59
|
+
}
|
|
60
|
+
// if the request fails we retry once, invalidating the cached value
|
|
61
|
+
if (info &&
|
|
62
|
+
info.code >= 400 &&
|
|
63
|
+
this.cachedVariables.length > 0 &&
|
|
64
|
+
!this.hasRerun) {
|
|
65
|
+
this.hasRerun = true;
|
|
66
|
+
// invalidate the cache value
|
|
67
|
+
yield threadUtils.invalidateDynamicVariables(this.cachedVariables);
|
|
68
|
+
return this.execute();
|
|
69
|
+
}
|
|
70
|
+
// needs to an array for next step
|
|
71
|
+
if (!Array.isArray(rows)) {
|
|
72
|
+
rows = [rows];
|
|
73
|
+
}
|
|
74
|
+
// map into JSON if just raw primitive here
|
|
75
|
+
if (rows.find(row => typeof row !== "object")) {
|
|
76
|
+
rows = rows.map(value => ({ value }));
|
|
77
|
+
}
|
|
78
|
+
// get all the potential fields in the schema
|
|
79
|
+
let keys = rows.flatMap(Object.keys);
|
|
80
|
+
if (integration.end) {
|
|
81
|
+
integration.end();
|
|
82
|
+
}
|
|
83
|
+
return { rows, keys, info, extra };
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
runAnotherQuery(queryId, parameters) {
|
|
87
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
88
|
+
const db = new CouchDB(this.appId);
|
|
89
|
+
const query = yield db.get(queryId);
|
|
90
|
+
const datasource = yield db.get(query.datasourceId);
|
|
91
|
+
return new QueryRunner({
|
|
92
|
+
appId: this.appId,
|
|
93
|
+
datasource,
|
|
94
|
+
queryVerb: query.queryVerb,
|
|
95
|
+
fields: query.fields,
|
|
96
|
+
parameters,
|
|
97
|
+
transformer: query.transformer,
|
|
98
|
+
}, { noRecursiveQuery: true }).execute();
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
getDynamicVariable(variable) {
|
|
102
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
103
|
+
let { parameters } = this;
|
|
104
|
+
const queryId = variable.queryId, name = variable.name;
|
|
105
|
+
let value = yield threadUtils.checkCacheForDynamicVariable(queryId, name);
|
|
106
|
+
if (!value) {
|
|
107
|
+
value = this.queryResponse[queryId]
|
|
108
|
+
? this.queryResponse[queryId]
|
|
109
|
+
: yield this.runAnotherQuery(queryId, parameters);
|
|
110
|
+
// store incase this query is to be called again
|
|
111
|
+
this.queryResponse[queryId] = value;
|
|
112
|
+
yield threadUtils.storeDynamicVariable(queryId, name, value);
|
|
113
|
+
}
|
|
114
|
+
else {
|
|
115
|
+
this.cachedVariables.push({ queryId, name });
|
|
116
|
+
}
|
|
117
|
+
return value;
|
|
118
|
+
});
|
|
119
|
+
}
|
|
120
|
+
addDatasourceVariables() {
|
|
121
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
122
|
+
let { datasource, parameters, fields } = this;
|
|
123
|
+
if (!datasource || !datasource.config) {
|
|
124
|
+
return parameters;
|
|
125
|
+
}
|
|
126
|
+
const staticVars = datasource.config.staticVariables || {};
|
|
127
|
+
const dynamicVars = datasource.config.dynamicVariables || [];
|
|
128
|
+
for (let [key, value] of Object.entries(staticVars)) {
|
|
129
|
+
if (!parameters[key]) {
|
|
130
|
+
parameters[key] = value;
|
|
131
|
+
}
|
|
132
|
+
}
|
|
133
|
+
if (!this.noRecursiveQuery) {
|
|
134
|
+
// need to see if this uses any variables
|
|
135
|
+
const stringFields = JSON.stringify(fields);
|
|
136
|
+
const foundVars = dynamicVars.filter(variable => {
|
|
137
|
+
// don't allow a query to use its own dynamic variable (loop)
|
|
138
|
+
if (variable.queryId === this.queryId) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
// look for {{ variable }} but allow spaces between handlebars
|
|
142
|
+
const regex = new RegExp(`{{[ ]*${variable.name}[ ]*}}`);
|
|
143
|
+
return regex.test(stringFields);
|
|
144
|
+
});
|
|
145
|
+
const dynamics = foundVars.map(dynVar => this.getDynamicVariable(dynVar));
|
|
146
|
+
const responses = yield Promise.all(dynamics);
|
|
147
|
+
for (let i = 0; i < foundVars.length; i++) {
|
|
148
|
+
const variable = foundVars[i];
|
|
149
|
+
parameters[variable.name] = processStringSync(variable.value, {
|
|
150
|
+
data: responses[i].rows,
|
|
151
|
+
info: responses[i].extra,
|
|
152
|
+
});
|
|
153
|
+
// make sure its known that this uses dynamic variables in case it fails
|
|
154
|
+
this.hasDynamicVariables = true;
|
|
155
|
+
}
|
|
156
|
+
}
|
|
157
|
+
return parameters;
|
|
158
|
+
});
|
|
22
159
|
}
|
|
23
|
-
return resp;
|
|
24
|
-
}
|
|
25
|
-
function runAndTransform(datasource, queryVerb, query, transformer) {
|
|
26
|
-
return __awaiter(this, void 0, void 0, function* () {
|
|
27
|
-
const Integration = integrations[datasource.source];
|
|
28
|
-
if (!Integration) {
|
|
29
|
-
throw "Integration type does not exist.";
|
|
30
|
-
}
|
|
31
|
-
const integration = new Integration(datasource.config);
|
|
32
|
-
let rows = formatResponse(yield integration[queryVerb](query));
|
|
33
|
-
// transform as required
|
|
34
|
-
if (transformer) {
|
|
35
|
-
const runner = new ScriptRunner(transformer, { data: rows });
|
|
36
|
-
rows = runner.execute();
|
|
37
|
-
}
|
|
38
|
-
// needs to an array for next step
|
|
39
|
-
if (!Array.isArray(rows)) {
|
|
40
|
-
rows = [rows];
|
|
41
|
-
}
|
|
42
|
-
// map into JSON if just raw primitive here
|
|
43
|
-
if (rows.find(row => typeof row !== "object")) {
|
|
44
|
-
rows = rows.map(value => ({ value }));
|
|
45
|
-
}
|
|
46
|
-
// get all the potential fields in the schema
|
|
47
|
-
let keys = rows.flatMap(Object.keys);
|
|
48
|
-
if (integration.end) {
|
|
49
|
-
integration.end();
|
|
50
|
-
}
|
|
51
|
-
return { rows, keys };
|
|
52
|
-
});
|
|
53
160
|
}
|
|
54
161
|
module.exports = (input, callback) => {
|
|
55
|
-
|
|
162
|
+
const Runner = new QueryRunner(input);
|
|
163
|
+
Runner.execute()
|
|
56
164
|
.then(response => {
|
|
57
165
|
callback(null, response);
|
|
58
166
|
})
|
package/dist/threads/utils.js
CHANGED
|
@@ -1,7 +1,35 @@
|
|
|
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
|
+
};
|
|
2
11
|
const env = require("../environment");
|
|
3
12
|
const CouchDB = require("../db");
|
|
4
13
|
const { init } = require("@budibase/auth");
|
|
14
|
+
const redis = require("@budibase/auth/redis");
|
|
15
|
+
const { SEPARATOR } = require("@budibase/auth/db");
|
|
16
|
+
const { processStringSync } = require("@budibase/string-templates");
|
|
17
|
+
const VARIABLE_TTL_SECONDS = 3600;
|
|
18
|
+
let client;
|
|
19
|
+
const IS_TRIPLE_BRACE = new RegExp(/^{{3}.*}{3}$/);
|
|
20
|
+
const IS_HANDLEBARS = new RegExp(/^{{2}.*}{2}$/);
|
|
21
|
+
function getClient() {
|
|
22
|
+
return __awaiter(this, void 0, void 0, function* () {
|
|
23
|
+
if (!client) {
|
|
24
|
+
client = yield new redis.Client(redis.utils.Databases.QUERY_VARS).init();
|
|
25
|
+
}
|
|
26
|
+
return client;
|
|
27
|
+
});
|
|
28
|
+
}
|
|
29
|
+
process.on("exit", () => __awaiter(void 0, void 0, void 0, function* () {
|
|
30
|
+
if (client)
|
|
31
|
+
yield client.finish();
|
|
32
|
+
}));
|
|
5
33
|
exports.threadSetup = () => {
|
|
6
34
|
// don't run this if not threading
|
|
7
35
|
if (env.isTest() || env.DISABLE_THREADING) {
|
|
@@ -11,3 +39,80 @@ exports.threadSetup = () => {
|
|
|
11
39
|
env.setInThread();
|
|
12
40
|
init(CouchDB);
|
|
13
41
|
};
|
|
42
|
+
function makeVariableKey(queryId, variable) {
|
|
43
|
+
return `${queryId}${SEPARATOR}${variable}`;
|
|
44
|
+
}
|
|
45
|
+
exports.checkCacheForDynamicVariable = (queryId, variable) => __awaiter(void 0, void 0, void 0, function* () {
|
|
46
|
+
const cache = yield getClient();
|
|
47
|
+
return cache.get(makeVariableKey(queryId, variable));
|
|
48
|
+
});
|
|
49
|
+
exports.invalidateDynamicVariables = (cachedVars) => __awaiter(void 0, void 0, void 0, function* () {
|
|
50
|
+
const cache = yield getClient();
|
|
51
|
+
let promises = [];
|
|
52
|
+
for (let variable of cachedVars) {
|
|
53
|
+
promises.push(cache.delete(makeVariableKey(variable.queryId, variable.name)));
|
|
54
|
+
}
|
|
55
|
+
yield Promise.all(promises);
|
|
56
|
+
});
|
|
57
|
+
exports.storeDynamicVariable = (queryId, variable, value) => __awaiter(void 0, void 0, void 0, function* () {
|
|
58
|
+
const cache = yield getClient();
|
|
59
|
+
yield cache.store(makeVariableKey(queryId, variable), value, VARIABLE_TTL_SECONDS);
|
|
60
|
+
});
|
|
61
|
+
exports.formatResponse = resp => {
|
|
62
|
+
if (typeof resp === "string") {
|
|
63
|
+
try {
|
|
64
|
+
resp = JSON.parse(resp);
|
|
65
|
+
}
|
|
66
|
+
catch (err) {
|
|
67
|
+
resp = { response: resp };
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return resp;
|
|
71
|
+
};
|
|
72
|
+
exports.hasExtraData = response => {
|
|
73
|
+
return (typeof response === "object" &&
|
|
74
|
+
!Array.isArray(response) &&
|
|
75
|
+
response.data != null &&
|
|
76
|
+
response.info != null);
|
|
77
|
+
};
|
|
78
|
+
exports.enrichQueryFields = (fields, parameters = {}) => {
|
|
79
|
+
const enrichedQuery = {};
|
|
80
|
+
// enrich the fields with dynamic parameters
|
|
81
|
+
for (let key of Object.keys(fields)) {
|
|
82
|
+
if (fields[key] == null) {
|
|
83
|
+
continue;
|
|
84
|
+
}
|
|
85
|
+
if (typeof fields[key] === "object") {
|
|
86
|
+
// enrich nested fields object
|
|
87
|
+
enrichedQuery[key] = this.enrichQueryFields(fields[key], parameters);
|
|
88
|
+
}
|
|
89
|
+
else if (typeof fields[key] === "string") {
|
|
90
|
+
// enrich string value as normal
|
|
91
|
+
let value = fields[key];
|
|
92
|
+
// add triple brace to avoid escaping e.g. '=' in cookie header
|
|
93
|
+
if (IS_HANDLEBARS.test(value) && !IS_TRIPLE_BRACE.test(value)) {
|
|
94
|
+
value = `{${value}}`;
|
|
95
|
+
}
|
|
96
|
+
enrichedQuery[key] = processStringSync(value, parameters, {
|
|
97
|
+
noHelpers: true,
|
|
98
|
+
});
|
|
99
|
+
}
|
|
100
|
+
else {
|
|
101
|
+
enrichedQuery[key] = fields[key];
|
|
102
|
+
}
|
|
103
|
+
}
|
|
104
|
+
if (enrichedQuery.json ||
|
|
105
|
+
enrichedQuery.customData ||
|
|
106
|
+
enrichedQuery.requestBody) {
|
|
107
|
+
try {
|
|
108
|
+
enrichedQuery.json = JSON.parse(enrichedQuery.json ||
|
|
109
|
+
enrichedQuery.customData ||
|
|
110
|
+
enrichedQuery.requestBody);
|
|
111
|
+
}
|
|
112
|
+
catch (err) {
|
|
113
|
+
// no json found, ignore
|
|
114
|
+
}
|
|
115
|
+
delete enrichedQuery.customData;
|
|
116
|
+
}
|
|
117
|
+
return enrichedQuery;
|
|
118
|
+
};
|