@budibase/server 1.0.113 → 1.0.116

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 (238) hide show
  1. package/builder/assets/{index.e3e5909b.js → index.28d8c64f.js} +4 -4
  2. package/builder/index.html +1 -1
  3. package/coverage/clover.xml +1212 -1269
  4. package/coverage/coverage-final.json +86 -87
  5. package/coverage/lcov-report/api/controllers/analytics.js.html +1 -1
  6. package/coverage/lcov-report/api/controllers/apikeys.js.html +1 -1
  7. package/coverage/lcov-report/api/controllers/application.js.html +126 -120
  8. package/coverage/lcov-report/api/controllers/auth.js.html +1 -1
  9. package/coverage/lcov-report/api/controllers/automation.js.html +21 -21
  10. package/coverage/lcov-report/api/controllers/backup.js.html +1 -1
  11. package/coverage/lcov-report/api/controllers/cloud.js.html +1 -1
  12. package/coverage/lcov-report/api/controllers/component.js.html +1 -1
  13. package/coverage/lcov-report/api/controllers/datasource.js.html +16 -16
  14. package/coverage/lcov-report/api/controllers/dev.js.html +23 -14
  15. package/coverage/lcov-report/api/controllers/index.html +17 -17
  16. package/coverage/lcov-report/api/controllers/integration.js.html +1 -1
  17. package/coverage/lcov-report/api/controllers/layout.js.html +6 -6
  18. package/coverage/lcov-report/api/controllers/metadata.js.html +1 -1
  19. package/coverage/lcov-report/api/controllers/migrations.js.html +1 -1
  20. package/coverage/lcov-report/api/controllers/permission.js.html +15 -15
  21. package/coverage/lcov-report/api/controllers/query/index.html +1 -1
  22. package/coverage/lcov-report/api/controllers/query/index.js.html +19 -19
  23. package/coverage/lcov-report/api/controllers/query/validation.js.html +1 -1
  24. package/coverage/lcov-report/api/controllers/role.js.html +9 -9
  25. package/coverage/lcov-report/api/controllers/routing.js.html +1 -1
  26. package/coverage/lcov-report/api/controllers/row/external.js.html +19 -19
  27. package/coverage/lcov-report/api/controllers/row/index.html +10 -10
  28. package/coverage/lcov-report/api/controllers/row/index.js.html +14 -14
  29. package/coverage/lcov-report/api/controllers/row/internal.js.html +37 -34
  30. package/coverage/lcov-report/api/controllers/row/internalSearch.js.html +9 -9
  31. package/coverage/lcov-report/api/controllers/row/staticFormula.js.html +10 -10
  32. package/coverage/lcov-report/api/controllers/row/utils.js.html +12 -12
  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/bulkFormula.js.html +9 -9
  36. package/coverage/lcov-report/api/controllers/table/external.js.html +12 -12
  37. package/coverage/lcov-report/api/controllers/table/index.html +1 -1
  38. package/coverage/lcov-report/api/controllers/table/index.js.html +14 -14
  39. package/coverage/lcov-report/api/controllers/table/internal.js.html +13 -13
  40. package/coverage/lcov-report/api/controllers/table/utils.js.html +30 -30
  41. package/coverage/lcov-report/api/controllers/templates.js.html +1 -1
  42. package/coverage/lcov-report/api/controllers/user.js.html +19 -19
  43. package/coverage/lcov-report/api/controllers/view/exporters.js.html +4 -4
  44. package/coverage/lcov-report/api/controllers/view/index.html +1 -1
  45. package/coverage/lcov-report/api/controllers/view/index.js.html +14 -14
  46. package/coverage/lcov-report/api/controllers/view/utils.js.html +12 -12
  47. package/coverage/lcov-report/api/controllers/view/viewBuilder.js.html +7 -7
  48. package/coverage/lcov-report/api/controllers/webhook.js.html +17 -17
  49. package/coverage/lcov-report/api/index.html +1 -1
  50. package/coverage/lcov-report/api/index.js.html +1 -1
  51. package/coverage/lcov-report/api/routes/analytics.js.html +1 -1
  52. package/coverage/lcov-report/api/routes/apikeys.js.html +1 -1
  53. package/coverage/lcov-report/api/routes/application.js.html +1 -1
  54. package/coverage/lcov-report/api/routes/auth.js.html +1 -1
  55. package/coverage/lcov-report/api/routes/automation.js.html +1 -1
  56. package/coverage/lcov-report/api/routes/backup.js.html +1 -1
  57. package/coverage/lcov-report/api/routes/cloud.js.html +1 -1
  58. package/coverage/lcov-report/api/routes/component.js.html +1 -1
  59. package/coverage/lcov-report/api/routes/datasource.js.html +1 -1
  60. package/coverage/lcov-report/api/routes/deploy.js.html +1 -1
  61. package/coverage/lcov-report/api/routes/dev.js.html +1 -1
  62. package/coverage/lcov-report/api/routes/index.html +1 -1
  63. package/coverage/lcov-report/api/routes/index.js.html +1 -1
  64. package/coverage/lcov-report/api/routes/integration.js.html +1 -1
  65. package/coverage/lcov-report/api/routes/layout.js.html +1 -1
  66. package/coverage/lcov-report/api/routes/metadata.js.html +1 -1
  67. package/coverage/lcov-report/api/routes/migrations.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/utils/index.html +1 -1
  80. package/coverage/lcov-report/api/routes/utils/validators.js.html +1 -1
  81. package/coverage/lcov-report/api/routes/view.js.html +1 -1
  82. package/coverage/lcov-report/api/routes/webhook.js.html +1 -1
  83. package/coverage/lcov-report/automations/actions.js.html +24 -24
  84. package/coverage/lcov-report/automations/automationUtils.js.html +5 -5
  85. package/coverage/lcov-report/automations/bullboard.js.html +16 -16
  86. package/coverage/lcov-report/automations/index.html +5 -5
  87. package/coverage/lcov-report/automations/index.js.html +1 -1
  88. package/coverage/lcov-report/automations/steps/bash.js.html +7 -7
  89. package/coverage/lcov-report/automations/steps/createRow.js.html +7 -7
  90. package/coverage/lcov-report/automations/steps/delay.js.html +4 -4
  91. package/coverage/lcov-report/automations/steps/deleteRow.js.html +7 -7
  92. package/coverage/lcov-report/automations/steps/discord.js.html +7 -7
  93. package/coverage/lcov-report/automations/steps/executeQuery.js.html +6 -6
  94. package/coverage/lcov-report/automations/steps/executeScript.js.html +6 -6
  95. package/coverage/lcov-report/automations/steps/filter.js.html +7 -7
  96. package/coverage/lcov-report/automations/steps/index.html +1 -1
  97. package/coverage/lcov-report/automations/steps/integromat.js.html +5 -5
  98. package/coverage/lcov-report/automations/steps/outgoingWebhook.js.html +8 -8
  99. package/coverage/lcov-report/automations/steps/queryRows.js.html +10 -10
  100. package/coverage/lcov-report/automations/steps/sendSmtpEmail.js.html +5 -5
  101. package/coverage/lcov-report/automations/steps/serverLog.js.html +3 -3
  102. package/coverage/lcov-report/automations/steps/slack.js.html +5 -5
  103. package/coverage/lcov-report/automations/steps/updateRow.js.html +6 -6
  104. package/coverage/lcov-report/automations/steps/utils.js.html +3 -3
  105. package/coverage/lcov-report/automations/steps/zapier.js.html +5 -5
  106. package/coverage/lcov-report/automations/triggerInfo/app.js.html +2 -2
  107. package/coverage/lcov-report/automations/triggerInfo/cron.js.html +2 -2
  108. package/coverage/lcov-report/automations/triggerInfo/index.html +1 -1
  109. package/coverage/lcov-report/automations/triggerInfo/index.js.html +8 -8
  110. package/coverage/lcov-report/automations/triggerInfo/rowDeleted.js.html +2 -2
  111. package/coverage/lcov-report/automations/triggerInfo/rowSaved.js.html +2 -2
  112. package/coverage/lcov-report/automations/triggerInfo/rowUpdated.js.html +2 -2
  113. package/coverage/lcov-report/automations/triggerInfo/webhook.js.html +2 -2
  114. package/coverage/lcov-report/automations/triggers.js.html +18 -18
  115. package/coverage/lcov-report/automations/utils.js.html +43 -40
  116. package/coverage/lcov-report/constants/index.html +1 -1
  117. package/coverage/lcov-report/constants/index.js.html +27 -27
  118. package/coverage/lcov-report/constants/layouts.js.html +5 -5
  119. package/coverage/lcov-report/constants/screens.js.html +4 -4
  120. package/coverage/lcov-report/db/inMemoryView.js.html +52 -49
  121. package/coverage/lcov-report/db/index.html +21 -36
  122. package/coverage/lcov-report/db/index.js.html +49 -10
  123. package/coverage/lcov-report/db/linkedRows/LinkController.js.html +7 -7
  124. package/coverage/lcov-report/db/linkedRows/index.html +1 -1
  125. package/coverage/lcov-report/db/linkedRows/index.js.html +18 -18
  126. package/coverage/lcov-report/db/linkedRows/linkUtils.js.html +13 -13
  127. package/coverage/lcov-report/db/newid.js.html +2 -2
  128. package/coverage/lcov-report/db/utils.js.html +13 -13
  129. package/coverage/lcov-report/events/AutomationEmitter.js.html +1 -1
  130. package/coverage/lcov-report/events/index.html +1 -1
  131. package/coverage/lcov-report/events/index.js.html +5 -5
  132. package/coverage/lcov-report/events/utils.js.html +3 -3
  133. package/coverage/lcov-report/index.html +52 -52
  134. package/coverage/lcov-report/integrations/tests/TestConfiguration.js.html +1 -1
  135. package/coverage/lcov-report/integrations/tests/index.html +1 -1
  136. package/coverage/lcov-report/middleware/appInfo.js.html +1 -1
  137. package/coverage/lcov-report/middleware/authorized.js.html +1 -1
  138. package/coverage/lcov-report/middleware/builder.js.html +15 -12
  139. package/coverage/lcov-report/middleware/currentapp.js.html +1 -1
  140. package/coverage/lcov-report/middleware/index.html +4 -4
  141. package/coverage/lcov-report/middleware/joi-validator.js.html +1 -1
  142. package/coverage/lcov-report/middleware/publicApi.js.html +1 -1
  143. package/coverage/lcov-report/middleware/resourceId.js.html +1 -1
  144. package/coverage/lcov-report/middleware/selfhost.js.html +1 -1
  145. package/coverage/lcov-report/middleware/usageQuota.js.html +1 -1
  146. package/coverage/lcov-report/middleware/utils.js.html +1 -1
  147. package/coverage/lcov-report/threads/automation.js.html +1 -1
  148. package/coverage/lcov-report/threads/index.html +34 -34
  149. package/coverage/lcov-report/threads/index.js.html +35 -17
  150. package/coverage/lcov-report/threads/query.js.html +20 -356
  151. package/coverage/lcov-report/threads/utils.js.html +19 -22
  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 +15 -15
  157. package/coverage/lcov-report/utilities/index.html +1 -1
  158. package/coverage/lcov-report/utilities/index.js.html +26 -26
  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 +18 -18
  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 +13 -13
  170. package/coverage/lcov-report/utilities/usageQuota/index.js.html +8 -8
  171. package/coverage/lcov-report/utilities/usageQuota/rows.js.html +38 -26
  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 +28 -28
  175. package/coverage/lcov.info +1570 -1660
  176. package/dist/api/controllers/application.js +4 -1
  177. package/dist/api/controllers/deploy/index.js +5 -1
  178. package/dist/api/controllers/dev.js +9 -5
  179. package/dist/api/controllers/row/internal.js +2 -1
  180. package/dist/api/controllers/webhook.js +2 -2
  181. package/dist/api/routes/tests/utilities/TestFunctions.js +1 -4
  182. package/dist/app.js +5 -2
  183. package/dist/automations/utils.js +6 -5
  184. package/dist/db/inMemoryView.js +33 -31
  185. package/dist/db/index.js +13 -2
  186. package/dist/integrations/couchdb.js +19 -25
  187. package/dist/integrations/queries/sql.js +108 -0
  188. package/dist/middleware/builder.js +10 -9
  189. package/dist/migrations/index.js +1 -5
  190. package/dist/package.json +5 -5
  191. package/dist/tests/utilities/TestConfiguration.js +66 -59
  192. package/dist/threads/index.js +6 -1
  193. package/dist/threads/query.js +4 -103
  194. package/dist/threads/utils.js +2 -3
  195. package/dist/tsconfig.tsbuildinfo +1 -1
  196. package/dist/utilities/fileSystem/index.js +27 -26
  197. package/dist/utilities/usageQuota/rows.js +16 -16
  198. package/package.json +6 -6
  199. package/scripts/exportAppTemplate.js +10 -8
  200. package/scripts/integrations/mysql/init.sql +1 -0
  201. package/src/api/controllers/application.js +3 -1
  202. package/src/api/controllers/deploy/index.js +4 -4
  203. package/src/api/controllers/dev.js +9 -6
  204. package/src/api/controllers/query/import/tests/index.spec.js +7 -11
  205. package/src/api/controllers/row/internal.js +2 -1
  206. package/src/api/controllers/webhook.js +2 -2
  207. package/src/api/routes/tests/row.spec.js +15 -11
  208. package/src/api/routes/tests/table.spec.js +3 -2
  209. package/src/api/routes/tests/utilities/TestFunctions.js +1 -5
  210. package/src/app.ts +2 -2
  211. package/src/automations/tests/deleteRow.spec.js +1 -1
  212. package/src/automations/utils.js +6 -5
  213. package/src/db/inMemoryView.js +37 -36
  214. package/src/db/index.js +15 -2
  215. package/src/db/tests/linkTests.spec.js +8 -7
  216. package/src/integrations/couchdb.ts +28 -24
  217. package/src/integrations/queries/sql.ts +125 -0
  218. package/src/integrations/tests/airtable.spec.js +3 -1
  219. package/src/integrations/tests/couchdb.spec.js +4 -1
  220. package/src/middleware/builder.js +10 -9
  221. package/src/middleware/tests/authorized.spec.js +0 -1
  222. package/src/middleware/tests/usageQuota.spec.js +6 -20
  223. package/src/migrations/functions/tests/appUrls.spec.js +8 -11
  224. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +13 -10
  225. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +25 -21
  226. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +26 -24
  227. package/src/migrations/index.ts +1 -2
  228. package/src/module.d.ts +8 -0
  229. package/src/tests/utilities/TestConfiguration.js +62 -55
  230. package/src/threads/index.js +7 -1
  231. package/src/threads/query.js +7 -119
  232. package/src/threads/utils.js +2 -3
  233. package/src/utilities/fileSystem/index.js +30 -30
  234. package/src/utilities/usageQuota/rows.js +22 -18
  235. package/coverage/lcov-report/db/client.js.html +0 -173
  236. package/dist/db/client.js +0 -25
  237. package/scripts/replicateApp.js +0 -50
  238. package/src/db/client.js +0 -31
@@ -12,7 +12,7 @@ const { budibaseTempDir } = require("../budibaseDir");
12
12
  const fs = require("fs");
13
13
  const { join } = require("path");
14
14
  const uuid = require("uuid/v4");
15
- const CouchDB = require("../../db");
15
+ const { doWithDB } = require("@budibase/backend-core/db");
16
16
  const { ObjectStoreBuckets } = require("../../constants");
17
17
  const { upload, retrieve, retrieveToTmp, streamUpload, deleteFolder, downloadTarball, } = require("./utilities");
18
18
  const { updateClientLibrary } = require("./clientLibrary");
@@ -135,32 +135,33 @@ exports.streamBackup = (appId) => __awaiter(void 0, void 0, void 0, function* ()
135
135
  * @return {*} either a readable stream or a string
136
136
  */
137
137
  exports.exportDB = (dbName, { stream, filter, exportName } = {}) => __awaiter(void 0, void 0, void 0, function* () {
138
- const instanceDb = new CouchDB(dbName);
139
- // Stream the dump if required
140
- if (stream) {
141
- const memStream = new MemoryStream();
142
- instanceDb.dump(memStream, { filter });
143
- return memStream;
144
- }
145
- // Write the dump to file if required
146
- if (exportName) {
147
- const path = join(budibaseTempDir(), exportName);
148
- const writeStream = fs.createWriteStream(path);
149
- yield instanceDb.dump(writeStream, { filter });
150
- // Upload the dump to the object store if self hosted
151
- if (env.SELF_HOSTED) {
152
- yield streamUpload(ObjectStoreBuckets.BACKUPS, join(dbName, exportName), fs.createReadStream(path));
138
+ return doWithDB(dbName, (db) => __awaiter(void 0, void 0, void 0, function* () {
139
+ // Stream the dump if required
140
+ if (stream) {
141
+ const memStream = new MemoryStream();
142
+ db.dump(memStream, { filter });
143
+ return memStream;
153
144
  }
154
- return fs.createReadStream(path);
155
- }
156
- // Stringify the dump in memory if required
157
- const memStream = new MemoryStream();
158
- let appString = "";
159
- memStream.on("data", chunk => {
160
- appString += chunk.toString();
161
- });
162
- yield instanceDb.dump(memStream, { filter });
163
- return appString;
145
+ // Write the dump to file if required
146
+ if (exportName) {
147
+ const path = join(budibaseTempDir(), exportName);
148
+ const writeStream = fs.createWriteStream(path);
149
+ yield db.dump(writeStream, { filter });
150
+ // Upload the dump to the object store if self hosted
151
+ if (env.SELF_HOSTED) {
152
+ yield streamUpload(ObjectStoreBuckets.BACKUPS, join(dbName, exportName), fs.createReadStream(path));
153
+ }
154
+ return fs.createReadStream(path);
155
+ }
156
+ // Stringify the dump in memory if required
157
+ const memStream = new MemoryStream();
158
+ let appString = "";
159
+ memStream.on("data", chunk => {
160
+ appString += chunk.toString();
161
+ });
162
+ yield db.dump(memStream, { filter });
163
+ return appString;
164
+ }));
164
165
  });
165
166
  /**
166
167
  * Writes the provided contents to a temporary file, which can be used briefly.
@@ -9,8 +9,7 @@ var __awaiter = (this && this.__awaiter) || function (thisArg, _arguments, P, ge
9
9
  });
10
10
  };
11
11
  const { getRowParams, USER_METDATA_PREFIX } = require("../../db/utils");
12
- const CouchDB = require("../../db");
13
- const { isDevAppID, getDevelopmentAppID } = require("@budibase/backend-core/db");
12
+ const { isDevAppID, getDevelopmentAppID, doWithDB, } = require("@budibase/backend-core/db");
14
13
  const ROW_EXCLUSIONS = [USER_METDATA_PREFIX];
15
14
  const getAppPairs = appIds => {
16
15
  // collect the app ids into dev / prod pairs
@@ -32,20 +31,21 @@ const getAppPairs = appIds => {
32
31
  };
33
32
  const getAppRows = (appId) => __awaiter(void 0, void 0, void 0, function* () {
34
33
  // need to specify the app ID, as this is used for different apps in one call
35
- const appDb = new CouchDB(appId);
36
- const response = yield appDb.allDocs(getRowParams(null, null, {
37
- include_docs: false,
38
- }));
39
- return response.rows
40
- .map(r => r.id)
41
- .filter(id => {
42
- for (let exclusion of ROW_EXCLUSIONS) {
43
- if (id.startsWith(exclusion)) {
44
- return false;
34
+ return doWithDB(appId, (db) => __awaiter(void 0, void 0, void 0, function* () {
35
+ const response = yield db.allDocs(getRowParams(null, null, {
36
+ include_docs: false,
37
+ }));
38
+ return response.rows
39
+ .map(r => r.id)
40
+ .filter(id => {
41
+ for (let exclusion of ROW_EXCLUSIONS) {
42
+ if (id.startsWith(exclusion)) {
43
+ return false;
44
+ }
45
45
  }
46
- }
47
- return true;
48
- });
46
+ return true;
47
+ });
48
+ }));
49
49
  });
50
50
  /**
51
51
  * Return a set of all rows in the given app ids.
@@ -62,7 +62,7 @@ exports.getUniqueRows = (appIds) => __awaiter(void 0, void 0, void 0, function*
62
62
  continue;
63
63
  }
64
64
  try {
65
- appRows.push(yield getAppRows(appId));
65
+ appRows = appRows.concat(yield getAppRows(appId));
66
66
  }
67
67
  catch (e) {
68
68
  console.error(e);
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/server",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.113",
4
+ "version": "1.0.116",
5
5
  "description": "Budibase Web Server",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -68,9 +68,9 @@
68
68
  "license": "GPL-3.0",
69
69
  "dependencies": {
70
70
  "@apidevtools/swagger-parser": "^10.0.3",
71
- "@budibase/backend-core": "^1.0.113",
72
- "@budibase/client": "^1.0.113",
73
- "@budibase/string-templates": "^1.0.113",
71
+ "@budibase/backend-core": "^1.0.116",
72
+ "@budibase/client": "^1.0.116",
73
+ "@budibase/string-templates": "^1.0.116",
74
74
  "@bull-board/api": "^3.7.0",
75
75
  "@bull-board/koa": "^3.7.0",
76
76
  "@elastic/elasticsearch": "7.10.0",
@@ -117,7 +117,7 @@
117
117
  "pg": "8.5.1",
118
118
  "pino-pretty": "4.0.0",
119
119
  "posthog-node": "^1.1.4",
120
- "pouchdb": "7.2.1",
120
+ "pouchdb": "7.3.0",
121
121
  "pouchdb-adapter-memory": "^7.2.1",
122
122
  "pouchdb-all-dbs": "1.0.2",
123
123
  "pouchdb-find": "^7.2.2",
@@ -174,5 +174,5 @@
174
174
  "optionalDependencies": {
175
175
  "oracledb": "^5.3.0"
176
176
  },
177
- "gitHead": "20852c30cca5054eb7ef8bacdb91343a56be0d30"
177
+ "gitHead": "70b8daeff623c77a66abb6ddcc994bb69edb062b"
178
178
  }
@@ -2,7 +2,8 @@
2
2
  const yargs = require("yargs")
3
3
  const fs = require("fs")
4
4
  const { join } = require("path")
5
- const CouchDB = require("../src/db")
5
+ require("../src/db").init()
6
+ const { doWithDB } = require("@budibase/backend-core/db")
6
7
  // load environment
7
8
  const env = require("../src/environment")
8
9
  const {
@@ -47,13 +48,14 @@ yargs
47
48
  const writeStream = fs.createWriteStream(join(exportPath, "dump.text"))
48
49
  // perform couch dump
49
50
 
50
- const instanceDb = new CouchDB(appId)
51
- await instanceDb.dump(writeStream, {
52
- filter: doc =>
53
- !(
54
- doc._id.includes(USER_METDATA_PREFIX) ||
55
- doc.includes(LINK_USER_METADATA_PREFIX)
56
- ),
51
+ await doWithDB(appId, async db => {
52
+ return db.dump(writeStream, {
53
+ filter: doc =>
54
+ !(
55
+ doc._id.includes(USER_METDATA_PREFIX) ||
56
+ doc.includes(LINK_USER_METADATA_PREFIX)
57
+ ),
58
+ })
57
59
  })
58
60
  console.log(`Template ${name} exported to ${exportPath}`)
59
61
  }
@@ -26,6 +26,7 @@ CREATE TABLE Products (
26
26
  updated time
27
27
  );
28
28
  INSERT INTO Persons (FirstName, LastName, Age, Address, City, CreatedAt) VALUES ('Mike', 'Hughes', 28.2, '123 Fake Street', 'Belfast', '2021-01-19 03:14:07');
29
+ INSERT INTO Persons (FirstName, LastName, Age, Address, City, CreatedAt) VALUES ('Dave', 'Johnson', 29, '124 Fake Street', 'Belfast', '2022-04-01 00:11:11');
29
30
  INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (1, 'assembling', '2020-01-01');
30
31
  INSERT INTO Tasks (PersonID, TaskName, CreatedAt) VALUES (2, 'processing', '2019-12-31');
31
32
  INSERT INTO Products (name, updated) VALUES ('Meat', '11:00:22'), ('Fruit', '10:00:00');
@@ -123,7 +123,7 @@ async function createInstance(template) {
123
123
  const tenantId = isMultiTenant() ? getTenantId() : null
124
124
  const baseAppId = generateAppID(tenantId)
125
125
  const appId = generateDevAppID(baseAppId)
126
- updateAppId(appId)
126
+ await updateAppId(appId)
127
127
 
128
128
  const db = getAppDB()
129
129
  await db.put({
@@ -404,6 +404,8 @@ exports.sync = async (ctx, next) => {
404
404
  })
405
405
  } catch (err) {
406
406
  error = err
407
+ } finally {
408
+ await replication.close()
407
409
  }
408
410
 
409
411
  // sync the users
@@ -94,18 +94,16 @@ async function initDeployedApp(prodAppId) {
94
94
  }
95
95
 
96
96
  async function deployApp(deployment) {
97
+ let replication
97
98
  try {
98
99
  const appId = getAppId()
99
100
  const devAppId = getDevelopmentAppID(appId)
100
101
  const productionAppId = getProdAppID(appId)
101
-
102
- const replication = new Replication({
102
+ replication = new Replication({
103
103
  source: devAppId,
104
104
  target: productionAppId,
105
105
  })
106
-
107
106
  console.log("Replication object created")
108
-
109
107
  await replication.replicate()
110
108
  console.log("replication complete.. replacing app meta doc")
111
109
  const db = getProdAppDB()
@@ -126,6 +124,8 @@ async function deployApp(deployment) {
126
124
  ...err,
127
125
  message: `Deployment Failed: ${err.message}`,
128
126
  }
127
+ } finally {
128
+ await replication.close()
129
129
  }
130
130
  }
131
131
 
@@ -83,7 +83,9 @@ exports.revert = async ctx => {
83
83
  try {
84
84
  const db = getProdAppDB({ skip_setup: true })
85
85
  const info = await db.info()
86
- if (info.error) throw info.error
86
+ if (info.error) {
87
+ throw info.error
88
+ }
87
89
  const deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS)
88
90
  if (
89
91
  !deploymentDoc.history ||
@@ -95,12 +97,11 @@ exports.revert = async ctx => {
95
97
  return ctx.throw(400, "App has not yet been deployed")
96
98
  }
97
99
 
100
+ const replication = new Replication({
101
+ source: productionAppId,
102
+ target: appId,
103
+ })
98
104
  try {
99
- const replication = new Replication({
100
- source: productionAppId,
101
- target: appId,
102
- })
103
-
104
105
  await replication.rollback()
105
106
  // update appID in reverted app to be dev version again
106
107
  const db = getAppDB()
@@ -114,6 +115,8 @@ exports.revert = async ctx => {
114
115
  }
115
116
  } catch (err) {
116
117
  ctx.throw(400, `Unable to revert. ${err}`)
118
+ } finally {
119
+ await replication.close()
117
120
  }
118
121
  }
119
122
 
@@ -1,12 +1,4 @@
1
-
2
- const bulkDocs = jest.fn()
3
- const db = jest.fn(() => {
4
- return {
5
- bulkDocs
6
- }
7
- })
8
- jest.mock("../../../../../db", () => db)
9
- require("@budibase/backend-core").init(require("../../../../../db"))
1
+ const TestConfig = require("../../../../../tests/utilities/TestConfiguration")
10
2
 
11
3
  const { RestImporter } = require("../index")
12
4
 
@@ -48,6 +40,12 @@ const datasets = {
48
40
  }
49
41
 
50
42
  describe("Rest Importer", () => {
43
+ const config = new TestConfig(false)
44
+
45
+ beforeEach(async () => {
46
+ await config.init()
47
+ })
48
+
51
49
  let restImporter
52
50
 
53
51
  const init = async (data) => {
@@ -105,11 +103,9 @@ describe("Rest Importer", () => {
105
103
 
106
104
  const testImportQueries = async (key, data, assertions) => {
107
105
  await init(data)
108
- bulkDocs.mockReturnValue([])
109
106
  const importResult = await restImporter.importQueries("datasourceId")
110
107
  expect(importResult.errorQueries.length).toBe(0)
111
108
  expect(importResult.queries.length).toBe(assertions[key].count)
112
- expect(bulkDocs).toHaveBeenCalledTimes(1)
113
109
  jest.clearAllMocks()
114
110
  }
115
111
 
@@ -6,6 +6,7 @@ const {
6
6
  DocumentTypes,
7
7
  InternalTables,
8
8
  } = require("../../../db/utils")
9
+ const { dangerousGetDB } = require("@budibase/backend-core/db")
9
10
  const userController = require("../user")
10
11
  const {
11
12
  inputProcessing,
@@ -250,7 +251,7 @@ exports.fetch = async ctx => {
250
251
  }
251
252
 
252
253
  exports.find = async ctx => {
253
- const db = getAppDB()
254
+ const db = dangerousGetDB(ctx.appId)
254
255
  const table = await db.get(ctx.params.tableId)
255
256
  let row = await findRow(ctx, ctx.params.tableId, ctx.params.rowId)
256
257
  row = await outputProcessing(table, row)
@@ -54,7 +54,7 @@ exports.destroy = async ctx => {
54
54
  }
55
55
 
56
56
  exports.buildSchema = async ctx => {
57
- updateAppId(ctx.params.instance)
57
+ await updateAppId(ctx.params.instance)
58
58
  const db = getAppDB()
59
59
  const webhook = await db.get(ctx.params.id)
60
60
  webhook.bodySchema = toJsonSchema(ctx.request.body)
@@ -80,7 +80,7 @@ exports.buildSchema = async ctx => {
80
80
 
81
81
  exports.trigger = async ctx => {
82
82
  const prodAppId = getProdAppID(ctx.params.instance)
83
- updateAppId(prodAppId)
83
+ await updateAppId(prodAppId)
84
84
  try {
85
85
  const db = getAppDB()
86
86
  const webhook = await db.get(ctx.params.id)
@@ -2,6 +2,7 @@ const { outputProcessing } = require("../../../utilities/rowProcessor")
2
2
  const setup = require("./utilities")
3
3
  const { basicRow } = setup.structures
4
4
  const { doInAppContext } = require("@budibase/backend-core/context")
5
+ const { doInTenant } = require("@budibase/backend-core/tenancy")
5
6
 
6
7
  // mock the fetch for the search system
7
8
  jest.mock("node-fetch")
@@ -340,17 +341,20 @@ describe("/rows", () => {
340
341
 
341
342
  describe("fetchEnrichedRows", () => {
342
343
  it("should allow enriching some linked rows", async () => {
343
- const table = await config.createLinkedTable()
344
- const firstRow = await config.createRow({
345
- name: "Test Contact",
346
- description: "original description",
347
- tableId: table._id
348
- })
349
- const secondRow = await config.createRow({
350
- name: "Test 2",
351
- description: "og desc",
352
- link: [{_id: firstRow._id}],
353
- tableId: table._id,
344
+ const { table, firstRow, secondRow } = await doInTenant(setup.structures.TENANT_ID, async () => {
345
+ const table = await config.createLinkedTable()
346
+ const firstRow = await config.createRow({
347
+ name: "Test Contact",
348
+ description: "original description",
349
+ tableId: table._id
350
+ })
351
+ const secondRow = await config.createRow({
352
+ name: "Test 2",
353
+ description: "og desc",
354
+ link: [{_id: firstRow._id}],
355
+ tableId: table._id,
356
+ })
357
+ return { table, firstRow, secondRow }
354
358
  })
355
359
 
356
360
  // test basic enrichment
@@ -1,4 +1,5 @@
1
- const { checkBuilderEndpoint, getDB } = require("./utilities/TestFunctions")
1
+ const { checkBuilderEndpoint } = require("./utilities/TestFunctions")
2
+ const { getAppDB } = require("@budibase/backend-core/context")
2
3
  const setup = require("./utilities")
3
4
  const { basicTable } = setup.structures
4
5
 
@@ -122,7 +123,7 @@ describe("/tables", () => {
122
123
 
123
124
  describe("indexing", () => {
124
125
  it("should be able to create a table with indexes", async () => {
125
- const db = getDB(config)
126
+ const db = getAppDB(config)
126
127
  const indexCount = (await db.getIndexes()).total_rows
127
128
  const table = basicTable()
128
129
  table.indexes = ["name"]
@@ -3,7 +3,7 @@ const appController = require("../../../controllers/application")
3
3
  const { AppStatus } = require("../../../../db/utils")
4
4
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
5
5
  const { TENANT_ID } = require("../../../../tests/utilities/structures")
6
- const { getAppDB, doInAppContext } = require("@budibase/backend-core/context")
6
+ const { doInAppContext } = require("@budibase/backend-core/context")
7
7
  const env = require("../../../../environment")
8
8
 
9
9
  function Request(appId, params) {
@@ -106,10 +106,6 @@ exports.checkPermissionsEndpoint = async ({
106
106
  .expect(403)
107
107
  }
108
108
 
109
- exports.getDB = () => {
110
- return getAppDB()
111
- }
112
-
113
109
  exports.testAutomation = async (config, automation) => {
114
110
  return runRequest(automation.appId, async () => {
115
111
  return await config.request
package/src/app.ts CHANGED
@@ -2,8 +2,8 @@
2
2
  import { ExtendableContext } from "koa"
3
3
 
4
4
  import * as env from "./environment"
5
- const CouchDB = require("./db")
6
- require("@budibase/backend-core").init(CouchDB)
5
+ import db from "./db"
6
+ db.init()
7
7
  const Koa = require("koa")
8
8
  const destroyable = require("server-destroy")
9
9
  const koaBody = require("koa-body")
@@ -27,7 +27,7 @@ describe("test the delete row action", () => {
27
27
  expect(res.row._id).toEqual(row._id)
28
28
  let error
29
29
  try {
30
- await config.getRow(table._id, res.id)
30
+ await config.getRow(table._id, res.row._id)
31
31
  } catch (err) {
32
32
  error = err
33
33
  }
@@ -1,13 +1,13 @@
1
1
  const { Thread, ThreadType } = require("../threads")
2
2
  const { definitions } = require("./triggerInfo")
3
3
  const webhooks = require("../api/controllers/webhook")
4
- const CouchDB = require("../db")
5
4
  const { queue } = require("./bullboard")
6
5
  const newid = require("../db/newid")
7
6
  const { updateEntityMetadata } = require("../utilities")
8
7
  const { MetadataTypes, WebhookType } = require("../constants")
9
8
  const { getProdAppID } = require("@budibase/backend-core/db")
10
9
  const { cloneDeep } = require("lodash/fp")
10
+ const { doWithDB } = require("@budibase/backend-core/db")
11
11
  const { getAppDB, getAppId } = require("@budibase/backend-core/context")
12
12
 
13
13
  const WH_STEP_ID = definitions.WEBHOOK.stepId
@@ -101,10 +101,11 @@ exports.enableCronTrigger = async (appId, automation) => {
101
101
  // can't use getAppDB here as this is likely to be called from dev app,
102
102
  // but this call could be for dev app or prod app, need to just use what
103
103
  // was passed in
104
- const db = new CouchDB(appId)
105
- const response = await db.put(automation)
106
- automation._id = response.id
107
- automation._rev = response.rev
104
+ await doWithDB(appId, async db => {
105
+ const response = await db.put(automation)
106
+ automation._id = response.id
107
+ automation._rev = response.rev
108
+ })
108
109
  }
109
110
  return automation
110
111
  }
@@ -1,48 +1,49 @@
1
- const PouchDB = require("pouchdb")
2
- const memory = require("pouchdb-adapter-memory")
3
1
  const newid = require("./newid")
4
2
 
5
- PouchDB.plugin(memory)
6
- const Pouch = PouchDB.defaults({
7
- prefix: undefined,
8
- adapter: "memory",
9
- })
3
+ // bypass the main application db config
4
+ // use in memory pouchdb directly
5
+ const { getPouch, closeDB } = require("@budibase/backend-core/db")
6
+ const Pouch = getPouch({ inMemory: true })
10
7
 
11
8
  exports.runView = async (view, calculation, group, data) => {
12
9
  // use a different ID each time for the DB, make sure they
13
10
  // are always unique for each query, don't want overlap
14
11
  // which could cause 409s
15
12
  const db = new Pouch(newid())
16
- // write all the docs to the in memory Pouch (remove revs)
17
- await db.bulkDocs(
18
- data.map(row => ({
19
- ...row,
20
- _rev: undefined,
21
- }))
22
- )
23
- let fn = (doc, emit) => emit(doc._id)
24
- eval("fn = " + view.map.replace("function (doc)", "function (doc, emit)"))
25
- const queryFns = {
26
- meta: view.meta,
27
- map: fn,
28
- }
29
- if (view.reduce) {
30
- queryFns.reduce = view.reduce
31
- }
32
- const response = await db.query(queryFns, {
33
- include_docs: !calculation,
34
- group: !!group,
35
- })
36
- // need to fix the revs to be totally accurate
37
- for (let row of response.rows) {
38
- if (!row._rev || !row._id) {
39
- continue
13
+ try {
14
+ // write all the docs to the in memory Pouch (remove revs)
15
+ await db.bulkDocs(
16
+ data.map(row => ({
17
+ ...row,
18
+ _rev: undefined,
19
+ }))
20
+ )
21
+ let fn = (doc, emit) => emit(doc._id)
22
+ eval("fn = " + view.map.replace("function (doc)", "function (doc, emit)"))
23
+ const queryFns = {
24
+ meta: view.meta,
25
+ map: fn,
26
+ }
27
+ if (view.reduce) {
28
+ queryFns.reduce = view.reduce
40
29
  }
41
- const found = data.find(possible => possible._id === row._id)
42
- if (found) {
43
- row._rev = found._rev
30
+ const response = await db.query(queryFns, {
31
+ include_docs: !calculation,
32
+ group: !!group,
33
+ })
34
+ // need to fix the revs to be totally accurate
35
+ for (let row of response.rows) {
36
+ if (!row._rev || !row._id) {
37
+ continue
38
+ }
39
+ const found = data.find(possible => possible._id === row._id)
40
+ if (found) {
41
+ row._rev = found._rev
42
+ }
44
43
  }
44
+ return response
45
+ } finally {
46
+ await db.destroy()
47
+ await closeDB(db)
45
48
  }
46
- await db.destroy()
47
- return response
48
49
  }
package/src/db/index.js CHANGED
@@ -1,3 +1,16 @@
1
- const client = require("./client")
1
+ const core = require("@budibase/backend-core")
2
+ const env = require("../environment")
2
3
 
3
- module.exports = client
4
+ exports.init = () => {
5
+ const dbConfig = {
6
+ replication: true,
7
+ find: true,
8
+ }
9
+
10
+ if (env.isTest()) {
11
+ dbConfig.inMemory = true
12
+ dbConfig.allDbs = true
13
+ }
14
+
15
+ core.init({ db: dbConfig })
16
+ }
@@ -1,8 +1,8 @@
1
1
  const TestConfig = require("../../tests/utilities/TestConfiguration")
2
2
  const { basicTable } = require("../../tests/utilities/structures")
3
3
  const linkUtils = require("../linkedRows/linkUtils")
4
- const CouchDB = require("../index")
5
4
  const { getAppDB } = require("@budibase/backend-core/context")
5
+ const { doWithDB } = require("@budibase/backend-core/db")
6
6
 
7
7
  describe("test link functionality", () => {
8
8
  const config = new TestConfig(false)
@@ -48,12 +48,13 @@ describe("test link functionality", () => {
48
48
  describe("getLinkDocuments", () => {
49
49
  it("should create the link view when it doesn't exist", async () => {
50
50
  // create the DB and a very basic app design DB
51
- const db = new CouchDB("test")
52
- await db.put({ _id: "_design/database", views: {} })
53
- const output = await linkUtils.getLinkDocuments({
54
- tableId: "test",
55
- rowId: "test",
56
- includeDocs: false,
51
+ const output = await doWithDB("test", async db => {
52
+ await db.put({ _id: "_design/database", views: {} })
53
+ return await linkUtils.getLinkDocuments({
54
+ tableId: "test",
55
+ rowId: "test",
56
+ includeDocs: false,
57
+ })
57
58
  })
58
59
  expect(Array.isArray(output)).toBe(true)
59
60
  })