@budibase/worker 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.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@budibase/worker",
3
3
  "email": "hi@budibase.com",
4
- "version": "1.0.113",
4
+ "version": "1.0.116",
5
5
  "description": "Budibase background service",
6
6
  "main": "src/index.ts",
7
7
  "repository": {
@@ -31,8 +31,8 @@
31
31
  "author": "Budibase",
32
32
  "license": "GPL-3.0",
33
33
  "dependencies": {
34
- "@budibase/backend-core": "^1.0.113",
35
- "@budibase/string-templates": "^1.0.113",
34
+ "@budibase/backend-core": "^1.0.116",
35
+ "@budibase/string-templates": "^1.0.116",
36
36
  "@koa/router": "^8.0.0",
37
37
  "@sentry/node": "^6.0.0",
38
38
  "@techpass/passport-openidconnect": "^0.3.0",
@@ -55,7 +55,7 @@
55
55
  "passport-jwt": "^4.0.0",
56
56
  "passport-local": "^1.0.0",
57
57
  "pino-pretty": "^4.0.0",
58
- "pouchdb": "^7.2.1",
58
+ "pouchdb": "7.3.0",
59
59
  "pouchdb-all-dbs": "^1.0.2",
60
60
  "server-destroy": "^1.0.1"
61
61
  },
@@ -84,5 +84,5 @@
84
84
  "./scripts/jestSetup.js"
85
85
  ]
86
86
  },
87
- "gitHead": "20852c30cca5054eb7ef8bacdb91343a56be0d30"
87
+ "gitHead": "70b8daeff623c77a66abb6ddcc994bb69edb062b"
88
88
  }
@@ -15,6 +15,7 @@ const { invalidateSessions } = require("@budibase/backend-core/sessions")
15
15
  const accounts = require("@budibase/backend-core/accounts")
16
16
  const {
17
17
  getGlobalDB,
18
+ doWithGlobalDB,
18
19
  getTenantId,
19
20
  getTenantUser,
20
21
  doesTenantExist,
@@ -51,26 +52,27 @@ exports.adminUser = async ctx => {
51
52
  ctx.throw(403, "Organisation already exists.")
52
53
  }
53
54
 
54
- const db = getGlobalDB(tenantId)
55
- const response = await db.allDocs(
56
- getGlobalUserParams(null, {
57
- include_docs: true,
58
- })
59
- )
60
-
61
- // write usage quotas for cloud
62
- if (!env.SELF_HOSTED) {
63
- // could be a scenario where it exists, make sure its clean
64
- try {
65
- const usageQuota = await db.get(StaticDatabases.GLOBAL.docs.usageQuota)
66
- if (usageQuota) {
67
- await db.remove(usageQuota._id, usageQuota._rev)
55
+ const response = await doWithGlobalDB(tenantId, async db => {
56
+ const response = await db.allDocs(
57
+ getGlobalUserParams(null, {
58
+ include_docs: true,
59
+ })
60
+ )
61
+ // write usage quotas for cloud
62
+ if (!env.SELF_HOSTED) {
63
+ // could be a scenario where it exists, make sure its clean
64
+ try {
65
+ const usageQuota = await db.get(StaticDatabases.GLOBAL.docs.usageQuota)
66
+ if (usageQuota) {
67
+ await db.remove(usageQuota._id, usageQuota._rev)
68
+ }
69
+ } catch (err) {
70
+ // don't worry about errors
68
71
  }
69
- } catch (err) {
70
- // don't worry about errors
72
+ await db.put(generateNewUsageQuotaDoc())
71
73
  }
72
- await db.put(generateNewUsageQuotaDoc())
73
- }
74
+ return response
75
+ })
74
76
 
75
77
  if (response.rows.some(row => row.doc.admin)) {
76
78
  ctx.throw(
@@ -1,37 +1,42 @@
1
- const CouchDB = require("../../../db")
2
- const { StaticDatabases } = require("@budibase/backend-core/db")
1
+ const { StaticDatabases, doWithDB } = require("@budibase/backend-core/db")
3
2
  const { getTenantId } = require("@budibase/backend-core/tenancy")
4
3
  const { deleteTenant } = require("@budibase/backend-core/deprovision")
5
4
 
6
5
  exports.exists = async ctx => {
7
6
  const tenantId = ctx.request.params
8
- const db = new CouchDB(StaticDatabases.PLATFORM_INFO.name)
9
- let exists = false
10
- try {
11
- const tenantsDoc = await db.get(StaticDatabases.PLATFORM_INFO.docs.tenants)
12
- if (tenantsDoc) {
13
- exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
14
- }
15
- } catch (err) {
16
- // if error it doesn't exist
17
- }
18
7
  ctx.body = {
19
- exists,
8
+ exists: await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
9
+ let exists = false
10
+ try {
11
+ const tenantsDoc = await db.get(
12
+ StaticDatabases.PLATFORM_INFO.docs.tenants
13
+ )
14
+ if (tenantsDoc) {
15
+ exists = tenantsDoc.tenantIds.indexOf(tenantId) !== -1
16
+ }
17
+ } catch (err) {
18
+ // if error it doesn't exist
19
+ }
20
+ return exists
21
+ }),
20
22
  }
21
23
  }
22
24
 
23
25
  exports.fetch = async ctx => {
24
- const db = new CouchDB(StaticDatabases.PLATFORM_INFO.name)
25
- let tenants = []
26
- try {
27
- const tenantsDoc = await db.get(StaticDatabases.PLATFORM_INFO.docs.tenants)
28
- if (tenantsDoc) {
29
- tenants = tenantsDoc.tenantIds
26
+ ctx.body = await doWithDB(StaticDatabases.PLATFORM_INFO.name, async db => {
27
+ let tenants = []
28
+ try {
29
+ const tenantsDoc = await db.get(
30
+ StaticDatabases.PLATFORM_INFO.docs.tenants
31
+ )
32
+ if (tenantsDoc) {
33
+ tenants = tenantsDoc.tenantIds
34
+ }
35
+ } catch (err) {
36
+ // if error it doesn't exist
30
37
  }
31
- } catch (err) {
32
- // if error it doesn't exist
33
- }
34
- ctx.body = tenants
38
+ return tenants
39
+ })
35
40
  }
36
41
 
37
42
  exports.delete = async ctx => {
@@ -1,3 +1,4 @@
1
+ require("../../../../db").init()
1
2
  const env = require("../../../../environment")
2
3
  const controllers = require("./controllers")
3
4
  const supertest = require("supertest")
@@ -8,15 +9,12 @@ const { getGlobalUserByEmail } = require("@budibase/backend-core/utils")
8
9
  const { createASession } = require("@budibase/backend-core/sessions")
9
10
  const { newid } = require("@budibase/backend-core/src/hashing")
10
11
  const { TENANT_ID, CSRF_TOKEN } = require("./structures")
11
- const core = require("@budibase/backend-core")
12
- const CouchDB = require("../../../../db")
13
12
  const { doInTenant } = require("@budibase/backend-core/tenancy")
14
- core.init(CouchDB)
15
13
 
16
14
  class TestConfiguration {
17
15
  constructor(openServer = true) {
18
16
  if (openServer) {
19
- env.PORT = 4003
17
+ env.PORT = 4012
20
18
  this.server = require("../../../../index")
21
19
  // we need the request for logging in, involves cookies, hard to fake
22
20
  this.request = supertest(this.server)
package/src/db/index.js CHANGED
@@ -1,26 +1,11 @@
1
- const PouchDB = require("pouchdb")
2
- const allDbs = require("pouchdb-all-dbs")
1
+ const core = require("@budibase/backend-core")
3
2
  const env = require("../environment")
4
- const { getCouchUrl } = require("@budibase/backend-core/db")
5
3
 
6
- // level option is purely for testing (development)
7
- const COUCH_DB_URL = getCouchUrl() || "http://localhost:4005"
8
-
9
- let POUCH_DB_DEFAULTS = {
10
- prefix: COUCH_DB_URL,
11
- }
12
-
13
- if (env.isTest()) {
14
- PouchDB.plugin(require("pouchdb-adapter-memory"))
15
- POUCH_DB_DEFAULTS = {
16
- prefix: undefined,
17
- adapter: "memory",
4
+ exports.init = () => {
5
+ const dbConfig = {}
6
+ if (env.isTest()) {
7
+ dbConfig.inMemory = true
8
+ dbConfig.allDbs = true
18
9
  }
10
+ core.init({ db: dbConfig })
19
11
  }
20
-
21
- const Pouch = PouchDB.defaults(POUCH_DB_DEFAULTS)
22
-
23
- // have to still have pouch alldbs for testing
24
- allDbs(Pouch)
25
-
26
- module.exports = Pouch
package/src/index.ts CHANGED
@@ -4,8 +4,8 @@ import { Event } from "@sentry/types/dist/event"
4
4
  import Application from "koa"
5
5
 
6
6
  const env = require("./environment")
7
- const CouchDB = require("./db")
8
- require("@budibase/backend-core").init(CouchDB)
7
+ import db from "./db"
8
+ db.init()
9
9
  const Koa = require("koa")
10
10
  const destroyable = require("server-destroy")
11
11
  const koaBody = require("koa-body")