@budibase/server 1.0.48 → 1.0.49-alpha.0

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 (257) hide show
  1. package/builder/assets/{index.e083dcaf.js → index.8184eed9.js} +224 -222
  2. package/builder/assets/{index.39e01193.css → index.dd9e386f.css} +1 -1
  3. package/builder/assets/{vendor.f9faf6ab.js → vendor.3bd2f661.js} +12 -12
  4. package/builder/index.html +3 -3
  5. package/coverage/clover.xml +1860 -1683
  6. package/coverage/coverage-final.json +99 -95
  7. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  8. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/application.js.html +210 -126
  10. package/coverage/lcov-report/api/controllers/auth.js.html +18 -6
  11. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  12. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  14. package/coverage/lcov-report/api/controllers/component.js.html +4 -4
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +72 -36
  16. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/index.html +38 -53
  18. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  20. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  22. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/index.js.html +19 -19
  24. package/coverage/lcov-report/api/controllers/query/validation.js.html +6 -6
  25. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  26. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/external.js.html +16 -16
  28. package/coverage/lcov-report/api/controllers/row/index.html +15 -15
  29. package/coverage/lcov-report/api/controllers/row/index.js.html +26 -26
  30. package/coverage/lcov-report/api/controllers/row/internal.js.html +44 -44
  31. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +8 -8
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +39 -57
  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 +23 -23
  37. package/coverage/lcov-report/api/controllers/table/index.js.html +57 -39
  38. package/coverage/lcov-report/api/controllers/table/internal.js.html +40 -34
  39. package/coverage/lcov-report/api/controllers/table/utils.js.html +87 -63
  40. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  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 +10 -10
  50. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/index.html +7 -22
  62. package/coverage/lcov-report/api/routes/index.js.html +4 -10
  63. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/static.js.html +17 -2
  74. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  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 +1 -1
  84. package/coverage/lcov-report/automations/steps/bash.js.html +6 -6
  85. package/coverage/lcov-report/automations/steps/createRow.js.html +13 -19
  86. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  87. package/coverage/lcov-report/automations/steps/deleteRow.js.html +11 -20
  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 +13 -13
  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 +38 -26
  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 +15 -15
  119. package/coverage/lcov-report/db/index.js.html +3 -3
  120. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +18 -18
  121. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  122. package/coverage/lcov-report/db/linkedRows/index.js.html +31 -31
  123. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +15 -15
  124. package/coverage/lcov-report/db/newid.js.html +4 -4
  125. package/coverage/lcov-report/db/utils.js.html +85 -85
  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 +5 -5
  129. package/coverage/lcov-report/events/utils.js.html +3 -3
  130. package/coverage/lcov-report/index.html +107 -77
  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 +1 -1
  134. package/coverage/lcov-report/middleware/authorized.js.html +166 -64
  135. package/coverage/lcov-report/middleware/builder.js.html +8 -8
  136. package/coverage/lcov-report/middleware/currentapp.js.html +29 -29
  137. package/coverage/lcov-report/middleware/index.html +30 -30
  138. package/coverage/lcov-report/middleware/joi-validator.js.html +3 -3
  139. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  140. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  141. package/coverage/lcov-report/middleware/usageQuota.js.html +275 -41
  142. package/coverage/lcov-report/middleware/utils.js.html +2 -2
  143. package/coverage/lcov-report/migrations/usageQuotas/index.html +141 -0
  144. package/coverage/lcov-report/migrations/usageQuotas/index.js.html +152 -0
  145. package/coverage/lcov-report/{api/routes/hosting.js.html → migrations/usageQuotas/syncApps.js.html} +42 -27
  146. package/coverage/lcov-report/{api/controllers/hosting.js.html → migrations/usageQuotas/syncRows.js.html} +41 -44
  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 +8 -8
  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 +1 -1
  163. package/coverage/lcov-report/utilities/rowProcessor/index.html +1 -1
  164. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +53 -53
  165. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +10 -10
  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 +141 -0
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +356 -0
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +302 -0
  172. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +134 -0
  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 +2730 -2419
  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/row/utils.js +6 -8
  180. package/dist/api/controllers/static/index.js +59 -10
  181. package/dist/api/controllers/table/index.js +10 -4
  182. package/dist/api/controllers/table/internal.js +2 -0
  183. package/dist/api/controllers/table/utils.js +9 -2
  184. package/dist/api/controllers/user.js +2 -0
  185. package/dist/api/routes/index.js +0 -2
  186. package/dist/api/routes/static.js +2 -1
  187. package/dist/automations/steps/createRow.js +2 -4
  188. package/dist/automations/steps/deleteRow.js +1 -4
  189. package/dist/constants/index.js +3 -0
  190. package/dist/definitions/datasource.js +1 -0
  191. package/dist/environment.js +1 -0
  192. package/dist/integrations/base/sql.js +13 -6
  193. package/dist/integrations/googlesheets.js +340 -0
  194. package/dist/integrations/index.js +3 -0
  195. package/dist/integrations/utils.js +57 -9
  196. package/dist/middleware/authorized.js +55 -30
  197. package/dist/middleware/usageQuota.js +82 -13
  198. package/dist/migrations/usageQuotas/index.js +24 -0
  199. package/dist/migrations/usageQuotas/syncApps.js +26 -0
  200. package/dist/migrations/usageQuotas/syncRows.js +29 -0
  201. package/dist/package.json +8 -5
  202. package/dist/tests/utilities/TestConfiguration.js +10 -6
  203. package/dist/threads/query.js +4 -0
  204. package/dist/tsconfig.tsbuildinfo +1 -1
  205. package/dist/utilities/{usageQuota.js → usageQuota/index.js} +44 -31
  206. package/dist/utilities/usageQuota/rows.js +77 -0
  207. package/dist/utilities/usageQuota/usageQuoteReset.js +16 -0
  208. package/dist/utilities/workerRequests.js +0 -20
  209. package/nodemon.json +1 -1
  210. package/package.json +9 -6
  211. package/src/api/controllers/application.js +52 -24
  212. package/src/api/controllers/auth.js +4 -0
  213. package/src/api/controllers/datasource.js +12 -0
  214. package/src/api/controllers/row/utils.js +6 -12
  215. package/src/api/controllers/static/index.js +63 -10
  216. package/src/api/controllers/table/index.js +15 -9
  217. package/src/api/controllers/table/internal.js +2 -0
  218. package/src/api/controllers/table/utils.js +9 -1
  219. package/src/api/controllers/user.js +2 -0
  220. package/src/api/routes/index.js +0 -2
  221. package/src/api/routes/static.js +5 -0
  222. package/src/api/routes/tests/application.spec.js +2 -2
  223. package/src/api/routes/tests/auth.spec.js +1 -2
  224. package/src/api/routes/tests/static.spec.js +98 -0
  225. package/src/automations/steps/createRow.js +2 -4
  226. package/src/automations/steps/deleteRow.js +1 -4
  227. package/src/constants/index.js +4 -0
  228. package/src/definitions/datasource.ts +3 -0
  229. package/src/environment.js +1 -0
  230. package/src/integrations/base/sql.ts +14 -7
  231. package/src/integrations/googlesheets.ts +353 -0
  232. package/src/integrations/index.ts +3 -0
  233. package/src/integrations/tests/sql.spec.js +16 -0
  234. package/src/integrations/utils.ts +71 -11
  235. package/src/middleware/authorized.js +64 -30
  236. package/src/middleware/tests/authorized.spec.js +6 -4
  237. package/src/middleware/tests/usageQuota.spec.js +18 -24
  238. package/src/middleware/usageQuota.js +93 -15
  239. package/src/migrations/tests/usageQuotas/index.spec.js +27 -0
  240. package/src/migrations/tests/usageQuotas/syncApps.spec.js +37 -0
  241. package/src/migrations/tests/usageQuotas/syncRows.spec.js +43 -0
  242. package/src/migrations/usageQuotas/index.js +24 -0
  243. package/src/migrations/usageQuotas/syncApps.js +18 -0
  244. package/src/migrations/usageQuotas/syncRows.js +21 -0
  245. package/src/tests/utilities/TestConfiguration.js +10 -6
  246. package/src/threads/query.js +5 -0
  247. package/src/utilities/tests/usageQuota/usageQuota.spec.js +72 -0
  248. package/src/utilities/usageQuota/index.js +92 -0
  249. package/src/utilities/usageQuota/rows.js +74 -0
  250. package/src/utilities/usageQuota/usageQuoteReset.js +18 -0
  251. package/src/utilities/workerRequests.js +0 -23
  252. package/dist/api/controllers/hosting.js +0 -30
  253. package/dist/api/routes/hosting.js +0 -11
  254. package/src/api/controllers/hosting.js +0 -22
  255. package/src/api/routes/hosting.js +0 -13
  256. package/src/api/routes/tests/hosting.spec.js +0 -36
  257. package/src/utilities/usageQuota.js +0 -73
@@ -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 = {}));
@@ -10,6 +10,7 @@ const airtable = require("./airtable");
10
10
  const mysql = require("./mysql");
11
11
  const arangodb = require("./arangodb");
12
12
  const rest = require("./rest");
13
+ const googlesheets = require("./googlesheets");
13
14
  const { SourceNames } = require("../definitions/datasource");
14
15
  const DEFINITIONS = {
15
16
  [SourceNames.POSTGRES]: postgres.schema,
@@ -23,6 +24,7 @@ const DEFINITIONS = {
23
24
  [SourceNames.MYSQL]: mysql.schema,
24
25
  [SourceNames.ARANGODB]: arangodb.schema,
25
26
  [SourceNames.REST]: rest.schema,
27
+ [SourceNames.GOOGLE_SHEETS]: googlesheets.schema,
26
28
  };
27
29
  const INTEGRATIONS = {
28
30
  [SourceNames.POSTGRES]: postgres.integration,
@@ -36,6 +38,7 @@ const INTEGRATIONS = {
36
38
  [SourceNames.MYSQL]: mysql.integration,
37
39
  [SourceNames.ARANGODB]: arangodb.integration,
38
40
  [SourceNames.REST]: rest.integration,
41
+ [SourceNames.GOOGLE_SHEETS]: googlesheets.integration,
39
42
  };
40
43
  // optionally add oracle integration if the oracle binary can be installed
41
44
  if (!(process.arch === "arm64" && process.platform === "darwin")) {
@@ -125,7 +125,12 @@ function isSQL(datasource) {
125
125
  if (!datasource || !datasource.source) {
126
126
  return false;
127
127
  }
128
- const SQL = [datasource_1.SourceNames.POSTGRES, datasource_1.SourceNames.SQL_SERVER, datasource_1.SourceNames.MYSQL];
128
+ const SQL = [
129
+ datasource_1.SourceNames.POSTGRES,
130
+ datasource_1.SourceNames.SQL_SERVER,
131
+ datasource_1.SourceNames.MYSQL,
132
+ datasource_1.SourceNames.ORACLE,
133
+ ];
129
134
  return SQL.indexOf(datasource.source) !== -1;
130
135
  }
131
136
  exports.isSQL = isSQL;
@@ -137,8 +142,44 @@ function isIsoDateString(str) {
137
142
  return d.toISOString() === str;
138
143
  }
139
144
  exports.isIsoDateString = isIsoDateString;
140
- // add the existing relationships from the entities if they exist, to prevent them from being overridden
141
- function copyExistingPropsOver(tableName, table, entities) {
145
+ /**
146
+ * This function will determine whether a column is a relationship and whether it
147
+ * is currently valid. The reason for the validity check is that tables can be deleted
148
+ * outside of Budibase control and if this is the case it will break Budibase relationships.
149
+ * The tableIds is a list passed down from the main finalise tables function, which is
150
+ * based on the tables that have just been fetched. This will only really be used on subsequent
151
+ * fetches to the first one - if the user is periodically refreshing Budibase knowledge of tables.
152
+ * @param column The column to check, to see if it is a valid relationship.
153
+ * @param tableIds The IDs of the tables which currently exist.
154
+ */
155
+ function shouldCopyRelationship(column, tableIds) {
156
+ return (column.type === constants_1.FieldTypes.LINK &&
157
+ column.tableId &&
158
+ tableIds.includes(column.tableId));
159
+ }
160
+ /**
161
+ * Similar function to the shouldCopyRelationship function, but instead this looks for options and boolean
162
+ * types. It is possible to switch a string -> options and a number -> boolean (and vice versus) need to make
163
+ * sure that these get copied over when tables are fetched. Also checks whether they are still valid, if a
164
+ * column has changed type in the external database then copying it over may not be possible.
165
+ * @param column The column to check for options or boolean type.
166
+ * @param fetchedColumn The fetched column to check for the type in the external database.
167
+ */
168
+ function shouldCopySpecialColumn(column, fetchedColumn) {
169
+ return (column.type === constants_1.FieldTypes.OPTIONS ||
170
+ column.type === constants_1.FieldTypes.ARRAY ||
171
+ ((!fetchedColumn || fetchedColumn.type === constants_1.FieldTypes.NUMBER) &&
172
+ column.type === constants_1.FieldTypes.BOOLEAN));
173
+ }
174
+ /**
175
+ * Looks for columns which need to be copied over into the new table definitions, like relationships
176
+ * and options types.
177
+ * @param tableName The name of the table which is being checked.
178
+ * @param table The specific table which is being checked.
179
+ * @param entities All the tables that existed before - the old table definitions.
180
+ * @param tableIds The IDs of the tables which exist now, to check if anything has been removed.
181
+ */
182
+ function copyExistingPropsOver(tableName, table, entities, tableIds) {
142
183
  if (entities && entities[tableName]) {
143
184
  if (entities[tableName].primaryDisplay) {
144
185
  table.primaryDisplay = entities[tableName].primaryDisplay;
@@ -148,21 +189,28 @@ function copyExistingPropsOver(tableName, table, entities) {
148
189
  if (!existingTableSchema.hasOwnProperty(key)) {
149
190
  continue;
150
191
  }
151
- if (existingTableSchema[key].type === constants_1.FieldTypes.LINK ||
152
- existingTableSchema[key].type === constants_1.FieldTypes.OPTIONS ||
153
- existingTableSchema[key].type === constants_1.FieldTypes.ARRAY ||
154
- ((!table.schema[key] || table.schema[key].type === constants_1.FieldTypes.NUMBER) &&
155
- existingTableSchema[key].type === constants_1.FieldTypes.BOOLEAN)) {
192
+ const column = existingTableSchema[key];
193
+ if (shouldCopyRelationship(column, tableIds) ||
194
+ shouldCopySpecialColumn(column, table.schema[key])) {
156
195
  table.schema[key] = existingTableSchema[key];
157
196
  }
158
197
  }
159
198
  }
160
199
  return table;
161
200
  }
201
+ /**
202
+ * Look through the final table definitions to see if anything needs to be
203
+ * copied over from the old and if any errors have occurred mark them so
204
+ * that the user can be made aware.
205
+ * @param tables The list of tables that have been retrieved from the external database.
206
+ * @param entities The old list of tables, if there was any to look for definitions in.
207
+ */
162
208
  function finaliseExternalTables(tables, entities) {
163
209
  const invalidColumns = Object.values(constants_1.InvalidColumns);
164
210
  let finalTables = {};
165
211
  const errors = {};
212
+ // @ts-ignore
213
+ const tableIds = Object.values(tables).map(table => table._id);
166
214
  for (let [name, table] of Object.entries(tables)) {
167
215
  const schemaFields = Object.keys(table.schema);
168
216
  // make sure every table has a key
@@ -175,7 +223,7 @@ function finaliseExternalTables(tables, entities) {
175
223
  continue;
176
224
  }
177
225
  // make sure all previous props have been added back
178
- finalTables[name] = copyExistingPropsOver(name, table, entities);
226
+ finalTables[name] = copyExistingPropsOver(name, table, entities, tableIds);
179
227
  }
180
228
  // sort the tables by name
181
229
  finalTables = Object.entries(finalTables)
@@ -12,9 +12,49 @@ const { getUserRoleHierarchy, getRequiredResourceRole, BUILTIN_ROLE_IDS, } = req
12
12
  const { PermissionTypes, doesHaveBasePermission, } = require("@budibase/backend-core/permissions");
13
13
  const builderMiddleware = require("./builder");
14
14
  const { isWebhookEndpoint } = require("./utils");
15
+ const { buildCsrfMiddleware } = require("@budibase/backend-core/auth");
15
16
  function hasResource(ctx) {
16
17
  return ctx.resourceId != null;
17
18
  }
19
+ const csrf = buildCsrfMiddleware();
20
+ /**
21
+ * Apply authorization to the requested resource:
22
+ * - If this is a builder resource the user must be a builder.
23
+ * - Builders can access all resources.
24
+ * - Otherwise the user must have the required role.
25
+ */
26
+ const checkAuthorized = (ctx, resourceRoles, permType, permLevel) => __awaiter(void 0, void 0, void 0, function* () {
27
+ // check if this is a builder api and the user is not a builder
28
+ const isBuilder = ctx.user && ctx.user.builder && ctx.user.builder.global;
29
+ const isBuilderApi = permType === PermissionTypes.BUILDER;
30
+ if (isBuilderApi && !isBuilder) {
31
+ return ctx.throw(403, "Not Authorized");
32
+ }
33
+ // check for resource authorization
34
+ if (!isBuilder) {
35
+ yield checkAuthorizedResource(ctx, resourceRoles, permType, permLevel);
36
+ }
37
+ });
38
+ const checkAuthorizedResource = (ctx, resourceRoles, permType, permLevel) => __awaiter(void 0, void 0, void 0, function* () {
39
+ // get the user's roles
40
+ const roleId = ctx.roleId || BUILTIN_ROLE_IDS.PUBLIC;
41
+ const userRoles = yield getUserRoleHierarchy(ctx.appId, roleId, {
42
+ idOnly: false,
43
+ });
44
+ const permError = "User does not have permission";
45
+ // check if the user has the required role
46
+ if (resourceRoles.length > 0) {
47
+ // deny access if the user doesn't have the required resource role
48
+ const found = userRoles.find(role => resourceRoles.indexOf(role._id) !== -1);
49
+ if (!found) {
50
+ ctx.throw(403, permError);
51
+ }
52
+ // fallback to the base permissions when no resource roles are found
53
+ }
54
+ else if (!doesHaveBasePermission(permType, permLevel, userRoles)) {
55
+ ctx.throw(403, permError);
56
+ }
57
+ });
18
58
  module.exports =
19
59
  (permType, permLevel = null) => (ctx, next) => __awaiter(void 0, void 0, void 0, function* () {
20
60
  // webhooks don't need authentication, each webhook unique
@@ -28,37 +68,22 @@ module.exports =
28
68
  // check general builder stuff, this middleware is a good way
29
69
  // to find API endpoints which are builder focused
30
70
  yield builderMiddleware(ctx, permType);
31
- const isAuthed = ctx.isAuthenticated;
32
- // builders for now have permission to do anything
33
- let isBuilder = ctx.user && ctx.user.builder && ctx.user.builder.global;
34
- const isBuilderApi = permType === PermissionTypes.BUILDER;
35
- if (isBuilder) {
36
- return next();
37
- }
38
- else if (isBuilderApi && !isBuilder) {
39
- return ctx.throw(403, "Not Authorized");
71
+ // get the resource roles
72
+ let resourceRoles = [];
73
+ if (ctx.appId && hasResource(ctx)) {
74
+ resourceRoles = yield getRequiredResourceRole(ctx.appId, permLevel, ctx);
40
75
  }
41
- // need to check this first, in-case public access, don't check authed until last
42
- const roleId = ctx.roleId || BUILTIN_ROLE_IDS.PUBLIC;
43
- const hierarchy = yield getUserRoleHierarchy(ctx.appId, roleId, {
44
- idOnly: false,
45
- });
46
- const permError = "User does not have permission";
47
- let possibleRoleIds = [];
48
- if (hasResource(ctx)) {
49
- possibleRoleIds = yield getRequiredResourceRole(ctx.appId, permLevel, ctx);
50
- }
51
- // check if we found a role, if not fallback to base permissions
52
- if (possibleRoleIds.length > 0) {
53
- const found = hierarchy.find(role => possibleRoleIds.indexOf(role._id) !== -1);
54
- return found ? next() : ctx.throw(403, permError);
55
- }
56
- else if (!doesHaveBasePermission(permType, permLevel, hierarchy)) {
57
- ctx.throw(403, permError);
76
+ // if the resource is public, proceed
77
+ const isPublicResource = resourceRoles.includes(BUILTIN_ROLE_IDS.PUBLIC);
78
+ if (isPublicResource) {
79
+ return next();
58
80
  }
59
- // if they are not authed, then anything using the authorized middleware will fail
60
- if (!isAuthed) {
61
- ctx.throw(403, "Session not authenticated");
81
+ // check authenticated
82
+ if (!ctx.isAuthenticated) {
83
+ return ctx.throw(403, "Session not authenticated");
62
84
  }
63
- return next();
85
+ // check authorized
86
+ yield checkAuthorized(ctx, resourceRoles, permType, permLevel);
87
+ // csrf protection
88
+ return csrf(ctx, next);
64
89
  });