@budibase/server 1.0.46-alpha.4 → 1.0.46-alpha.8

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 (226) hide show
  1. package/builder/assets/{index.c21ee1fb.js → index.51efc215.js} +243 -243
  2. package/builder/assets/{index.243ca52c.css → index.dd9e386f.css} +1 -1
  3. package/builder/assets/{vendor.10e2ad1b.js → vendor.3bd2f661.js} +1 -1
  4. package/builder/index.html +3 -3
  5. package/coverage/clover.xml +1794 -1793
  6. package/coverage/coverage-final.json +136 -138
  7. package/coverage/lcov-report/api/controllers/analytics.js.html +6 -6
  8. package/coverage/lcov-report/api/controllers/apikeys.js.html +6 -6
  9. package/coverage/lcov-report/api/controllers/application.js.html +209 -125
  10. package/coverage/lcov-report/api/controllers/auth.js.html +23 -11
  11. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  12. package/coverage/lcov-report/api/controllers/backup.js.html +3 -3
  13. package/coverage/lcov-report/api/controllers/cloud.js.html +12 -12
  14. package/coverage/lcov-report/api/controllers/component.js.html +5 -5
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +58 -22
  16. package/coverage/lcov-report/api/controllers/dev.js.html +19 -19
  17. package/coverage/lcov-report/api/controllers/index.html +38 -53
  18. package/coverage/lcov-report/api/controllers/integration.js.html +4 -4
  19. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  20. package/coverage/lcov-report/api/controllers/metadata.js.html +9 -9
  21. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  22. package/coverage/lcov-report/api/controllers/query/index.html +15 -15
  23. package/coverage/lcov-report/api/controllers/query/index.js.html +61 -28
  24. package/coverage/lcov-report/api/controllers/query/validation.js.html +9 -9
  25. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  26. package/coverage/lcov-report/api/controllers/routing.js.html +9 -9
  27. package/coverage/lcov-report/api/controllers/row/external.js.html +16 -16
  28. package/coverage/lcov-report/api/controllers/row/index.html +5 -5
  29. package/coverage/lcov-report/api/controllers/row/index.js.html +13 -13
  30. package/coverage/lcov-report/api/controllers/row/internal.js.html +26 -26
  31. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +8 -8
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +17 -26
  33. package/coverage/lcov-report/api/controllers/screen.js.html +7 -7
  34. package/coverage/lcov-report/api/controllers/script.js.html +4 -4
  35. package/coverage/lcov-report/api/controllers/table/external.js.html +12 -12
  36. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  37. package/coverage/lcov-report/api/controllers/table/index.js.html +14 -14
  38. package/coverage/lcov-report/api/controllers/table/internal.js.html +10 -10
  39. package/coverage/lcov-report/api/controllers/table/utils.js.html +26 -26
  40. package/coverage/lcov-report/api/controllers/templates.js.html +5 -5
  41. package/coverage/lcov-report/api/controllers/user.js.html +30 -24
  42. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  43. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/index.js.html +14 -14
  45. package/coverage/lcov-report/api/controllers/view/utils.js.html +11 -11
  46. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  47. package/coverage/lcov-report/api/controllers/webhook.js.html +15 -15
  48. package/coverage/lcov-report/api/index.html +1 -1
  49. package/coverage/lcov-report/api/index.js.html +26 -26
  50. package/coverage/lcov-report/api/routes/analytics.js.html +6 -6
  51. package/coverage/lcov-report/api/routes/apikeys.js.html +8 -8
  52. package/coverage/lcov-report/api/routes/application.js.html +9 -9
  53. package/coverage/lcov-report/api/routes/auth.js.html +6 -6
  54. package/coverage/lcov-report/api/routes/automation.js.html +14 -14
  55. package/coverage/lcov-report/api/routes/backup.js.html +8 -8
  56. package/coverage/lcov-report/api/routes/cloud.js.html +8 -8
  57. package/coverage/lcov-report/api/routes/component.js.html +8 -8
  58. package/coverage/lcov-report/api/routes/datasource.js.html +13 -13
  59. package/coverage/lcov-report/api/routes/deploy.js.html +8 -8
  60. package/coverage/lcov-report/api/routes/dev.js.html +13 -13
  61. package/coverage/lcov-report/api/routes/index.html +7 -22
  62. package/coverage/lcov-report/api/routes/index.js.html +31 -37
  63. package/coverage/lcov-report/api/routes/integration.js.html +8 -8
  64. package/coverage/lcov-report/api/routes/layout.js.html +8 -8
  65. package/coverage/lcov-report/api/routes/metadata.js.html +9 -9
  66. package/coverage/lcov-report/api/routes/permission.js.html +12 -12
  67. package/coverage/lcov-report/api/routes/query.js.html +10 -10
  68. package/coverage/lcov-report/api/routes/role.js.html +12 -12
  69. package/coverage/lcov-report/api/routes/routing.js.html +8 -8
  70. package/coverage/lcov-report/api/routes/row.js.html +10 -10
  71. package/coverage/lcov-report/api/routes/screen.js.html +11 -11
  72. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/static.js.html +13 -13
  74. package/coverage/lcov-report/api/routes/table.js.html +12 -12
  75. package/coverage/lcov-report/api/routes/templates.js.html +8 -8
  76. package/coverage/lcov-report/api/routes/user.js.html +8 -8
  77. package/coverage/lcov-report/api/routes/view.js.html +10 -10
  78. package/coverage/lcov-report/api/routes/webhook.js.html +11 -11
  79. package/coverage/lcov-report/automations/actions.js.html +24 -24
  80. package/coverage/lcov-report/automations/automationUtils.js.html +5 -5
  81. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  82. package/coverage/lcov-report/automations/index.html +1 -1
  83. package/coverage/lcov-report/automations/index.js.html +7 -7
  84. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  85. package/coverage/lcov-report/automations/steps/createRow.js.html +7 -7
  86. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  87. package/coverage/lcov-report/automations/steps/deleteRow.js.html +7 -7
  88. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  89. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  90. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  91. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  92. package/coverage/lcov-report/automations/steps/index.html +1 -1
  93. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  94. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  95. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  96. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  97. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  98. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  99. package/coverage/lcov-report/automations/steps/updateRow.js.html +6 -6
  100. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  101. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  102. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  103. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  104. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  105. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  106. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  107. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  108. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  109. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  110. package/coverage/lcov-report/automations/triggers.js.html +18 -18
  111. package/coverage/lcov-report/automations/utils.js.html +32 -32
  112. package/coverage/lcov-report/constants/index.html +5 -5
  113. package/coverage/lcov-report/constants/index.js.html +36 -24
  114. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  115. package/coverage/lcov-report/constants/screens.js.html +4 -4
  116. package/coverage/lcov-report/db/client.js.html +18 -18
  117. package/coverage/lcov-report/db/inMemoryView.js.html +7 -7
  118. package/coverage/lcov-report/db/index.html +1 -1
  119. package/coverage/lcov-report/db/index.js.html +3 -3
  120. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +7 -7
  121. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  122. package/coverage/lcov-report/db/linkedRows/index.js.html +18 -18
  123. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +13 -13
  124. package/coverage/lcov-report/db/newid.js.html +4 -4
  125. package/coverage/lcov-report/db/utils.js.html +73 -73
  126. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  127. package/coverage/lcov-report/events/index.html +1 -1
  128. package/coverage/lcov-report/events/index.js.html +6 -6
  129. package/coverage/lcov-report/events/utils.js.html +3 -3
  130. package/coverage/lcov-report/index.html +62 -62
  131. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  132. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  133. package/coverage/lcov-report/middleware/appInfo.js.html +5 -5
  134. package/coverage/lcov-report/middleware/authorized.js.html +171 -69
  135. package/coverage/lcov-report/middleware/builder.js.html +16 -16
  136. package/coverage/lcov-report/middleware/currentapp.js.html +46 -73
  137. package/coverage/lcov-report/middleware/index.html +24 -24
  138. package/coverage/lcov-report/middleware/joi-validator.js.html +6 -6
  139. package/coverage/lcov-report/middleware/resourceId.js.html +22 -22
  140. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  141. package/coverage/lcov-report/middleware/usageQuota.js.html +17 -17
  142. package/coverage/lcov-report/middleware/utils.js.html +4 -4
  143. package/coverage/lcov-report/migrations/usageQuotas/index.html +1 -1
  144. package/coverage/lcov-report/migrations/usageQuotas/index.js.html +6 -6
  145. package/coverage/lcov-report/migrations/usageQuotas/syncApps.js.html +6 -6
  146. package/coverage/lcov-report/migrations/usageQuotas/syncRows.js.html +7 -7
  147. package/coverage/lcov-report/threads/automation.js.html +1 -1
  148. package/coverage/lcov-report/threads/index.html +15 -15
  149. package/coverage/lcov-report/threads/index.js.html +10 -10
  150. package/coverage/lcov-report/threads/query.js.html +23 -8
  151. package/coverage/lcov-report/threads/utils.js.html +14 -14
  152. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  153. package/coverage/lcov-report/utilities/budibaseDir.js.html +7 -7
  154. package/coverage/lcov-report/utilities/centralPath.js.html +4 -4
  155. package/coverage/lcov-report/utilities/csvParser.js.html +6 -6
  156. package/coverage/lcov-report/utilities/global.js.html +26 -26
  157. package/coverage/lcov-report/utilities/index.html +19 -19
  158. package/coverage/lcov-report/utilities/index.js.html +25 -25
  159. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +9 -9
  160. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  161. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  162. package/coverage/lcov-report/utilities/routing/index.js.html +6 -6
  163. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +17 -17
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +4 -4
  166. package/coverage/lcov-report/utilities/scriptRunner.js.html +5 -5
  167. package/coverage/lcov-report/utilities/security.js.html +8 -8
  168. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  169. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +8 -8
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +8 -8
  172. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  173. package/coverage/lcov-report/utilities/users.js.html +5 -5
  174. package/coverage/lcov-report/utilities/workerRequests.js.html +39 -108
  175. package/coverage/lcov.info +2292 -2272
  176. package/dist/api/controllers/application.js +49 -20
  177. package/dist/api/controllers/auth.js +4 -0
  178. package/dist/api/controllers/datasource.js +11 -0
  179. package/dist/api/controllers/query/index.js +11 -3
  180. package/dist/api/controllers/row/utils.js +1 -2
  181. package/dist/api/controllers/static/index.js +9 -10
  182. package/dist/api/controllers/user.js +2 -0
  183. package/dist/api/routes/index.js +0 -2
  184. package/dist/constants/index.js +3 -0
  185. package/dist/definitions/datasource.js +1 -0
  186. package/dist/integrations/googlesheets.js +340 -0
  187. package/dist/integrations/index.js +3 -0
  188. package/dist/integrations/utils.js +6 -3
  189. package/dist/middleware/authorized.js +55 -30
  190. package/dist/middleware/currentapp.js +0 -9
  191. package/dist/package.json +8 -5
  192. package/dist/tests/utilities/TestConfiguration.js +10 -6
  193. package/dist/threads/query.js +4 -0
  194. package/dist/tsconfig.tsbuildinfo +1 -1
  195. package/dist/utilities/workerRequests.js +0 -20
  196. package/nodemon.json +1 -1
  197. package/package.json +9 -6
  198. package/src/api/controllers/application.js +52 -24
  199. package/src/api/controllers/auth.js +4 -0
  200. package/src/api/controllers/datasource.js +12 -0
  201. package/src/api/controllers/query/index.js +14 -3
  202. package/src/api/controllers/row/utils.js +1 -4
  203. package/src/api/controllers/static/index.js +13 -10
  204. package/src/api/controllers/user.js +2 -0
  205. package/src/api/routes/index.js +0 -2
  206. package/src/api/routes/tests/application.spec.js +2 -2
  207. package/src/api/routes/tests/auth.spec.js +1 -2
  208. package/src/constants/index.js +4 -0
  209. package/src/definitions/datasource.ts +3 -0
  210. package/src/integrations/googlesheets.ts +353 -0
  211. package/src/integrations/index.ts +3 -0
  212. package/src/integrations/utils.ts +18 -5
  213. package/src/middleware/authorized.js +64 -30
  214. package/src/middleware/currentapp.js +0 -9
  215. package/src/middleware/tests/authorized.spec.js +6 -4
  216. package/src/migrations/tests/usageQuotas/syncRows.spec.js +1 -1
  217. package/src/tests/utilities/TestConfiguration.js +10 -6
  218. package/src/threads/query.js +5 -0
  219. package/src/utilities/workerRequests.js +0 -23
  220. package/coverage/lcov-report/api/controllers/hosting.js.html +0 -146
  221. package/coverage/lcov-report/api/routes/hosting.js.html +0 -119
  222. package/dist/api/controllers/hosting.js +0 -30
  223. package/dist/api/routes/hosting.js +0 -11
  224. package/src/api/controllers/hosting.js +0 -22
  225. package/src/api/routes/hosting.js +0 -13
  226. package/src/api/routes/tests/hosting.spec.js +0 -36
@@ -20,7 +20,7 @@ const { cloneDeep } = require("lodash/fp");
20
20
  const { processObject } = require("@budibase/string-templates");
21
21
  const { getAllApps, isDevAppID, getDeployedAppID, Replication, } = require("@budibase/backend-core/db");
22
22
  const { USERS_TABLE_SCHEMA } = require("../../constants");
23
- const { getDeployedApps, removeAppFromUserRoles, } = require("../../utilities/workerRequests");
23
+ const { removeAppFromUserRoles } = require("../../utilities/workerRequests");
24
24
  const { clientLibraryPath, stringToReadStream } = require("../../utilities");
25
25
  const { getAllLocks } = require("../../utilities/redis");
26
26
  const { updateClientLibrary, backupClientLibrary, revertClientLibrary, } = require("../../utilities/fileSystem/clientLibrary");
@@ -49,31 +49,44 @@ function getUserRoleId(ctx) {
49
49
  ? BUILTIN_ROLE_IDS.PUBLIC
50
50
  : ctx.user.role._id;
51
51
  }
52
- function getAppUrlIfNotInUse(ctx) {
52
+ function getAppUrl(ctx) {
53
53
  return __awaiter(this, void 0, void 0, function* () {
54
+ // construct the url
54
55
  let url;
55
56
  if (ctx.request.body.url) {
57
+ // if the url is provided, use that
56
58
  url = encodeURI(ctx.request.body.url);
57
59
  }
58
60
  else if (ctx.request.body.name) {
61
+ // otherwise use the name
59
62
  url = encodeURI(`${ctx.request.body.name}`);
60
63
  }
61
64
  if (url) {
62
65
  url = `/${url.replace(URL_REGEX_SLASH, "")}`.toLowerCase();
63
66
  }
64
- if (!env.SELF_HOSTED) {
65
- return url;
66
- }
67
- const deployedApps = yield getDeployedApps();
68
- if (url &&
69
- deployedApps[url] != null &&
70
- ctx.params != null &&
71
- deployedApps[url].appId !== ctx.params.appId) {
72
- ctx.throw(400, "App name/URL is already in use.");
73
- }
74
67
  return url;
75
68
  });
76
69
  }
70
+ const checkAppUrl = (ctx, apps, url, currentAppId) => {
71
+ if (currentAppId) {
72
+ apps = apps.filter(app => app.appId !== currentAppId);
73
+ }
74
+ if (apps.some(app => app.url === url)) {
75
+ ctx.throw(400, "App URL is already in use.");
76
+ }
77
+ };
78
+ const checkAppName = (ctx, apps, name, currentAppId) => {
79
+ // TODO: Replace with Joi
80
+ if (!name) {
81
+ ctx.throw(400, "Name is required");
82
+ }
83
+ if (currentAppId) {
84
+ apps = apps.filter(app => app.appId !== currentAppId);
85
+ }
86
+ if (apps.some(app => app.name === name)) {
87
+ ctx.throw(400, "App name is already in use.");
88
+ }
89
+ };
77
90
  function createInstance(template) {
78
91
  return __awaiter(this, void 0, void 0, function* () {
79
92
  const tenantId = isMultiTenant() ? getTenantId() : null;
@@ -167,6 +180,11 @@ exports.fetchAppPackage = (ctx) => __awaiter(void 0, void 0, void 0, function* (
167
180
  };
168
181
  });
169
182
  exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
183
+ const apps = yield getAllApps(CouchDB, { dev: true });
184
+ const name = ctx.request.body.name;
185
+ checkAppName(ctx, apps, name);
186
+ const url = yield getAppUrl(ctx);
187
+ checkAppUrl(ctx, apps, url);
170
188
  const { useTemplate, templateKey, templateString } = ctx.request.body;
171
189
  const instanceConfig = {
172
190
  useTemplate,
@@ -178,7 +196,6 @@ exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
178
196
  }
179
197
  const instance = yield createInstance(instanceConfig);
180
198
  const appId = instance._id;
181
- const url = yield getAppUrlIfNotInUse(ctx);
182
199
  const db = new CouchDB(appId);
183
200
  let _rev;
184
201
  try {
@@ -196,7 +213,7 @@ exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
196
213
  type: "app",
197
214
  version: packageJson.version,
198
215
  componentLibraries: ["@budibase/standard-components"],
199
- name: ctx.request.body.name,
216
+ name: name,
200
217
  url: url,
201
218
  template: ctx.request.body.template,
202
219
  instance: instance,
@@ -219,8 +236,21 @@ exports.create = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
219
236
  ctx.status = 200;
220
237
  ctx.body = newApplication;
221
238
  });
239
+ // This endpoint currently operates as a PATCH rather than a PUT
240
+ // Thus name and url fields are handled only if present
222
241
  exports.update = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
223
- const data = yield updateAppPackage(ctx, ctx.request.body, ctx.params.appId);
242
+ const apps = yield getAllApps(CouchDB, { dev: true });
243
+ // validation
244
+ const name = ctx.request.body.name;
245
+ if (name) {
246
+ checkAppName(ctx, apps, name, ctx.params.appId);
247
+ }
248
+ const url = yield getAppUrl(ctx);
249
+ if (url) {
250
+ checkAppUrl(ctx, apps, url, ctx.params.appId);
251
+ ctx.request.body.url = url;
252
+ }
253
+ const data = yield updateAppPackage(ctx.request.body, ctx.params.appId);
224
254
  ctx.status = 200;
225
255
  ctx.body = data;
226
256
  });
@@ -239,7 +269,7 @@ exports.updateClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
239
269
  version: packageJson.version,
240
270
  revertableVersion: currentVersion,
241
271
  };
242
- const data = yield updateAppPackage(ctx, appPackageUpdates, ctx.params.appId);
272
+ const data = yield updateAppPackage(appPackageUpdates, ctx.params.appId);
243
273
  ctx.status = 200;
244
274
  ctx.body = data;
245
275
  });
@@ -259,7 +289,7 @@ exports.revertClient = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
259
289
  version: application.revertableVersion,
260
290
  revertableVersion: null,
261
291
  };
262
- const data = yield updateAppPackage(ctx, appPackageUpdates, ctx.params.appId);
292
+ const data = yield updateAppPackage(appPackageUpdates, ctx.params.appId);
263
293
  ctx.status = 200;
264
294
  ctx.body = data;
265
295
  });
@@ -326,11 +356,10 @@ exports.sync = (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
326
356
  };
327
357
  }
328
358
  });
329
- const updateAppPackage = (ctx, appPackage, appId) => __awaiter(void 0, void 0, void 0, function* () {
330
- const url = yield getAppUrlIfNotInUse(ctx);
359
+ const updateAppPackage = (appPackage, appId) => __awaiter(void 0, void 0, void 0, function* () {
331
360
  const db = new CouchDB(appId);
332
361
  const application = yield db.get(DocumentTypes.APP_METADATA);
333
- const newAppPackage = Object.assign(Object.assign(Object.assign({}, application), appPackage), { url });
362
+ const newAppPackage = Object.assign(Object.assign({}, application), appPackage);
334
363
  if (appPackage._rev !== application._rev) {
335
364
  newAppPackage._rev = application._rev;
336
365
  }
@@ -24,6 +24,8 @@ exports.fetchSelf = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
24
24
  const user = yield getFullUser(ctx, userId);
25
25
  // this shouldn't be returned by the app self
26
26
  delete user.roles;
27
+ // forward the csrf token from the session
28
+ user.csrfToken = ctx.user.csrfToken;
27
29
  if (appId) {
28
30
  const db = new CouchDB(appId);
29
31
  // remove the full roles structure
@@ -31,6 +33,8 @@ exports.fetchSelf = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
31
33
  try {
32
34
  const userTable = yield db.get(InternalTables.USER_METADATA);
33
35
  const metadata = yield db.get(userId);
36
+ // make sure there is never a stale csrf token
37
+ delete metadata.csrfToken;
34
38
  // specifically needs to make sure is enriched
35
39
  ctx.body = yield outputProcessing(ctx, userTable, Object.assign(Object.assign({}, user), metadata));
36
40
  }
@@ -28,6 +28,12 @@ exports.fetch = function (ctx) {
28
28
  const datasources = (yield database.allDocs(getDatasourceParams(null, {
29
29
  include_docs: true,
30
30
  }))).rows.map(row => row.doc);
31
+ for (let datasource of datasources) {
32
+ if (datasource.config && datasource.config.auth) {
33
+ // strip secrets from response so they don't show in the network request
34
+ delete datasource.config.auth;
35
+ }
36
+ }
31
37
  ctx.body = [bbInternalDb, ...datasources];
32
38
  });
33
39
  };
@@ -78,8 +84,13 @@ exports.update = function (ctx) {
78
84
  const db = new CouchDB(ctx.appId);
79
85
  const datasourceId = ctx.params.datasourceId;
80
86
  let datasource = yield db.get(datasourceId);
87
+ const auth = datasource.config.auth;
81
88
  yield invalidateVariables(datasource, ctx.request.body);
82
89
  datasource = Object.assign(Object.assign({}, datasource), ctx.request.body);
90
+ if (auth && !ctx.request.body.auth) {
91
+ // don't strip auth config from DB
92
+ datasource.config.auth = auth;
93
+ }
83
94
  const response = yield db.put(datasource);
84
95
  datasource._rev = response.rev;
85
96
  // Drain connection pools when configuration is changed
@@ -130,6 +130,15 @@ function execute(ctx, opts = { rowsOnly: false }) {
130
130
  const db = new CouchDB(ctx.appId);
131
131
  const query = yield db.get(ctx.params.queryId);
132
132
  const datasource = yield db.get(query.datasourceId);
133
+ const enrichedParameters = ctx.request.body.parameters || {};
134
+ // make sure parameters are fully enriched with defaults
135
+ if (query && query.parameters) {
136
+ for (let parameter of query.parameters) {
137
+ if (!enrichedParameters[parameter.name]) {
138
+ enrichedParameters[parameter.name] = parameter.default;
139
+ }
140
+ }
141
+ }
133
142
  // call the relevant CRUD method on the integration class
134
143
  try {
135
144
  const { rows, pagination, extra } = yield Runner.run({
@@ -138,7 +147,7 @@ function execute(ctx, opts = { rowsOnly: false }) {
138
147
  queryVerb: query.queryVerb,
139
148
  fields: query.fields,
140
149
  pagination: ctx.request.body.pagination,
141
- parameters: ctx.request.body.parameters,
150
+ parameters: enrichedParameters,
142
151
  transformer: query.transformer,
143
152
  queryId: ctx.params.queryId,
144
153
  });
@@ -170,8 +179,7 @@ const removeDynamicVariables = (db, queryId) => __awaiter(void 0, void 0, void 0
170
179
  const dynamicVariables = datasource.config.dynamicVariables;
171
180
  if (dynamicVariables) {
172
181
  // delete dynamic variables from the datasource
173
- const newVariables = dynamicVariables.filter(dv => dv.queryId !== queryId);
174
- datasource.config.dynamicVariables = newVariables;
182
+ datasource.config.dynamicVariables = dynamicVariables.filter(dv => dv.queryId !== queryId);
175
183
  yield db.put(datasource);
176
184
  // invalidate the deleted variables
177
185
  const variablesToDelete = dynamicVariables.filter(dv => dv.queryId === queryId);
@@ -59,8 +59,7 @@ exports.validate = ({ appId, tableId, row, table }) => __awaiter(void 0, void 0,
59
59
  const constraints = cloneDeep(table.schema[fieldName].constraints);
60
60
  const type = table.schema[fieldName].type;
61
61
  // special case for options, need to always allow unselected (null)
62
- if ((type === FieldTypes.OPTIONS || type === FieldTypes.ARRAY) &&
63
- constraints.inclusion) {
62
+ if (type === FieldTypes.OPTIONS && constraints.inclusion) {
64
63
  constraints.inclusion.push(null);
65
64
  }
66
65
  let res;
@@ -14,7 +14,7 @@ const { resolve, join } = require("../../../utilities/centralPath");
14
14
  const uuid = require("uuid");
15
15
  const { ObjectStoreBuckets } = require("../../../constants");
16
16
  const { processString } = require("@budibase/string-templates");
17
- const { getDeployedApps } = require("../../../utilities/workerRequests");
17
+ const { getAllApps } = require("@budibase/backend-core/db");
18
18
  const CouchDB = require("../../../db");
19
19
  const { loadHandlebarsFile, NODE_MODULES_PATH, TOP_LEVEL_PATH, } = require("../../../utilities/fileSystem");
20
20
  const env = require("../../../environment");
@@ -43,13 +43,15 @@ function prepareUpload({ s3Key, bucket, metadata, file }) {
43
43
  };
44
44
  });
45
45
  }
46
- function checkForSelfHostedURL(ctx) {
46
+ function getAppIdFromUrl(ctx) {
47
47
  return __awaiter(this, void 0, void 0, function* () {
48
- // the "appId" component of the URL may actually be a specific self hosted URL
48
+ // the "appId" component of the URL can be the id or the custom url
49
49
  let possibleAppUrl = `/${encodeURI(ctx.params.appId).toLowerCase()}`;
50
- const apps = yield getDeployedApps();
51
- if (apps[possibleAppUrl] && apps[possibleAppUrl].appId) {
52
- return apps[possibleAppUrl].appId;
50
+ // search prod apps for a url that matches, exclude dev where id is always used
51
+ const apps = yield getAllApps(CouchDB, { dev: false });
52
+ const app = apps.filter(a => a.url && a.url.toLowerCase() === possibleAppUrl)[0];
53
+ if (app && app.appId) {
54
+ return app.appId;
53
55
  }
54
56
  else {
55
57
  return ctx.params.appId;
@@ -82,10 +84,7 @@ exports.uploadFile = function (ctx) {
82
84
  };
83
85
  exports.serveApp = function (ctx) {
84
86
  return __awaiter(this, void 0, void 0, function* () {
85
- let appId = ctx.params.appId;
86
- if (env.SELF_HOSTED) {
87
- appId = yield checkForSelfHostedURL(ctx);
88
- }
87
+ let appId = yield getAppIdFromUrl(ctx);
89
88
  const App = require("./templates/BudibaseApp.svelte").default;
90
89
  const db = new CouchDB(appId, { skip_setup: true });
91
90
  const appInfo = yield db.get(DocumentTypes.APP_METADATA);
@@ -155,6 +155,8 @@ exports.updateSelfMetadata = function (ctx) {
155
155
  ctx.request.body._id = ctx.user._id;
156
156
  // make sure no stale rev
157
157
  delete ctx.request.body._rev;
158
+ // make sure no csrf token
159
+ delete ctx.request.body.csrfToken;
158
160
  yield exports.updateMetadata(ctx);
159
161
  });
160
162
  };
@@ -21,7 +21,6 @@ const integrationRoutes = require("./integration");
21
21
  const permissionRoutes = require("./permission");
22
22
  const datasourceRoutes = require("./datasource");
23
23
  const queryRoutes = require("./query");
24
- const hostingRoutes = require("./hosting");
25
24
  const backupRoutes = require("./backup");
26
25
  const metadataRoutes = require("./metadata");
27
26
  const devRoutes = require("./dev");
@@ -46,7 +45,6 @@ exports.mainRoutes = [
46
45
  permissionRoutes,
47
46
  datasourceRoutes,
48
47
  queryRoutes,
49
- hostingRoutes,
50
48
  backupRoutes,
51
49
  metadataRoutes,
52
50
  devRoutes,
@@ -67,6 +67,9 @@ exports.DataSourceOperation = {
67
67
  UPDATE_TABLE: "UPDATE_TABLE",
68
68
  DELETE_TABLE: "DELETE_TABLE",
69
69
  };
70
+ exports.DatasourceAuthTypes = {
71
+ GOOGLE: "google",
72
+ };
70
73
  exports.SortDirection = {
71
74
  ASCENDING: "ASCENDING",
72
75
  DESCENDING: "DESCENDING",
@@ -49,6 +49,7 @@ var SourceNames;
49
49
  SourceNames["ARANGODB"] = "ARANGODB";
50
50
  SourceNames["REST"] = "REST";
51
51
  SourceNames["ORACLE"] = "ORACLE";
52
+ SourceNames["GOOGLE_SHEETS"] = "GOOGLE_SHEETS";
52
53
  })(SourceNames = exports.SourceNames || (exports.SourceNames = {}));
53
54
  var IncludeRelationships;
54
55
  (function (IncludeRelationships) {
@@ -0,0 +1,340 @@
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
+ Object.defineProperty(exports, "__esModule", { value: true });
12
+ const datasource_1 = require("../definitions/datasource");
13
+ const google_auth_library_1 = require("google-auth-library");
14
+ const utils_1 = require("./utils");
15
+ const constants_1 = require("../constants");
16
+ const google_spreadsheet_1 = require("google-spreadsheet");
17
+ var GoogleSheetsModule;
18
+ (function (GoogleSheetsModule) {
19
+ const { getGlobalDB } = require("@budibase/backend-core/tenancy");
20
+ const { getScopedConfig } = require("@budibase/backend-core/db");
21
+ const { Configs } = require("@budibase/backend-core/constants");
22
+ const SCHEMA = {
23
+ plus: true,
24
+ auth: {
25
+ type: "google",
26
+ },
27
+ relationships: false,
28
+ docs: "https://developers.google.com/sheets/api/quickstart/nodejs",
29
+ description: "Create and collaborate on online spreadsheets in real-time and from any device. ",
30
+ friendlyName: "Google Sheets",
31
+ datasource: {
32
+ spreadsheetId: {
33
+ type: datasource_1.DatasourceFieldTypes.STRING,
34
+ required: true,
35
+ },
36
+ },
37
+ query: {
38
+ create: {
39
+ type: datasource_1.QueryTypes.FIELDS,
40
+ fields: {
41
+ sheet: {
42
+ type: datasource_1.DatasourceFieldTypes.STRING,
43
+ required: true,
44
+ },
45
+ row: {
46
+ type: datasource_1.QueryTypes.JSON,
47
+ required: true,
48
+ },
49
+ },
50
+ },
51
+ read: {
52
+ type: datasource_1.QueryTypes.FIELDS,
53
+ fields: {
54
+ sheet: {
55
+ type: datasource_1.DatasourceFieldTypes.STRING,
56
+ required: true,
57
+ },
58
+ },
59
+ },
60
+ update: {
61
+ type: datasource_1.QueryTypes.FIELDS,
62
+ fields: {
63
+ sheet: {
64
+ type: datasource_1.DatasourceFieldTypes.STRING,
65
+ required: true,
66
+ },
67
+ rowIndex: {
68
+ type: datasource_1.DatasourceFieldTypes.STRING,
69
+ required: true,
70
+ },
71
+ row: {
72
+ type: datasource_1.QueryTypes.JSON,
73
+ required: true,
74
+ },
75
+ },
76
+ },
77
+ delete: {
78
+ type: datasource_1.QueryTypes.FIELDS,
79
+ fields: {
80
+ sheet: {
81
+ type: datasource_1.DatasourceFieldTypes.STRING,
82
+ required: true,
83
+ },
84
+ rowIndex: {
85
+ type: datasource_1.DatasourceFieldTypes.NUMBER,
86
+ required: true,
87
+ },
88
+ },
89
+ },
90
+ },
91
+ };
92
+ class GoogleSheetsIntegration {
93
+ constructor(config) {
94
+ this.tables = {};
95
+ this.schemaErrors = {};
96
+ this.config = config;
97
+ const spreadsheetId = this.cleanSpreadsheetUrl(this.config.spreadsheetId);
98
+ this.client = new google_spreadsheet_1.GoogleSpreadsheet(spreadsheetId);
99
+ }
100
+ /**
101
+ * Pull the spreadsheet ID out from a valid google sheets URL
102
+ * @param spreadsheetId - the URL or standard spreadsheetId of the google sheet
103
+ * @returns spreadsheet Id of the google sheet
104
+ */
105
+ cleanSpreadsheetUrl(spreadsheetId) {
106
+ if (!spreadsheetId) {
107
+ throw new Error("You must set a spreadsheet ID in your configuration to fetch tables.");
108
+ }
109
+ const parts = spreadsheetId.split("/");
110
+ return parts.length > 5 ? parts[5] : spreadsheetId;
111
+ }
112
+ connect() {
113
+ return __awaiter(this, void 0, void 0, function* () {
114
+ try {
115
+ // Initialise oAuth client
116
+ const db = getGlobalDB();
117
+ const googleConfig = yield getScopedConfig(db, {
118
+ type: Configs.GOOGLE,
119
+ });
120
+ const oauthClient = new google_auth_library_1.OAuth2Client({
121
+ clientId: googleConfig.clientID,
122
+ clientSecret: googleConfig.clientSecret,
123
+ });
124
+ oauthClient.credentials.access_token = this.config.auth.accessToken;
125
+ oauthClient.credentials.refresh_token = this.config.auth.refreshToken;
126
+ this.client.useOAuth2Client(oauthClient);
127
+ yield this.client.loadInfo();
128
+ }
129
+ catch (err) {
130
+ console.error("Error connecting to google sheets", err);
131
+ throw err;
132
+ }
133
+ });
134
+ }
135
+ buildSchema(datasourceId) {
136
+ return __awaiter(this, void 0, void 0, function* () {
137
+ yield this.connect();
138
+ const sheets = yield this.client.sheetsByIndex;
139
+ const tables = {};
140
+ for (let sheet of sheets) {
141
+ // must fetch rows to determine schema
142
+ yield sheet.getRows();
143
+ // build schema
144
+ const schema = {};
145
+ // build schema from headers
146
+ for (let header of sheet.headerValues) {
147
+ schema[header] = {
148
+ name: header,
149
+ type: constants_1.FieldTypes.STRING,
150
+ };
151
+ }
152
+ // create tables
153
+ tables[sheet.title] = {
154
+ _id: utils_1.buildExternalTableId(datasourceId, sheet.title),
155
+ name: sheet.title,
156
+ primary: ["rowNumber"],
157
+ schema,
158
+ };
159
+ }
160
+ this.tables = tables;
161
+ });
162
+ }
163
+ query(json) {
164
+ return __awaiter(this, void 0, void 0, function* () {
165
+ const sheet = json.endpoint.entityId;
166
+ const handlers = {
167
+ [constants_1.DataSourceOperation.CREATE]: () => this.create({ sheet, row: json.body }),
168
+ [constants_1.DataSourceOperation.READ]: () => this.read({ sheet }),
169
+ [constants_1.DataSourceOperation.UPDATE]: () => {
170
+ var _a, _b, _c;
171
+ return this.update({
172
+ // exclude the header row and zero index
173
+ rowIndex: ((_c = (_b = (_a = json.extra) === null || _a === void 0 ? void 0 : _a.idFilter) === null || _b === void 0 ? void 0 : _b.equal) === null || _c === void 0 ? void 0 : _c.rowNumber) - 2,
174
+ sheet,
175
+ row: json.body,
176
+ });
177
+ },
178
+ [constants_1.DataSourceOperation.DELETE]: () => {
179
+ var _a, _b, _c;
180
+ return this.delete({
181
+ // exclude the header row and zero index
182
+ rowIndex: ((_c = (_b = (_a = json.extra) === null || _a === void 0 ? void 0 : _a.idFilter) === null || _b === void 0 ? void 0 : _b.equal) === null || _c === void 0 ? void 0 : _c.rowNumber) - 2,
183
+ sheet,
184
+ });
185
+ },
186
+ [constants_1.DataSourceOperation.CREATE_TABLE]: () => { var _a; return this.createTable((_a = json === null || json === void 0 ? void 0 : json.table) === null || _a === void 0 ? void 0 : _a.name); },
187
+ [constants_1.DataSourceOperation.UPDATE_TABLE]: () => this.updateTable(json.table),
188
+ [constants_1.DataSourceOperation.DELETE_TABLE]: () => { var _a; return this.deleteTable((_a = json === null || json === void 0 ? void 0 : json.table) === null || _a === void 0 ? void 0 : _a.name); },
189
+ };
190
+ const internalQueryMethod = handlers[json.endpoint.operation];
191
+ return yield internalQueryMethod();
192
+ });
193
+ }
194
+ buildRowObject(headers, values, rowNumber) {
195
+ const rowObject = { rowNumber };
196
+ for (let i = 0; i < headers.length; i++) {
197
+ rowObject._id = rowNumber;
198
+ rowObject[headers[i]] = values[i];
199
+ }
200
+ return rowObject;
201
+ }
202
+ createTable(name) {
203
+ return __awaiter(this, void 0, void 0, function* () {
204
+ try {
205
+ yield this.connect();
206
+ const sheet = yield this.client.addSheet({ title: name });
207
+ return sheet;
208
+ }
209
+ catch (err) {
210
+ console.error("Error creating new table in google sheets", err);
211
+ throw err;
212
+ }
213
+ });
214
+ }
215
+ updateTable(table) {
216
+ return __awaiter(this, void 0, void 0, function* () {
217
+ try {
218
+ yield this.connect();
219
+ const sheet = yield this.client.sheetsByTitle[table.name];
220
+ yield sheet.loadHeaderRow();
221
+ if (table._rename) {
222
+ const headers = [];
223
+ for (let header of sheet.headerValues) {
224
+ if (header === table._rename.old) {
225
+ headers.push(table._rename.updated);
226
+ }
227
+ else {
228
+ headers.push(header);
229
+ }
230
+ }
231
+ yield sheet.setHeaderRow(headers);
232
+ }
233
+ else {
234
+ let newField = Object.keys(table.schema).find(key => !sheet.headerValues.includes(key));
235
+ yield sheet.setHeaderRow([...sheet.headerValues, newField]);
236
+ }
237
+ }
238
+ catch (err) {
239
+ console.error("Error updating table in google sheets", err);
240
+ throw err;
241
+ }
242
+ });
243
+ }
244
+ deleteTable(sheet) {
245
+ return __awaiter(this, void 0, void 0, function* () {
246
+ try {
247
+ yield this.connect();
248
+ const sheetToDelete = yield this.client.sheetsByTitle[sheet];
249
+ return yield sheetToDelete.delete();
250
+ }
251
+ catch (err) {
252
+ console.error("Error deleting table in google sheets", err);
253
+ throw err;
254
+ }
255
+ });
256
+ }
257
+ create(query) {
258
+ return __awaiter(this, void 0, void 0, function* () {
259
+ try {
260
+ yield this.connect();
261
+ const sheet = yield this.client.sheetsByTitle[query.sheet];
262
+ const rowToInsert = typeof query.row === "string" ? JSON.parse(query.row) : query.row;
263
+ const row = yield sheet.addRow(rowToInsert);
264
+ return [
265
+ this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber),
266
+ ];
267
+ }
268
+ catch (err) {
269
+ console.error("Error writing to google sheets", err);
270
+ throw err;
271
+ }
272
+ });
273
+ }
274
+ read(query) {
275
+ return __awaiter(this, void 0, void 0, function* () {
276
+ try {
277
+ yield this.connect();
278
+ const sheet = yield this.client.sheetsByTitle[query.sheet];
279
+ const rows = yield sheet.getRows();
280
+ const headerValues = sheet.headerValues;
281
+ const response = [];
282
+ for (let row of rows) {
283
+ response.push(this.buildRowObject(headerValues, row._rawData, row._rowNumber));
284
+ }
285
+ return response;
286
+ }
287
+ catch (err) {
288
+ console.error("Error reading from google sheets", err);
289
+ throw err;
290
+ }
291
+ });
292
+ }
293
+ update(query) {
294
+ return __awaiter(this, void 0, void 0, function* () {
295
+ try {
296
+ yield this.connect();
297
+ const sheet = yield this.client.sheetsByTitle[query.sheet];
298
+ const rows = yield sheet.getRows();
299
+ const row = rows[query.rowIndex];
300
+ if (row) {
301
+ const updateValues = query.row;
302
+ for (let key in updateValues) {
303
+ row[key] = updateValues[key];
304
+ }
305
+ yield row.save();
306
+ return [
307
+ this.buildRowObject(sheet.headerValues, row._rawData, row._rowNumber),
308
+ ];
309
+ }
310
+ else {
311
+ throw new Error("Row does not exist.");
312
+ }
313
+ }
314
+ catch (err) {
315
+ console.error("Error reading from google sheets", err);
316
+ throw err;
317
+ }
318
+ });
319
+ }
320
+ delete(query) {
321
+ return __awaiter(this, void 0, void 0, function* () {
322
+ yield this.connect();
323
+ const sheet = yield this.client.sheetsByTitle[query.sheet];
324
+ const rows = yield sheet.getRows();
325
+ const row = rows[query.rowIndex];
326
+ if (row) {
327
+ yield row.delete();
328
+ return [{ deleted: query.rowIndex }];
329
+ }
330
+ else {
331
+ throw new Error("Row does not exist.");
332
+ }
333
+ });
334
+ }
335
+ }
336
+ module.exports = {
337
+ schema: SCHEMA,
338
+ integration: GoogleSheetsIntegration,
339
+ };
340
+ })(GoogleSheetsModule || (GoogleSheetsModule = {}));