@budibase/backend-core 3.2.4 → 3.2.6

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 (272) hide show
  1. package/dist/index.js.map +1 -1
  2. package/dist/index.js.meta.json +1 -1
  3. package/dist/package.json +11 -4
  4. package/dist/plugins.js.meta.json +1 -1
  5. package/package.json +11 -4
  6. package/src/accounts/accounts.ts +0 -82
  7. package/src/accounts/api.ts +0 -59
  8. package/src/accounts/index.ts +0 -1
  9. package/src/auth/auth.ts +0 -210
  10. package/src/auth/index.ts +0 -1
  11. package/src/auth/tests/auth.spec.ts +0 -14
  12. package/src/blacklist/blacklist.ts +0 -54
  13. package/src/blacklist/index.ts +0 -1
  14. package/src/blacklist/tests/blacklist.spec.ts +0 -46
  15. package/src/cache/appMetadata.ts +0 -88
  16. package/src/cache/base/index.ts +0 -150
  17. package/src/cache/docWritethrough.ts +0 -105
  18. package/src/cache/generic.ts +0 -33
  19. package/src/cache/index.ts +0 -8
  20. package/src/cache/invite.ts +0 -86
  21. package/src/cache/passwordReset.ts +0 -49
  22. package/src/cache/tests/docWritethrough.spec.ts +0 -296
  23. package/src/cache/tests/user.spec.ts +0 -145
  24. package/src/cache/tests/writethrough.spec.ts +0 -139
  25. package/src/cache/user.ts +0 -154
  26. package/src/cache/writethrough.ts +0 -133
  27. package/src/configs/configs.ts +0 -263
  28. package/src/configs/index.ts +0 -1
  29. package/src/configs/tests/configs.spec.ts +0 -184
  30. package/src/constants/db.ts +0 -75
  31. package/src/constants/index.ts +0 -2
  32. package/src/constants/misc.ts +0 -36
  33. package/src/context/Context.ts +0 -14
  34. package/src/context/identity.ts +0 -58
  35. package/src/context/index.ts +0 -3
  36. package/src/context/mainContext.ts +0 -422
  37. package/src/context/tests/index.spec.ts +0 -255
  38. package/src/context/types.ts +0 -26
  39. package/src/db/Replication.ts +0 -94
  40. package/src/db/couch/DatabaseImpl.ts +0 -511
  41. package/src/db/couch/connections.ts +0 -89
  42. package/src/db/couch/index.ts +0 -4
  43. package/src/db/couch/pouchDB.ts +0 -97
  44. package/src/db/couch/pouchDump.ts +0 -0
  45. package/src/db/couch/tests/DatabaseImpl.spec.ts +0 -118
  46. package/src/db/couch/utils.ts +0 -55
  47. package/src/db/db.ts +0 -34
  48. package/src/db/errors.ts +0 -14
  49. package/src/db/index.ts +0 -12
  50. package/src/db/instrumentation.ts +0 -199
  51. package/src/db/lucene.ts +0 -721
  52. package/src/db/searchIndexes/index.ts +0 -1
  53. package/src/db/searchIndexes/searchIndexes.ts +0 -62
  54. package/src/db/tests/DatabaseImpl.spec.ts +0 -55
  55. package/src/db/tests/connections.spec.ts +0 -22
  56. package/src/db/tests/index.spec.ts +0 -32
  57. package/src/db/tests/lucene.spec.ts +0 -400
  58. package/src/db/tests/pouch.spec.js +0 -62
  59. package/src/db/tests/utils.spec.ts +0 -63
  60. package/src/db/utils.ts +0 -208
  61. package/src/db/views.ts +0 -245
  62. package/src/docIds/conversions.ts +0 -60
  63. package/src/docIds/ids.ts +0 -126
  64. package/src/docIds/index.ts +0 -2
  65. package/src/docIds/newid.ts +0 -5
  66. package/src/docIds/params.ts +0 -189
  67. package/src/docUpdates/index.ts +0 -24
  68. package/src/environment.ts +0 -293
  69. package/src/errors/errors.ts +0 -119
  70. package/src/errors/index.ts +0 -1
  71. package/src/events/analytics.ts +0 -6
  72. package/src/events/asyncEvents/index.ts +0 -2
  73. package/src/events/asyncEvents/publisher.ts +0 -12
  74. package/src/events/asyncEvents/queue.ts +0 -22
  75. package/src/events/backfill.ts +0 -183
  76. package/src/events/documentId.ts +0 -56
  77. package/src/events/events.ts +0 -47
  78. package/src/events/identification.ts +0 -311
  79. package/src/events/index.ts +0 -15
  80. package/src/events/processors/AnalyticsProcessor.ts +0 -64
  81. package/src/events/processors/AuditLogsProcessor.ts +0 -92
  82. package/src/events/processors/LoggingProcessor.ts +0 -36
  83. package/src/events/processors/Processors.ts +0 -52
  84. package/src/events/processors/async/DocumentUpdateProcessor.ts +0 -38
  85. package/src/events/processors/index.ts +0 -19
  86. package/src/events/processors/posthog/PosthogProcessor.ts +0 -118
  87. package/src/events/processors/posthog/index.ts +0 -3
  88. package/src/events/processors/posthog/rateLimiting.ts +0 -106
  89. package/src/events/processors/posthog/tests/PosthogProcessor.spec.ts +0 -164
  90. package/src/events/processors/types.ts +0 -1
  91. package/src/events/publishers/account.ts +0 -41
  92. package/src/events/publishers/ai.ts +0 -21
  93. package/src/events/publishers/app.ts +0 -168
  94. package/src/events/publishers/auditLog.ts +0 -26
  95. package/src/events/publishers/auth.ts +0 -73
  96. package/src/events/publishers/automation.ts +0 -110
  97. package/src/events/publishers/backfill.ts +0 -74
  98. package/src/events/publishers/backup.ts +0 -42
  99. package/src/events/publishers/datasource.ts +0 -48
  100. package/src/events/publishers/email.ts +0 -17
  101. package/src/events/publishers/environmentVariable.ts +0 -38
  102. package/src/events/publishers/group.ts +0 -99
  103. package/src/events/publishers/index.ts +0 -25
  104. package/src/events/publishers/installation.ts +0 -38
  105. package/src/events/publishers/layout.ts +0 -26
  106. package/src/events/publishers/license.ts +0 -84
  107. package/src/events/publishers/org.ts +0 -37
  108. package/src/events/publishers/plugin.ts +0 -47
  109. package/src/events/publishers/query.ts +0 -89
  110. package/src/events/publishers/role.ts +0 -62
  111. package/src/events/publishers/rows.ts +0 -29
  112. package/src/events/publishers/screen.ts +0 -36
  113. package/src/events/publishers/serve.ts +0 -43
  114. package/src/events/publishers/table.ts +0 -70
  115. package/src/events/publishers/user.ts +0 -202
  116. package/src/events/publishers/view.ts +0 -107
  117. package/src/features/features.ts +0 -277
  118. package/src/features/index.ts +0 -2
  119. package/src/features/tests/features.spec.ts +0 -267
  120. package/src/features/tests/utils.ts +0 -64
  121. package/src/helpers.ts +0 -9
  122. package/src/index.ts +0 -59
  123. package/src/installation.ts +0 -115
  124. package/src/logging/alerts.ts +0 -26
  125. package/src/logging/correlation/correlation.ts +0 -15
  126. package/src/logging/correlation/index.ts +0 -1
  127. package/src/logging/correlation/middleware.ts +0 -18
  128. package/src/logging/index.ts +0 -4
  129. package/src/logging/pino/logger.ts +0 -239
  130. package/src/logging/pino/middleware.ts +0 -48
  131. package/src/logging/system.ts +0 -81
  132. package/src/logging/tests/system.spec.ts +0 -61
  133. package/src/middleware/adminOnly.ts +0 -9
  134. package/src/middleware/auditLog.ts +0 -6
  135. package/src/middleware/authenticated.ts +0 -247
  136. package/src/middleware/builderOnly.ts +0 -21
  137. package/src/middleware/builderOrAdmin.ts +0 -21
  138. package/src/middleware/contentSecurityPolicy.ts +0 -113
  139. package/src/middleware/csrf.ts +0 -81
  140. package/src/middleware/errorHandling.ts +0 -43
  141. package/src/middleware/index.ts +0 -24
  142. package/src/middleware/internalApi.ts +0 -23
  143. package/src/middleware/ip.ts +0 -12
  144. package/src/middleware/joi-validator.ts +0 -58
  145. package/src/middleware/matchers.ts +0 -39
  146. package/src/middleware/passport/datasource/google.ts +0 -102
  147. package/src/middleware/passport/local.ts +0 -54
  148. package/src/middleware/passport/sso/google.ts +0 -77
  149. package/src/middleware/passport/sso/oidc.ts +0 -152
  150. package/src/middleware/passport/sso/sso.ts +0 -138
  151. package/src/middleware/passport/sso/tests/google.spec.ts +0 -68
  152. package/src/middleware/passport/sso/tests/oidc.spec.ts +0 -144
  153. package/src/middleware/passport/sso/tests/sso.spec.ts +0 -197
  154. package/src/middleware/passport/utils.ts +0 -38
  155. package/src/middleware/querystringToBody.ts +0 -28
  156. package/src/middleware/tenancy.ts +0 -36
  157. package/src/middleware/tests/builder.spec.ts +0 -181
  158. package/src/middleware/tests/contentSecurityPolicy.spec.ts +0 -75
  159. package/src/middleware/tests/matchers.spec.ts +0 -100
  160. package/src/migrations/definitions.ts +0 -40
  161. package/src/migrations/index.ts +0 -2
  162. package/src/migrations/migrations.ts +0 -186
  163. package/src/migrations/tests/__snapshots__/migrations.spec.ts.snap +0 -11
  164. package/src/migrations/tests/migrations.spec.ts +0 -64
  165. package/src/objectStore/buckets/app.ts +0 -53
  166. package/src/objectStore/buckets/global.ts +0 -29
  167. package/src/objectStore/buckets/index.ts +0 -3
  168. package/src/objectStore/buckets/plugins.ts +0 -71
  169. package/src/objectStore/buckets/tests/app.spec.ts +0 -161
  170. package/src/objectStore/buckets/tests/global.spec.ts +0 -74
  171. package/src/objectStore/buckets/tests/plugins.spec.ts +0 -111
  172. package/src/objectStore/cloudfront.ts +0 -41
  173. package/src/objectStore/index.ts +0 -3
  174. package/src/objectStore/objectStore.ts +0 -585
  175. package/src/objectStore/utils.ts +0 -113
  176. package/src/platform/index.ts +0 -3
  177. package/src/platform/platformDb.ts +0 -6
  178. package/src/platform/tenants.ts +0 -101
  179. package/src/platform/tests/tenants.spec.ts +0 -26
  180. package/src/platform/users.ts +0 -129
  181. package/src/plugin/index.ts +0 -1
  182. package/src/plugin/tests/validation.spec.ts +0 -209
  183. package/src/plugin/utils.ts +0 -175
  184. package/src/queue/constants.ts +0 -8
  185. package/src/queue/inMemoryQueue.ts +0 -189
  186. package/src/queue/index.ts +0 -2
  187. package/src/queue/listeners.ts +0 -199
  188. package/src/queue/queue.ts +0 -84
  189. package/src/redis/index.ts +0 -6
  190. package/src/redis/init.ts +0 -118
  191. package/src/redis/redis.ts +0 -358
  192. package/src/redis/redlockImpl.ts +0 -155
  193. package/src/redis/tests/redis.spec.ts +0 -207
  194. package/src/redis/tests/redlockImpl.spec.ts +0 -105
  195. package/src/redis/utils.ts +0 -128
  196. package/src/security/auth.ts +0 -24
  197. package/src/security/encryption.ts +0 -185
  198. package/src/security/index.ts +0 -1
  199. package/src/security/permissions.ts +0 -166
  200. package/src/security/roles.ts +0 -655
  201. package/src/security/secrets.ts +0 -20
  202. package/src/security/sessions.ts +0 -123
  203. package/src/security/tests/auth.spec.ts +0 -45
  204. package/src/security/tests/encryption.spec.ts +0 -31
  205. package/src/security/tests/permissions.spec.ts +0 -146
  206. package/src/security/tests/secrets.spec.ts +0 -35
  207. package/src/security/tests/sessions.spec.ts +0 -12
  208. package/src/sql/designDoc.ts +0 -17
  209. package/src/sql/index.ts +0 -5
  210. package/src/sql/sql.ts +0 -1854
  211. package/src/sql/sqlTable.ts +0 -319
  212. package/src/sql/utils.ts +0 -193
  213. package/src/tenancy/db.ts +0 -6
  214. package/src/tenancy/index.ts +0 -2
  215. package/src/tenancy/tenancy.ts +0 -148
  216. package/src/tenancy/tests/tenancy.spec.ts +0 -184
  217. package/src/timers/index.ts +0 -1
  218. package/src/timers/timers.ts +0 -22
  219. package/src/users/db.ts +0 -582
  220. package/src/users/events.ts +0 -176
  221. package/src/users/index.ts +0 -4
  222. package/src/users/lookup.ts +0 -99
  223. package/src/users/test/db.spec.ts +0 -188
  224. package/src/users/test/utils.spec.ts +0 -67
  225. package/src/users/users.ts +0 -353
  226. package/src/users/utils.ts +0 -81
  227. package/src/utils/Duration.ts +0 -56
  228. package/src/utils/hashing.ts +0 -15
  229. package/src/utils/index.ts +0 -4
  230. package/src/utils/stringUtils.ts +0 -8
  231. package/src/utils/tests/Duration.spec.ts +0 -19
  232. package/src/utils/tests/utils.spec.ts +0 -204
  233. package/src/utils/utils.ts +0 -249
  234. package/tests/core/logging.ts +0 -34
  235. package/tests/core/users/users.spec.js +0 -53
  236. package/tests/core/utilities/index.ts +0 -7
  237. package/tests/core/utilities/jestUtils.ts +0 -33
  238. package/tests/core/utilities/mocks/alerts.ts +0 -4
  239. package/tests/core/utilities/mocks/date.ts +0 -3
  240. package/tests/core/utilities/mocks/events.ts +0 -132
  241. package/tests/core/utilities/mocks/index.ts +0 -9
  242. package/tests/core/utilities/mocks/licenses.ts +0 -119
  243. package/tests/core/utilities/queue.ts +0 -9
  244. package/tests/core/utilities/structures/Chance.ts +0 -20
  245. package/tests/core/utilities/structures/accounts.ts +0 -80
  246. package/tests/core/utilities/structures/apps.ts +0 -21
  247. package/tests/core/utilities/structures/common.ts +0 -7
  248. package/tests/core/utilities/structures/db.ts +0 -12
  249. package/tests/core/utilities/structures/documents/index.ts +0 -1
  250. package/tests/core/utilities/structures/documents/platform/index.ts +0 -1
  251. package/tests/core/utilities/structures/documents/platform/installation.ts +0 -12
  252. package/tests/core/utilities/structures/generator.ts +0 -3
  253. package/tests/core/utilities/structures/index.ts +0 -15
  254. package/tests/core/utilities/structures/koa.ts +0 -16
  255. package/tests/core/utilities/structures/licenses.ts +0 -190
  256. package/tests/core/utilities/structures/plugins.ts +0 -19
  257. package/tests/core/utilities/structures/quotas.ts +0 -72
  258. package/tests/core/utilities/structures/scim.ts +0 -80
  259. package/tests/core/utilities/structures/sso.ts +0 -118
  260. package/tests/core/utilities/structures/tenants.ts +0 -5
  261. package/tests/core/utilities/structures/userGroups.ts +0 -10
  262. package/tests/core/utilities/structures/users.ts +0 -89
  263. package/tests/core/utilities/testContainerUtils.ts +0 -165
  264. package/tests/core/utilities/utils/index.ts +0 -2
  265. package/tests/core/utilities/utils/queue.ts +0 -27
  266. package/tests/core/utilities/utils/time.ts +0 -3
  267. package/tests/extra/DBTestConfiguration.ts +0 -36
  268. package/tests/extra/index.ts +0 -2
  269. package/tests/extra/testEnv.ts +0 -95
  270. package/tests/index.ts +0 -2
  271. package/tests/jestEnv.ts +0 -10
  272. package/tests/jestSetup.ts +0 -36
@@ -1,422 +0,0 @@
1
- // some test cases call functions directly, need to
2
- // store an app ID to pretend there is a context
3
- import env from "../environment"
4
- import Context from "./Context"
5
- import * as conversions from "../docIds/conversions"
6
- import { getDB } from "../db/db"
7
- import {
8
- DocumentType,
9
- SEPARATOR,
10
- StaticDatabases,
11
- DEFAULT_TENANT_ID,
12
- } from "../constants"
13
- import { Database, IdentityContext, Snippet, App, Table } from "@budibase/types"
14
- import { ContextMap } from "./types"
15
-
16
- let TEST_APP_ID: string | null = null
17
-
18
- export function getGlobalDBName(tenantId?: string) {
19
- // tenant ID can be set externally, for example user API where
20
- // new tenants are being created, this may be the case
21
- if (!tenantId) {
22
- tenantId = getTenantId()
23
- }
24
- return baseGlobalDBName(tenantId)
25
- }
26
-
27
- export function getAuditLogDBName(tenantId?: string) {
28
- if (!tenantId) {
29
- tenantId = getTenantId()
30
- }
31
- if (tenantId === DEFAULT_TENANT_ID) {
32
- return StaticDatabases.AUDIT_LOGS.name
33
- } else {
34
- return `${tenantId}${SEPARATOR}${StaticDatabases.AUDIT_LOGS.name}`
35
- }
36
- }
37
-
38
- export function getScimDBName(tenantId?: string) {
39
- if (!tenantId) {
40
- tenantId = getTenantId()
41
- }
42
- if (tenantId === DEFAULT_TENANT_ID) {
43
- return StaticDatabases.SCIM_LOGS.name
44
- } else {
45
- return `${tenantId}${SEPARATOR}${StaticDatabases.SCIM_LOGS.name}`
46
- }
47
- }
48
-
49
- export function baseGlobalDBName(tenantId: string | undefined | null) {
50
- if (!tenantId || tenantId === DEFAULT_TENANT_ID) {
51
- return StaticDatabases.GLOBAL.name
52
- } else {
53
- return `${tenantId}${SEPARATOR}${StaticDatabases.GLOBAL.name}`
54
- }
55
- }
56
-
57
- export function getPlatformURL() {
58
- return env.PLATFORM_URL
59
- }
60
-
61
- export function isMultiTenant() {
62
- return !!env.MULTI_TENANCY
63
- }
64
-
65
- export function isTenantIdSet() {
66
- const context = Context.get()
67
- return !!context?.tenantId
68
- }
69
-
70
- export function isTenancyEnabled() {
71
- return env.MULTI_TENANCY
72
- }
73
-
74
- /**
75
- * Given an app ID this will attempt to retrieve the tenant ID from it.
76
- * @return The tenant ID found within the app ID.
77
- */
78
- export function getTenantIDFromAppID(appId: string) {
79
- if (!appId) {
80
- return undefined
81
- }
82
- if (!isMultiTenant()) {
83
- return DEFAULT_TENANT_ID
84
- }
85
- const split = appId.split(SEPARATOR)
86
- const hasDev = split[1] === DocumentType.DEV
87
- if ((hasDev && split.length === 3) || (!hasDev && split.length === 2)) {
88
- return undefined
89
- }
90
- if (hasDev) {
91
- return split[2]
92
- } else {
93
- return split[1]
94
- }
95
- }
96
-
97
- function updateContext(updates: ContextMap): ContextMap {
98
- let context: ContextMap
99
- try {
100
- context = Context.get()
101
- } catch (err) {
102
- // no context, start empty
103
- context = {}
104
- }
105
- context = {
106
- ...context,
107
- ...updates,
108
- }
109
- return context
110
- }
111
-
112
- async function newContext<T>(updates: ContextMap, task: () => T) {
113
- guardMigration()
114
-
115
- // see if there already is a context setup
116
- let context: ContextMap = updateContext(updates)
117
- return Context.run(context, task)
118
- }
119
-
120
- export async function doInAutomationContext<T>(params: {
121
- appId: string
122
- automationId: string
123
- task: () => T
124
- }): Promise<T> {
125
- await ensureSnippetContext()
126
- return newContext(
127
- {
128
- tenantId: getTenantIDFromAppID(params.appId),
129
- appId: params.appId,
130
- automationId: params.automationId,
131
- },
132
- params.task
133
- )
134
- }
135
-
136
- export async function doInContext(appId: string, task: any): Promise<any> {
137
- const tenantId = getTenantIDFromAppID(appId)
138
- return newContext(
139
- {
140
- tenantId,
141
- appId,
142
- },
143
- task
144
- )
145
- }
146
-
147
- export async function doInTenant<T>(
148
- tenantId: string | undefined,
149
- task: () => T
150
- ): Promise<T> {
151
- // make sure default always selected in single tenancy
152
- if (!env.MULTI_TENANCY) {
153
- tenantId = tenantId || DEFAULT_TENANT_ID
154
- }
155
-
156
- const updates = tenantId ? { tenantId } : {}
157
- return newContext(updates, task)
158
- }
159
-
160
- export async function doInAppContext<T>(
161
- appId: string,
162
- task: () => T
163
- ): Promise<T> {
164
- return _doInAppContext(appId, task)
165
- }
166
-
167
- async function _doInAppContext<T>(
168
- appId: string,
169
- task: () => T,
170
- extraContextSettings?: ContextMap
171
- ): Promise<T> {
172
- if (!appId) {
173
- throw new Error("appId is required")
174
- }
175
-
176
- const tenantId = getTenantIDFromAppID(appId)
177
- const updates: ContextMap = { appId, ...extraContextSettings }
178
- if (tenantId) {
179
- updates.tenantId = tenantId
180
- }
181
-
182
- return newContext(updates, task)
183
- }
184
-
185
- export async function doInIdentityContext<T>(
186
- identity: IdentityContext,
187
- task: () => T
188
- ): Promise<T> {
189
- if (!identity) {
190
- throw new Error("identity is required")
191
- }
192
-
193
- const context: ContextMap = {
194
- identity,
195
- }
196
- if (identity.tenantId) {
197
- context.tenantId = identity.tenantId
198
- }
199
- return newContext(context, task)
200
- }
201
-
202
- function guardMigration() {
203
- const context = Context.get()
204
- if (context?.isMigrating) {
205
- throw new Error(
206
- "The context cannot be changed, a migration is currently running"
207
- )
208
- }
209
- }
210
-
211
- export async function doInAppMigrationContext<T>(
212
- appId: string,
213
- task: () => T
214
- ): Promise<T> {
215
- return _doInAppContext(appId, task, {
216
- isMigrating: true,
217
- })
218
- }
219
-
220
- export function getIdentity(): IdentityContext | undefined {
221
- try {
222
- const context = Context.get()
223
- return context?.identity
224
- } catch (e) {
225
- // do nothing - identity is not in context
226
- }
227
- }
228
-
229
- export function getTenantId(): string {
230
- if (!isMultiTenant()) {
231
- return DEFAULT_TENANT_ID
232
- }
233
- const context = Context.get()
234
- const tenantId = context?.tenantId
235
- if (!tenantId) {
236
- throw new Error("Tenant id not found")
237
- }
238
- return tenantId
239
- }
240
-
241
- export function getAutomationId(): string | undefined {
242
- const context = Context.get()
243
- return context?.automationId
244
- }
245
-
246
- export function getAppId(): string | undefined {
247
- const context = Context.get()
248
- const foundId = context?.appId
249
- if (!foundId && env.isTest() && TEST_APP_ID) {
250
- return TEST_APP_ID
251
- } else {
252
- return foundId
253
- }
254
- }
255
-
256
- export function getIP(): string | undefined {
257
- const context = Context.get()
258
- return context?.ip
259
- }
260
-
261
- export const getProdAppId = () => {
262
- const appId = getAppId()
263
- if (!appId) {
264
- throw new Error("Could not get appId")
265
- }
266
- return conversions.getProdAppID(appId)
267
- }
268
-
269
- export function doInEnvironmentContext(
270
- values: Record<string, string>,
271
- task: any
272
- ) {
273
- if (!values) {
274
- throw new Error("Must supply environment variables.")
275
- }
276
- const updates = {
277
- environmentVariables: values,
278
- }
279
- return newContext(updates, task)
280
- }
281
-
282
- export function doInScimContext(task: any) {
283
- const updates: ContextMap = {
284
- isScim: true,
285
- }
286
- return newContext(updates, task)
287
- }
288
-
289
- export function doInIPContext(ip: string, task: any) {
290
- return newContext({ ip }, task)
291
- }
292
-
293
- export async function ensureSnippetContext(enabled = !env.isTest()) {
294
- const ctx = getCurrentContext()
295
-
296
- // If we've already added snippets to context, continue
297
- if (!ctx || ctx.snippets) {
298
- return
299
- }
300
-
301
- // Otherwise get snippets for this app and update context
302
- let snippets: Snippet[] | undefined
303
- const db = getAppDB()
304
- if (db && enabled) {
305
- const app = await db.get<App>(DocumentType.APP_METADATA)
306
- snippets = app.snippets
307
- }
308
-
309
- // Always set snippets to a non-null value so that we can tell we've attempted
310
- // to load snippets
311
- ctx.snippets = snippets || []
312
- }
313
-
314
- export function getEnvironmentVariables() {
315
- const context = Context.get()
316
- if (!context.environmentVariables) {
317
- return null
318
- } else {
319
- return context.environmentVariables
320
- }
321
- }
322
-
323
- export function getGlobalDB(): Database {
324
- const context = Context.get()
325
- if (!context || (env.MULTI_TENANCY && !context.tenantId)) {
326
- throw new Error("Global DB not found")
327
- }
328
- return getDB(baseGlobalDBName(context?.tenantId))
329
- }
330
-
331
- export function getAuditLogsDB(): Database {
332
- if (!getTenantId()) {
333
- throw new Error("No tenant ID found - cannot open audit log DB")
334
- }
335
- return getDB(getAuditLogDBName())
336
- }
337
-
338
- /**
339
- * Gets the app database based on whatever the request
340
- * contained, dev or prod.
341
- */
342
- export function getAppDB(opts?: any): Database {
343
- const appId = getAppId()
344
- if (!appId) {
345
- throw new Error("Unable to retrieve app DB - no app ID.")
346
- }
347
- return getDB(appId, opts)
348
- }
349
-
350
- /**
351
- * This specifically gets the prod app ID, if the request
352
- * contained a development app ID, this will get the prod one.
353
- */
354
- export function getProdAppDB(opts?: any): Database {
355
- const appId = getAppId()
356
- if (!appId) {
357
- throw new Error("Unable to retrieve prod DB - no app ID.")
358
- }
359
- return getDB(conversions.getProdAppID(appId), opts)
360
- }
361
-
362
- /**
363
- * This specifically gets the dev app ID, if the request
364
- * contained a prod app ID, this will get the dev one.
365
- */
366
- export function getDevAppDB(opts?: any): Database {
367
- const appId = getAppId()
368
- if (!appId) {
369
- throw new Error("Unable to retrieve dev DB - no app ID.")
370
- }
371
- return getDB(conversions.getDevelopmentAppID(appId), opts)
372
- }
373
-
374
- export function isScim(): boolean {
375
- const context = Context.get()
376
- const scimCall = context?.isScim
377
- return !!scimCall
378
- }
379
-
380
- export function getCurrentContext(): ContextMap | undefined {
381
- try {
382
- return Context.get()
383
- } catch (e) {
384
- return undefined
385
- }
386
- }
387
-
388
- export function getFeatureFlags<T extends Record<string, any>>(
389
- key: string
390
- ): T | undefined {
391
- const context = getCurrentContext()
392
- if (!context) {
393
- return undefined
394
- }
395
- return context.featureFlagCache?.[key] as T
396
- }
397
-
398
- export function setFeatureFlags(key: string, value: Record<string, any>) {
399
- const context = getCurrentContext()
400
- if (!context) {
401
- return
402
- }
403
- context.featureFlagCache ??= {}
404
- context.featureFlagCache[key] = value
405
- }
406
-
407
- export function getTableForView(viewId: string): Table | undefined {
408
- const context = getCurrentContext()
409
- if (!context) {
410
- return
411
- }
412
- return context.viewToTableCache?.[viewId]
413
- }
414
-
415
- export function setTableForView(viewId: string, table: Table) {
416
- const context = getCurrentContext()
417
- if (!context) {
418
- return
419
- }
420
- context.viewToTableCache ??= {}
421
- context.viewToTableCache[viewId] = table
422
- }
@@ -1,255 +0,0 @@
1
- import { testEnv } from "../../../tests/extra"
2
- import * as context from "../"
3
- import { DEFAULT_TENANT_ID } from "../../constants"
4
- import { structures } from "../../../tests"
5
- import * as db from "../../db"
6
- import Context from "../Context"
7
- import { ContextMap } from "../types"
8
- import { IdentityType } from "@budibase/types"
9
-
10
- describe("context", () => {
11
- describe("doInTenant", () => {
12
- describe("single-tenancy", () => {
13
- beforeAll(() => {
14
- testEnv.singleTenant()
15
- })
16
-
17
- it("defaults to the default tenant", () => {
18
- const tenantId = context.getTenantId()
19
- expect(tenantId).toBe(DEFAULT_TENANT_ID)
20
- })
21
-
22
- it("defaults to the default tenant db", async () => {
23
- await context.doInTenant(DEFAULT_TENANT_ID, () => {
24
- const db = context.getGlobalDB()
25
- expect(db.name).toBe("global-db")
26
- })
27
- })
28
- })
29
-
30
- describe("multi-tenancy", () => {
31
- beforeAll(() => {
32
- testEnv.multiTenant()
33
- })
34
-
35
- it("fails when no tenant id is set", () => {
36
- const test = () => {
37
- let error: any
38
- try {
39
- context.getTenantId()
40
- } catch (e) {
41
- error = e
42
- }
43
- expect(error.message).toBe("Tenant id not found")
44
- }
45
-
46
- // test under no tenancy
47
- test()
48
-
49
- // test after tenancy has been accessed to ensure cleanup
50
- context.doInTenant("test", () => {})
51
- test()
52
- })
53
-
54
- it("fails when no tenant db is set", () => {
55
- const test = () => {
56
- let error: any
57
- try {
58
- context.getGlobalDB()
59
- } catch (e) {
60
- error = e
61
- }
62
- expect(error.message).toBe("Global DB not found")
63
- }
64
-
65
- // test under no tenancy
66
- test()
67
-
68
- // test after tenancy has been accessed to ensure cleanup
69
- context.doInTenant("test", () => {})
70
- test()
71
- })
72
-
73
- it("sets tenant id", () => {
74
- context.doInTenant("test", () => {
75
- const tenantId = context.getTenantId()
76
- expect(tenantId).toBe("test")
77
- })
78
- })
79
-
80
- it("initialises the tenant db", async () => {
81
- await context.doInTenant("test", () => {
82
- const db = context.getGlobalDB()
83
- expect(db.name).toBe("test_global-db")
84
- })
85
- })
86
-
87
- it("sets the tenant id when nested with same tenant id", async () => {
88
- await context.doInTenant("test", async () => {
89
- const tenantId = context.getTenantId()
90
- expect(tenantId).toBe("test")
91
-
92
- await context.doInTenant("test", async () => {
93
- const tenantId = context.getTenantId()
94
- expect(tenantId).toBe("test")
95
-
96
- await context.doInTenant("test", () => {
97
- const tenantId = context.getTenantId()
98
- expect(tenantId).toBe("test")
99
- })
100
- })
101
- })
102
- })
103
-
104
- it("initialises the tenant db when nested with same tenant id", async () => {
105
- await context.doInTenant("test", async () => {
106
- const db = context.getGlobalDB()
107
- expect(db.name).toBe("test_global-db")
108
-
109
- await context.doInTenant("test", async () => {
110
- const db = context.getGlobalDB()
111
- expect(db.name).toBe("test_global-db")
112
-
113
- await context.doInTenant("test", () => {
114
- const db = context.getGlobalDB()
115
- expect(db.name).toBe("test_global-db")
116
- })
117
- })
118
- })
119
- })
120
-
121
- it("sets different tenant id inside another context", () => {
122
- context.doInTenant("test", () => {
123
- const tenantId = context.getTenantId()
124
- expect(tenantId).toBe("test")
125
-
126
- context.doInTenant("nested", () => {
127
- const tenantId = context.getTenantId()
128
- expect(tenantId).toBe("nested")
129
-
130
- context.doInTenant("double-nested", () => {
131
- const tenantId = context.getTenantId()
132
- expect(tenantId).toBe("double-nested")
133
- })
134
- })
135
- })
136
- })
137
- })
138
- })
139
-
140
- describe("doInScimContext", () => {
141
- it("returns true when set", () => {
142
- context.doInScimContext(() => {
143
- const isScim = context.isScim()
144
- expect(isScim).toBe(true)
145
- })
146
- })
147
- it("returns false when not set", () => {
148
- const isScim = context.isScim()
149
- expect(isScim).toBe(false)
150
- })
151
- })
152
-
153
- describe("doInAppMigrationContext", () => {
154
- it("the context is set correctly", async () => {
155
- const appId = db.generateAppID()
156
-
157
- await context.doInAppMigrationContext(appId, () => {
158
- const context = Context.get()
159
-
160
- const expected: ContextMap = {
161
- appId,
162
- isMigrating: true,
163
- }
164
- expect(context).toEqual(expected)
165
- })
166
- })
167
-
168
- it("the context is set correctly when running in a tenant id", async () => {
169
- const tenantId = structures.tenant.id()
170
- const appId = db.generateAppID(tenantId)
171
-
172
- await context.doInAppMigrationContext(appId, () => {
173
- const context = Context.get()
174
-
175
- const expected: ContextMap = {
176
- appId,
177
- isMigrating: true,
178
- tenantId,
179
- }
180
- expect(context).toEqual(expected)
181
- })
182
- })
183
-
184
- it("the context is not modified outside the delegate", async () => {
185
- const appId = db.generateAppID()
186
-
187
- expect(Context.get()).toBeUndefined()
188
-
189
- await context.doInAppMigrationContext(appId, () => {
190
- const context = Context.get()
191
-
192
- const expected: ContextMap = {
193
- appId,
194
- isMigrating: true,
195
- }
196
- expect(context).toEqual(expected)
197
- })
198
-
199
- expect(Context.get()).toBeUndefined()
200
- })
201
-
202
- it.each([
203
- [
204
- "doInAppMigrationContext",
205
- () => context.doInAppMigrationContext(db.generateAppID(), () => {}),
206
- ],
207
- [
208
- "doInAppContext",
209
- () => context.doInAppContext(db.generateAppID(), () => {}),
210
- ],
211
- [
212
- "doInAutomationContext",
213
- () =>
214
- context.doInAutomationContext({
215
- appId: db.generateAppID(),
216
- automationId: structures.generator.guid(),
217
- task: () => {},
218
- }),
219
- ],
220
- ["doInContext", () => context.doInContext(db.generateAppID(), () => {})],
221
- [
222
- "doInEnvironmentContext",
223
- () => context.doInEnvironmentContext({}, () => {}),
224
- ],
225
- [
226
- "doInIdentityContext",
227
- () =>
228
- context.doInIdentityContext(
229
- {
230
- account: undefined,
231
- type: IdentityType.USER,
232
- _id: structures.users.user()._id!,
233
- },
234
- () => {}
235
- ),
236
- ],
237
- ["doInScimContext", () => context.doInScimContext(() => {})],
238
- [
239
- "doInTenant",
240
- () => context.doInTenant(structures.tenant.id(), () => {}),
241
- ],
242
- ])(
243
- "a nested context.%s function cannot run",
244
- async (_, otherContextCall: () => Promise<void>) => {
245
- await expect(
246
- context.doInAppMigrationContext(db.generateAppID(), async () => {
247
- await otherContextCall()
248
- })
249
- ).rejects.toThrow(
250
- "The context cannot be changed, a migration is currently running"
251
- )
252
- }
253
- )
254
- })
255
- })
@@ -1,26 +0,0 @@
1
- import { IdentityContext, Snippet, Table, VM } from "@budibase/types"
2
- import { OAuth2Client } from "google-auth-library"
3
- import { GoogleSpreadsheet } from "google-spreadsheet"
4
-
5
- // keep this out of Budibase types, don't want to expose context info
6
- export type ContextMap = {
7
- tenantId?: string
8
- appId?: string
9
- identity?: IdentityContext
10
- environmentVariables?: Record<string, string>
11
- isScim?: boolean
12
- ip?: string
13
- automationId?: string
14
- isMigrating?: boolean
15
- vm?: VM
16
- cleanup?: (() => void | Promise<void>)[]
17
- snippets?: Snippet[]
18
- googleSheets?: {
19
- oauthClient: OAuth2Client
20
- clients: Record<string, GoogleSpreadsheet>
21
- }
22
- featureFlagCache?: {
23
- [key: string]: Record<string, any>
24
- }
25
- viewToTableCache?: Record<string, Table>
26
- }