diff --git a/packages/auth/src/db/utils.js b/packages/auth/src/db/utils.js index 0e37ea03ee..a39166a53e 100644 --- a/packages/auth/src/db/utils.js +++ b/packages/auth/src/db/utils.js @@ -11,9 +11,13 @@ exports.ViewNames = { exports.StaticDatabases = { GLOBAL: { name: "global-db", + docs: { + apiKeys: "apikeys", + }, }, - DEPLOYMENTS: { - name: "deployments", + // contains information about tenancy and so on + PLATFORM_INFO: { + name: "global-info", }, } diff --git a/packages/server/src/api/controllers/apikeys.js b/packages/server/src/api/controllers/apikeys.js index 55422ee603..9c1acfbc2a 100644 --- a/packages/server/src/api/controllers/apikeys.js +++ b/packages/server/src/api/controllers/apikeys.js @@ -1,8 +1,32 @@ -const builderDB = require("../../db/builder") +const CouchDB = require("../../db") +const { StaticDatabases } = require("@budibase/auth/db") + +const GLOBAL_DB = StaticDatabases.GLOBAL.name +const KEYS_DOC = StaticDatabases.GLOBAL.docs.apiKeys + +async function getBuilderMainDoc() { + const db = new CouchDB(GLOBAL_DB) + try { + return await db.get(KEYS_DOC) + } catch (err) { + // doesn't exist yet, nothing to get + return { + _id: KEYS_DOC, + } + } +} + +async function setBuilderMainDoc(doc) { + // make sure to override the ID + doc._id = KEYS_DOC + const db = new CouchDB(GLOBAL_DB) + return db.put(doc) +} + exports.fetch = async function (ctx) { try { - const mainDoc = await builderDB.getBuilderMainDoc() + const mainDoc = await getBuilderMainDoc() ctx.body = mainDoc.apiKeys ? mainDoc.apiKeys : {} } catch (err) { /* istanbul ignore next */ @@ -15,12 +39,12 @@ exports.update = async function (ctx) { const value = ctx.request.body.value try { - const mainDoc = await builderDB.getBuilderMainDoc() + const mainDoc = await getBuilderMainDoc() if (mainDoc.apiKeys == null) { mainDoc.apiKeys = {} } mainDoc.apiKeys[key] = value - const resp = await builderDB.setBuilderMainDoc(mainDoc) + const resp = await setBuilderMainDoc(mainDoc) ctx.body = { _id: resp.id, _rev: resp.rev, diff --git a/packages/server/src/api/controllers/deploy/index.js b/packages/server/src/api/controllers/deploy/index.js index f5db81e6e8..4125f04438 100644 --- a/packages/server/src/api/controllers/deploy/index.js +++ b/packages/server/src/api/controllers/deploy/index.js @@ -1,6 +1,6 @@ -const PouchDB = require("../../../db") +const CouchDB = require("../../../db") const Deployment = require("./Deployment") -const { Replication, StaticDatabases } = require("@budibase/auth/db") +const { Replication } = require("@budibase/auth/db") const { DocumentTypes } = require("../../../db/utils") // the max time we can wait for an invalidation to complete before considering it failed @@ -31,11 +31,12 @@ async function checkAllDeployments(deployments) { async function storeDeploymentHistory(deployment) { const appId = deployment.getAppId() const deploymentJSON = deployment.getJSON() - const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) + const db = new CouchDB(appId) let deploymentDoc try { - deploymentDoc = await db.get(appId) + // theres only one deployment doc per app database + deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS) } catch (err) { deploymentDoc = { _id: appId, history: {} } } @@ -67,7 +68,7 @@ async function deployApp(deployment) { }) await replication.replicate() - const db = new PouchDB(productionAppId) + const db = new CouchDB(productionAppId) const appDoc = await db.get(DocumentTypes.APP_METADATA) appDoc.appId = productionAppId appDoc.instance._id = productionAppId @@ -98,8 +99,9 @@ async function deployApp(deployment) { exports.fetchDeployments = async function (ctx) { try { - const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) - const deploymentDoc = await db.get(ctx.appId) + const appId = ctx.appId + const db = new CouchDB(appId) + const deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS) const { updated, deployments } = await checkAllDeployments( deploymentDoc, ctx.user @@ -115,8 +117,9 @@ exports.fetchDeployments = async function (ctx) { exports.deploymentProgress = async function (ctx) { try { - const db = new PouchDB(StaticDatabases.DEPLOYMENTS.name) - const deploymentDoc = await db.get(ctx.appId) + const appId = ctx.appId + const db = new CouchDB(appId) + const deploymentDoc = await db.get(DocumentTypes.DEPLOYMENTS) ctx.body = deploymentDoc[ctx.params.deploymentId] } catch (err) { ctx.throw( diff --git a/packages/server/src/db/builder.js b/packages/server/src/db/builder.js deleted file mode 100644 index d2bbcd404b..0000000000 --- a/packages/server/src/db/builder.js +++ /dev/null @@ -1,38 +0,0 @@ -const CouchDB = require("./index") -const { StaticDatabases } = require("./utils") -const env = require("../environment") - -const SELF_HOST_ERR = "Unable to access builder DB/doc - not self hosted." -const BUILDER_DB = StaticDatabases.BUILDER - -/** - * This is the builder database, right now this is a single, static database - * that is present across the whole system and determines some core functionality - * for the builder (e.g. storage of API keys). This has been limited to self hosting - * as it doesn't make as much sense against the currently design Cloud system. - */ - -exports.getBuilderMainDoc = async () => { - if (!env.SELF_HOSTED) { - throw SELF_HOST_ERR - } - const db = new CouchDB(BUILDER_DB.name) - try { - return await db.get(BUILDER_DB.baseDoc) - } catch (err) { - // doesn't exist yet, nothing to get - return { - _id: BUILDER_DB.baseDoc, - } - } -} - -exports.setBuilderMainDoc = async doc => { - if (!env.SELF_HOSTED) { - throw SELF_HOST_ERR - } - // make sure to override the ID - doc._id = BUILDER_DB.baseDoc - const db = new CouchDB(BUILDER_DB.name) - return db.put(doc) -} diff --git a/packages/server/src/db/utils.js b/packages/server/src/db/utils.js index 74ddf87176..eb8c32bb5d 100644 --- a/packages/server/src/db/utils.js +++ b/packages/server/src/db/utils.js @@ -34,6 +34,7 @@ const DocumentTypes = { DATASOURCE: "datasource", DATASOURCE_PLUS: "datasource_plus", QUERY: "query", + DEPLOYMENTS: "deployments", } const ViewNames = { @@ -49,13 +50,7 @@ const SearchIndexes = { ROWS: "rows", } -exports.StaticDatabases = { - BUILDER: { - name: "builder-db", - baseDoc: "builder-doc", - }, - ...StaticDatabases, -} +exports.StaticDatabases = StaticDatabases const BudibaseInternalDB = { _id: "bb_internal",