@budibase/worker 2.0.39 → 2.0.40-alpha.1

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "2.0.39",
4
+ "version": "2.0.40-alpha.1",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -36,10 +36,10 @@
36
36
  "author": "Budibase",
37
37
  "license": "GPL-3.0",
38
38
  "dependencies": {
39
- "@budibase/backend-core": "^2.0.39",
40
- "@budibase/pro": "2.0.38",
41
- "@budibase/string-templates": "^2.0.39",
42
- "@budibase/types": "^2.0.39",
39
+ "@budibase/backend-core": "2.0.40-alpha.1",
40
+ "@budibase/pro": "2.0.40-alpha.0",
41
+ "@budibase/string-templates": "2.0.40-alpha.1",
42
+ "@budibase/types": "2.0.40-alpha.1",
43
43
  "@koa/router": "8.0.8",
44
44
  "@sentry/node": "6.17.7",
45
45
  "@techpass/passport-openidconnect": "0.3.2",
@@ -72,9 +72,9 @@
72
72
  "devDependencies": {
73
73
  "@types/jest": "26.0.23",
74
74
  "@types/koa": "2.13.4",
75
- "@types/koa-router": "7.4.4",
76
75
  "@types/koa__router": "8.0.11",
77
76
  "@types/node": "14.18.20",
77
+ "@types/pouchdb": "6.4.0",
78
78
  "@types/uuid": "8.3.4",
79
79
  "@typescript-eslint/parser": "5.12.0",
80
80
  "copyfiles": "2.4.1",
@@ -104,5 +104,5 @@
104
104
  "./scripts/jestSetup.js"
105
105
  ]
106
106
  },
107
- "gitHead": "dce0d52b0ad22c3214c348154a7090f872a82ecf"
107
+ "gitHead": "d7ab0afac13397f1dcc34fe581a270287c17c177"
108
108
  }
@@ -7,6 +7,7 @@ import {
7
7
  CloudAccount,
8
8
  InviteUserRequest,
9
9
  InviteUsersRequest,
10
+ SearchUsersRequest,
10
11
  User,
11
12
  } from "@budibase/types"
12
13
  import {
@@ -144,7 +145,8 @@ export const destroy = async (ctx: any) => {
144
145
  }
145
146
 
146
147
  export const search = async (ctx: any) => {
147
- const paginated = await sdk.users.paginatedUsers(ctx.request.body)
148
+ const body = ctx.request.body as SearchUsersRequest
149
+ const paginated = await sdk.users.paginatedUsers(body)
148
150
  // user hashed password shouldn't ever be returned
149
151
  for (let user of paginated.data) {
150
152
  if (user) {
@@ -0,0 +1,13 @@
1
+ import env from "../../../environment"
2
+ import { BBContext } from "@budibase/types"
3
+ import { cache } from "@budibase/backend-core"
4
+
5
+ export async function systemRestored(ctx: BBContext) {
6
+ if (!env.SELF_HOSTED) {
7
+ ctx.throw(405, "This operation is not allowed in cloud.")
8
+ }
9
+ await cache.bustCache(cache.CacheKeys.CHECKLIST)
10
+ ctx.body = {
11
+ message: "System prepared after restore.",
12
+ }
13
+ }
@@ -0,0 +1,66 @@
1
+ const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
2
+ const { getTenantId } = require("@budibase/backend-core/tenancy")
3
+ const { deleteTenant } = require("@budibase/backend-core/deprovision")
4
+ import { quotas } from "@budibase/pro"
5
+
6
+ export const exists = async (ctx: any) => {
7
+ const tenantId = ctx.request.params
8
+ ctx.body = {
9
+ exists: await doWithDB(
10
+ StaticDatabases.PLATFORM_INFO.name,
11
+ async (db: any) => {
12
+ let exists = false
13
+ try {
14
+ const tenantsDoc = await db.get(
15
+ StaticDatabases.PLATFORM_INFO.docs.tenants
16
+ )
17
+ if (tenantsDoc) {
18
+ exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
19
+ }
20
+ } catch (err) {
21
+ // if error it doesn't exist
22
+ }
23
+ return exists
24
+ }
25
+ ),
26
+ }
27
+ }
28
+
29
+ export const fetch = async (ctx: any) => {
30
+ ctx.body = await doWithDB(
31
+ StaticDatabases.PLATFORM_INFO.name,
32
+ async (db: any) => {
33
+ let tenants = []
34
+ try {
35
+ const tenantsDoc = await db.get(
36
+ StaticDatabases.PLATFORM_INFO.docs.tenants
37
+ )
38
+ if (tenantsDoc) {
39
+ tenants = tenantsDoc.tenantIds
40
+ }
41
+ } catch (err) {
42
+ // if error it doesn't exist
43
+ }
44
+ return tenants
45
+ }
46
+ )
47
+ }
48
+
49
+ const _delete = async (ctx: any) => {
50
+ const tenantId = getTenantId()
51
+
52
+ if (ctx.params.tenantId !== tenantId) {
53
+ ctx.throw(403, "Unauthorized")
54
+ }
55
+
56
+ try {
57
+ await deleteTenant(tenantId)
58
+ await quotas.bustCache()
59
+ ctx.status = 204
60
+ } catch (err) {
61
+ ctx.log.error(err)
62
+ throw err
63
+ }
64
+ }
65
+
66
+ export { _delete as delete }
package/src/api/index.ts CHANGED
@@ -55,6 +55,10 @@ const PUBLIC_ENDPOINTS = [
55
55
  route: "/api/global/users/tenant/:id",
56
56
  method: "GET",
57
57
  },
58
+ {
59
+ route: "/api/system/restored",
60
+ method: "POST",
61
+ },
58
62
  ]
59
63
 
60
64
  const NO_TENANCY_ENDPOINTS = [
@@ -13,6 +13,7 @@ import selfRoutes from "./global/self"
13
13
  import licenseRoutes from "./global/license"
14
14
  import migrationRoutes from "./system/migrations"
15
15
  import accountRoutes from "./system/accounts"
16
+ import restoreRoutes from "./system/restore"
16
17
 
17
18
  let userGroupRoutes = api.groups
18
19
  export const routes = [
@@ -31,4 +32,5 @@ export const routes = [
31
32
  userGroupRoutes,
32
33
  migrationRoutes,
33
34
  accountRoutes,
35
+ restoreRoutes,
34
36
  ]
@@ -0,0 +1,8 @@
1
+ import * as controller from "../../controllers/system/restore"
2
+ import Router from "@koa/router"
3
+
4
+ const router = new Router()
5
+
6
+ router.post("/api/system/restored", controller.systemRestored)
7
+
8
+ export = router
@@ -1,5 +1,11 @@
1
- import { migrations, redis } from "@budibase/backend-core"
2
- import { Migration, MigrationOptions, MigrationName } from "@budibase/types"
1
+ import { migrations, locks } from "@budibase/backend-core"
2
+ import {
3
+ Migration,
4
+ MigrationOptions,
5
+ MigrationName,
6
+ LockType,
7
+ LockName,
8
+ } from "@budibase/types"
3
9
  import env from "../environment"
4
10
 
5
11
  // migration functions
@@ -42,33 +48,15 @@ export const migrate = async (options?: MigrationOptions) => {
42
48
  }
43
49
 
44
50
  const migrateWithLock = async (options?: MigrationOptions) => {
45
- // get a new lock client
46
- const redlock = await redis.clients.getMigrationsRedlock()
47
- // lock for 15 minutes
48
- const ttl = 1000 * 60 * 15
49
-
50
- let migrationLock
51
-
52
- // acquire lock
53
- try {
54
- migrationLock = await redlock.lock("migrations", ttl)
55
- } catch (e: any) {
56
- if (e.name === "LockError") {
57
- return
58
- } else {
59
- throw e
51
+ await locks.doWithLock(
52
+ {
53
+ type: LockType.TRY_ONCE,
54
+ name: LockName.MIGRATIONS,
55
+ ttl: 1000 * 60 * 15, // auto expire the migration lock after 15 minutes
56
+ systemLock: true,
57
+ },
58
+ async () => {
59
+ await migrations.runMigrations(MIGRATIONS, options)
60
60
  }
61
- }
62
-
63
- // run migrations
64
- try {
65
- await migrations.runMigrations(MIGRATIONS, options)
66
- } finally {
67
- // release lock
68
- try {
69
- await migrationLock.unlock()
70
- } catch (e) {
71
- console.error("unable to release migration lock")
72
- }
73
- }
61
+ )
74
62
  }
@@ -1,7 +1,6 @@
1
1
  import env from "../../environment"
2
2
  import { events, accounts, tenancy } from "@budibase/backend-core"
3
3
  import { User, UserRoles, CloudAccount } from "@budibase/types"
4
- import { users as pro } from "@budibase/pro"
5
4
 
6
5
  export const handleDeleteEvents = async (user: any) => {
7
6
  await events.user.deleted(user)
@@ -27,6 +27,7 @@ import {
27
27
  MigrationType,
28
28
  PlatformUserByEmail,
29
29
  RowResponse,
30
+ SearchUsersRequest,
30
31
  User,
31
32
  } from "@budibase/types"
32
33
  import { sendEmail } from "../../utilities/email"
@@ -56,7 +57,8 @@ export const paginatedUsers = async ({
56
57
  page,
57
58
  email,
58
59
  appId,
59
- }: { page?: string; email?: string; appId?: string } = {}) => {
60
+ userIds,
61
+ }: SearchUsersRequest = {}) => {
60
62
  const db = tenancy.getGlobalDB()
61
63
  // get one extra document, to have the next page
62
64
  const opts: any = {
@@ -94,16 +96,7 @@ export const paginatedUsers = async ({
94
96
  */
95
97
  export const getUser = async (userId: string) => {
96
98
  const db = tenancy.getGlobalDB()
97
- let user
98
- try {
99
- user = await db.get(userId)
100
- } catch (err: any) {
101
- // no user found, just return nothing
102
- if (err.status === 404) {
103
- return {}
104
- }
105
- throw err
106
- }
99
+ let user = await db.get(userId)
107
100
  if (user) {
108
101
  delete user.password
109
102
  }
@@ -1,58 +0,0 @@
1
- const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
2
- const { getTenantId } = require("@budibase/backend-core/tenancy")
3
- const { deleteTenant } = require("@budibase/backend-core/deprovision")
4
- const { quotas } = require("@budibase/pro")
5
-
6
- exports.exists = async ctx => {
7
- const tenantId = ctx.request.params
8
- ctx.body = {
9
- exists: await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
10
- let exists = false
11
- try {
12
- const tenantsDoc = await db.get(
13
- StaticDatabases.PLATFORM_INFO.docs.tenants
14
- )
15
- if (tenantsDoc) {
16
- exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
17
- }
18
- } catch (err) {
19
- // if error it doesn't exist
20
- }
21
- return exists
22
- }),
23
- }
24
- }
25
-
26
- exports.fetch = async ctx => {
27
- ctx.body = await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
28
- let tenants = []
29
- try {
30
- const tenantsDoc = await db.get(
31
- StaticDatabases.PLATFORM_INFO.docs.tenants
32
- )
33
- if (tenantsDoc) {
34
- tenants = tenantsDoc.tenantIds
35
- }
36
- } catch (err) {
37
- // if error it doesn't exist
38
- }
39
- return tenants
40
- })
41
- }
42
-
43
- exports.delete = async ctx => {
44
- const tenantId = getTenantId()
45
-
46
- if (ctx.params.tenantId !== tenantId) {
47
- ctx.throw(403, "Unauthorized")
48
- }
49
-
50
- try {
51
- await deleteTenant(tenantId)
52
- await quotas.bustCache()
53
- ctx.status = 204
54
- } catch (err) {
55
- ctx.log.error(err)
56
- throw err
57
- }
58
- }