@budibase/server 1.0.115 → 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 (231) hide show
  1. package/builder/assets/{index.dae9f8cf.js → index.28d8c64f.js} +1 -1
  2. package/builder/index.html +1 -1
  3. package/coverage/clover.xml +1113 -1125
  4. package/coverage/coverage-final.json +85 -86
  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 +46 -46
  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 +10 -10
  149. package/coverage/lcov-report/threads/index.js.html +10 -10
  150. package/coverage/lcov-report/threads/query.js.html +1 -1
  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 +1392 -1404
  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/middleware/builder.js +10 -9
  188. package/dist/migrations/index.js +1 -5
  189. package/dist/package.json +5 -5
  190. package/dist/tests/utilities/TestConfiguration.js +66 -59
  191. package/dist/threads/utils.js +2 -3
  192. package/dist/tsconfig.tsbuildinfo +1 -1
  193. package/dist/utilities/fileSystem/index.js +27 -26
  194. package/dist/utilities/usageQuota/rows.js +16 -16
  195. package/package.json +6 -6
  196. package/scripts/exportAppTemplate.js +10 -8
  197. package/src/api/controllers/application.js +3 -1
  198. package/src/api/controllers/deploy/index.js +4 -4
  199. package/src/api/controllers/dev.js +9 -6
  200. package/src/api/controllers/query/import/tests/index.spec.js +7 -11
  201. package/src/api/controllers/row/internal.js +2 -1
  202. package/src/api/controllers/webhook.js +2 -2
  203. package/src/api/routes/tests/row.spec.js +15 -11
  204. package/src/api/routes/tests/table.spec.js +3 -2
  205. package/src/api/routes/tests/utilities/TestFunctions.js +1 -5
  206. package/src/app.ts +2 -2
  207. package/src/automations/tests/deleteRow.spec.js +1 -1
  208. package/src/automations/utils.js +6 -5
  209. package/src/db/inMemoryView.js +37 -36
  210. package/src/db/index.js +15 -2
  211. package/src/db/tests/linkTests.spec.js +8 -7
  212. package/src/integrations/couchdb.ts +28 -24
  213. package/src/integrations/tests/airtable.spec.js +3 -1
  214. package/src/integrations/tests/couchdb.spec.js +4 -1
  215. package/src/middleware/builder.js +10 -9
  216. package/src/middleware/tests/authorized.spec.js +0 -1
  217. package/src/middleware/tests/usageQuota.spec.js +6 -20
  218. package/src/migrations/functions/tests/appUrls.spec.js +8 -11
  219. package/src/migrations/functions/tests/userEmailViewCasing.spec.js +13 -10
  220. package/src/migrations/functions/usageQuotas/tests/syncApps.spec.js +25 -21
  221. package/src/migrations/functions/usageQuotas/tests/syncRows.spec.js +26 -24
  222. package/src/migrations/index.ts +1 -2
  223. package/src/module.d.ts +8 -0
  224. package/src/tests/utilities/TestConfiguration.js +62 -55
  225. package/src/threads/utils.js +2 -3
  226. package/src/utilities/fileSystem/index.js +30 -30
  227. package/src/utilities/usageQuota/rows.js +22 -18
  228. package/coverage/lcov-report/db/client.js.html +0 -173
  229. package/dist/db/client.js +0 -25
  230. package/scripts/replicateApp.js +0 -50
  231. package/src/db/client.js +0 -31
@@ -53,51 +53,55 @@ module CouchDBModule {
53
53
 
54
54
  class CouchDBIntegration implements IntegrationBase {
55
55
  private config: CouchDBConfig
56
- private client: any
56
+ private readonly client: any
57
57
 
58
58
  constructor(config: CouchDBConfig) {
59
59
  this.config = config
60
60
  this.client = new PouchDB(`${config.url}/${config.database}`)
61
61
  }
62
62
 
63
- async create(query: { json: object }) {
63
+ async query(
64
+ command: string,
65
+ errorMsg: string,
66
+ query: { json?: object; id?: string }
67
+ ) {
64
68
  try {
65
- return this.client.post(query.json)
69
+ const response = await this.client[command](query.id || query.json)
70
+ await this.client.close()
71
+ return response
66
72
  } catch (err) {
67
- console.error("Error writing to couchDB", err)
73
+ console.error(errorMsg, err)
68
74
  throw err
69
75
  }
70
76
  }
71
77
 
78
+ async create(query: { json: object }) {
79
+ return this.query("post", "Error writing to couchDB", query)
80
+ }
81
+
72
82
  async read(query: { json: object }) {
73
- try {
74
- const result = await this.client.allDocs({
83
+ const result = await this.query("allDocs", "Error querying couchDB", {
84
+ json: {
75
85
  include_docs: true,
76
86
  ...query.json,
77
- })
78
- return result.rows.map((row: { doc: object }) => row.doc)
79
- } catch (err) {
80
- console.error("Error querying couchDB", err)
81
- throw err
82
- }
87
+ },
88
+ })
89
+ return result.rows.map((row: { doc: object }) => row.doc)
83
90
  }
84
91
 
85
92
  async update(query: { json: object }) {
86
- try {
87
- return this.client.put(query.json)
88
- } catch (err) {
89
- console.error("Error updating couchDB document", err)
90
- throw err
91
- }
93
+ return this.query("put", "Error updating couchDB document", query)
92
94
  }
93
95
 
94
96
  async delete(query: { id: string }) {
95
- try {
96
- return await this.client.remove(query.id)
97
- } catch (err) {
98
- console.error("Error deleting couchDB document", err)
99
- throw err
100
- }
97
+ const doc = await this.query(
98
+ "get",
99
+ "Cannot find doc to be deleted",
100
+ query
101
+ )
102
+ return this.query("remove", "Error deleting couchDB document", {
103
+ json: doc,
104
+ })
101
105
  }
102
106
  }
103
107
 
@@ -7,7 +7,9 @@ class TestConfiguration {
7
7
  this.integration = new AirtableIntegration.integration(config)
8
8
  this.client = {
9
9
  create: jest.fn(),
10
- select: jest.fn(),
10
+ select: jest.fn(() => ({
11
+ firstPage: jest.fn(() => []),
12
+ })),
11
13
  update: jest.fn(),
12
14
  destroy: jest.fn(),
13
15
  }
@@ -2,8 +2,10 @@ jest.mock("pouchdb", () => function CouchDBMock() {
2
2
  this.post = jest.fn()
3
3
  this.allDocs = jest.fn(() => ({ rows: [] }))
4
4
  this.put = jest.fn()
5
+ this.get = jest.fn()
5
6
  this.remove = jest.fn()
6
7
  this.plugin = jest.fn()
8
+ this.close = jest.fn()
7
9
  })
8
10
 
9
11
  const CouchDBIntegration = require("../couchdb")
@@ -62,6 +64,7 @@ describe("CouchDB Integration", () => {
62
64
  it("calls the delete method with the correct params", async () => {
63
65
  const id = "1234"
64
66
  const response = await config.integration.delete({ id })
65
- expect(config.integration.client.remove).toHaveBeenCalledWith(id)
67
+ expect(config.integration.client.get).toHaveBeenCalledWith(id)
68
+ expect(config.integration.client.remove).toHaveBeenCalled()
66
69
  })
67
70
  })
@@ -5,7 +5,7 @@ const {
5
5
  checkDebounce,
6
6
  setDebounce,
7
7
  } = require("../utilities/redis")
8
- const { getDB } = require("@budibase/backend-core/db")
8
+ const { doWithDB } = require("@budibase/backend-core/db")
9
9
  const { DocumentTypes } = require("../db/utils")
10
10
  const { PermissionTypes } = require("@budibase/backend-core/permissions")
11
11
  const { app: appCache } = require("@budibase/backend-core/cache")
@@ -48,14 +48,15 @@ async function updateAppUpdatedAt(ctx) {
48
48
  if (ctx.method === "GET" || (await checkDebounce(appId))) {
49
49
  return
50
50
  }
51
- const db = getDB(appId)
52
- const metadata = await db.get(DocumentTypes.APP_METADATA)
53
- metadata.updatedAt = new Date().toISOString()
54
- const response = await db.put(metadata)
55
- metadata._rev = response.rev
56
- await appCache.invalidateAppMetadata(appId, metadata)
57
- // set a new debounce record with a short TTL
58
- await setDebounce(appId, DEBOUNCE_TIME_SEC)
51
+ await doWithDB(appId, async db => {
52
+ const metadata = await db.get(DocumentTypes.APP_METADATA)
53
+ metadata.updatedAt = new Date().toISOString()
54
+ const response = await db.put(metadata)
55
+ metadata._rev = response.rev
56
+ await appCache.invalidateAppMetadata(appId, metadata)
57
+ // set a new debounce record with a short TTL
58
+ await setDebounce(appId, DEBOUNCE_TIME_SEC)
59
+ })
59
60
  }
60
61
 
61
62
  module.exports = async (ctx, permType) => {
@@ -10,7 +10,6 @@ jest.mock("../../environment", () => ({
10
10
  const authorizedMiddleware = require("../authorized")
11
11
  const env = require("../../environment")
12
12
  const { PermissionTypes, PermissionLevels } = require("@budibase/backend-core/permissions")
13
- require("@budibase/backend-core").init(require("../../db"))
14
13
  const { doInAppContext } = require("@budibase/backend-core/context")
15
14
 
16
15
  const APP_ID = ""
@@ -6,7 +6,10 @@ jest.mock("@budibase/backend-core/tenancy", () => ({
6
6
 
7
7
  const usageQuotaMiddleware = require("../usageQuota")
8
8
  const usageQuota = require("../../utilities/usageQuota")
9
- const CouchDB = require("../../db")
9
+
10
+ jest.mock("@budibase/backend-core/db")
11
+ const { dangerousGetDB } = require("@budibase/backend-core/db")
12
+
10
13
  const env = require("../../environment")
11
14
 
12
15
  class TestConfiguration {
@@ -80,7 +83,7 @@ describe("usageQuota middleware", () => {
80
83
  _rev: "test",
81
84
  })
82
85
 
83
- CouchDB.mockImplementationOnce(() => ({
86
+ dangerousGetDB.mockImplementationOnce(() => ({
84
87
  get: async () => true
85
88
  }))
86
89
 
@@ -96,7 +99,7 @@ describe("usageQuota middleware", () => {
96
99
  })
97
100
  config.setProd(true)
98
101
 
99
- CouchDB.mockImplementationOnce(() => ({
102
+ dangerousGetDB.mockImplementationOnce(() => ({
100
103
  get: async () => {
101
104
  throw new Error()
102
105
  }
@@ -114,21 +117,4 @@ describe("usageQuota middleware", () => {
114
117
  expect(usageQuota.update).toHaveBeenCalledWith("rows", 1)
115
118
  expect(config.next).toHaveBeenCalled()
116
119
  })
117
-
118
- // it("calculates the correct file size from a file upload call and adds it to quota", async () => {
119
- // config.setUrl("/upload")
120
- // config.setProd(true)
121
- // config.setFiles([
122
- // {
123
- // size: 100
124
- // },
125
- // {
126
- // size: 10000
127
- // },
128
- // ])
129
- // await config.executeMiddleware()
130
-
131
- // expect(usageQuota.update).toHaveBeenCalledWith("storage", 10100)
132
- // expect(config.next).toHaveBeenCalled()
133
- // })
134
120
  })
@@ -1,12 +1,10 @@
1
- const { DocumentTypes } = require("@budibase/backend-core/db")
2
- const env = require("../../../environment")
1
+ const { DocumentTypes, doWithDB } = require("@budibase/backend-core/db")
3
2
  const TestConfig = require("../../../tests/utilities/TestConfiguration")
4
3
 
5
4
  const migration = require("../appUrls")
6
5
 
7
6
  describe("run", () => {
8
7
  let config = new TestConfig(false)
9
- const CouchDB = config.getCouch()
10
8
 
11
9
  beforeEach(async () => {
12
10
  await config.init()
@@ -16,14 +14,13 @@ describe("run", () => {
16
14
 
17
15
  it("runs successfully", async () => {
18
16
  const app = await config.createApp("testApp")
19
- const appDb = new CouchDB(app.appId)
20
- let metadata = await appDb.get(DocumentTypes.APP_METADATA)
21
- delete metadata.url
22
- await appDb.put(metadata)
23
-
24
- await migration.run(appDb)
25
-
26
- metadata = await appDb.get(DocumentTypes.APP_METADATA)
17
+ const metadata = await doWithDB(app.appId, async db => {
18
+ const metadataDoc = await db.get(DocumentTypes.APP_METADATA)
19
+ delete metadataDoc.url
20
+ await db.put(metadataDoc)
21
+ await migration.run(db)
22
+ return await db.get(DocumentTypes.APP_METADATA)
23
+ })
27
24
  expect(metadata.url).toEqual("/testapp")
28
25
  })
29
26
  })
@@ -1,5 +1,6 @@
1
1
  const TestConfig = require("../../../tests/utilities/TestConfiguration")
2
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
2
+ const { TENANT_ID } = require("../../../tests/utilities/structures")
3
+ const { getGlobalDB, doInTenant } = require("@budibase/backend-core/tenancy")
3
4
 
4
5
  // mock email view creation
5
6
  const coreDb = require("@budibase/backend-core/db")
@@ -9,17 +10,19 @@ coreDb.createUserEmailView = createUserEmailView
9
10
  const migration = require("../userEmailViewCasing")
10
11
 
11
12
  describe("run", () => {
12
- let config = new TestConfig(false)
13
- const globalDb = getGlobalDB()
13
+ doInTenant(TENANT_ID, () => {
14
+ let config = new TestConfig(false)
15
+ const globalDb = getGlobalDB()
14
16
 
15
- beforeEach(async () => {
16
- await config.init()
17
- })
17
+ beforeEach(async () => {
18
+ await config.init()
19
+ })
18
20
 
19
- afterAll(config.end)
21
+ afterAll(config.end)
20
22
 
21
- it("runs successfully", async () => {
22
- await migration.run(globalDb)
23
- expect(createUserEmailView).toHaveBeenCalledTimes(1)
23
+ it("runs successfully", async () => {
24
+ await migration.run(globalDb)
25
+ expect(createUserEmailView).toHaveBeenCalledTimes(1)
26
+ })
24
27
  })
25
28
  })
@@ -1,37 +1,41 @@
1
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
1
+ const { doInTenant, getGlobalDB } = require("@budibase/backend-core/tenancy")
2
2
  const TestConfig = require("../../../../tests/utilities/TestConfiguration")
3
+ const { TENANT_ID } = require("../../../../tests/utilities/structures")
3
4
  const { getUsageQuotaDoc, update, Properties } = require("../../../../utilities/usageQuota")
4
5
  const syncApps = require("../syncApps")
5
6
  const env = require("../../../../environment")
6
7
 
7
8
  describe("syncApps", () => {
9
+
8
10
  let config = new TestConfig(false)
9
11
 
10
- beforeEach(async () => {
11
- await config.init()
12
- env._set("USE_QUOTAS", 1)
13
- })
12
+ beforeEach(async () => {
13
+ await config.init()
14
+ env._set("USE_QUOTAS", 1)
15
+ })
14
16
 
15
- afterAll(config.end)
17
+ afterAll(config.end)
16
18
 
17
- it("runs successfully", async () => {
18
- // create the usage quota doc and mock usages
19
- const db = getGlobalDB()
20
- await getUsageQuotaDoc(db)
21
- await update(Properties.APPS, 3)
19
+ it("runs successfully", async () => {
20
+ await doInTenant(TENANT_ID, async () => {
21
+ const db = getGlobalDB()
22
+ // create the usage quota doc and mock usages
23
+ await getUsageQuotaDoc(db)
24
+ await update(Properties.APPS, 3)
22
25
 
23
- let usageDoc = await getUsageQuotaDoc(db)
24
- expect(usageDoc.usageQuota.apps).toEqual(3)
26
+ let usageDoc = await getUsageQuotaDoc(db)
27
+ expect(usageDoc.usageQuota.apps).toEqual(3)
25
28
 
26
- // create an extra app to test the migration
27
- await config.createApp("quota-test")
29
+ // create an extra app to test the migration
30
+ await config.createApp("quota-test")
28
31
 
29
- // migrate
30
- await syncApps.run()
32
+ // migrate
33
+ await syncApps.run()
31
34
 
32
- // assert the migration worked
33
- usageDoc = await getUsageQuotaDoc(db)
34
- expect(usageDoc.usageQuota.apps).toEqual(2)
35
- })
35
+ // assert the migration worked
36
+ usageDoc = await getUsageQuotaDoc(db)
37
+ expect(usageDoc.usageQuota.apps).toEqual(2)
38
+ })
39
+ })
36
40
  })
37
41
 
@@ -1,5 +1,6 @@
1
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
1
+ const { doInTenant, getGlobalDB } = require("@budibase/backend-core/tenancy")
2
2
  const TestConfig = require("../../../../tests/utilities/TestConfiguration")
3
+ const { TENANT_ID } = require("../../../../tests/utilities/structures")
3
4
  const { getUsageQuotaDoc, update, Properties } = require("../../../../utilities/usageQuota")
4
5
  const syncRows = require("../syncRows")
5
6
  const env = require("../../../../environment")
@@ -15,29 +16,30 @@ describe("syncRows", () => {
15
16
  afterAll(config.end)
16
17
 
17
18
  it("runs successfully", async () => {
18
- // create the usage quota doc and mock usages
19
- const db = getGlobalDB()
20
- await getUsageQuotaDoc(db)
21
- await update(Properties.ROW, 300)
22
-
23
- let usageDoc = await getUsageQuotaDoc(db)
24
- expect(usageDoc.usageQuota.rows).toEqual(300)
25
-
26
- // app 1
27
- await config.createTable()
28
- await config.createRow()
29
- // app 2
30
- await config.createApp("second-app")
31
- await config.createTable()
32
- await config.createRow()
33
- await config.createRow()
34
-
35
- // migrate
36
- await syncRows.run()
37
-
38
- // assert the migration worked
39
- usageDoc = await getUsageQuotaDoc(db)
40
- expect(usageDoc.usageQuota.rows).toEqual(3)
19
+ await doInTenant(TENANT_ID, async () => {
20
+ const db = getGlobalDB()
21
+ await getUsageQuotaDoc(db)
22
+ await update(Properties.ROW, 300)
23
+
24
+ let usageDoc = await getUsageQuotaDoc(db)
25
+ expect(usageDoc.usageQuota.rows).toEqual(300)
26
+
27
+ // app 1
28
+ await config.createTable()
29
+ await config.createRow()
30
+ // app 2
31
+ await config.createApp("second-app")
32
+ await config.createTable()
33
+ await config.createRow()
34
+ await config.createRow()
35
+
36
+ // migrate
37
+ await syncRows.run()
38
+
39
+ // assert the migration worked
40
+ usageDoc = await getUsageQuotaDoc(db)
41
+ expect(usageDoc.usageQuota.rows).toEqual(3)
42
+ })
41
43
  })
42
44
  })
43
45
 
@@ -1,4 +1,3 @@
1
- import CouchDB from "../db"
2
1
  const {
3
2
  MIGRATION_TYPES,
4
3
  runMigrations,
@@ -52,5 +51,5 @@ export const MIGRATIONS: Migration[] = [
52
51
  ]
53
52
 
54
53
  export const migrate = async (options?: MigrationOptions) => {
55
- await runMigrations(CouchDB, MIGRATIONS, options)
54
+ await runMigrations(MIGRATIONS, options)
56
55
  }
package/src/module.d.ts CHANGED
@@ -1,3 +1,11 @@
1
1
  declare module "@budibase/backend-core"
2
2
  declare module "@budibase/backend-core/tenancy"
3
3
  declare module "@budibase/backend-core/db"
4
+ declare module "@budibase/backend-core/context"
5
+ declare module "@budibase/backend-core/cache"
6
+ declare module "@budibase/backend-core/permissions"
7
+ declare module "@budibase/backend-core/roles"
8
+ declare module "@budibase/backend-core/constants"
9
+ declare module "@budibase/backend-core/auth"
10
+ declare module "@budibase/backend-core/sessions"
11
+ declare module "@budibase/backend-core/encryption"
@@ -1,6 +1,4 @@
1
- const core = require("@budibase/backend-core")
2
- const CouchDB = require("../../db")
3
- core.init(CouchDB)
1
+ require("../../db").init()
4
2
  const { BUILTIN_ROLE_IDS } = require("@budibase/backend-core/roles")
5
3
  const env = require("../../environment")
6
4
  const {
@@ -20,7 +18,7 @@ const supertest = require("supertest")
20
18
  const { cleanup } = require("../../utilities/fileSystem")
21
19
  const { Cookies, Headers } = require("@budibase/backend-core/constants")
22
20
  const { jwt } = require("@budibase/backend-core/auth")
23
- const { getGlobalDB } = require("@budibase/backend-core/tenancy")
21
+ const { doInTenant, doWithGlobalDB } = require("@budibase/backend-core/tenancy")
24
22
  const { createASession } = require("@budibase/backend-core/sessions")
25
23
  const { user: userCache } = require("@budibase/backend-core/cache")
26
24
  const newid = require("../../db/newid")
@@ -57,8 +55,20 @@ class TestConfiguration {
57
55
  return this.appId
58
56
  }
59
57
 
60
- getCouch() {
61
- return CouchDB
58
+ async doInContext(appId, task) {
59
+ if (!appId) {
60
+ appId = this.appId
61
+ }
62
+ return doInTenant(TENANT_ID, () => {
63
+ // check if already in a context
64
+ if (context.getAppId() == null && appId !== null) {
65
+ return context.doInAppContext(appId, async () => {
66
+ return task()
67
+ })
68
+ } else {
69
+ return task()
70
+ }
71
+ })
62
72
  }
63
73
 
64
74
  async _req(config, params, controlFunc) {
@@ -72,35 +82,28 @@ class TestConfiguration {
72
82
  request.request = {
73
83
  body: config,
74
84
  }
75
- async function run() {
85
+ return this.doInContext(this.appId, async () => {
76
86
  if (params) {
77
87
  request.params = params
78
88
  }
79
89
  await controlFunc(request)
80
90
  return request.body
81
- }
82
- // check if already in a context
83
- if (context.getAppId() == null && this.appId !== null) {
84
- return context.doInAppContext(this.appId, async () => {
85
- return run()
86
- })
87
- } else {
88
- return run()
89
- }
91
+ })
90
92
  }
91
93
 
92
94
  async generateApiKey(userId = GLOBAL_USER_ID) {
93
- const db = getGlobalDB(TENANT_ID)
94
- const id = generateDevInfoID(userId)
95
- let devInfo
96
- try {
97
- devInfo = await db.get(id)
98
- } catch (err) {
99
- devInfo = { _id: id, userId }
100
- }
101
- devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`)
102
- await db.put(devInfo)
103
- return devInfo.apiKey
95
+ return doWithGlobalDB(TENANT_ID, async db => {
96
+ const id = generateDevInfoID(userId)
97
+ let devInfo
98
+ try {
99
+ devInfo = await db.get(id)
100
+ } catch (err) {
101
+ devInfo = { _id: id, userId }
102
+ }
103
+ devInfo.apiKey = encrypt(`${TENANT_ID}${SEPARATOR}${newid()}`)
104
+ await db.put(devInfo)
105
+ return devInfo.apiKey
106
+ })
104
107
  }
105
108
 
106
109
  async globalUser({
@@ -109,34 +112,35 @@ class TestConfiguration {
109
112
  email = EMAIL,
110
113
  roles,
111
114
  } = {}) {
112
- const db = getGlobalDB(TENANT_ID)
113
- let existing
114
- try {
115
- existing = await db.get(id)
116
- } catch (err) {
117
- existing = { email }
118
- }
119
- const user = {
120
- _id: id,
121
- ...existing,
122
- roles: roles || {},
123
- tenantId: TENANT_ID,
124
- }
125
- await createASession(id, {
126
- sessionId: "sessionid",
127
- tenantId: TENANT_ID,
128
- csrfToken: CSRF_TOKEN,
115
+ return doWithGlobalDB(TENANT_ID, async db => {
116
+ let existing
117
+ try {
118
+ existing = await db.get(id)
119
+ } catch (err) {
120
+ existing = { email }
121
+ }
122
+ const user = {
123
+ _id: id,
124
+ ...existing,
125
+ roles: roles || {},
126
+ tenantId: TENANT_ID,
127
+ }
128
+ await createASession(id, {
129
+ sessionId: "sessionid",
130
+ tenantId: TENANT_ID,
131
+ csrfToken: CSRF_TOKEN,
132
+ })
133
+ if (builder) {
134
+ user.builder = { global: true }
135
+ } else {
136
+ user.builder = { global: false }
137
+ }
138
+ const resp = await db.put(user)
139
+ return {
140
+ _rev: resp._rev,
141
+ ...user,
142
+ }
129
143
  })
130
- if (builder) {
131
- user.builder = { global: true }
132
- } else {
133
- user.builder = { global: false }
134
- }
135
- const resp = await db.put(user)
136
- return {
137
- _rev: resp._rev,
138
- ...user,
139
- }
140
144
  }
141
145
 
142
146
  // use a new id as the name to avoid name collisions
@@ -205,9 +209,12 @@ class TestConfiguration {
205
209
 
206
210
  async createApp(appName) {
207
211
  // create dev app
212
+ // clear any old app
213
+ this.appId = null
214
+ await context.updateAppId(null)
208
215
  this.app = await this._req({ name: appName }, null, controllers.app.create)
209
216
  this.appId = this.app.appId
210
- context.updateAppId(this.appId)
217
+ await context.updateAppId(this.appId)
211
218
 
212
219
  // create production app
213
220
  this.prodApp = await this.deploy()
@@ -1,6 +1,5 @@
1
1
  const env = require("../environment")
2
- const CouchDB = require("../db")
3
- const { init } = require("@budibase/backend-core")
2
+ const db = require("../db")
4
3
  const redis = require("@budibase/backend-core/redis")
5
4
  const { SEPARATOR } = require("@budibase/backend-core/db")
6
5
 
@@ -25,7 +24,7 @@ exports.threadSetup = () => {
25
24
  }
26
25
  // when thread starts, make sure it is recorded
27
26
  env.setInThread()
28
- init(CouchDB)
27
+ db.init()
29
28
  }
30
29
 
31
30
  function makeVariableKey(queryId, variable) {