@budibase/server 1.0.49-alpha.0 → 1.0.49-alpha.1

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 (203) hide show
  1. package/builder/assets/{index.dd9e386f.css → index.ba78c49d.css} +1 -1
  2. package/builder/assets/{index.8184eed9.js → index.ed1cf508.js} +205 -204
  3. package/builder/assets/{vendor.3bd2f661.js → vendor.d526bfe8.js} +1 -1
  4. package/builder/index.html +3 -3
  5. package/coverage/clover.xml +844 -680
  6. package/coverage/coverage-final.json +12 -10
  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 +1 -1
  10. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/automation.js.html +1 -1
  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 +1 -1
  15. package/coverage/lcov-report/api/controllers/datasource.js.html +1 -1
  16. package/coverage/lcov-report/api/controllers/dev.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/index.html +1 -1
  18. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/layout.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  21. package/coverage/lcov-report/api/controllers/permission.js.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  23. package/coverage/lcov-report/api/controllers/query/index.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  25. package/coverage/lcov-report/api/controllers/role.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  27. package/coverage/lcov-report/api/controllers/row/external.js.html +1 -1
  28. package/coverage/lcov-report/api/controllers/row/index.html +40 -25
  29. package/coverage/lcov-report/api/controllers/row/index.js.html +1 -1
  30. package/coverage/lcov-report/api/controllers/row/internal.js.html +60 -123
  31. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +35 -11
  32. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +551 -0
  33. package/coverage/lcov-report/api/controllers/row/utils.js.html +1 -1
  34. package/coverage/lcov-report/api/controllers/screen.js.html +1 -1
  35. package/coverage/lcov-report/api/controllers/script.js.html +1 -1
  36. package/coverage/lcov-report/api/controllers/table/bulkFormula.js.html +629 -0
  37. package/coverage/lcov-report/api/controllers/table/external.js.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/index.html +45 -30
  39. package/coverage/lcov-report/api/controllers/table/index.js.html +11 -53
  40. package/coverage/lcov-report/api/controllers/table/internal.js.html +25 -7
  41. package/coverage/lcov-report/api/controllers/table/utils.js.html +136 -16
  42. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  43. package/coverage/lcov-report/api/controllers/user.js.html +1 -1
  44. package/coverage/lcov-report/api/controllers/view/exporters.js.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  46. package/coverage/lcov-report/api/controllers/view/index.js.html +1 -1
  47. package/coverage/lcov-report/api/controllers/view/utils.js.html +1 -1
  48. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +1 -1
  49. package/coverage/lcov-report/api/controllers/webhook.js.html +1 -1
  50. package/coverage/lcov-report/api/index.html +1 -1
  51. package/coverage/lcov-report/api/index.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.html +1 -1
  64. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  68. package/coverage/lcov-report/api/routes/permission.js.html +1 -1
  69. package/coverage/lcov-report/api/routes/query.js.html +1 -1
  70. package/coverage/lcov-report/api/routes/role.js.html +1 -1
  71. package/coverage/lcov-report/api/routes/routing.js.html +1 -1
  72. package/coverage/lcov-report/api/routes/row.js.html +1 -1
  73. package/coverage/lcov-report/api/routes/screen.js.html +1 -1
  74. package/coverage/lcov-report/api/routes/script.js.html +1 -1
  75. package/coverage/lcov-report/api/routes/static.js.html +1 -1
  76. package/coverage/lcov-report/api/routes/table.js.html +1 -1
  77. package/coverage/lcov-report/api/routes/templates.js.html +1 -1
  78. package/coverage/lcov-report/api/routes/user.js.html +1 -1
  79. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  80. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  81. package/coverage/lcov-report/automations/actions.js.html +1 -1
  82. package/coverage/lcov-report/automations/automationUtils.js.html +1 -1
  83. package/coverage/lcov-report/automations/bullboard.js.html +1 -1
  84. package/coverage/lcov-report/automations/index.html +1 -1
  85. package/coverage/lcov-report/automations/index.js.html +1 -1
  86. package/coverage/lcov-report/automations/steps/bash.js.html +1 -1
  87. package/coverage/lcov-report/automations/steps/createRow.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/delay.js.html +1 -1
  89. package/coverage/lcov-report/automations/steps/deleteRow.js.html +1 -1
  90. package/coverage/lcov-report/automations/steps/discord.js.html +1 -1
  91. package/coverage/lcov-report/automations/steps/executeQuery.js.html +1 -1
  92. package/coverage/lcov-report/automations/steps/executeScript.js.html +1 -1
  93. package/coverage/lcov-report/automations/steps/filter.js.html +1 -1
  94. package/coverage/lcov-report/automations/steps/index.html +1 -1
  95. package/coverage/lcov-report/automations/steps/integromat.js.html +1 -1
  96. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +1 -1
  97. package/coverage/lcov-report/automations/steps/queryRows.js.html +1 -1
  98. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +1 -1
  99. package/coverage/lcov-report/automations/steps/serverLog.js.html +1 -1
  100. package/coverage/lcov-report/automations/steps/slack.js.html +1 -1
  101. package/coverage/lcov-report/automations/steps/updateRow.js.html +1 -1
  102. package/coverage/lcov-report/automations/steps/utils.js.html +1 -1
  103. package/coverage/lcov-report/automations/steps/zapier.js.html +1 -1
  104. package/coverage/lcov-report/automations/triggerInfo/app.js.html +1 -1
  105. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +1 -1
  106. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  107. package/coverage/lcov-report/automations/triggerInfo/index.js.html +1 -1
  108. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +1 -1
  110. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +1 -1
  111. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +1 -1
  112. package/coverage/lcov-report/automations/triggers.js.html +1 -1
  113. package/coverage/lcov-report/automations/utils.js.html +1 -1
  114. package/coverage/lcov-report/constants/index.html +5 -5
  115. package/coverage/lcov-report/constants/index.js.html +19 -4
  116. package/coverage/lcov-report/constants/layouts.js.html +1 -1
  117. package/coverage/lcov-report/constants/screens.js.html +1 -1
  118. package/coverage/lcov-report/db/client.js.html +1 -1
  119. package/coverage/lcov-report/db/inMemoryView.js.html +1 -1
  120. package/coverage/lcov-report/db/index.html +1 -1
  121. package/coverage/lcov-report/db/index.js.html +1 -1
  122. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +1 -1
  123. package/coverage/lcov-report/db/linkedRows/index.html +10 -10
  124. package/coverage/lcov-report/db/linkedRows/index.js.html +10 -13
  125. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +1 -1
  126. package/coverage/lcov-report/db/newid.js.html +1 -1
  127. package/coverage/lcov-report/db/utils.js.html +5 -5
  128. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  129. package/coverage/lcov-report/events/index.html +1 -1
  130. package/coverage/lcov-report/events/index.js.html +1 -1
  131. package/coverage/lcov-report/events/utils.js.html +1 -1
  132. package/coverage/lcov-report/index.html +44 -44
  133. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  134. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  135. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  136. package/coverage/lcov-report/middleware/authorized.js.html +1 -1
  137. package/coverage/lcov-report/middleware/builder.js.html +1 -1
  138. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  139. package/coverage/lcov-report/middleware/index.html +1 -1
  140. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  141. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  142. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  143. package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
  144. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  145. package/coverage/lcov-report/migrations/usageQuotas/index.html +1 -1
  146. package/coverage/lcov-report/migrations/usageQuotas/index.js.html +1 -1
  147. package/coverage/lcov-report/migrations/usageQuotas/syncApps.js.html +1 -1
  148. package/coverage/lcov-report/migrations/usageQuotas/syncRows.js.html +1 -1
  149. package/coverage/lcov-report/threads/automation.js.html +1 -1
  150. package/coverage/lcov-report/threads/index.html +1 -1
  151. package/coverage/lcov-report/threads/index.js.html +1 -1
  152. package/coverage/lcov-report/threads/query.js.html +1 -1
  153. package/coverage/lcov-report/threads/utils.js.html +1 -1
  154. package/coverage/lcov-report/utilities/bcrypt.js.html +1 -1
  155. package/coverage/lcov-report/utilities/budibaseDir.js.html +1 -1
  156. package/coverage/lcov-report/utilities/centralPath.js.html +1 -1
  157. package/coverage/lcov-report/utilities/csvParser.js.html +1 -1
  158. package/coverage/lcov-report/utilities/global.js.html +1 -1
  159. package/coverage/lcov-report/utilities/index.html +1 -1
  160. package/coverage/lcov-report/utilities/index.js.html +1 -1
  161. package/coverage/lcov-report/utilities/queue/inMemoryQueue.js.html +1 -1
  162. package/coverage/lcov-report/utilities/queue/index.html +1 -1
  163. package/coverage/lcov-report/utilities/routing/index.html +1 -1
  164. package/coverage/lcov-report/utilities/routing/index.js.html +1 -1
  165. package/coverage/lcov-report/utilities/rowProcessor/index.html +27 -27
  166. package/coverage/lcov-report/utilities/rowProcessor/index.js.html +65 -29
  167. package/coverage/lcov-report/utilities/rowProcessor/utils.js.html +73 -25
  168. package/coverage/lcov-report/utilities/scriptRunner.js.html +1 -1
  169. package/coverage/lcov-report/utilities/security.js.html +1 -1
  170. package/coverage/lcov-report/utilities/statusCodes.js.html +1 -1
  171. package/coverage/lcov-report/utilities/usageQuota/index.html +1 -1
  172. package/coverage/lcov-report/utilities/usageQuota/index.js.html +1 -1
  173. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +1 -1
  174. package/coverage/lcov-report/utilities/usageQuota/usageQuoteReset.js.html +1 -1
  175. package/coverage/lcov-report/utilities/users.js.html +1 -1
  176. package/coverage/lcov-report/utilities/workerRequests.js.html +1 -1
  177. package/coverage/lcov.info +1509 -1173
  178. package/dist/api/controllers/row/internal.js +30 -53
  179. package/dist/api/controllers/row/internalSearch.js +12 -4
  180. package/dist/api/controllers/row/staticFormula.js +147 -0
  181. package/dist/api/controllers/table/bulkFormula.js +160 -0
  182. package/dist/api/controllers/table/index.js +3 -6
  183. package/dist/api/controllers/table/internal.js +7 -0
  184. package/dist/api/controllers/table/utils.js +32 -10
  185. package/dist/constants/index.js +4 -0
  186. package/dist/db/linkedRows/index.js +4 -5
  187. package/dist/package.json +4 -4
  188. package/dist/tsconfig.tsbuildinfo +1 -1
  189. package/dist/utilities/rowProcessor/index.js +18 -11
  190. package/dist/utilities/rowProcessor/utils.js +14 -4
  191. package/package.json +5 -5
  192. package/src/api/controllers/row/internal.js +29 -50
  193. package/src/api/controllers/row/internalSearch.js +12 -4
  194. package/src/api/controllers/row/staticFormula.js +157 -0
  195. package/src/api/controllers/table/bulkFormula.js +183 -0
  196. package/src/api/controllers/table/index.js +3 -17
  197. package/src/api/controllers/table/internal.js +7 -1
  198. package/src/api/controllers/table/utils.js +48 -8
  199. package/src/constants/index.js +5 -0
  200. package/src/db/linkedRows/index.js +4 -5
  201. package/src/definitions/common.ts +3 -0
  202. package/src/utilities/rowProcessor/index.js +24 -12
  203. package/src/utilities/rowProcessor/utils.js +23 -7
@@ -21,11 +21,10 @@ var __rest = (this && this.__rest) || function (s, e) {
21
21
  };
22
22
  const CouchDB = require("../../../db");
23
23
  const linkRows = require("../../../db/linkedRows");
24
- const { getRowParams, generateRowID, DocumentTypes, InternalTables, } = require("../../../db/utils");
24
+ const { generateRowID, getRowParams, DocumentTypes, InternalTables, } = require("../../../db/utils");
25
25
  const userController = require("../user");
26
- const { inputProcessing, outputProcessing, processAutoColumn, cleanupAttachments, } = require("../../../utilities/rowProcessor");
26
+ const { inputProcessing, outputProcessing, cleanupAttachments, } = require("../../../utilities/rowProcessor");
27
27
  const { FieldTypes } = require("../../../constants");
28
- const { isEqual } = require("lodash");
29
28
  const { validate, findRow } = require("./utils");
30
29
  const { fullSearch, paginatedSearch } = require("./internalSearch");
31
30
  const { getGlobalUsersFromMetadata } = require("../../../utilities/global");
@@ -33,59 +32,12 @@ const inMemoryViews = require("../../../db/inMemoryView");
33
32
  const env = require("../../../environment");
34
33
  const { migrateToInMemoryView, migrateToDesignView, getFromDesignDoc, getFromMemoryDoc, } = require("../view/utils");
35
34
  const { cloneDeep } = require("lodash/fp");
35
+ const { finaliseRow, updateRelatedFormula } = require("./staticFormula");
36
36
  const CALCULATION_TYPES = {
37
37
  SUM: "sum",
38
38
  COUNT: "count",
39
39
  STATS: "stats",
40
40
  };
41
- function storeResponse(ctx, db, row, oldTable, table) {
42
- return __awaiter(this, void 0, void 0, function* () {
43
- row.type = "row";
44
- // don't worry about rev, tables handle rev/lastID updates
45
- // if another row has been written since processing this will
46
- // handle the auto ID clash
47
- if (!isEqual(oldTable, table)) {
48
- try {
49
- yield db.put(table);
50
- }
51
- catch (err) {
52
- if (err.status === 409) {
53
- const updatedTable = yield db.get(table._id);
54
- let response = processAutoColumn(null, updatedTable, row, {
55
- reprocessing: true,
56
- });
57
- yield db.put(response.table);
58
- row = response.row;
59
- }
60
- else {
61
- throw err;
62
- }
63
- }
64
- }
65
- const response = yield db.put(row);
66
- row._rev = response.rev;
67
- // process the row before return, to include relationships
68
- row = yield outputProcessing(ctx, table, row, { squash: false });
69
- return { row, table };
70
- });
71
- }
72
- // doesn't do the outputProcessing
73
- function getRawTableData(ctx, db, tableId) {
74
- return __awaiter(this, void 0, void 0, function* () {
75
- let rows;
76
- if (tableId === InternalTables.USER_METADATA) {
77
- yield userController.fetchMetadata(ctx);
78
- rows = ctx.body;
79
- }
80
- else {
81
- const response = yield db.allDocs(getRowParams(tableId, null, {
82
- include_docs: true,
83
- }));
84
- rows = response.rows.map(row => row.doc);
85
- }
86
- return rows;
87
- });
88
- }
89
41
  function getView(db, viewName) {
90
42
  return __awaiter(this, void 0, void 0, function* () {
91
43
  let mainGetter = env.SELF_HOSTED ? getFromDesignDoc : getFromMemoryDoc;
@@ -114,6 +66,22 @@ function getView(db, viewName) {
114
66
  return viewInfo;
115
67
  });
116
68
  }
69
+ function getRawTableData(ctx, db, tableId) {
70
+ return __awaiter(this, void 0, void 0, function* () {
71
+ let rows;
72
+ if (tableId === InternalTables.USER_METADATA) {
73
+ yield userController.fetchMetadata(ctx);
74
+ rows = ctx.body;
75
+ }
76
+ else {
77
+ const response = yield db.allDocs(getRowParams(tableId, null, {
78
+ include_docs: true,
79
+ }));
80
+ rows = response.rows.map(row => row.doc);
81
+ }
82
+ return rows;
83
+ });
84
+ }
117
85
  exports.patch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
118
86
  const appId = ctx.appId;
119
87
  const db = new CouchDB(appId);
@@ -169,7 +137,10 @@ exports.patch = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
169
137
  yield userController.updateMetadata(ctx);
170
138
  return { row: ctx.body, table };
171
139
  }
172
- return storeResponse(ctx, db, row, dbTable, table);
140
+ return finaliseRow(ctx.appId, table, row, {
141
+ oldTable: dbTable,
142
+ updateFormula: true,
143
+ });
173
144
  });
174
145
  exports.save = function (ctx) {
175
146
  return __awaiter(this, void 0, void 0, function* () {
@@ -198,7 +169,10 @@ exports.save = function (ctx) {
198
169
  tableId: row.tableId,
199
170
  table,
200
171
  });
201
- return storeResponse(ctx, db, row, dbTable, table);
172
+ return finaliseRow(ctx.appId, table, row, {
173
+ oldTable: dbTable,
174
+ updateFormula: true,
175
+ });
202
176
  });
203
177
  };
204
178
  exports.fetchView = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
@@ -290,6 +264,8 @@ exports.destroy = function (ctx) {
290
264
  });
291
265
  // remove any attachments that were on the row from object storage
292
266
  yield cleanupAttachments(appId, table, { row });
267
+ // remove any static formula
268
+ yield updateRelatedFormula(appId, table, row);
293
269
  let response;
294
270
  if (ctx.params.tableId === InternalTables.USER_METADATA) {
295
271
  ctx.params = {
@@ -333,6 +309,7 @@ exports.bulkDestroy = (ctx) => __awaiter(void 0, void 0, void 0, function* () {
333
309
  }
334
310
  // remove any attachments that were on the rows from object storage
335
311
  yield cleanupAttachments(appId, table, { rows });
312
+ yield updateRelatedFormula(appId, table, rows);
336
313
  yield Promise.all(updates);
337
314
  return { response: { ok: true }, rows };
338
315
  });
@@ -34,19 +34,27 @@ class QueryBuilder {
34
34
  return this;
35
35
  }
36
36
  setLimit(limit) {
37
- this.limit = limit;
37
+ if (limit != null) {
38
+ this.limit = limit;
39
+ }
38
40
  return this;
39
41
  }
40
42
  setSort(sort) {
41
- this.sort = sort;
43
+ if (sort != null) {
44
+ this.sort = sort;
45
+ }
42
46
  return this;
43
47
  }
44
48
  setSortOrder(sortOrder) {
45
- this.sortOrder = sortOrder;
49
+ if (sortOrder != null) {
50
+ this.sortOrder = sortOrder;
51
+ }
46
52
  return this;
47
53
  }
48
54
  setSortType(sortType) {
49
- this.sortType = sortType;
55
+ if (sortType != null) {
56
+ this.sortType = sortType;
57
+ }
50
58
  return this;
51
59
  }
52
60
  setBookmark(bookmark) {
@@ -0,0 +1,147 @@
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
+ const CouchDB = require("../../../db");
12
+ const { getRowParams } = require("../../../db/utils");
13
+ const { outputProcessing, processAutoColumn, processFormulas, } = require("../../../utilities/rowProcessor");
14
+ const { FieldTypes, FormulaTypes } = require("../../../constants");
15
+ const { isEqual } = require("lodash");
16
+ const { cloneDeep } = require("lodash/fp");
17
+ /**
18
+ * This function runs through a list of enriched rows, looks at the rows which
19
+ * are related and then checks if they need the state of their formulas
20
+ * updated.
21
+ * NOTE: this will only for affect static formulas.
22
+ */
23
+ exports.updateRelatedFormula = (appId, table, enrichedRows) => __awaiter(void 0, void 0, void 0, function* () {
24
+ const db = new CouchDB(appId);
25
+ // no formula to update, we're done
26
+ if (!table.relatedFormula) {
27
+ return;
28
+ }
29
+ let promises = [];
30
+ for (let enrichedRow of Array.isArray(enrichedRows)
31
+ ? enrichedRows
32
+ : [enrichedRows]) {
33
+ // the related rows by tableId
34
+ let relatedRows = {};
35
+ for (let [key, field] of Object.entries(enrichedRow)) {
36
+ const columnDefinition = table.schema[key];
37
+ if (columnDefinition && columnDefinition.type === FieldTypes.LINK) {
38
+ const relatedTableId = columnDefinition.tableId;
39
+ if (!relatedRows[relatedTableId]) {
40
+ relatedRows[relatedTableId] = [];
41
+ }
42
+ relatedRows[relatedTableId] = relatedRows[relatedTableId].concat(field);
43
+ }
44
+ }
45
+ for (let tableId of table.relatedFormula) {
46
+ let relatedTable;
47
+ try {
48
+ // no rows to update, skip
49
+ if (!relatedRows[tableId] || relatedRows[tableId].length === 0) {
50
+ continue;
51
+ }
52
+ relatedTable = yield db.get(tableId);
53
+ }
54
+ catch (err) {
55
+ // no error scenario, table doesn't seem to exist anymore, ignore
56
+ }
57
+ for (let column of Object.values(relatedTable.schema)) {
58
+ // needs updated in related rows
59
+ if (column.type === FieldTypes.FORMULA &&
60
+ column.formulaType === FormulaTypes.STATIC) {
61
+ // re-enrich rows for all the related, don't update the related formula for them
62
+ promises = promises.concat(relatedRows[tableId].map(related => exports.finaliseRow(appId, relatedTable, related, {
63
+ updateFormula: false,
64
+ })));
65
+ }
66
+ }
67
+ }
68
+ }
69
+ yield Promise.all(promises);
70
+ });
71
+ exports.updateAllFormulasInTable = (appId, table) => __awaiter(void 0, void 0, void 0, function* () {
72
+ const db = new CouchDB(appId);
73
+ // start by getting the raw rows (which will be written back to DB after update)
74
+ let rows = (yield db.allDocs(getRowParams(table._id, null, {
75
+ include_docs: true,
76
+ }))).rows.map(row => row.doc);
77
+ // now enrich the rows, note the clone so that we have the base state of the
78
+ // rows so that we don't write any of the enriched information back
79
+ let enrichedRows = yield outputProcessing({ appId }, table, cloneDeep(rows), {
80
+ squash: false,
81
+ });
82
+ const updatedRows = [];
83
+ for (let row of rows) {
84
+ // find the enriched row, if found process the formulas
85
+ const enrichedRow = enrichedRows.find(enriched => enriched._id === row._id);
86
+ if (enrichedRow) {
87
+ const processed = processFormulas(table, cloneDeep(row), {
88
+ dynamic: false,
89
+ contextRows: enrichedRow,
90
+ });
91
+ // values have changed, need to add to bulk docs to update
92
+ if (!isEqual(processed, row)) {
93
+ updatedRows.push(processed);
94
+ }
95
+ }
96
+ }
97
+ yield db.bulkDocs(updatedRows);
98
+ });
99
+ /**
100
+ * This function runs at the end of the save/patch functions of the row controller, all this
101
+ * really does is enrich the row, handle any static formula processing, then return the enriched
102
+ * row. The reason we need to return the enriched row is that the automation row created trigger
103
+ * expects the row to be totally enriched/contain all relationships.
104
+ */
105
+ exports.finaliseRow = (appId, table, row, { oldTable, updateFormula } = { updateFormula: true }) => __awaiter(void 0, void 0, void 0, function* () {
106
+ const db = new CouchDB(appId);
107
+ row.type = "row";
108
+ // process the row before return, to include relationships
109
+ let enrichedRow = yield outputProcessing({ appId }, table, cloneDeep(row), {
110
+ squash: false,
111
+ });
112
+ // use enriched row to generate formulas for saving, specifically only use as context
113
+ row = processFormulas(table, row, {
114
+ dynamic: false,
115
+ contextRows: enrichedRow,
116
+ });
117
+ // don't worry about rev, tables handle rev/lastID updates
118
+ // if another row has been written since processing this will
119
+ // handle the auto ID clash
120
+ if (oldTable && !isEqual(oldTable, table)) {
121
+ try {
122
+ yield db.put(table);
123
+ }
124
+ catch (err) {
125
+ if (err.status === 409) {
126
+ const updatedTable = yield db.get(table._id);
127
+ let response = processAutoColumn(null, updatedTable, row, {
128
+ reprocessing: true,
129
+ });
130
+ yield db.put(response.table);
131
+ row = response.row;
132
+ }
133
+ else {
134
+ throw err;
135
+ }
136
+ }
137
+ }
138
+ const response = yield db.put(row);
139
+ // for response, calculate the formulas for the enriched row
140
+ enrichedRow._rev = response.rev;
141
+ enrichedRow = yield processFormulas(table, enrichedRow, { dynamic: false });
142
+ // this updates the related formulas in other rows based on the relations to this row
143
+ if (updateFormula) {
144
+ yield exports.updateRelatedFormula(appId, table, enrichedRow);
145
+ }
146
+ return { row: enrichedRow, table };
147
+ });
@@ -0,0 +1,160 @@
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
+ const CouchDB = require("../../../db");
12
+ const { FieldTypes, FormulaTypes } = require("../../../constants");
13
+ const { getAllInternalTables, clearColumns } = require("./utils");
14
+ const { doesContainStrings } = require("@budibase/string-templates");
15
+ const { cloneDeep } = require("lodash/fp");
16
+ const { isEqual, uniq } = require("lodash");
17
+ const { updateAllFormulasInTable } = require("../row/staticFormula");
18
+ function isStaticFormula(column) {
19
+ return (column.type === FieldTypes.FORMULA &&
20
+ column.formulaType === FormulaTypes.STATIC);
21
+ }
22
+ /**
23
+ * This retrieves the formula columns from a table schema that use a specified column name
24
+ * in the formula.
25
+ */
26
+ function getFormulaThatUseColumn(table, columnNames) {
27
+ let formula = [];
28
+ columnNames = Array.isArray(columnNames) ? columnNames : [columnNames];
29
+ for (let column of Object.values(table.schema)) {
30
+ // not a static formula, or doesn't contain a relationship
31
+ if (!isStaticFormula(column)) {
32
+ continue;
33
+ }
34
+ if (!doesContainStrings(column.formula, columnNames)) {
35
+ continue;
36
+ }
37
+ formula.push(column.name);
38
+ }
39
+ return formula;
40
+ }
41
+ /**
42
+ * This functions checks for when a related table, column or related column is deleted, if any
43
+ * tables need to have the formula column removed.
44
+ */
45
+ function checkIfFormulaNeedsCleared(appId, table, { oldTable, deletion }) {
46
+ return __awaiter(this, void 0, void 0, function* () {
47
+ const db = new CouchDB(appId);
48
+ // start by retrieving all tables, remove the current table from the list
49
+ const tables = (yield getAllInternalTables(appId)).filter(tbl => tbl._id !== table._id);
50
+ const schemaToUse = oldTable ? oldTable.schema : table.schema;
51
+ let removedColumns = Object.values(schemaToUse).filter(column => deletion || !table.schema[column.name]);
52
+ // remove any formula columns that used related columns
53
+ for (let removed of removedColumns) {
54
+ let tableToUse = table;
55
+ // if relationship, get the related table
56
+ if (removed.type === FieldTypes.LINK) {
57
+ tableToUse = tables.find(table => table._id === removed.tableId);
58
+ }
59
+ const columnsToDelete = getFormulaThatUseColumn(tableToUse, removed.name);
60
+ if (columnsToDelete.length > 0) {
61
+ yield clearColumns(db, table, columnsToDelete);
62
+ }
63
+ // need a special case, where a column has been removed from this table, but was used
64
+ // in a different, related tables formula
65
+ if (!table.relatedFormula) {
66
+ continue;
67
+ }
68
+ for (let relatedTableId of table.relatedFormula) {
69
+ const relatedColumns = Object.values(table.schema).filter(column => column.tableId === relatedTableId);
70
+ const relatedTable = tables.find(table => table._id === relatedTableId);
71
+ // look to see if the column was used in a relationship formula,
72
+ // relationships won't be used for this
73
+ if (relatedTable && relatedColumns && removed.type !== FieldTypes.LINK) {
74
+ let relatedFormulaToRemove = [];
75
+ for (let column of relatedColumns) {
76
+ relatedFormulaToRemove = relatedFormulaToRemove.concat(getFormulaThatUseColumn(relatedTable, [
77
+ column.fieldName,
78
+ removed.name,
79
+ ]));
80
+ }
81
+ if (relatedFormulaToRemove.length > 0) {
82
+ yield clearColumns(db, relatedTable, uniq(relatedFormulaToRemove));
83
+ }
84
+ }
85
+ }
86
+ }
87
+ });
88
+ }
89
+ /**
90
+ * This function adds a note to related tables that they are
91
+ * used in a static formula - so that the link controller
92
+ * can manage hydrating related rows formula fields. This is
93
+ * specifically only for static formula.
94
+ */
95
+ function updateRelatedFormulaLinksOnTables(appId, table, { deletion } = { deletion: false }) {
96
+ return __awaiter(this, void 0, void 0, function* () {
97
+ const db = new CouchDB(appId);
98
+ // start by retrieving all tables, remove the current table from the list
99
+ const tables = (yield getAllInternalTables(appId)).filter(tbl => tbl._id !== table._id);
100
+ // clone the tables, so we can compare at end
101
+ const initialTables = cloneDeep(tables);
102
+ // first find the related column names
103
+ const relatedColumns = Object.values(table.schema).filter(col => col.type === FieldTypes.LINK);
104
+ // we start by removing the formula field from all tables
105
+ for (let otherTable of tables) {
106
+ if (!otherTable.relatedFormula) {
107
+ continue;
108
+ }
109
+ const index = otherTable.relatedFormula.indexOf(table._id);
110
+ if (index !== -1) {
111
+ otherTable.relatedFormula.splice(index, 1);
112
+ }
113
+ }
114
+ // if deleting, just remove the table IDs, don't try add
115
+ if (!deletion) {
116
+ for (let relatedCol of relatedColumns) {
117
+ let columns = getFormulaThatUseColumn(table, relatedCol.name);
118
+ if (!columns || columns.length === 0) {
119
+ continue;
120
+ }
121
+ const relatedTable = tables.find(related => related._id === relatedCol.tableId);
122
+ // check if the table is already in the list of related formula, if it isn't, then add it
123
+ if (relatedTable &&
124
+ (!relatedTable.relatedFormula ||
125
+ !relatedTable.relatedFormula.includes(table._id))) {
126
+ relatedTable.relatedFormula = relatedTable.relatedFormula
127
+ ? [...relatedTable.relatedFormula, table._id]
128
+ : [table._id];
129
+ }
130
+ }
131
+ }
132
+ // now we just need to compare all the tables and see if any need saved
133
+ for (let initial of initialTables) {
134
+ const found = tables.find(tbl => initial._id === tbl._id);
135
+ if (found && !isEqual(initial, found)) {
136
+ yield db.put(found);
137
+ }
138
+ }
139
+ });
140
+ }
141
+ function checkIfFormulaUpdated(appId, table, { oldTable }) {
142
+ return __awaiter(this, void 0, void 0, function* () {
143
+ // look to see if any formula values have changed
144
+ const shouldUpdate = Object.values(table.schema).find(column => isStaticFormula(column) &&
145
+ (!oldTable ||
146
+ !oldTable.schema[column.name] ||
147
+ !isEqual(oldTable.schema[column.name], column)));
148
+ // if a static formula column has updated, then need to run the update
149
+ if (shouldUpdate != null) {
150
+ yield updateAllFormulasInTable(appId, table);
151
+ }
152
+ });
153
+ }
154
+ exports.runStaticFormulaChecks = (appId, table, { oldTable, deletion }) => __awaiter(void 0, void 0, void 0, function* () {
155
+ yield updateRelatedFormulaLinksOnTables(appId, table, { deletion });
156
+ yield checkIfFormulaNeedsCleared(appId, table, { oldTable, deletion });
157
+ if (!deletion) {
158
+ yield checkIfFormulaUpdated(appId, table, { oldTable });
159
+ }
160
+ });
@@ -13,8 +13,8 @@ const internal = require("./internal");
13
13
  const external = require("./external");
14
14
  const csvParser = require("../../../utilities/csvParser");
15
15
  const { isExternalTable, isSQL } = require("../../../integrations/utils");
16
- const { getTableParams, getDatasourceParams, BudibaseInternalDB, } = require("../../../db/utils");
17
- const { getTable } = require("./utils");
16
+ const { getDatasourceParams } = require("../../../db/utils");
17
+ const { getTable, getAllInternalTables } = require("./utils");
18
18
  function pickApi({ tableId, table }) {
19
19
  if (table && !tableId) {
20
20
  tableId = table._id;
@@ -31,10 +31,7 @@ function pickApi({ tableId, table }) {
31
31
  exports.fetch = function (ctx) {
32
32
  return __awaiter(this, void 0, void 0, function* () {
33
33
  const db = new CouchDB(ctx.appId);
34
- const internalTables = yield db.allDocs(getTableParams(null, {
35
- include_docs: true,
36
- }));
37
- const internal = internalTables.rows.map(tableDoc => (Object.assign(Object.assign({}, tableDoc.doc), { type: "internal", sourceId: BudibaseInternalDB._id })));
34
+ const internal = yield getAllInternalTables(ctx.appId);
38
35
  const externalTables = yield db.allDocs(getDatasourceParams("plus", {
39
36
  include_docs: true,
40
37
  }));
@@ -25,6 +25,8 @@ const { getRowParams, generateTableID } = require("../../../db/utils");
25
25
  const { FieldTypes } = require("../../../constants");
26
26
  const { TableSaveFunctions, hasTypeChanged, getTable, handleDataImport, } = require("./utils");
27
27
  const usageQuota = require("../../../utilities/usageQuota");
28
+ const { cleanupAttachments } = require("../../../utilities/rowProcessor");
29
+ const { runStaticFormulaChecks } = require("./bulkFormula");
28
30
  exports.save = function (ctx) {
29
31
  return __awaiter(this, void 0, void 0, function* () {
30
32
  const appId = ctx.appId;
@@ -106,6 +108,8 @@ exports.save = function (ctx) {
106
108
  const result = yield db.put(tableToSave);
107
109
  tableToSave._rev = result.rev;
108
110
  tableToSave = yield tableSaveFunctions.after(tableToSave);
111
+ // has to run after, make sure it has _id
112
+ yield runStaticFormulaChecks(appId, tableToSave, { oldTable });
109
113
  return tableToSave;
110
114
  });
111
115
  };
@@ -134,6 +138,9 @@ exports.destroy = function (ctx) {
134
138
  if (existingIndex) {
135
139
  yield db.deleteIndex(existingIndex);
136
140
  }
141
+ // has to run after, make sure it has _id
142
+ yield runStaticFormulaChecks(appId, tableToDelete, { deletion: true });
143
+ yield cleanupAttachments(appId, tableToDelete, { rows });
137
144
  return tableToDelete;
138
145
  });
139
146
  };
@@ -10,16 +10,27 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
10
10
  };
11
11
  const CouchDB = require("../../../db");
12
12
  const csvParser = require("../../../utilities/csvParser");
13
- const { getRowParams, generateRowID, InternalTables, } = require("../../../db/utils");
14
- const { isEqual } = require("lodash/fp");
13
+ const { getRowParams, generateRowID, InternalTables, getTableParams, BudibaseInternalDB, } = require("../../../db/utils");
14
+ const { isEqual } = require("lodash");
15
15
  const { AutoFieldSubTypes, FieldTypes } = require("../../../constants");
16
- const { inputProcessing } = require("../../../utilities/rowProcessor");
16
+ const { inputProcessing, cleanupAttachments, } = require("../../../utilities/rowProcessor");
17
17
  const { USERS_TABLE_SCHEMA, SwitchableTypes, CanSwitchTypes, } = require("../../../constants");
18
18
  const { isExternalTable, breakExternalTableId, isSQL, } = require("../../../integrations/utils");
19
19
  const { getViews, saveView } = require("../view/utils");
20
20
  const viewTemplate = require("../view/viewBuilder");
21
21
  const usageQuota = require("../../../utilities/usageQuota");
22
- exports.checkForColumnUpdates = (db, oldTable, updatedTable) => __awaiter(void 0, void 0, void 0, function* () {
22
+ const { cloneDeep } = require("lodash/fp");
23
+ exports.clearColumns = (appId, table, columnNames) => __awaiter(void 0, void 0, void 0, function* () {
24
+ const db = new CouchDB(appId);
25
+ const rows = yield db.allDocs(getRowParams(table._id, null, {
26
+ include_docs: true,
27
+ }));
28
+ return db.bulkDocs(rows.rows.map(({ doc }) => {
29
+ columnNames.forEach(colName => delete doc[colName]);
30
+ return doc;
31
+ }));
32
+ });
33
+ exports.checkForColumnUpdates = (appId, db, oldTable, updatedTable) => __awaiter(void 0, void 0, void 0, function* () {
23
34
  let updatedRows = [];
24
35
  const rename = updatedTable._rename;
25
36
  let deletedColumns = [];
@@ -32,16 +43,20 @@ exports.checkForColumnUpdates = (db, oldTable, updatedTable) => __awaiter(void 0
32
43
  const rows = yield db.allDocs(getRowParams(updatedTable._id, null, {
33
44
  include_docs: true,
34
45
  }));
35
- updatedRows = rows.rows.map(({ doc }) => {
46
+ const rawRows = rows.rows.map(({ doc }) => doc);
47
+ updatedRows = rawRows.map(row => {
48
+ row = cloneDeep(row);
36
49
  if (rename) {
37
- doc[rename.updated] = doc[rename.old];
38
- delete doc[rename.old];
50
+ row[rename.updated] = row[rename.old];
51
+ delete row[rename.old];
39
52
  }
40
53
  else if (deletedColumns.length !== 0) {
41
- deletedColumns.forEach(colName => delete doc[colName]);
54
+ deletedColumns.forEach(colName => delete row[colName]);
42
55
  }
43
- return doc;
56
+ return row;
44
57
  });
58
+ // cleanup any attachments from object storage for deleted attachment columns
59
+ yield cleanupAttachments(appId, updatedTable, { oldTable, rows: rawRows });
45
60
  // Update views
46
61
  yield exports.checkForViewUpdates(db, updatedTable, rename, deletedColumns);
47
62
  delete updatedTable._rename;
@@ -179,7 +194,7 @@ class TableSaveFunctions {
179
194
  // when confirmed valid
180
195
  mid(table) {
181
196
  return __awaiter(this, void 0, void 0, function* () {
182
- let response = yield exports.checkForColumnUpdates(this.db, this.oldTable, table);
197
+ let response = yield exports.checkForColumnUpdates(this.appId, this.db, this.oldTable, table);
183
198
  this.rows = this.rows.concat(response.rows);
184
199
  return table;
185
200
  });
@@ -196,6 +211,13 @@ class TableSaveFunctions {
196
211
  return this.rows;
197
212
  }
198
213
  }
214
+ exports.getAllInternalTables = (appId) => __awaiter(void 0, void 0, void 0, function* () {
215
+ const db = new CouchDB(appId);
216
+ const internalTables = yield db.allDocs(getTableParams(null, {
217
+ include_docs: true,
218
+ }));
219
+ return internalTables.rows.map(tableDoc => (Object.assign(Object.assign({}, tableDoc.doc), { type: "internal", sourceId: BudibaseInternalDB._id })));
220
+ });
199
221
  exports.getAllExternalTables = (appId, datasourceId) => __awaiter(void 0, void 0, void 0, function* () {
200
222
  const db = new CouchDB(appId);
201
223
  const datasource = yield db.get(datasourceId);
@@ -52,6 +52,10 @@ exports.RelationshipTypes = {
52
52
  MANY_TO_ONE: "many-to-one",
53
53
  MANY_TO_MANY: "many-to-many",
54
54
  };
55
+ exports.FormulaTypes = {
56
+ STATIC: "static",
57
+ DYNAMIC: "dynamic",
58
+ };
55
59
  exports.AuthTypes = {
56
60
  APP: "app",
57
61
  BUILDER: "builder",
@@ -61,7 +61,7 @@ function getLinksForRows(appId, rows) {
61
61
  .map(el => (Object.assign(Object.assign({}, el), { unique: el.id + el.thisId + el.fieldName }))), "unique");
62
62
  });
63
63
  }
64
- function getFullLinkedDocs(ctx, appId, links) {
64
+ function getFullLinkedDocs(appId, links) {
65
65
  return __awaiter(this, void 0, void 0, function* () {
66
66
  // create DBs
67
67
  const db = new CouchDB(appId);
@@ -132,13 +132,12 @@ exports.updateLinks = function (args) {
132
132
  /**
133
133
  * Given a table and a list of rows this will retrieve all of the attached docs and enrich them into the row.
134
134
  * This is required for formula fields, this may only be utilised internally (for now).
135
- * @param {object} ctx The request which is looking for rows.
135
+ * @param {string} appId The ID of the app which this request is in the context of.
136
136
  * @param {object} table The table from which the rows originated.
137
137
  * @param {array<object>} rows The rows which are to be enriched.
138
138
  * @return {Promise<*>} returns the rows with all of the enriched relationships on it.
139
139
  */
140
- exports.attachFullLinkedDocs = (ctx, table, rows) => __awaiter(void 0, void 0, void 0, function* () {
141
- const appId = ctx.appId;
140
+ exports.attachFullLinkedDocs = (appId, table, rows) => __awaiter(void 0, void 0, void 0, function* () {
142
141
  const linkedTableIds = getLinkedTableIDs(table);
143
142
  if (linkedTableIds.length === 0) {
144
143
  return rows;
@@ -150,7 +149,7 @@ exports.attachFullLinkedDocs = (ctx, table, rows) => __awaiter(void 0, void 0, v
150
149
  // clear any existing links that could be dupe'd
151
150
  rows = clearRelationshipFields(table, rows);
152
151
  // now get the docs and combine into the rows
153
- let linked = yield getFullLinkedDocs(ctx, appId, links);
152
+ let linked = yield getFullLinkedDocs(appId, links);
154
153
  const linkedTables = [];
155
154
  for (let row of rows) {
156
155
  for (let link of links.filter(link => link.thisId === row._id)) {