Merge pull request #806 from Budibase/feature/remove-multitenancy

Remove multi-tenancy
This commit is contained in:
Michael Drury 2020-10-29 12:19:08 +00:00 committed by GitHub
commit adac862ccd
68 changed files with 521 additions and 948 deletions

View File

@ -137,10 +137,10 @@ const setPackage = (store, initial) => async pkg => {
...Object.values(unauth_screens), ...Object.values(unauth_screens),
] ]
initial.builtins = [getBuiltin("##builtin/screenslot")] initial.builtins = [getBuiltin("##builtin/screenslot")]
initial.appInstances = pkg.application.instances initial.appInstance = pkg.application.instance
initial.appId = pkg.application._id initial.appId = pkg.application._id
store.set(initial) store.set(initial)
await backendUiStore.actions.database.select(initial.appInstances[0]) await backendUiStore.actions.database.select(initial.appInstance)
return initial return initial
} }

View File

@ -7,15 +7,15 @@
import { last } from "lodash/fp" import { last } from "lodash/fp"
import FrontendNavigatePane from "components/userInterface/FrontendNavigatePane.svelte" import FrontendNavigatePane from "components/userInterface/FrontendNavigatePane.svelte"
$: instances = $store.appInstances $: instance = $store.appInstance
async function selectDatabase(database) { async function selectDatabase(database) {
backendUiStore.actions.database.select(database) backendUiStore.actions.database.select(database)
} }
onMount(async () => { onMount(async () => {
if ($store.appInstances.length > 0 && !$backendUiStore.database) { if ($store.appInstance && !$backendUiStore.database) {
await selectDatabase($store.appInstances[0]) await selectDatabase($store.appInstance)
} }
}) })

View File

@ -1,5 +1,6 @@
// THIS will create API Keys and App Ids input in a local Dynamo instance if it is running // THIS will create API Keys and App Ids input in a local Dynamo instance if it is running
const dynamoClient = require("../src/db/dynamoClient") const dynamoClient = require("../src/db/dynamoClient")
const env = require("../src/environment")
if (process.argv[2] == null || process.argv[3] == null) { if (process.argv[2] == null || process.argv[3] == null) {
console.error( console.error(
@ -11,8 +12,8 @@ if (process.argv[2] == null || process.argv[3] == null) {
const FAKE_STRING = "fakestring" const FAKE_STRING = "fakestring"
// set fake credentials for local dynamo to actually work // set fake credentials for local dynamo to actually work
process.env.AWS_ACCESS_KEY_ID = "KEY_ID" env._set("AWS_ACCESS_KEY_ID", "KEY_ID")
process.env.AWS_SECRET_ACCESS_KEY = "SECRET_KEY" env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY")
dynamoClient.init("http://localhost:8333") dynamoClient.init("http://localhost:8333")
async function run() { async function run() {

View File

@ -3,7 +3,7 @@ const { exportTemplateFromApp } = require("../src/utilities/templates")
const yargs = require("yargs") const yargs = require("yargs")
// Script to export a chosen budibase app into a package // Script to export a chosen budibase app into a package
// Usage: ./scripts/exportAppTemplate.js export --name=Funky --instanceId=someInstanceId --appId=appId // Usage: ./scripts/exportAppTemplate.js export --name=Funky --appId=someInstanceId --appId=appId
yargs yargs
.command( .command(
@ -15,11 +15,6 @@ yargs
alias: "n", alias: "n",
type: "string", type: "string",
}, },
instanceId: {
description: "The instanceId to dump the database for",
alias: "inst",
type: "string",
},
appId: { appId: {
description: "The appId of the application you want to export", description: "The appId of the application you want to export",
alias: "app", alias: "app",
@ -30,7 +25,6 @@ yargs
console.log("Exporting app..") console.log("Exporting app..")
const exportPath = await exportTemplateFromApp({ const exportPath = await exportTemplateFromApp({
templateName: args.name, templateName: args.name,
instanceId: args.instanceId,
appId: args.appId, appId: args.appId,
}) })
console.log(`Template ${args.name} exported to ${exportPath}`) console.log(`Template ${args.name} exported to ${exportPath}`)

View File

@ -1,7 +1,8 @@
const { tmpdir } = require("os") const { tmpdir } = require("os")
const env = require("../src/environment")
process.env.NODE_ENV = "jest" env._set("NODE_ENV", "jest")
process.env.JWT_SECRET = "test-jwtsecret" env._set("JWT_SECRET", "test-jwtsecret")
process.env.CLIENT_ID = "test-client-id" env._set("CLIENT_ID", "test-client-id")
process.env.BUDIBASE_DIR = tmpdir("budibase-unittests") env._set("BUDIBASE_DIR", tmpdir("budibase-unittests"))
process.env.LOG_LEVEL = "silent" env._set("LOG_LEVEL", "silent")

View File

@ -5,14 +5,7 @@
* e.g. node scripts/replicateApp Mike http://admin:password@127.0.0.1:5984 * e.g. node scripts/replicateApp Mike http://admin:password@127.0.0.1:5984
*/ */
const { resolve, join } = require("path")
const { homedir } = require("os")
const budibaseDir = join(homedir(), ".budibase")
process.env.BUDIBASE_DIR = budibaseDir
require("dotenv").config({ path: resolve(budibaseDir, ".env") })
const env = require("../src/environment")
const CouchDB = require("../src/db") const CouchDB = require("../src/db")
const clientDbName = require("../src/db/clientDb").name(env.CLIENT_ID)
const appName = process.argv[2].toLowerCase() const appName = process.argv[2].toLowerCase()
const remoteUrl = process.argv[3] const remoteUrl = process.argv[3]
@ -20,27 +13,26 @@ const remoteUrl = process.argv[3]
console.log(`Replicating from ${appName} to ${remoteUrl}/${appName}`) console.log(`Replicating from ${appName} to ${remoteUrl}/${appName}`)
const run = async () => { const run = async () => {
const clientDb = new CouchDB(clientDbName) const allDbs = await CouchDB.allDbs()
const appDbNames = allDbs.filter(dbName => dbName.startsWith("inst_app"))
const body = await clientDb.query("client/by_type", { let apps = []
include_docs: true, for (let dbName of appDbNames) {
key: ["app"], const db = new CouchDB(dbName)
}) apps.push(db.get(dbName))
}
const app = body.rows apps = await Promise.all(apps)
.map(r => r.doc) const app = apps.find(
.find(a => a.name == appName || a.name.toLowerCase() === appName) a => a.name === appName || a.name.toLowerCase() === appName
)
if (!app) { if (!app) {
console.log( console.log(
`Could not find app... apps: ${body.rows.map(r => r.doc.name).join(", ")}` `Could not find app... apps: ${apps.map(app => app.name).join(", ")}`
) )
return return
} }
const devInstance = app.instances.find(i => i.name === `dev-${env.CLIENT_ID}`) const instanceDb = new CouchDB(app._id)
const instanceDb = new CouchDB(devInstance._id)
const remoteDb = new CouchDB(`${remoteUrl}/${appName}`) const remoteDb = new CouchDB(`${remoteUrl}/${appName}`)
instanceDb.replicate instanceDb.replicate

View File

@ -11,7 +11,7 @@ const {
} = require("../../db/utils") } = require("../../db/utils")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const body = await db.allDocs( const body = await db.allDocs(
getAccessLevelParams(null, { getAccessLevelParams(null, {
include_docs: true, include_docs: true,
@ -23,12 +23,12 @@ exports.fetch = async function(ctx) {
{ {
_id: ADMIN_LEVEL_ID, _id: ADMIN_LEVEL_ID,
name: "Admin", name: "Admin",
permissions: await generateAdminPermissions(ctx.user.instanceId), permissions: await generateAdminPermissions(ctx.user.appId),
}, },
{ {
_id: POWERUSER_LEVEL_ID, _id: POWERUSER_LEVEL_ID,
name: "Power User", name: "Power User",
permissions: await generatePowerUserPermissions(ctx.user.instanceId), permissions: await generatePowerUserPermissions(ctx.user.appId),
}, },
] ]
@ -36,12 +36,12 @@ exports.fetch = async function(ctx) {
} }
exports.find = async function(ctx) { exports.find = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
ctx.body = await db.get(ctx.params.levelId) ctx.body = await db.get(ctx.params.levelId)
} }
exports.update = async function(ctx) { exports.update = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const level = await db.get(ctx.params.levelId) const level = await db.get(ctx.params.levelId)
level.name = ctx.body.name level.name = ctx.body.name
level.permissions = ctx.request.body.permissions level.permissions = ctx.request.body.permissions
@ -52,7 +52,7 @@ exports.update = async function(ctx) {
} }
exports.patch = async function(ctx) { exports.patch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const level = await db.get(ctx.params.levelId) const level = await db.get(ctx.params.levelId)
const { removedPermissions, addedPermissions, _rev } = ctx.request.body const { removedPermissions, addedPermissions, _rev } = ctx.request.body
@ -88,7 +88,7 @@ exports.patch = async function(ctx) {
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const level = { const level = {
name: ctx.request.body.name, name: ctx.request.body.name,
@ -105,7 +105,7 @@ exports.create = async function(ctx) {
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
await db.remove(ctx.params.levelId, ctx.params.rev) await db.remove(ctx.params.levelId, ctx.params.rev)
ctx.message = `Access Level ${ctx.params.id} deleted successfully` ctx.message = `Access Level ${ctx.params.id} deleted successfully`
ctx.status = 200 ctx.status = 200

View File

@ -1,3 +1,5 @@
const env = require("../../environment")
exports.isEnabled = async function(ctx) { exports.isEnabled = async function(ctx) {
ctx.body = JSON.stringify(process.env.ENABLE_ANALYTICS === "true") ctx.body = JSON.stringify(env.ENABLE_ANALYTICS === "true")
} }

View File

@ -2,21 +2,23 @@ const fs = require("fs")
const { join } = require("../../utilities/centralPath") const { join } = require("../../utilities/centralPath")
const readline = require("readline") const readline = require("readline")
const { budibaseAppsDir } = require("../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const env = require("../../environment")
const ENV_FILE_PATH = "/.env" const ENV_FILE_PATH = "/.env"
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
ctx.status = 200 ctx.status = 200
ctx.body = { ctx.body = {
budibase: process.env.BUDIBASE_API_KEY, budibase: env.BUDIBASE_API_KEY,
userId: process.env.USERID_API_KEY, userId: env.USERID_API_KEY,
} }
} }
exports.update = async function(ctx) { exports.update = async function(ctx) {
const key = `${ctx.params.key.toUpperCase()}_API_KEY` const key = `${ctx.params.key.toUpperCase()}_API_KEY`
const value = ctx.request.body.value const value = ctx.request.body.value
// Set process.env
process.env[key] = value // set environment variables
env._set(key, value)
// Write to file // Write to file
await updateValues([key, value]) await updateValues([key, value])

View File

@ -1,8 +1,6 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const { getPackageForBuilder, buildPage } = require("../../utilities/builder") const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
const env = require("../../environment") const env = require("../../environment")
const instanceController = require("./instance")
const { copy, existsSync, readFile, writeFile } = require("fs-extra") const { copy, existsSync, readFile, writeFile } = require("fs-extra")
const { budibaseAppsDir } = require("../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const sqrl = require("squirrelly") const sqrl = require("squirrelly")
@ -11,89 +9,78 @@ const fs = require("fs-extra")
const { join, resolve } = require("../../utilities/centralPath") const { join, resolve } = require("../../utilities/centralPath")
const { promisify } = require("util") const { promisify } = require("util")
const chmodr = require("chmodr") const chmodr = require("chmodr")
const { generateAppID, getAppParams } = require("../../db/utils")
const packageJson = require("../../../package.json") const packageJson = require("../../../package.json")
const { createLinkView } = require("../../db/linkedRows")
const { downloadTemplate } = require("../../utilities/templates")
const { generateAppID, DocumentTypes, SEPARATOR } = require("../../db/utils")
const { const {
downloadExtractComponentLibraries, downloadExtractComponentLibraries,
} = require("../../utilities/createAppPackage") } = require("../../utilities/createAppPackage")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
async function createInstance(template) {
const appId = generateAppID()
const db = new CouchDB(appId)
await db.put({
_id: "_design/database",
// view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
views: {},
})
// add view for linked rows
await createLinkView(appId)
// replicate the template data to the instance DB
if (template) {
const templatePath = await downloadTemplate(...template.key.split("/"))
const dbDumpReadStream = fs.createReadStream(
join(templatePath, "db", "dump.txt")
)
const { ok } = await db.load(dbDumpReadStream)
if (!ok) {
throw "Error loading database dump from template."
}
}
return { _id: appId }
}
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ClientDb.name(getClientId(ctx))) let allDbs = await CouchDB.allDbs()
const body = await db.allDocs( const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
getAppParams(null, { const apps = appDbNames.map(db => new CouchDB(db).get(db))
include_docs: true, if (apps.length === 0) {
}) ctx.body = []
) } else {
ctx.body = body.rows.map(row => row.doc) ctx.body = await Promise.all(apps)
}
} }
exports.fetchAppPackage = async function(ctx) { exports.fetchAppPackage = async function(ctx) {
const clientId = await lookupClientId(ctx.params.applicationId) const db = new CouchDB(ctx.params.appId)
const db = new CouchDB(ClientDb.name(clientId)) const application = await db.get(ctx.params.appId)
const application = await db.get(ctx.params.applicationId)
ctx.body = await getPackageForBuilder(ctx.config, application) ctx.body = await getPackageForBuilder(ctx.config, application)
/* setBuilderToken(ctx, ctx.params.appId, application.version)
instance is hardcoded now - this can only change when we move
pages and screens into the database
*/
const devInstance = application.instances.find(
i => i.name === `dev-${clientId}`
)
setBuilderToken(
ctx,
ctx.params.applicationId,
devInstance._id,
application.version
)
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
const clientId = const instance = await createInstance(ctx.request.body.template)
(ctx.request.body && ctx.request.body.clientId) || env.CLIENT_ID const appId = instance._id
if (!clientId) {
ctx.throw(400, "ClientId not suplied")
}
const appId = generateAppID()
// insert an appId -> clientId lookup
const masterDb = new CouchDB("client_app_lookup")
await masterDb.put({
_id: appId,
clientId,
})
const db = new CouchDB(ClientDb.name(clientId))
const newApplication = { const newApplication = {
_id: appId, _id: appId,
type: "app", type: "app",
instances: [],
userInstanceMap: {}, userInstanceMap: {},
version: packageJson.version, version: packageJson.version,
componentLibraries: ["@budibase/standard-components"], componentLibraries: ["@budibase/standard-components"],
name: ctx.request.body.name, name: ctx.request.body.name,
template: ctx.request.body.template, template: ctx.request.body.template,
instance: instance,
} }
const instanceDb = new CouchDB(appId)
await instanceDb.put(newApplication)
const { rev } = await db.put(newApplication) if (env.NODE_ENV !== "jest") {
newApplication._rev = rev
const createInstCtx = {
user: {
appId: newApplication._id,
},
request: {
body: {
name: `dev-${clientId}`,
template: ctx.request.body.template,
},
},
}
await instanceController.create(createInstCtx)
newApplication.instances.push(createInstCtx.body)
if (process.env.NODE_ENV !== "jest") {
const newAppFolder = await createEmptyAppPackage(ctx, newApplication) const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
await downloadExtractComponentLibraries(newAppFolder) await downloadExtractComponentLibraries(newAppFolder)
} }
@ -104,9 +91,8 @@ exports.create = async function(ctx) {
} }
exports.update = async function(ctx) { exports.update = async function(ctx) {
const clientId = await lookupClientId(ctx.params.applicationId) const db = new CouchDB(ctx.params.appId)
const db = new CouchDB(ClientDb.name(clientId)) const application = await db.get(ctx.params.appId)
const application = await db.get(ctx.params.applicationId)
const data = ctx.request.body const data = ctx.request.body
const newData = { ...application, ...data } const newData = { ...application, ...data }
@ -120,16 +106,12 @@ exports.update = async function(ctx) {
} }
exports.delete = async function(ctx) { exports.delete = async function(ctx) {
const db = new CouchDB(ClientDb.name(getClientId(ctx))) const db = new CouchDB(ctx.params.appId)
const app = await db.get(ctx.params.applicationId) const app = await db.get(ctx.params.appId)
const result = await db.remove(app) const result = await db.destroy()
for (let instance of app.instances) {
const instanceDb = new CouchDB(instance._id)
await instanceDb.destroy()
}
// remove top level directory // remove top level directory
await fs.rmdir(join(budibaseAppsDir(), ctx.params.applicationId), { await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), {
recursive: true, recursive: true,
}) })
@ -222,24 +204,6 @@ const loadScreens = async (appFolder, page) => {
return screens return screens
} }
const lookupClientId = async appId => {
const masterDb = new CouchDB("client_app_lookup")
const { clientId } = await masterDb.get(appId)
return clientId
}
const getClientId = ctx => {
const clientId =
(ctx.request.body && ctx.request.body.clientId) ||
(ctx.query && ctx.query.clientId) ||
env.CLIENT_ID
if (!clientId) {
ctx.throw(400, "ClientId not supplied")
}
return clientId
}
const updateJsonFile = async (filePath, app) => { const updateJsonFile = async (filePath, app) => {
const json = await readFile(filePath, "utf8") const json = await readFile(filePath, "utf8")
const newJson = sqrl.Render(json, app) const newJson = sqrl.Render(json, app)

View File

@ -1,44 +1,26 @@
const jwt = require("jsonwebtoken") const jwt = require("jsonwebtoken")
const CouchDB = require("../../db") const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const bcrypt = require("../../utilities/bcrypt") const bcrypt = require("../../utilities/bcrypt")
const environment = require("../../environment") const env = require("../../environment")
const { getAPIKey } = require("../../utilities/usageQuota") const { getAPIKey } = require("../../utilities/usageQuota")
const { generateUserID } = require("../../db/utils") const { generateUserID } = require("../../db/utils")
exports.authenticate = async ctx => { exports.authenticate = async ctx => {
if (!ctx.user.appId) ctx.throw(400, "No appId") const appId = ctx.user.appId
if (!appId) ctx.throw(400, "No appId")
const { username, password } = ctx.request.body const { username, password } = ctx.request.body
if (!username) ctx.throw(400, "Username Required.") if (!username) ctx.throw(400, "Username Required.")
if (!password) ctx.throw(400, "Password Required") if (!password) ctx.throw(400, "Password Required.")
const masterDb = new CouchDB("client_app_lookup")
const { clientId } = await masterDb.get(ctx.user.appId)
if (!clientId) {
ctx.throw(400, "ClientId not supplied")
}
// find the instance that the user is associated with
const db = new CouchDB(ClientDb.name(clientId))
const app = await db.get(ctx.user.appId)
const instanceId = app.userInstanceMap[username]
if (!instanceId)
ctx.throw(
500,
"User is not associated with an instance of app",
ctx.user.appId
)
// Check the user exists in the instance DB by username // Check the user exists in the instance DB by username
const instanceDb = new CouchDB(instanceId) const db = new CouchDB(appId)
const app = await db.get(appId)
let dbUser let dbUser
try { try {
dbUser = await instanceDb.get(generateUserID(username)) dbUser = await db.get(generateUserID(username))
} catch (_) { } catch (_) {
// do not want to throw a 404 - as this could be // do not want to throw a 404 - as this could be
// used to determine valid usernames // used to determine valid usernames
@ -50,12 +32,11 @@ exports.authenticate = async ctx => {
const payload = { const payload = {
userId: dbUser._id, userId: dbUser._id,
accessLevelId: dbUser.accessLevelId, accessLevelId: dbUser.accessLevelId,
appId: ctx.user.appId,
version: app.version, version: app.version,
instanceId, appId,
} }
// if in cloud add the user api key // if in cloud add the user api key
if (environment.CLOUD) { if (env.CLOUD) {
const { apiKey } = await getAPIKey(ctx.user.appId) const { apiKey } = await getAPIKey(ctx.user.appId)
payload.apiKey = apiKey payload.apiKey = apiKey
} }

View File

@ -56,7 +56,7 @@ async function checkForWebhooks({ user, oldAuto, newAuto }) {
!isWebhookTrigger(newAuto) && !isWebhookTrigger(newAuto) &&
oldTrigger.webhookId oldTrigger.webhookId
) { ) {
let db = new CouchDB(user.instanceId) let db = new CouchDB(user.appId)
// need to get the webhook to get the rev // need to get the webhook to get the rev
const webhook = await db.get(oldTrigger.webhookId) const webhook = await db.get(oldTrigger.webhookId)
const ctx = { const ctx = {
@ -86,15 +86,15 @@ async function checkForWebhooks({ user, oldAuto, newAuto }) {
const id = ctx.body.webhook._id const id = ctx.body.webhook._id
newTrigger.webhookId = id newTrigger.webhookId = id
newTrigger.inputs = { newTrigger.inputs = {
schemaUrl: `api/webhooks/schema/${user.instanceId}/${id}`, schemaUrl: `api/webhooks/schema/${user.appId}/${id}`,
triggerUrl: `api/webhooks/trigger/${user.instanceId}/${id}`, triggerUrl: `api/webhooks/trigger/${user.appId}/${id}`,
} }
} }
return newAuto return newAuto
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
let automation = ctx.request.body let automation = ctx.request.body
automation.appId = ctx.user.appId automation.appId = ctx.user.appId
@ -117,7 +117,7 @@ exports.create = async function(ctx) {
} }
exports.update = async function(ctx) { exports.update = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
let automation = ctx.request.body let automation = ctx.request.body
automation.appId = ctx.user.appId automation.appId = ctx.user.appId
const oldAutomation = await db.get(automation._id) const oldAutomation = await db.get(automation._id)
@ -142,7 +142,7 @@ exports.update = async function(ctx) {
} }
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const response = await db.allDocs( const response = await db.allDocs(
getAutomationParams(null, { getAutomationParams(null, {
include_docs: true, include_docs: true,
@ -152,12 +152,12 @@ exports.fetch = async function(ctx) {
} }
exports.find = async function(ctx) { exports.find = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
ctx.body = await db.get(ctx.params.id) ctx.body = await db.get(ctx.params.id)
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const oldAutomation = await db.get(ctx.params.id) const oldAutomation = await db.get(ctx.params.id)
await checkForWebhooks({ user: ctx.user, oldAuto: oldAutomation }) await checkForWebhooks({ user: ctx.user, oldAuto: oldAutomation })
ctx.body = await db.remove(ctx.params.id, ctx.params.rev) ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
@ -190,11 +190,11 @@ module.exports.getDefinitionList = async function(ctx) {
*********************/ *********************/
exports.trigger = async function(ctx) { exports.trigger = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
let automation = await db.get(ctx.params.id) let automation = await db.get(ctx.params.id)
await triggers.externalTrigger(automation, { await triggers.externalTrigger(automation, {
...ctx.request.body, ...ctx.request.body,
instanceId: ctx.user.instanceId, appId: ctx.user.appId,
}) })
ctx.status = 200 ctx.status = 200
ctx.body = { ctx.body = {

View File

@ -1,31 +0,0 @@
const { create, destroy } = require("../../db/clientDb")
const env = require("../../environment")
exports.getClientId = async function(ctx) {
ctx.body = env.CLIENT_ID
}
exports.create = async function(ctx) {
const clientId = getClientId(ctx)
await create(clientId)
ctx.status = 200
ctx.message = `Client Database ${clientId} successfully provisioned.`
}
exports.destroy = async function(ctx) {
const clientId = getClientId(ctx)
await destroy(clientId)
ctx.status = 200
ctx.message = `Client Database ${clientId} successfully deleted.`
}
const getClientId = ctx => {
const clientId =
(ctx.query && ctx.query.clientId) ||
(ctx.body && ctx.body.clientId) ||
env.CLIENT_ID
if (!clientId) {
ctx.throw(400, "ClientId not supplied")
}
return clientId
}

View File

@ -1,5 +1,4 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const ClientDb = require("../../db/clientDb")
const { resolve, join } = require("../../utilities/centralPath") const { resolve, join } = require("../../utilities/centralPath")
const { const {
budibaseTempDir, budibaseTempDir,
@ -7,49 +6,42 @@ const {
} = require("../../utilities/budibaseDir") } = require("../../utilities/budibaseDir")
exports.fetchAppComponentDefinitions = async function(ctx) { exports.fetchAppComponentDefinitions = async function(ctx) {
const masterDb = new CouchDB("client_app_lookup") const db = new CouchDB(ctx.params.appId)
const { clientId } = await masterDb.get(ctx.params.appId)
const db = new CouchDB(ClientDb.name(clientId))
const app = await db.get(ctx.params.appId) const app = await db.get(ctx.params.appId)
const componentDefinitions = app.componentLibraries.reduce( ctx.body = app.componentLibraries.reduce((acc, componentLibrary) => {
(acc, componentLibrary) => { let appDirectory = resolve(
let appDirectory = resolve( budibaseAppsDir(),
budibaseAppsDir(), ctx.params.appId,
ctx.params.appId, "node_modules"
"node_modules" )
)
if (ctx.isDev) { if (ctx.isDev) {
appDirectory = budibaseTempDir() appDirectory = budibaseTempDir()
}
const componentJson = require(join(
appDirectory,
componentLibrary,
ctx.isDev ? "" : "package",
"components.json"
))
const result = {}
// map over the components.json and add the library identifier as a key
// button -> @budibase/standard-components/button
for (let key of Object.keys(componentJson)) {
const fullComponentName = `${componentLibrary}/${key}`
result[fullComponentName] = {
_component: fullComponentName,
...componentJson[key],
} }
}
const componentJson = require(join( return {
appDirectory, ...acc,
componentLibrary, ...result,
ctx.isDev ? "" : "package", }
"components.json" }, {})
))
const result = {}
// map over the components.json and add the library identifier as a key
// button -> @budibase/standard-components/button
for (key in componentJson) {
const fullComponentName = `${componentLibrary}/${key}`
result[fullComponentName] = {
_component: fullComponentName,
...componentJson[key],
}
}
return {
...acc,
...result,
}
},
{}
)
ctx.body = componentDefinitions
} }

View File

@ -6,7 +6,7 @@ const uuid = require("uuid")
const sanitize = require("sanitize-s3-objectkey") const sanitize = require("sanitize-s3-objectkey")
const { budibaseAppsDir } = require("../../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../../utilities/budibaseDir")
const PouchDB = require("../../../db") const PouchDB = require("../../../db")
const environment = require("../../../environment") const env = require("../../../environment")
async function invalidateCDN(cfDistribution, appId) { async function invalidateCDN(cfDistribution, appId) {
const cf = new AWS.CloudFront({}) const cf = new AWS.CloudFront({})
@ -44,12 +44,12 @@ exports.isInvalidationComplete = async function(
exports.updateDeploymentQuota = async function(quota) { exports.updateDeploymentQuota = async function(quota) {
const DEPLOYMENT_SUCCESS_URL = const DEPLOYMENT_SUCCESS_URL =
environment.DEPLOYMENT_CREDENTIALS_URL + "deploy/success" env.DEPLOYMENT_CREDENTIALS_URL + "deploy/success"
const response = await fetch(DEPLOYMENT_SUCCESS_URL, { const response = await fetch(DEPLOYMENT_SUCCESS_URL, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
apiKey: process.env.BUDIBASE_API_KEY, apiKey: env.BUDIBASE_API_KEY,
quota, quota,
}), }),
headers: { headers: {
@ -62,24 +62,21 @@ exports.updateDeploymentQuota = async function(quota) {
throw new Error(`Error updating deployment quota for API Key`) throw new Error(`Error updating deployment quota for API Key`)
} }
const json = await response.json() return await response.json()
return json
} }
/** /**
* Verifies the users API key and * Verifies the users API key and
* Verifies that the deployment fits within the quota of the user, * Verifies that the deployment fits within the quota of the user,
* @param {String} instanceId - instanceId being deployed * @param {String} appId - appId being deployed
* @param {String} appId - appId being deployed * @param {String} appId - appId being deployed
* @param {quota} quota - current quota being changed with this application * @param {quota} quota - current quota being changed with this application
*/ */
exports.verifyDeployment = async function({ instanceId, appId, quota }) { exports.verifyDeployment = async function({ appId, quota }) {
const response = await fetch(process.env.DEPLOYMENT_CREDENTIALS_URL, { const response = await fetch(env.DEPLOYMENT_CREDENTIALS_URL, {
method: "POST", method: "POST",
body: JSON.stringify({ body: JSON.stringify({
apiKey: process.env.BUDIBASE_API_KEY, apiKey: env.BUDIBASE_API_KEY,
instanceId,
appId, appId,
quota, quota,
}), }),
@ -87,7 +84,7 @@ exports.verifyDeployment = async function({ instanceId, appId, quota }) {
if (response.status !== 200) { if (response.status !== 200) {
throw new Error( throw new Error(
`Error fetching temporary credentials for api key: ${process.env.BUDIBASE_API_KEY}` `Error fetching temporary credentials for api key: ${env.BUDIBASE_API_KEY}`
) )
} }
@ -159,7 +156,6 @@ exports.prepareUploadForS3 = prepareUploadForS3
exports.uploadAppAssets = async function({ exports.uploadAppAssets = async function({
appId, appId,
instanceId,
bucket, bucket,
cfDistribution, cfDistribution,
accountId, accountId,
@ -193,7 +189,7 @@ exports.uploadAppAssets = async function({
} }
// Upload file attachments // Upload file attachments
const db = new PouchDB(instanceId) const db = new PouchDB(appId)
let fileUploads let fileUploads
try { try {
fileUploads = await db.get("_local/fileuploads") fileUploads = await db.get("_local/fileuploads")

View File

@ -8,6 +8,7 @@ const {
} = require("./aws") } = require("./aws")
const { DocumentTypes, SEPARATOR, UNICODE_MAX } = require("../../../db/utils") const { DocumentTypes, SEPARATOR, UNICODE_MAX } = require("../../../db/utils")
const newid = require("../../../db/newid") const newid = require("../../../db/newid")
const env = require("../../../environment")
// the max time we can wait for an invalidation to complete before considering it failed // the max time we can wait for an invalidation to complete before considering it failed
const MAX_PENDING_TIME_MS = 30 * 60000 const MAX_PENDING_TIME_MS = 30 * 60000
@ -80,29 +81,20 @@ function replicate(local, remote) {
}) })
} }
async function replicateCouch({ instanceId, clientId, session }) { async function replicateCouch({ appId, session }) {
const databases = [`client_${clientId}`, "client_app_lookup", instanceId] const localDb = new PouchDB(appId)
const remoteDb = new CouchDB(`${env.DEPLOYMENT_DB_URL}/${appId}`, {
const replications = databases.map(localDbName => { fetch: function(url, opts) {
const localDb = new PouchDB(localDbName) opts.headers.set("Cookie", `${session};`)
const remoteDb = new CouchDB( return CouchDB.fetch(url, opts)
`${process.env.DEPLOYMENT_DB_URL}/${localDbName}`, },
{
fetch: function(url, opts) {
opts.headers.set("Cookie", `${session};`)
return CouchDB.fetch(url, opts)
},
}
)
return replicate(localDb, remoteDb)
}) })
await Promise.all(replications) return replicate(localDb, remoteDb)
} }
async function getCurrentInstanceQuota(instanceId) { async function getCurrentInstanceQuota(appId) {
const db = new PouchDB(instanceId) const db = new PouchDB(appId)
const rows = await db.allDocs({ const rows = await db.allDocs({
startkey: DocumentTypes.ROW + SEPARATOR, startkey: DocumentTypes.ROW + SEPARATOR,
@ -127,7 +119,7 @@ async function getCurrentInstanceQuota(instanceId) {
} }
async function storeLocalDeploymentHistory(deployment) { async function storeLocalDeploymentHistory(deployment) {
const db = new PouchDB(deployment.instanceId) const db = new PouchDB(deployment.appId)
let deploymentDoc let deploymentDoc
try { try {
@ -155,11 +147,10 @@ async function storeLocalDeploymentHistory(deployment) {
} }
} }
async function deployApp({ instanceId, appId, clientId, deploymentId }) { async function deployApp({ appId, deploymentId }) {
try { try {
const instanceQuota = await getCurrentInstanceQuota(instanceId) const instanceQuota = await getCurrentInstanceQuota(appId)
const verification = await verifyDeployment({ const verification = await verifyDeployment({
instanceId,
appId, appId,
quota: instanceQuota, quota: instanceQuota,
}) })
@ -167,17 +158,14 @@ async function deployApp({ instanceId, appId, clientId, deploymentId }) {
console.log(`Uploading assets for appID ${appId} assets to s3..`) console.log(`Uploading assets for appID ${appId} assets to s3..`)
const invalidationId = await uploadAppAssets({ const invalidationId = await uploadAppAssets({
clientId,
appId, appId,
instanceId,
...verification, ...verification,
}) })
// replicate the DB to the couchDB cluster in prod // replicate the DB to the couchDB cluster in prod
console.log("Replicating local PouchDB to remote..") console.log("Replicating local PouchDB to remote..")
await replicateCouch({ await replicateCouch({
instanceId, appId,
clientId,
session: verification.couchDbSession, session: verification.couchDbSession,
}) })
@ -185,7 +173,7 @@ async function deployApp({ instanceId, appId, clientId, deploymentId }) {
await storeLocalDeploymentHistory({ await storeLocalDeploymentHistory({
_id: deploymentId, _id: deploymentId,
instanceId, appId,
invalidationId, invalidationId,
cfDistribution: verification.cfDistribution, cfDistribution: verification.cfDistribution,
quota: verification.quota, quota: verification.quota,
@ -194,7 +182,7 @@ async function deployApp({ instanceId, appId, clientId, deploymentId }) {
} catch (err) { } catch (err) {
await storeLocalDeploymentHistory({ await storeLocalDeploymentHistory({
_id: deploymentId, _id: deploymentId,
instanceId, appId,
status: DeploymentStatus.FAILURE, status: DeploymentStatus.FAILURE,
err: err.message, err: err.message,
}) })
@ -204,7 +192,7 @@ async function deployApp({ instanceId, appId, clientId, deploymentId }) {
exports.fetchDeployments = async function(ctx) { exports.fetchDeployments = async function(ctx) {
try { try {
const db = new PouchDB(ctx.user.instanceId) const db = new PouchDB(ctx.user.appId)
const deploymentDoc = await db.get("_local/deployments") const deploymentDoc = await db.get("_local/deployments")
const { updated, deployments } = await checkAllDeployments( const { updated, deployments } = await checkAllDeployments(
deploymentDoc, deploymentDoc,
@ -221,7 +209,7 @@ exports.fetchDeployments = async function(ctx) {
exports.deploymentProgress = async function(ctx) { exports.deploymentProgress = async function(ctx) {
try { try {
const db = new PouchDB(ctx.user.instanceId) const db = new PouchDB(ctx.user.appId)
const deploymentDoc = await db.get("_local/deployments") const deploymentDoc = await db.get("_local/deployments")
ctx.body = deploymentDoc[ctx.params.deploymentId] ctx.body = deploymentDoc[ctx.params.deploymentId]
} catch (err) { } catch (err) {
@ -233,18 +221,13 @@ exports.deploymentProgress = async function(ctx) {
} }
exports.deployApp = async function(ctx) { exports.deployApp = async function(ctx) {
const clientAppLookupDB = new PouchDB("client_app_lookup")
const { clientId } = await clientAppLookupDB.get(ctx.user.appId)
const deployment = await storeLocalDeploymentHistory({ const deployment = await storeLocalDeploymentHistory({
instanceId: ctx.user.instanceId,
appId: ctx.user.appId, appId: ctx.user.appId,
status: DeploymentStatus.PENDING, status: DeploymentStatus.PENDING,
}) })
await deployApp({ await deployApp({
...ctx.user, ...ctx.user,
clientId,
deploymentId: deployment._id, deploymentId: deployment._id,
}) })

View File

@ -1,73 +0,0 @@
const fs = require("fs")
const CouchDB = require("../../db")
const client = require("../../db/clientDb")
const newid = require("../../db/newid")
const { createLinkView } = require("../../db/linkedRows")
const { join } = require("../../utilities/centralPath")
const { downloadTemplate } = require("../../utilities/templates")
exports.create = async function(ctx) {
const instanceName = ctx.request.body.name
const template = ctx.request.body.template
const { appId } = ctx.user
const appShortId = appId.substring(0, 7)
const instanceId = `inst_${appShortId}_${newid()}`
const masterDb = new CouchDB("client_app_lookup")
const { clientId } = await masterDb.get(appId)
const db = new CouchDB(instanceId)
await db.put({
_id: "_design/database",
metadata: {
clientId,
applicationId: appId,
},
// view collation information, read before writing any complex views:
// https://docs.couchdb.org/en/master/ddocs/views/collation.html#collation-specification
views: {},
})
// add view for linked rows
await createLinkView(instanceId)
// Add the new instance under the app clientDB
const clientDb = new CouchDB(client.name(clientId))
const budibaseApp = await clientDb.get(appId)
const instance = { _id: instanceId, name: instanceName }
budibaseApp.instances.push(instance)
await clientDb.put(budibaseApp)
// replicate the template data to the instance DB
if (template) {
const templatePath = await downloadTemplate(...template.key.split("/"))
const dbDumpReadStream = fs.createReadStream(
join(templatePath, "db", "dump.txt")
)
const { ok } = await db.load(dbDumpReadStream)
if (!ok) {
ctx.throw(500, "Error loading database dump from template.")
}
}
ctx.status = 200
ctx.message = `Instance Database ${instanceName} successfully provisioned.`
ctx.body = instance
}
exports.destroy = async function(ctx) {
const db = new CouchDB(ctx.params.instanceId)
const designDoc = await db.get("_design/database")
await db.destroy()
// remove instance from client application document
const { metadata } = designDoc
const clientDb = new CouchDB(client.name(metadata.clientId))
const budibaseApp = await clientDb.get(metadata.applicationId)
budibaseApp.instances = budibaseApp.instances.filter(
instance => instance !== ctx.params.instanceId
)
await clientDb.put(budibaseApp)
ctx.status = 200
ctx.message = `Instance Database ${ctx.params.instanceId} successfully destroyed.`
}

View File

@ -28,8 +28,8 @@ validateJs.extend(validateJs.validators.datetime, {
}) })
exports.patch = async function(ctx) { exports.patch = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
let row = await db.get(ctx.params.id) let row = await db.get(ctx.params.id)
const table = await db.get(row.tableId) const table = await db.get(row.tableId)
const patchfields = ctx.request.body const patchfields = ctx.request.body
@ -56,7 +56,7 @@ exports.patch = async function(ctx) {
// returned row is cleaned and prepared for writing to DB // returned row is cleaned and prepared for writing to DB
row = await linkRows.updateLinks({ row = await linkRows.updateLinks({
instanceId, appId,
eventType: linkRows.EventType.ROW_UPDATE, eventType: linkRows.EventType.ROW_UPDATE,
row, row,
tableId: row.tableId, tableId: row.tableId,
@ -66,16 +66,15 @@ exports.patch = async function(ctx) {
row._rev = response.rev row._rev = response.rev
row.type = "row" row.type = "row"
ctx.eventEmitter && ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:update`, appId, row, table)
ctx.eventEmitter.emitRow(`row:update`, instanceId, row, table)
ctx.body = row ctx.body = row
ctx.status = 200 ctx.status = 200
ctx.message = `${table.name} updated successfully.` ctx.message = `${table.name} updated successfully.`
} }
exports.save = async function(ctx) { exports.save = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
let row = ctx.request.body let row = ctx.request.body
row.tableId = ctx.params.tableId row.tableId = ctx.params.tableId
@ -112,7 +111,7 @@ exports.save = async function(ctx) {
// make sure link rows are up to date // make sure link rows are up to date
row = await linkRows.updateLinks({ row = await linkRows.updateLinks({
instanceId, appId,
eventType: linkRows.EventType.ROW_SAVE, eventType: linkRows.EventType.ROW_SAVE,
row, row,
tableId: row.tableId, tableId: row.tableId,
@ -133,16 +132,15 @@ exports.save = async function(ctx) {
const response = await db.post(row) const response = await db.post(row)
row._rev = response.rev row._rev = response.rev
ctx.eventEmitter && ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:save`, appId, row, table)
ctx.eventEmitter.emitRow(`row:save`, instanceId, row, table)
ctx.body = row ctx.body = row
ctx.status = 200 ctx.status = 200
ctx.message = `${table.name} created successfully` ctx.message = `${table.name} created successfully`
} }
exports.fetchView = async function(ctx) { exports.fetchView = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const { calculation, group, field } = ctx.query const { calculation, group, field } = ctx.query
const viewName = ctx.params.viewName const viewName = ctx.params.viewName
@ -160,7 +158,7 @@ exports.fetchView = async function(ctx) {
if (!calculation) { if (!calculation) {
response.rows = response.rows.map(row => row.doc) response.rows = response.rows.map(row => row.doc)
ctx.body = await linkRows.attachLinkInfo(instanceId, response.rows) ctx.body = await linkRows.attachLinkInfo(appId, response.rows)
} }
if (calculation === CALCULATION_TYPES.STATS) { if (calculation === CALCULATION_TYPES.STATS) {
@ -186,8 +184,8 @@ exports.fetchView = async function(ctx) {
} }
exports.fetchTableRows = async function(ctx) { exports.fetchTableRows = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const response = await db.allDocs( const response = await db.allDocs(
getRowParams(ctx.params.tableId, null, { getRowParams(ctx.params.tableId, null, {
include_docs: true, include_docs: true,
@ -195,45 +193,45 @@ exports.fetchTableRows = async function(ctx) {
) )
ctx.body = response.rows.map(row => row.doc) ctx.body = response.rows.map(row => row.doc)
ctx.body = await linkRows.attachLinkInfo( ctx.body = await linkRows.attachLinkInfo(
instanceId, appId,
response.rows.map(row => row.doc) response.rows.map(row => row.doc)
) )
} }
exports.search = async function(ctx) { exports.search = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const response = await db.allDocs({ const response = await db.allDocs({
include_docs: true, include_docs: true,
...ctx.request.body, ...ctx.request.body,
}) })
ctx.body = await linkRows.attachLinkInfo( ctx.body = await linkRows.attachLinkInfo(
instanceId, appId,
response.rows.map(row => row.doc) response.rows.map(row => row.doc)
) )
} }
exports.find = async function(ctx) { exports.find = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const row = await db.get(ctx.params.rowId) const row = await db.get(ctx.params.rowId)
if (row.tableId !== ctx.params.tableId) { if (row.tableId !== ctx.params.tableId) {
ctx.throw(400, "Supplied tableId does not match the rows tableId") ctx.throw(400, "Supplied tableId does not match the rows tableId")
return return
} }
ctx.body = await linkRows.attachLinkInfo(instanceId, row) ctx.body = await linkRows.attachLinkInfo(appId, row)
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const row = await db.get(ctx.params.rowId) const row = await db.get(ctx.params.rowId)
if (row.tableId !== ctx.params.tableId) { if (row.tableId !== ctx.params.tableId) {
ctx.throw(400, "Supplied tableId doesn't match the row's tableId") ctx.throw(400, "Supplied tableId doesn't match the row's tableId")
return return
} }
await linkRows.updateLinks({ await linkRows.updateLinks({
instanceId, appId,
eventType: linkRows.EventType.ROW_DELETE, eventType: linkRows.EventType.ROW_DELETE,
row, row,
tableId: row.tableId, tableId: row.tableId,
@ -243,12 +241,12 @@ exports.destroy = async function(ctx) {
// for automations include the row that was deleted // for automations include the row that was deleted
ctx.row = row ctx.row = row
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, instanceId, row) ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, appId, row)
} }
exports.validate = async function(ctx) { exports.validate = async function(ctx) {
const errors = await validate({ const errors = await validate({
instanceId: ctx.user.instanceId, appId: ctx.user.appId,
tableId: ctx.params.tableId, tableId: ctx.params.tableId,
row: ctx.request.body, row: ctx.request.body,
}) })
@ -256,9 +254,9 @@ exports.validate = async function(ctx) {
ctx.body = errors ctx.body = errors
} }
async function validate({ instanceId, tableId, row, table }) { async function validate({ appId, tableId, row, table }) {
if (!table) { if (!table) {
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
table = await db.get(tableId) table = await db.get(tableId)
} }
const errors = {} const errors = {}
@ -273,11 +271,11 @@ async function validate({ instanceId, tableId, row, table }) {
} }
exports.fetchEnrichedRow = async function(ctx) { exports.fetchEnrichedRow = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const tableId = ctx.params.tableId const tableId = ctx.params.tableId
const rowId = ctx.params.rowId const rowId = ctx.params.rowId
if (instanceId == null || tableId == null || rowId == null) { if (appId == null || tableId == null || rowId == null) {
ctx.status = 400 ctx.status = 400
ctx.body = { ctx.body = {
status: 400, status: 400,
@ -290,7 +288,7 @@ exports.fetchEnrichedRow = async function(ctx) {
const [table, row] = await Promise.all([db.get(tableId), db.get(rowId)]) const [table, row] = await Promise.all([db.get(tableId), db.get(rowId)])
// get the link docs // get the link docs
const linkVals = await linkRows.getLinkDocuments({ const linkVals = await linkRows.getLinkDocuments({
instanceId, appId,
tableId, tableId,
rowId, rowId,
}) })
@ -301,7 +299,7 @@ exports.fetchEnrichedRow = async function(ctx) {
}) })
// need to include the IDs in these rows for any links they may have // need to include the IDs in these rows for any links they may have
let linkedRows = await linkRows.attachLinkInfo( let linkedRows = await linkRows.attachLinkInfo(
instanceId, appId,
response.rows.map(row => row.doc) response.rows.map(row => row.doc)
) )
// insert the link rows in the correct place throughout the main row // insert the link rows in the correct place throughout the main row
@ -375,13 +373,13 @@ const TYPE_TRANSFORM_MAP = {
} }
async function bulkDelete(ctx) { async function bulkDelete(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const { rows } = ctx.request.body const { rows } = ctx.request.body
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const linkUpdates = rows.map(row => const linkUpdates = rows.map(row =>
linkRows.updateLinks({ linkRows.updateLinks({
instanceId, appId,
eventType: linkRows.EventType.ROW_DELETE, eventType: linkRows.EventType.ROW_DELETE,
row, row,
tableId: row.tableId, tableId: row.tableId,
@ -392,6 +390,6 @@ async function bulkDelete(ctx) {
await Promise.all(linkUpdates) await Promise.all(linkUpdates)
rows.forEach(row => { rows.forEach(row => {
ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, instanceId, row) ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, appId, row)
}) })
} }

View File

@ -13,6 +13,7 @@ const CouchDB = require("../../db")
const setBuilderToken = require("../../utilities/builder/setBuilderToken") const setBuilderToken = require("../../utilities/builder/setBuilderToken")
const fileProcessor = require("../../utilities/fileProcessor") const fileProcessor = require("../../utilities/fileProcessor")
const { AuthTypes } = require("../../constants") const { AuthTypes } = require("../../constants")
const env = require("../../environment")
// this was the version before we started versioning the component library // this was the version before we started versioning the component library
const COMP_LIB_BASE_APP_VERSION = "0.2.5" const COMP_LIB_BASE_APP_VERSION = "0.2.5"
@ -38,7 +39,7 @@ exports.uploadFile = async function(ctx) {
"attachments" "attachments"
) )
if (process.env.CLOUD) { if (env.CLOUD) {
// remote upload // remote upload
const s3 = new AWS.S3({ const s3 = new AWS.S3({
params: { params: {
@ -64,11 +65,11 @@ exports.uploadFile = async function(ctx) {
ctx.body = await processLocalFileUploads({ ctx.body = await processLocalFileUploads({
files, files,
outputPath: attachmentsPath, outputPath: attachmentsPath,
instanceId: ctx.user.instanceId, appId: ctx.user.appId,
}) })
} }
async function processLocalFileUploads({ files, outputPath, instanceId }) { async function processLocalFileUploads({ files, outputPath, appId }) {
// create attachments dir if it doesnt exist // create attachments dir if it doesnt exist
!fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true }) !fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true })
@ -97,7 +98,7 @@ async function processLocalFileUploads({ files, outputPath, instanceId }) {
// local document used to track which files need to be uploaded // local document used to track which files need to be uploaded
// db.get throws an error if the document doesn't exist // db.get throws an error if the document doesn't exist
// need to use a promise to default // need to use a promise to default
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
await db await db
.get("_local/fileuploads") .get("_local/fileuploads")
.then(data => { .then(data => {
@ -129,7 +130,7 @@ exports.performLocalFileProcessing = async function(ctx) {
ctx.body = await processLocalFileUploads({ ctx.body = await processLocalFileUploads({
files, files,
outputPath: processedFileOutputPath, outputPath: processedFileOutputPath,
instanceId: ctx.user.instanceId, appId: ctx.user.appId,
}) })
} catch (err) { } catch (err) {
ctx.throw(500, err) ctx.throw(500, err)
@ -150,7 +151,7 @@ exports.serveApp = async function(ctx) {
const appId = ctx.user.appId const appId = ctx.user.appId
if (process.env.CLOUD) { if (env.CLOUD) {
const S3_URL = `https://${appId}.app.budi.live/assets/${appId}/${mainOrAuth}/${ctx.file || const S3_URL = `https://${appId}.app.budi.live/assets/${appId}/${mainOrAuth}/${ctx.file ||
"index.production.html"}` "index.production.html"}`
@ -168,7 +169,7 @@ exports.serveAttachment = async function(ctx) {
const attachmentsPath = resolve(budibaseAppsDir(), appId, "attachments") const attachmentsPath = resolve(budibaseAppsDir(), appId, "attachments")
// Serve from CloudFront // Serve from CloudFront
if (process.env.CLOUD) { if (env.CLOUD) {
const S3_URL = `https://cdn.app.budi.live/assets/${appId}/attachments/${ctx.file}` const S3_URL = `https://cdn.app.budi.live/assets/${appId}/attachments/${ctx.file}`
const response = await fetch(S3_URL) const response = await fetch(S3_URL)
const body = await response.text() const body = await response.text()
@ -214,7 +215,7 @@ exports.serveComponentLibrary = async function(ctx) {
) )
} }
if (process.env.CLOUD) { if (env.CLOUD) {
let componentLib = "componentlibrary" let componentLib = "componentlibrary"
if (ctx.user.version) { if (ctx.user.version) {
componentLib += `-${ctx.user.version}` componentLib += `-${ctx.user.version}`

View File

@ -9,7 +9,7 @@ const {
} = require("../../db/utils") } = require("../../db/utils")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const body = await db.allDocs( const body = await db.allDocs(
getTableParams(null, { getTableParams(null, {
include_docs: true, include_docs: true,
@ -19,13 +19,13 @@ exports.fetch = async function(ctx) {
} }
exports.find = async function(ctx) { exports.find = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
ctx.body = await db.get(ctx.params.id) ctx.body = await db.get(ctx.params.id)
} }
exports.save = async function(ctx) { exports.save = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const { dataImport, ...rest } = ctx.request.body const { dataImport, ...rest } = ctx.request.body
const tableToSave = { const tableToSave = {
type: "table", type: "table",
@ -90,7 +90,7 @@ exports.save = async function(ctx) {
// update linked rows // update linked rows
await linkRows.updateLinks({ await linkRows.updateLinks({
instanceId, appId,
eventType: oldTable eventType: oldTable
? linkRows.EventType.TABLE_UPDATED ? linkRows.EventType.TABLE_UPDATED
: linkRows.EventType.TABLE_SAVE, : linkRows.EventType.TABLE_SAVE,
@ -107,7 +107,7 @@ exports.save = async function(ctx) {
tableToSave._rev = result.rev tableToSave._rev = result.rev
ctx.eventEmitter && ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave) ctx.eventEmitter.emitTable(`table:save`, appId, tableToSave)
if (dataImport && dataImport.csvString) { if (dataImport && dataImport.csvString) {
// Populate the table with rows imported from CSV in a bulk update // Populate the table with rows imported from CSV in a bulk update
@ -127,8 +127,8 @@ exports.save = async function(ctx) {
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const instanceId = ctx.user.instanceId const appId = ctx.user.appId
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const tableToDelete = await db.get(ctx.params.tableId) const tableToDelete = await db.get(ctx.params.tableId)
// Delete all rows for that table // Delete all rows for that table
@ -141,7 +141,7 @@ exports.destroy = async function(ctx) {
// update linked rows // update linked rows
await linkRows.updateLinks({ await linkRows.updateLinks({
instanceId, appId,
eventType: linkRows.EventType.TABLE_DELETE, eventType: linkRows.EventType.TABLE_DELETE,
table: tableToDelete, table: tableToDelete,
}) })
@ -150,7 +150,7 @@ exports.destroy = async function(ctx) {
await db.remove(tableToDelete) await db.remove(tableToDelete)
ctx.eventEmitter && ctx.eventEmitter &&
ctx.eventEmitter.emitTable(`table:delete`, instanceId, tableToDelete) ctx.eventEmitter.emitTable(`table:delete`, appId, tableToDelete)
ctx.status = 200 ctx.status = 200
ctx.message = `Table ${ctx.params.tableId} deleted.` ctx.message = `Table ${ctx.params.tableId} deleted.`
} }

View File

@ -28,12 +28,11 @@ exports.downloadTemplate = async function(ctx) {
} }
exports.exportTemplateFromApp = async function(ctx) { exports.exportTemplateFromApp = async function(ctx) {
const { appId, instanceId } = ctx.user const { appId } = ctx.user
const { templateName } = ctx.request.body const { templateName } = ctx.request.body
await exportTemplateFromApp({ await exportTemplateFromApp({
appId, appId,
instanceId,
templateName, templateName,
}) })

View File

@ -1,5 +1,4 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const clientDb = require("../../db/clientDb")
const bcrypt = require("../../utilities/bcrypt") const bcrypt = require("../../utilities/bcrypt")
const { generateUserID, getUserParams } = require("../../db/utils") const { generateUserID, getUserParams } = require("../../db/utils")
const { const {
@ -8,7 +7,7 @@ const {
} = require("../../utilities/accessLevels") } = require("../../utilities/accessLevels")
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
const database = new CouchDB(ctx.user.instanceId) const database = new CouchDB(ctx.user.appId)
const data = await database.allDocs( const data = await database.allDocs(
getUserParams(null, { getUserParams(null, {
include_docs: true, include_docs: true,
@ -18,15 +17,14 @@ exports.fetch = async function(ctx) {
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
const database = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const appId = (await database.get("_design/database")).metadata.applicationId
const { username, password, name, accessLevelId } = ctx.request.body const { username, password, name, accessLevelId } = ctx.request.body
if (!username || !password) { if (!username || !password) {
ctx.throw(400, "Username and Password Required.") ctx.throw(400, "Username and Password Required.")
} }
const accessLevel = await checkAccessLevel(database, accessLevelId) const accessLevel = await checkAccessLevel(db, accessLevelId)
if (!accessLevel) ctx.throw(400, "Invalid Access Level") if (!accessLevel) ctx.throw(400, "Invalid Access Level")
@ -39,18 +37,12 @@ exports.create = async function(ctx) {
accessLevelId, accessLevelId,
} }
const response = await database.post(user) const response = await db.post(user)
const masterDb = new CouchDB("client_app_lookup")
const { clientId } = await masterDb.get(appId)
// the clientDB needs to store a map of users against the app
const db = new CouchDB(clientDb.name(clientId))
const app = await db.get(appId)
const app = await db.get(ctx.user.appId)
app.userInstanceMap = { app.userInstanceMap = {
...app.userInstanceMap, ...app.userInstanceMap,
[username]: ctx.user.instanceId, [username]: ctx.user.appId,
} }
await db.put(app) await db.put(app)
@ -65,7 +57,7 @@ exports.create = async function(ctx) {
} }
exports.update = async function(ctx) { exports.update = async function(ctx) {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const user = ctx.request.body const user = ctx.request.body
const dbUser = db.get(ctx.request.body._id) const dbUser = db.get(ctx.request.body._id)
const newData = { ...dbUser, ...user } const newData = { ...dbUser, ...user }
@ -79,14 +71,14 @@ exports.update = async function(ctx) {
} }
exports.destroy = async function(ctx) { exports.destroy = async function(ctx) {
const database = new CouchDB(ctx.user.instanceId) const database = new CouchDB(ctx.user.appId)
await database.destroy(generateUserID(ctx.params.username)) await database.destroy(generateUserID(ctx.params.username))
ctx.message = `User ${ctx.params.username} deleted.` ctx.message = `User ${ctx.params.username} deleted.`
ctx.status = 200 ctx.status = 200
} }
exports.find = async function(ctx) { exports.find = async function(ctx) {
const database = new CouchDB(ctx.user.instanceId) const database = new CouchDB(ctx.user.appId)
const user = await database.get(generateUserID(ctx.params.username)) const user = await database.get(generateUserID(ctx.params.username))
ctx.body = { ctx.body = {
username: user.username, username: user.username,

View File

@ -8,7 +8,7 @@ const { fetchView } = require("../row")
const controller = { const controller = {
fetch: async ctx => { fetch: async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
const response = [] const response = []
@ -26,7 +26,7 @@ const controller = {
ctx.body = response ctx.body = response
}, },
save: async ctx => { save: async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const { originalName, ...viewToSave } = ctx.request.body const { originalName, ...viewToSave } = ctx.request.body
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
@ -63,7 +63,7 @@ const controller = {
ctx.message = `View ${viewToSave.name} saved successfully.` ctx.message = `View ${viewToSave.name} saved successfully.`
}, },
destroy: async ctx => { destroy: async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
const viewName = decodeURI(ctx.params.viewName) const viewName = decodeURI(ctx.params.viewName)

View File

@ -22,7 +22,7 @@ exports.WebhookType = {
} }
exports.fetch = async ctx => { exports.fetch = async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const response = await db.allDocs( const response = await db.allDocs(
getWebhookParams(null, { getWebhookParams(null, {
include_docs: true, include_docs: true,
@ -32,7 +32,7 @@ exports.fetch = async ctx => {
} }
exports.save = async ctx => { exports.save = async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
const webhook = ctx.request.body const webhook = ctx.request.body
webhook.appId = ctx.user.appId webhook.appId = ctx.user.appId
@ -53,7 +53,7 @@ exports.save = async ctx => {
} }
exports.destroy = async ctx => { exports.destroy = async ctx => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
ctx.body = await db.remove(ctx.params.id, ctx.params.rev) ctx.body = await db.remove(ctx.params.id, ctx.params.rev)
} }
@ -91,7 +91,7 @@ exports.trigger = async ctx => {
await triggers.externalTrigger(target, { await triggers.externalTrigger(target, {
body: ctx.request.body, body: ctx.request.body,
...ctx.request.body, ...ctx.request.body,
instanceId: ctx.params.instance, appId: ctx.params.instance,
}) })
} }
ctx.status = 200 ctx.status = 200

View File

@ -9,8 +9,6 @@ const {
pageRoutes, pageRoutes,
userRoutes, userRoutes,
deployRoutes, deployRoutes,
instanceRoutes,
clientRoutes,
applicationRoutes, applicationRoutes,
rowRoutes, rowRoutes,
tableRoutes, tableRoutes,
@ -83,9 +81,6 @@ router.use(rowRoutes.allowedMethods())
router.use(userRoutes.routes()) router.use(userRoutes.routes())
router.use(userRoutes.allowedMethods()) router.use(userRoutes.allowedMethods())
router.use(instanceRoutes.routes())
router.use(instanceRoutes.allowedMethods())
router.use(automationRoutes.routes()) router.use(automationRoutes.routes())
router.use(automationRoutes.allowedMethods()) router.use(automationRoutes.allowedMethods())
@ -108,9 +103,6 @@ router.use(applicationRoutes.allowedMethods())
router.use(componentRoutes.routes()) router.use(componentRoutes.routes())
router.use(componentRoutes.allowedMethods()) router.use(componentRoutes.allowedMethods())
router.use(clientRoutes.routes())
router.use(clientRoutes.allowedMethods())
router.use(accesslevelRoutes.routes()) router.use(accesslevelRoutes.routes())
router.use(accesslevelRoutes.allowedMethods()) router.use(accesslevelRoutes.allowedMethods())

View File

@ -8,12 +8,12 @@ const router = Router()
router router
.get("/api/applications", authorized(BUILDER), controller.fetch) .get("/api/applications", authorized(BUILDER), controller.fetch)
.get( .get(
"/api/:applicationId/appPackage", "/api/:appId/appPackage",
authorized(BUILDER), authorized(BUILDER),
controller.fetchAppPackage controller.fetchAppPackage
) )
.put("/api/:applicationId", authorized(BUILDER), controller.update) .put("/api/:appId", authorized(BUILDER), controller.update)
.post("/api/applications", authorized(BUILDER), controller.create) .post("/api/applications", authorized(BUILDER), controller.create)
.delete("/api/:applicationId", authorized(BUILDER), controller.delete) .delete("/api/:appId", authorized(BUILDER), controller.delete)
module.exports = router module.exports = router

View File

@ -1,10 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/client")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("../../utilities/accessLevels")
const router = Router()
router.get("/api/client/id", authorized(BUILDER), controller.getClientId)
module.exports = router

View File

@ -1,8 +1,6 @@
const authRoutes = require("./auth") const authRoutes = require("./auth")
const pageRoutes = require("./pages") const pageRoutes = require("./pages")
const userRoutes = require("./user") const userRoutes = require("./user")
const instanceRoutes = require("./instance")
const clientRoutes = require("./client")
const applicationRoutes = require("./application") const applicationRoutes = require("./application")
const tableRoutes = require("./table") const tableRoutes = require("./table")
const rowRoutes = require("./row") const rowRoutes = require("./row")
@ -22,8 +20,6 @@ module.exports = {
authRoutes, authRoutes,
pageRoutes, pageRoutes,
userRoutes, userRoutes,
instanceRoutes,
clientRoutes,
applicationRoutes, applicationRoutes,
rowRoutes, rowRoutes,
tableRoutes, tableRoutes,

View File

@ -1,12 +0,0 @@
const Router = require("@koa/router")
const controller = require("../controllers/instance")
const authorized = require("../../middleware/authorized")
const { BUILDER } = require("../../utilities/accessLevels")
const router = Router()
router
.post("/api/instances", authorized(BUILDER), controller.create)
.delete("/api/instances/:instanceId", authorized(BUILDER), controller.destroy)
module.exports = router

View File

@ -1,6 +1,4 @@
const { const {
createInstance,
createClientDatabase,
createApplication, createApplication,
createTable, createTable,
createView, createView,
@ -17,27 +15,25 @@ const {
} = require("../../../utilities/accessLevels") } = require("../../../utilities/accessLevels")
describe("/accesslevels", () => { describe("/accesslevels", () => {
let appId
let server let server
let request let request
let instanceId let appId
let table let table
let view let view
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ({ request, server } = await supertest())
await createClientDatabase(request);
appId = (await createApplication(request))._id
}); });
afterAll(async () => { afterAll(() => {
server.close(); server.close()
}) })
beforeEach(async () => { beforeEach(async () => {
instanceId = (await createInstance(request, appId))._id let app = await createApplication(request)
table = await createTable(request, appId, instanceId) appId = app.instance._id
view = await createView(request, appId, instanceId, table._id) table = await createTable(request, appId)
view = await createView(request, appId, table._id)
}) })
describe("create", () => { describe("create", () => {
@ -46,7 +42,7 @@ describe("/accesslevels", () => {
const res = await request const res = await request
.post(`/api/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user" }) .send({ name: "user" })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -64,7 +60,7 @@ describe("/accesslevels", () => {
const createRes = await request const createRes = await request
.post(`/api/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] }) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -72,7 +68,7 @@ describe("/accesslevels", () => {
const res = await request const res = await request
.get(`/api/accesslevels`) .get(`/api/accesslevels`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -80,11 +76,11 @@ describe("/accesslevels", () => {
const adminLevel = res.body.find(r => r._id === ADMIN_LEVEL_ID) const adminLevel = res.body.find(r => r._id === ADMIN_LEVEL_ID)
expect(adminLevel).toBeDefined() expect(adminLevel).toBeDefined()
expect(adminLevel.permissions).toEqual(await generateAdminPermissions(instanceId)) expect(adminLevel.permissions).toEqual(await generateAdminPermissions(appId))
const powerUserLevel = res.body.find(r => r._id === POWERUSER_LEVEL_ID) const powerUserLevel = res.body.find(r => r._id === POWERUSER_LEVEL_ID)
expect(powerUserLevel).toBeDefined() expect(powerUserLevel).toBeDefined()
expect(powerUserLevel.permissions).toEqual(await generatePowerUserPermissions(instanceId)) expect(powerUserLevel.permissions).toEqual(await generatePowerUserPermissions(appId))
const customLevelFetched = res.body.find(r => r._id === customLevel._id) const customLevelFetched = res.body.find(r => r._id === customLevel._id)
expect(customLevelFetched.permissions).toEqual(customLevel.permissions) expect(customLevelFetched.permissions).toEqual(customLevel.permissions)
@ -97,7 +93,7 @@ describe("/accesslevels", () => {
const createRes = await request const createRes = await request
.post(`/api/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE } ] }) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE } ] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -105,12 +101,12 @@ describe("/accesslevels", () => {
await request await request
.delete(`/api/accesslevels/${customLevel._id}/${customLevel._rev}`) .delete(`/api/accesslevels/${customLevel._id}/${customLevel._rev}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect(200) .expect(200)
await request await request
.get(`/api/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect(404) .expect(404)
}) })
}) })
@ -120,7 +116,7 @@ describe("/accesslevels", () => {
const createRes = await request const createRes = await request
.post(`/api/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] }) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -132,13 +128,13 @@ describe("/accesslevels", () => {
_rev: customLevel._rev, _rev: customLevel._rev,
addedPermissions: [ { itemId: table._id, name: WRITE_TABLE } ] addedPermissions: [ { itemId: table._id, name: WRITE_TABLE } ]
}) })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
const finalRes = await request const finalRes = await request
.get(`/api/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect(200) .expect(200)
expect(finalRes.body.permissions.length).toBe(2) expect(finalRes.body.permissions.length).toBe(2)
@ -156,7 +152,7 @@ describe("/accesslevels", () => {
{ itemId: table._id, name: WRITE_TABLE }, { itemId: table._id, name: WRITE_TABLE },
] ]
}) })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -168,13 +164,13 @@ describe("/accesslevels", () => {
_rev: customLevel._rev, _rev: customLevel._rev,
removedPermissions: [ { itemId: table._id, name: WRITE_TABLE }] removedPermissions: [ { itemId: table._id, name: WRITE_TABLE }]
}) })
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
const finalRes = await request const finalRes = await request
.get(`/api/accesslevels/${customLevel._id}`) .get(`/api/accesslevels/${customLevel._id}`)
.set(defaultHeaders(appId, instanceId)) .set(defaultHeaders(appId))
.expect(200) .expect(200)
expect(finalRes.body.permissions.length).toBe(1) expect(finalRes.body.permissions.length).toBe(1)

View File

@ -1,11 +1,8 @@
const { const {
createClientDatabase,
createApplication, createApplication,
createInstance,
destroyClientDatabase,
builderEndpointShouldBlockNormalUsers, builderEndpointShouldBlockNormalUsers,
supertest, supertest,
TEST_CLIENT_ID, clearApplications,
defaultHeaders, defaultHeaders,
} = require("./couchTestUtils") } = require("./couchTestUtils")
@ -18,14 +15,10 @@ describe("/applications", () => {
}); });
beforeEach(async () => { beforeEach(async () => {
await createClientDatabase() await clearApplications(request)
}) })
afterEach(async () => { afterAll(() => {
await destroyClientDatabase()
})
afterAll(async () => {
server.close() server.close()
}) })
@ -42,14 +35,13 @@ describe("/applications", () => {
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request) const otherApplication = await createApplication(request)
const instance = await createInstance(request, otherApplication._id) const appId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "POST", method: "POST",
url: `/api/applications`, url: `/api/applications`,
instanceId: instance._id, appId: appId,
appId: otherApplication._id,
body: { name: "My App" } body: { name: "My App" }
}) })
}) })
@ -58,7 +50,6 @@ describe("/applications", () => {
describe("fetch", () => { describe("fetch", () => {
it("lists all applications", async () => { it("lists all applications", async () => {
await createApplication(request, "app1") await createApplication(request, "app1")
await createApplication(request, "app2") await createApplication(request, "app2")
@ -71,46 +62,14 @@ describe("/applications", () => {
expect(res.body.length).toBe(2) expect(res.body.length).toBe(2)
}) })
it("lists only applications in requested client databse", async () => {
await createApplication(request, "app1")
await createClientDatabase("new_client")
const blah = await request
.post("/api/applications")
.send({ name: "app2", clientId: "new_client"})
.set(defaultHeaders())
.expect('Content-Type', /json/)
//.expect(200)
const client1Res = await request
.get(`/api/applications?clientId=${TEST_CLIENT_ID}`)
.set(defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(client1Res.body.length).toBe(1)
expect(client1Res.body[0].name).toBe("app1")
const client2Res = await request
.get(`/api/applications?clientId=new_client`)
.set(defaultHeaders())
.expect('Content-Type', /json/)
.expect(200)
expect(client2Res.body.length).toBe(1)
expect(client2Res.body[0].name).toBe("app2")
})
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request) const otherApplication = await createApplication(request)
const instance = await createInstance(request, otherApplication._id) const appId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "GET", method: "GET",
url: `/api/applications`, url: `/api/applications`,
instanceId: instance._id, appId: appId,
appId: otherApplication._id,
}) })
}) })
}) })

View File

@ -1,7 +1,5 @@
const { const {
createClientDatabase,
createApplication, createApplication,
createInstance,
createTable, createTable,
getAllFromTable, getAllFromTable,
defaultHeaders, defaultHeaders,
@ -41,27 +39,26 @@ describe("/automations", () => {
let request let request
let server let server
let app let app
let instance let appId
let automation let automation
let automationId let automationId
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ({ request, server } = await supertest())
await createClientDatabase(request)
app = await createApplication(request)
}) })
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request)
appId = app.instance._id
if (automation) await destroyDocument(automation.id) if (automation) await destroyDocument(automation.id)
instance = await createInstance(request, app._id)
}) })
afterAll(async () => { afterAll(() => {
server.close() server.close()
}) })
const createAutomation = async () => { const createAutomation = async () => {
automation = await insertDocument(instance._id, { automation = await insertDocument(appId, {
type: "automation", type: "automation",
...TEST_AUTOMATION ...TEST_AUTOMATION
}) })
@ -72,7 +69,7 @@ describe("/automations", () => {
return await request return await request
.post(`/api/automations/${automationId}/trigger`) .post(`/api/automations/${automationId}/trigger`)
.send({ name: "Test", description: "TEST" }) .send({ name: "Test", description: "TEST" })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
} }
@ -81,7 +78,7 @@ describe("/automations", () => {
it("returns a list of definitions for actions", async () => { it("returns a list of definitions for actions", async () => {
const res = await request const res = await request
.get(`/api/automations/action/list`) .get(`/api/automations/action/list`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -92,7 +89,7 @@ describe("/automations", () => {
it("returns a list of definitions for triggers", async () => { it("returns a list of definitions for triggers", async () => {
const res = await request const res = await request
.get(`/api/automations/trigger/list`) .get(`/api/automations/trigger/list`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -103,7 +100,7 @@ describe("/automations", () => {
it("returns a list of definitions for actions", async () => { it("returns a list of definitions for actions", async () => {
const res = await request const res = await request
.get(`/api/automations/logic/list`) .get(`/api/automations/logic/list`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -114,7 +111,7 @@ describe("/automations", () => {
it("returns all of the definitions in one", async () => { it("returns all of the definitions in one", async () => {
const res = await request const res = await request
.get(`/api/automations/definitions/list`) .get(`/api/automations/definitions/list`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -142,7 +139,7 @@ describe("/automations", () => {
it("returns a success message when the automation is successfully created", async () => { it("returns a success message when the automation is successfully created", async () => {
const res = await request const res = await request
.post(`/api/automations`) .post(`/api/automations`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.send(TEST_AUTOMATION) .send(TEST_AUTOMATION)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -158,8 +155,7 @@ describe("/automations", () => {
request, request,
method: "POST", method: "POST",
url: `/api/automations`, url: `/api/automations`,
instanceId: instance._id, appId: appId,
appId: app._id,
body: TEST_AUTOMATION body: TEST_AUTOMATION
}) })
}) })
@ -167,7 +163,7 @@ describe("/automations", () => {
describe("trigger", () => { describe("trigger", () => {
it("trigger the automation successfully", async () => { it("trigger the automation successfully", async () => {
let table = await createTable(request, app._id, instance._id) let table = await createTable(request, appId)
TEST_AUTOMATION.definition.trigger.inputs.tableId = table._id TEST_AUTOMATION.definition.trigger.inputs.tableId = table._id
TEST_AUTOMATION.definition.steps[0].inputs.row.tableId = table._id TEST_AUTOMATION.definition.steps[0].inputs.row.tableId = table._id
await createAutomation() await createAutomation()
@ -180,7 +176,7 @@ describe("/automations", () => {
expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`) expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`)
expect(res.body.automation.name).toEqual(TEST_AUTOMATION.name) expect(res.body.automation.name).toEqual(TEST_AUTOMATION.name)
await delay(500) await delay(500)
let elements = await getAllFromTable(request, app._id, instance._id, table._id) let elements = await getAllFromTable(request, appId, table._id)
// don't test it unless there are values to test // don't test it unless there are values to test
if (elements.length === 1) { if (elements.length === 1) {
expect(elements.length).toEqual(1) expect(elements.length).toEqual(1)
@ -203,7 +199,7 @@ describe("/automations", () => {
const res = await request const res = await request
.put(`/api/automations`) .put(`/api/automations`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.send(automation) .send(automation)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -218,7 +214,7 @@ describe("/automations", () => {
await createAutomation() await createAutomation()
const res = await request const res = await request
.get(`/api/automations`) .get(`/api/automations`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -230,8 +226,7 @@ describe("/automations", () => {
request, request,
method: "GET", method: "GET",
url: `/api/automations`, url: `/api/automations`,
instanceId: instance._id, appId: appId,
appId: app._id,
}) })
}) })
}) })
@ -241,7 +236,7 @@ describe("/automations", () => {
await createAutomation() await createAutomation()
const res = await request const res = await request
.delete(`/api/automations/${automation.id}/${automation.rev}`) .delete(`/api/automations/${automation.id}/${automation.rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -254,8 +249,7 @@ describe("/automations", () => {
request, request,
method: "DELETE", method: "DELETE",
url: `/api/automations/${automation.id}/${automation._rev}`, url: `/api/automations/${automation.id}/${automation._rev}`,
instanceId: instance._id, appId: appId,
appId: app._id,
}) })
}) })
}) })

View File

@ -1,5 +1,4 @@
const CouchDB = require("../../../db") const CouchDB = require("../../../db")
const { create, destroy } = require("../../../db/clientDb")
const supertest = require("supertest") const supertest = require("supertest")
const { const {
POWERUSER_LEVEL_ID, POWERUSER_LEVEL_ID,
@ -17,19 +16,18 @@ exports.TEST_CLIENT_ID = TEST_CLIENT_ID
exports.supertest = async () => { exports.supertest = async () => {
let request let request
let server let server
process.env.PORT = 4002 env.PORT = 4002
server = require("../../../app") server = require("../../../app")
request = supertest(server) request = supertest(server)
return { request, server } return { request, server }
} }
exports.defaultHeaders = (appId, instanceId) => { exports.defaultHeaders = appId => {
const builderUser = { const builderUser = {
userId: "BUILDER", userId: "BUILDER",
accessLevelId: BUILDER_LEVEL_ID, accessLevelId: BUILDER_LEVEL_ID,
appId, appId,
instanceId,
} }
const builderToken = jwt.sign(builderUser, env.JWT_SECRET) const builderToken = jwt.sign(builderUser, env.JWT_SECRET)
@ -40,7 +38,7 @@ exports.defaultHeaders = (appId, instanceId) => {
} }
} }
exports.createTable = async (request, appId, instanceId, table) => { exports.createTable = async (request, appId, table) => {
if (table != null && table._id) { if (table != null && table._id) {
delete table._id delete table._id
} }
@ -66,19 +64,19 @@ exports.createTable = async (request, appId, instanceId, table) => {
const res = await request const res = await request
.post(`/api/tables`) .post(`/api/tables`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
.send(table) .send(table)
return res.body return res.body
} }
exports.getAllFromTable = async (request, appId, instanceId, tableId) => { exports.getAllFromTable = async (request, appId, tableId) => {
const res = await request const res = await request
.get(`/api/${tableId}/rows`) .get(`/api/${tableId}/rows`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
return res.body return res.body
} }
exports.createView = async (request, appId, instanceId, tableId, view) => { exports.createView = async (request, appId, tableId, view) => {
view = view || { view = view || {
map: "function(doc) { emit(doc[doc.key], doc._id); } ", map: "function(doc) { emit(doc[doc.key], doc._id); } ",
tableId: tableId, tableId: tableId,
@ -86,13 +84,11 @@ exports.createView = async (request, appId, instanceId, tableId, view) => {
const res = await request const res = await request
.post(`/api/views`) .post(`/api/views`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
.send(view) .send(view)
return res.body return res.body
} }
exports.createClientDatabase = async id => await create(id || TEST_CLIENT_ID)
exports.createApplication = async (request, name = "test_application") => { exports.createApplication = async (request, name = "test_application") => {
const res = await request const res = await request
.post("/api/applications") .post("/api/applications")
@ -103,28 +99,25 @@ exports.createApplication = async (request, name = "test_application") => {
return res.body return res.body
} }
exports.destroyClientDatabase = async () => await destroy(TEST_CLIENT_ID) exports.clearApplications = async request => {
exports.createInstance = async (request, appId) => {
const res = await request const res = await request
.post(`/api/instances`) .get("/api/applications")
.send({ .set(exports.defaultHeaders())
name: "test-instance2", for (let app of res.body) {
}) const appId = app._id
.set(exports.defaultHeaders(appId)) await request.delete(`/api/${appId}`).set(exports.defaultHeaders(appId))
return res.body }
} }
exports.createUser = async ( exports.createUser = async (
request, request,
appId, appId,
instanceId,
username = "babs", username = "babs",
password = "babs_password" password = "babs_password"
) => { ) => {
const res = await request const res = await request
.post(`/api/users`) .post(`/api/users`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
.send({ .send({
name: "Bill", name: "Bill",
username, username,
@ -137,11 +130,10 @@ exports.createUser = async (
const createUserWithOnePermission = async ( const createUserWithOnePermission = async (
request, request,
appId, appId,
instanceId,
permName, permName,
itemId itemId
) => { ) => {
let permissions = await generateAdminPermissions(instanceId) let permissions = await generateAdminPermissions(appId)
permissions = permissions.filter( permissions = permissions.filter(
p => p.name === permName && p.itemId === itemId p => p.name === permName && p.itemId === itemId
) )
@ -149,19 +141,17 @@ const createUserWithOnePermission = async (
return await createUserWithPermissions( return await createUserWithPermissions(
request, request,
appId, appId,
instanceId,
permissions, permissions,
"onePermOnlyUser" "onePermOnlyUser"
) )
} }
const createUserWithAdminPermissions = async (request, appId, instanceId) => { const createUserWithAdminPermissions = async (request, appId) => {
let permissions = await generateAdminPermissions(instanceId) let permissions = await generateAdminPermissions(appId)
return await createUserWithPermissions( return await createUserWithPermissions(
request, request,
appId, appId,
instanceId,
permissions, permissions,
"adminUser" "adminUser"
) )
@ -170,11 +160,10 @@ const createUserWithAdminPermissions = async (request, appId, instanceId) => {
const createUserWithAllPermissionExceptOne = async ( const createUserWithAllPermissionExceptOne = async (
request, request,
appId, appId,
instanceId,
permName, permName,
itemId itemId
) => { ) => {
let permissions = await generateAdminPermissions(instanceId) let permissions = await generateAdminPermissions(appId)
permissions = permissions.filter( permissions = permissions.filter(
p => !(p.name === permName && p.itemId === itemId) p => !(p.name === permName && p.itemId === itemId)
) )
@ -182,7 +171,6 @@ const createUserWithAllPermissionExceptOne = async (
return await createUserWithPermissions( return await createUserWithPermissions(
request, request,
appId, appId,
instanceId,
permissions, permissions,
"allPermsExceptOneUser" "allPermsExceptOneUser"
) )
@ -191,19 +179,18 @@ const createUserWithAllPermissionExceptOne = async (
const createUserWithPermissions = async ( const createUserWithPermissions = async (
request, request,
appId, appId,
instanceId,
permissions, permissions,
username username
) => { ) => {
const accessRes = await request const accessRes = await request
.post(`/api/accesslevels`) .post(`/api/accesslevels`)
.send({ name: "TestLevel", permissions }) .send({ name: "TestLevel", permissions })
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
const password = `password_${username}` const password = `password_${username}`
await request await request
.post(`/api/users`) .post(`/api/users`)
.set(exports.defaultHeaders(appId, instanceId)) .set(exports.defaultHeaders(appId))
.send({ .send({
name: username, name: username,
username, username,
@ -238,14 +225,12 @@ exports.testPermissionsForEndpoint = async ({
url, url,
body, body,
appId, appId,
instanceId,
permissionName, permissionName,
itemId, itemId,
}) => { }) => {
const headers = await createUserWithOnePermission( const headers = await createUserWithOnePermission(
request, request,
appId, appId,
instanceId,
permissionName, permissionName,
itemId itemId
) )
@ -257,7 +242,6 @@ exports.testPermissionsForEndpoint = async ({
const noPermsHeaders = await createUserWithAllPermissionExceptOne( const noPermsHeaders = await createUserWithAllPermissionExceptOne(
request, request,
appId, appId,
instanceId,
permissionName, permissionName,
itemId itemId
) )
@ -273,13 +257,8 @@ exports.builderEndpointShouldBlockNormalUsers = async ({
url, url,
body, body,
appId, appId,
instanceId,
}) => { }) => {
const headers = await createUserWithAdminPermissions( const headers = await createUserWithAdminPermissions(request, appId)
request,
appId,
instanceId
)
await createRequest(request, method, url, body) await createRequest(request, method, url, body)
.set(headers) .set(headers)

View File

@ -1,51 +0,0 @@
const {
createInstance,
createClientDatabase,
createApplication,
supertest,
defaultHeaders
} = require("./couchTestUtils");
describe("/instances", () => {
let TEST_APP_ID;
let server
let request
beforeAll(async () => {
({ request, server } = await supertest())
await createClientDatabase(request);
TEST_APP_ID = (await createApplication(request))._id
});
afterAll(async () => {
server.close();
})
describe("create", () => {
it("returns a success message when the instance database is successfully created", async () => {
const res = await request
.post(`/api/instances`)
.send({ name: "test-instance" })
.set(defaultHeaders(TEST_APP_ID))
.expect('Content-Type', /json/)
.expect(200)
expect(res.res.statusMessage).toEqual("Instance Database test-instance successfully provisioned.");
expect(res.body._id).toBeDefined();
})
});
describe("destroy", () => {
it("returns a success message when the instance database is successfully deleted", async () => {
const instance = await createInstance(request, TEST_APP_ID);
const res = await request
.delete(`/api/instances/${instance._id}`)
.set(defaultHeaders(TEST_APP_ID))
.expect(200)
expect(res.res.statusMessage).toEqual(`Instance Database ${instance._id} successfully destroyed.`);
})
});
});

View File

@ -1,7 +1,5 @@
const { const {
createApplication, createApplication,
createClientDatabase,
createInstance,
createTable, createTable,
supertest, supertest,
defaultHeaders, defaultHeaders,
@ -10,24 +8,24 @@ const {
describe("/rows", () => { describe("/rows", () => {
let request let request
let server let server
let instance let appId
let table let table
let row let row
let app let app
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ({ request, server } = await supertest())
await createClientDatabase(request)
app = await createApplication(request)
}); });
afterAll(async () => { afterAll(() => {
server.close(); server.close()
}) })
beforeEach(async () => { beforeEach(async () => {
instance = await createInstance(request, app._id) app = await createApplication(request)
table = await createTable(request, app._id, instance._id) appId = app.instance._id
table = await createTable(request, appId)
row = { row = {
name: "Test Contact", name: "Test Contact",
description: "original description", description: "original description",
@ -40,14 +38,14 @@ describe("/rows", () => {
await request await request
.post(`/api/${r ? r.tableId : row.tableId}/rows`) .post(`/api/${r ? r.tableId : row.tableId}/rows`)
.send(r || row) .send(r || row)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
const loadRow = async id => const loadRow = async id =>
await request await request
.get(`/api/${table._id}/rows/${id}`) .get(`/api/${table._id}/rows/${id}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -74,7 +72,7 @@ describe("/rows", () => {
tableId: table._id, tableId: table._id,
name: "Updated Name", name: "Updated Name",
}) })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -88,7 +86,7 @@ describe("/rows", () => {
const res = await request const res = await request
.get(`/api/${table._id}/rows/${existing._id}`) .get(`/api/${table._id}/rows/${existing._id}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -111,7 +109,7 @@ describe("/rows", () => {
const res = await request const res = await request
.get(`/api/${table._id}/rows`) .get(`/api/${table._id}/rows`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -133,7 +131,7 @@ describe("/rows", () => {
const res = await request const res = await request
.post(`/api/rows/search`) .post(`/api/rows/search`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.send({ .send({
keys: rowIds keys: rowIds
}) })
@ -148,7 +146,7 @@ describe("/rows", () => {
await createRow() await createRow()
await request await request
.get(`/api/${table._id}/rows/not-a-valid-id`) .get(`/api/${table._id}/rows/not-a-valid-id`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(404) .expect(404)
}) })
@ -160,7 +158,7 @@ describe("/rows", () => {
const number = {type:"number", constraints: { type: "number", presence: false }} const number = {type:"number", constraints: { type: "number", presence: false }}
const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }} const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }}
table = await createTable(request, app._id, instance._id, { table = await createTable(request, appId, {
name: "TestTable2", name: "TestTable2",
type: "table", type: "table",
key: "name", key: "name",
@ -256,7 +254,7 @@ describe("/rows", () => {
tableId: table._id, tableId: table._id,
name: "Updated Name", name: "Updated Name",
}) })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -277,7 +275,7 @@ describe("/rows", () => {
const result = await request const result = await request
.post(`/api/${table._id}/rows/validate`) .post(`/api/${table._id}/rows/validate`)
.send({ name: "ivan" }) .send({ name: "ivan" })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -289,7 +287,7 @@ describe("/rows", () => {
const result = await request const result = await request
.post(`/api/${table._id}/rows/validate`) .post(`/api/${table._id}/rows/validate`)
.send({ name: 1 }) .send({ name: 1 })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)

View File

@ -1,8 +1,6 @@
const { const {
createInstance, createTable,
createTable, supertest,
supertest,
createClientDatabase,
createApplication, createApplication,
defaultHeaders, defaultHeaders,
builderEndpointShouldBlockNormalUsers, builderEndpointShouldBlockNormalUsers,
@ -13,23 +11,22 @@ describe("/tables", () => {
let request let request
let server let server
let app let app
let instance let appId
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ({ request, server } = await supertest())
await createClientDatabase(request)
app = await createApplication(request)
}); });
afterAll(async () => { afterAll(() => {
server.close(); server.close()
}) })
describe("create", () => { beforeEach(async () => {
beforeEach(async () => { app = await createApplication(request)
instance = await createInstance(request, app._id); appId = app.instance._id
}); });
describe("create", () => {
it("returns a success message when the table is successfully created", done => { it("returns a success message when the table is successfully created", done => {
request request
.post(`/api/tables`) .post(`/api/tables`)
@ -40,7 +37,7 @@ describe("/tables", () => {
name: { type: "string" } name: { type: "string" }
} }
}) })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (err, res) => { .end(async (err, res) => {
@ -51,14 +48,14 @@ describe("/tables", () => {
}) })
it("renames all the row fields for a table when a schema key is renamed", async () => { it("renames all the row fields for a table when a schema key is renamed", async () => {
const testTable = await createTable(request, app._id, instance._id); const testTable = await createTable(request, appId);
const testRow = await request const testRow = await request
.post(`/api/${testTable._id}/rows`) .post(`/api/${testTable._id}/rows`)
.send({ .send({
name: "test" name: "test"
}) })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -77,7 +74,7 @@ describe("/tables", () => {
updatedName: { type: "string" } updatedName: { type: "string" }
} }
}) })
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -86,7 +83,7 @@ describe("/tables", () => {
const res = await request const res = await request
.get(`/api/${testTable._id}/rows/${testRow.body._id}`) .get(`/api/${testTable._id}/rows/${testRow.body._id}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -99,8 +96,7 @@ describe("/tables", () => {
request, request,
method: "POST", method: "POST",
url: `/api/tables`, url: `/api/tables`,
instanceId: instance._id, appId: appId,
appId: app._id,
body: { body: {
name: "TestTable", name: "TestTable",
key: "name", key: "name",
@ -116,8 +112,7 @@ describe("/tables", () => {
let testTable let testTable
beforeEach(async () => { beforeEach(async () => {
instance = await createInstance(request, app._id) testTable = await createTable(request, appId, testTable)
testTable = await createTable(request, app._id, instance._id, testTable)
}); });
afterEach(() => { afterEach(() => {
@ -127,7 +122,7 @@ describe("/tables", () => {
it("returns all the tables for that instance in the response body", done => { it("returns all the tables for that instance in the response body", done => {
request request
.get(`/api/tables`) .get(`/api/tables`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
@ -143,8 +138,7 @@ describe("/tables", () => {
request, request,
method: "GET", method: "GET",
url: `/api/tables`, url: `/api/tables`,
instanceId: instance._id, appId: appId,
appId: app._id,
}) })
}) })
}); });
@ -153,8 +147,7 @@ describe("/tables", () => {
let testTable; let testTable;
beforeEach(async () => { beforeEach(async () => {
instance = await createInstance(request, app._id) testTable = await createTable(request, appId, testTable)
testTable = await createTable(request, app._id, instance._id, testTable)
}); });
afterEach(() => { afterEach(() => {
@ -164,7 +157,7 @@ describe("/tables", () => {
it("returns a success response when a table is deleted.", async done => { it("returns a success response when a table is deleted.", async done => {
request request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`) .delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
@ -174,7 +167,7 @@ describe("/tables", () => {
}) })
it("deletes linked references to the table after deletion", async done => { it("deletes linked references to the table after deletion", async done => {
const linkedTable = await createTable(request, app._id, instance._id, { const linkedTable = await createTable(request, appId, {
name: "LinkedTable", name: "LinkedTable",
type: "table", type: "table",
key: "name", key: "name",
@ -197,12 +190,12 @@ describe("/tables", () => {
request request
.delete(`/api/tables/${testTable._id}/${testTable._rev}`) .delete(`/api/tables/${testTable._id}/${testTable._rev}`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
.end(async (_, res) => { .end(async (_, res) => {
expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`);
const dependentTable = await getDocument(instance._id, linkedTable._id) const dependentTable = await getDocument(appId, linkedTable._id)
expect(dependentTable.schema.TestTable).not.toBeDefined(); expect(dependentTable.schema.TestTable).not.toBeDefined();
done(); done();
}); });
@ -213,8 +206,7 @@ describe("/tables", () => {
request, request,
method: "DELETE", method: "DELETE",
url: `/api/tables/${testTable._id}/${testTable._rev}`, url: `/api/tables/${testTable._id}/${testTable._rev}`,
instanceId: instance._id, appId: appId,
appId: app._id,
}) })
}) })

View File

@ -1,7 +1,5 @@
const { const {
createClientDatabase,
createApplication, createApplication,
createInstance,
supertest, supertest,
defaultHeaders, defaultHeaders,
createUser, createUser,
@ -17,30 +15,29 @@ describe("/users", () => {
let request let request
let server let server
let app let app
let instance let appId
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest(server)) ({ request, server } = await supertest(server))
await createClientDatabase(request)
app = await createApplication(request)
}); });
beforeEach(async () => { beforeEach(async () => {
instance = await createInstance(request, app._id) app = await createApplication(request)
appId = app.instance._id
}); });
afterAll(async () => { afterAll(() => {
server.close(); server.close()
server.destroy()
}) })
describe("fetch", () => { describe("fetch", () => {
it("returns a list of users from an instance db", async () => { it("returns a list of users from an instance db", async () => {
await createUser(request, app._id, instance._id, "brenda", "brendas_password") await createUser(request, appId, "brenda", "brendas_password")
await createUser(request, app._id, instance._id, "pam", "pam_password") await createUser(request, appId, "pam", "pam_password")
const res = await request const res = await request
.get(`/api/users`) .get(`/api/users`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
@ -50,13 +47,12 @@ describe("/users", () => {
}) })
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
await createUser(request, app._id, instance._id, "brenda", "brendas_password") await createUser(request, appId, "brenda", "brendas_password")
await testPermissionsForEndpoint({ await testPermissionsForEndpoint({
request, request,
method: "GET", method: "GET",
url: `/api/users`, url: `/api/users`,
instanceId: instance._id, appId: appId,
appId: app._id,
permissionName: LIST_USERS, permissionName: LIST_USERS,
}) })
}) })
@ -68,7 +64,7 @@ describe("/users", () => {
it("returns a success message when a user is successfully created", async () => { it("returns a success message when a user is successfully created", async () => {
const res = await request const res = await request
.post(`/api/users`) .post(`/api/users`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID }) .send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID })
.expect(200) .expect(200)
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
@ -83,8 +79,7 @@ describe("/users", () => {
method: "POST", method: "POST",
body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID }, body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID },
url: `/api/users`, url: `/api/users`,
instanceId: instance._id, appId: appId,
appId: app._id,
permissionName: USER_MANAGEMENT, permissionName: USER_MANAGEMENT,
}) })
}) })

View File

@ -1,7 +1,5 @@
const { const {
createClientDatabase,
createApplication, createApplication,
createInstance,
createTable, createTable,
supertest, supertest,
defaultHeaders, defaultHeaders,
@ -12,7 +10,7 @@ describe("/views", () => {
let request let request
let server let server
let app let app
let instance let appId
let table let table
const createView = async (config = { const createView = async (config = {
@ -24,34 +22,33 @@ describe("/views", () => {
await request await request
.post(`/api/views`) .post(`/api/views`)
.send(config) .send(config)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
const createRow = async row => request const createRow = async row => request
.post(`/api/${table._id}/rows`) .post(`/api/${table._id}/rows`)
.send(row) .send(row)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
beforeAll(async () => { beforeAll(async () => {
({ request, server } = await supertest()) ({ request, server } = await supertest())
await createClientDatabase(request)
app = await createApplication(request)
}) })
beforeEach(async () => { beforeEach(async () => {
instance = await createInstance(request, app._id) app = await createApplication(request)
appId = app.instance._id
}) })
afterAll(async () => { afterAll(() => {
server.close() server.close()
}) })
describe("create", () => { describe("create", () => {
beforeEach(async () => { beforeEach(async () => {
table = await createTable(request, app._id, instance._id); table = await createTable(request, appId);
}) })
it("returns a success message when the view is successfully created", async () => { it("returns a success message when the view is successfully created", async () => {
@ -62,7 +59,7 @@ describe("/views", () => {
it("updates the table row with the new view metadata", async () => { it("updates the table row with the new view metadata", async () => {
const res = await createView() const res = await createView()
expect(res.res.statusMessage).toEqual("View TestView saved successfully."); expect(res.res.statusMessage).toEqual("View TestView saved successfully.");
const updatedTable = await getDocument(instance._id, table._id) const updatedTable = await getDocument(appId, table._id)
expect(updatedTable.views).toEqual({ expect(updatedTable.views).toEqual({
TestView: { TestView: {
field: "Price", field: "Price",
@ -99,14 +96,14 @@ describe("/views", () => {
describe("fetch", () => { describe("fetch", () => {
beforeEach(async () => { beforeEach(async () => {
table = await createTable(request, app._id, instance._id); table = await createTable(request, appId);
}); });
it("returns only custom views", async () => { it("returns only custom views", async () => {
await createView() await createView()
const res = await request const res = await request
.get(`/api/views`) .get(`/api/views`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body.length).toBe(1) expect(res.body.length).toBe(1)
@ -116,7 +113,7 @@ describe("/views", () => {
describe("query", () => { describe("query", () => {
beforeEach(async () => { beforeEach(async () => {
table = await createTable(request, app._id, instance._id); table = await createTable(request, appId);
}); });
it("returns data for the created view", async () => { it("returns data for the created view", async () => {
@ -135,7 +132,7 @@ describe("/views", () => {
}) })
const res = await request const res = await request
.get(`/api/views/TestView?calculation=stats`) .get(`/api/views/TestView?calculation=stats`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)
expect(res.body.length).toBe(1) expect(res.body.length).toBe(1)
@ -167,7 +164,7 @@ describe("/views", () => {
}) })
const res = await request const res = await request
.get(`/api/views/TestView?calculation=stats&group=Category`) .get(`/api/views/TestView?calculation=stats&group=Category`)
.set(defaultHeaders(app._id, instance._id)) .set(defaultHeaders(appId))
.expect('Content-Type', /json/) .expect('Content-Type', /json/)
.expect(200) .expect(200)

View File

@ -30,7 +30,7 @@ app.context.auth = {}
app.use(api.routes()) app.use(api.routes())
if (electron.app && electron.app.isPackaged) { if (electron.app && electron.app.isPackaged) {
process.env.NODE_ENV = "production" env._set("NODE_ENV", "production")
Sentry.init() Sentry.init()
app.on("error", (err, ctx) => { app.on("error", (err, ctx) => {

View File

@ -4,7 +4,7 @@ const updateRow = require("./steps/updateRow")
const deleteRow = require("./steps/deleteRow") const deleteRow = require("./steps/deleteRow")
const createUser = require("./steps/createUser") const createUser = require("./steps/createUser")
const outgoingWebhook = require("./steps/outgoingWebhook") const outgoingWebhook = require("./steps/outgoingWebhook")
const environment = require("../environment") const env = require("../environment")
const download = require("download") const download = require("download")
const fetch = require("node-fetch") const fetch = require("node-fetch")
const { join } = require("../utilities/centralPath") const { join } = require("../utilities/centralPath")
@ -33,8 +33,8 @@ const BUILTIN_DEFINITIONS = {
OUTGOING_WEBHOOK: outgoingWebhook.definition, OUTGOING_WEBHOOK: outgoingWebhook.definition,
} }
let AUTOMATION_BUCKET = environment.AUTOMATION_BUCKET let AUTOMATION_BUCKET = env.AUTOMATION_BUCKET
let AUTOMATION_DIRECTORY = environment.AUTOMATION_DIRECTORY let AUTOMATION_DIRECTORY = env.AUTOMATION_DIRECTORY
let MANIFEST = null let MANIFEST = null
function buildBundleName(pkgName, version) { function buildBundleName(pkgName, version) {

View File

@ -88,13 +88,13 @@ module.exports.cleanInputValues = (inputs, schema) => {
* the automation but is instead part of the Table/Table. This function will get the table schema and use it to instead * the automation but is instead part of the Table/Table. This function will get the table schema and use it to instead
* perform the cleanInputValues function on the input row. * perform the cleanInputValues function on the input row.
* *
* @param {string} instanceId The instance which the Table/Table is contained under. * @param {string} appId The instance which the Table/Table is contained under.
* @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for. * @param {string} tableId The ID of the Table/Table which the schema is to be retrieved for.
* @param {object} row The input row structure which requires clean-up after having been through mustache statements. * @param {object} row The input row structure which requires clean-up after having been through mustache statements.
* @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types. * @returns {Promise<Object>} The cleaned up rows object, will should now have all the required primitive types.
*/ */
module.exports.cleanUpRow = async (instanceId, tableId, row) => { module.exports.cleanUpRow = async (appId, tableId, row) => {
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const table = await db.get(tableId) const table = await db.get(tableId)
return module.exports.cleanInputValues(row, { properties: table.schema }) return module.exports.cleanInputValues(row, { properties: table.schema })
@ -104,13 +104,13 @@ module.exports.cleanUpRow = async (instanceId, tableId, row) => {
* A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean * A utility function for the cleanUpRow, which can be used if only the row ID is known (not the table ID) to clean
* up a row after mustache statements have been replaced. This is specifically useful for the update row action. * up a row after mustache statements have been replaced. This is specifically useful for the update row action.
* *
* @param {string} instanceId The instance which the Table/Table is contained under. * @param {string} appId The instance which the Table/Table is contained under.
* @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema. * @param {string} rowId The ID of the row from which the tableId will be extracted, to get the Table/Table schema.
* @param {object} row The input row structure which requires clean-up after having been through mustache statements. * @param {object} row The input row structure which requires clean-up after having been through mustache statements.
* @returns {Promise<Object>} The cleaned up rows object, which will now have all the required primitive types. * @returns {Promise<Object>} The cleaned up rows object, which will now have all the required primitive types.
*/ */
module.exports.cleanUpRowById = async (instanceId, rowId, row) => { module.exports.cleanUpRowById = async (appId, rowId, row) => {
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const foundRow = await db.get(rowId) const foundRow = await db.get(rowId)
return module.exports.cleanUpRow(instanceId, foundRow.tableId, row) return module.exports.cleanUpRow(appId, foundRow.tableId, row)
} }

View File

@ -1,6 +1,6 @@
const triggers = require("./triggers") const triggers = require("./triggers")
const actions = require("./actions") const actions = require("./actions")
const environment = require("../environment") const env = require("../environment")
const workerFarm = require("worker-farm") const workerFarm = require("worker-farm")
const singleThread = require("./thread") const singleThread = require("./thread")
const { getAPIKey, update, Properties } = require("../utilities/usageQuota") const { getAPIKey, update, Properties } = require("../utilities/usageQuota")
@ -34,10 +34,10 @@ module.exports.init = function() {
actions.init().then(() => { actions.init().then(() => {
triggers.automationQueue.process(async job => { triggers.automationQueue.process(async job => {
try { try {
if (environment.CLOUD && job.data.automation) { if (env.CLOUD && job.data.automation) {
job.data.automation.apiKey = await updateQuota(job.data.automation) job.data.automation.apiKey = await updateQuota(job.data.automation)
} }
if (environment.BUDIBASE_ENVIRONMENT === "PRODUCTION") { if (env.BUDIBASE_ENVIRONMENT === "PRODUCTION") {
await runWorker(job) await runWorker(job)
} else { } else {
await singleThread(job) await singleThread(job)

View File

@ -1,6 +1,6 @@
const rowController = require("../../api/controllers/row") const rowController = require("../../api/controllers/row")
const automationUtils = require("../automationUtils") const automationUtils = require("../automationUtils")
const environment = require("../../environment") const env = require("../../environment")
const usage = require("../../utilities/usageQuota") const usage = require("../../utilities/usageQuota")
module.exports.definition = { module.exports.definition = {
@ -58,13 +58,13 @@ module.exports.definition = {
}, },
} }
module.exports.run = async function({ inputs, instanceId, apiKey }) { module.exports.run = async function({ inputs, appId, apiKey }) {
// TODO: better logging of when actions are missed due to missing parameters // TODO: better logging of when actions are missed due to missing parameters
if (inputs.row == null || inputs.row.tableId == null) { if (inputs.row == null || inputs.row.tableId == null) {
return return
} }
inputs.row = await automationUtils.cleanUpRow( inputs.row = await automationUtils.cleanUpRow(
instanceId, appId,
inputs.row.tableId, inputs.row.tableId,
inputs.row inputs.row
) )
@ -76,11 +76,11 @@ module.exports.run = async function({ inputs, instanceId, apiKey }) {
request: { request: {
body: inputs.row, body: inputs.row,
}, },
user: { instanceId }, user: { appId },
} }
try { try {
if (environment.CLOUD) { if (env.CLOUD) {
await usage.update(apiKey, usage.Properties.ROW, 1) await usage.update(apiKey, usage.Properties.ROW, 1)
} }
await rowController.save(ctx) await rowController.save(ctx)

View File

@ -1,6 +1,6 @@
const accessLevels = require("../../utilities/accessLevels") const accessLevels = require("../../utilities/accessLevels")
const userController = require("../../api/controllers/user") const userController = require("../../api/controllers/user")
const environment = require("../../environment") const env = require("../../environment")
const usage = require("../../utilities/usageQuota") const usage = require("../../utilities/usageQuota")
module.exports.definition = { module.exports.definition = {
@ -58,11 +58,11 @@ module.exports.definition = {
}, },
} }
module.exports.run = async function({ inputs, instanceId, apiKey }) { module.exports.run = async function({ inputs, appId, apiKey }) {
const { username, password, accessLevelId } = inputs const { username, password, accessLevelId } = inputs
const ctx = { const ctx = {
user: { user: {
instanceId: instanceId, appId: appId,
}, },
request: { request: {
body: { username, password, accessLevelId }, body: { username, password, accessLevelId },
@ -70,7 +70,7 @@ module.exports.run = async function({ inputs, instanceId, apiKey }) {
} }
try { try {
if (environment.CLOUD) { if (env.CLOUD) {
await usage.update(apiKey, usage.Properties.USER, 1) await usage.update(apiKey, usage.Properties.USER, 1)
} }
await userController.create(ctx) await userController.create(ctx)

View File

@ -1,5 +1,5 @@
const rowController = require("../../api/controllers/row") const rowController = require("../../api/controllers/row")
const environment = require("../../environment") const env = require("../../environment")
const usage = require("../../utilities/usageQuota") const usage = require("../../utilities/usageQuota")
module.exports.definition = { module.exports.definition = {
@ -50,7 +50,7 @@ module.exports.definition = {
}, },
} }
module.exports.run = async function({ inputs, instanceId, apiKey }) { module.exports.run = async function({ inputs, appId, apiKey }) {
// TODO: better logging of when actions are missed due to missing parameters // TODO: better logging of when actions are missed due to missing parameters
if (inputs.id == null || inputs.revision == null) { if (inputs.id == null || inputs.revision == null) {
return return
@ -61,11 +61,11 @@ module.exports.run = async function({ inputs, instanceId, apiKey }) {
rowId: inputs.id, rowId: inputs.id,
revId: inputs.revision, revId: inputs.revision,
}, },
user: { instanceId }, user: { appId },
} }
try { try {
if (environment.CLOUD) { if (env.CLOUD) {
await usage.update(apiKey, usage.Properties.ROW, -1) await usage.update(apiKey, usage.Properties.ROW, -1)
} }
await rowController.destroy(ctx) await rowController.destroy(ctx)

View File

@ -53,13 +53,13 @@ module.exports.definition = {
}, },
} }
module.exports.run = async function({ inputs, instanceId }) { module.exports.run = async function({ inputs, appId }) {
if (inputs.rowId == null || inputs.row == null) { if (inputs.rowId == null || inputs.row == null) {
return return
} }
inputs.row = await automationUtils.cleanUpRowById( inputs.row = await automationUtils.cleanUpRowById(
instanceId, appId,
inputs.rowId, inputs.rowId,
inputs.row inputs.row
) )
@ -78,7 +78,7 @@ module.exports.run = async function({ inputs, instanceId }) {
request: { request: {
body: inputs.row, body: inputs.row,
}, },
user: { instanceId }, user: { appId },
} }
try { try {

View File

@ -32,9 +32,9 @@ function recurseMustache(inputs, context) {
*/ */
class Orchestrator { class Orchestrator {
constructor(automation, triggerOutput) { constructor(automation, triggerOutput) {
this._instanceId = triggerOutput.instanceId this._appId = triggerOutput.appId
// remove from context // remove from context
delete triggerOutput.instanceId delete triggerOutput.appId
// step zero is never used as the mustache is zero indexed for customer facing // step zero is never used as the mustache is zero indexed for customer facing
this._context = { steps: [{}], trigger: triggerOutput } this._context = { steps: [{}], trigger: triggerOutput }
this._automation = automation this._automation = automation
@ -62,11 +62,11 @@ class Orchestrator {
step.inputs, step.inputs,
step.schema.inputs step.schema.inputs
) )
// instanceId is always passed // appId is always passed
try { try {
const outputs = await stepFn({ const outputs = await stepFn({
inputs: step.inputs, inputs: step.inputs,
instanceId: this._instanceId, appId: this._appId,
apiKey: automation.apiKey, apiKey: automation.apiKey,
}) })
if (step.stepId === FILTER_STEP_ID && !outputs.success) { if (step.stepId === FILTER_STEP_ID && !outputs.success) {

View File

@ -122,10 +122,10 @@ const BUILTIN_DEFINITIONS = {
} }
async function queueRelevantRowAutomations(event, eventType) { async function queueRelevantRowAutomations(event, eventType) {
if (event.instanceId == null) { if (event.appId == null) {
throw `No instanceId specified for ${eventType} - check event emitters.` throw `No appId specified for ${eventType} - check event emitters.`
} }
const db = new CouchDB(event.instanceId) const db = new CouchDB(event.appId)
let automations = await db.allDocs( let automations = await db.allDocs(
getAutomationParams(null, { include_docs: true }) getAutomationParams(null, { include_docs: true })
) )
@ -169,7 +169,7 @@ emitter.on("row:delete", async function(event) {
async function fillRowOutput(automation, params) { async function fillRowOutput(automation, params) {
let triggerSchema = automation.definition.trigger let triggerSchema = automation.definition.trigger
let tableId = triggerSchema.inputs.tableId let tableId = triggerSchema.inputs.tableId
const db = new CouchDB(params.instanceId) const db = new CouchDB(params.appId)
try { try {
let table = await db.get(tableId) let table = await db.get(tableId)
let row = {} let row = {}

View File

@ -1,26 +0,0 @@
const CouchDB = require("./client")
exports.create = async clientId => {
const dbId = exports.name(clientId)
const db = new CouchDB(dbId)
await db.put({
_id: "_design/client",
views: {
by_type: {
map: `function(doc) {
emit([doc.type], doc._id)
}`,
},
},
})
return db
}
exports.destroy = async function(clientId) {
const dbId = exports.name(clientId)
await new CouchDB(dbId).destroy()
}
exports.name = function(clientId) {
return `client_${clientId}`
}

View File

@ -1,7 +1,7 @@
let _ = require("lodash") let _ = require("lodash")
let environment = require("../environment") let env = require("../environment")
const AWS_REGION = environment.AWS_REGION ? environment.AWS_REGION : "eu-west-1" const AWS_REGION = env.AWS_REGION ? env.AWS_REGION : "eu-west-1"
const TableInfo = { const TableInfo = {
API_KEYS: { API_KEYS: {
@ -110,8 +110,8 @@ exports.init = endpoint => {
} }
if (endpoint) { if (endpoint) {
docClientParams.endpoint = endpoint docClientParams.endpoint = endpoint
} else if (environment.DYNAMO_ENDPOINT) { } else if (env.DYNAMO_ENDPOINT) {
docClientParams.endpoint = environment.DYNAMO_ENDPOINT docClientParams.endpoint = env.DYNAMO_ENDPOINT
} }
docClient = new AWS.DynamoDB.DocumentClient(docClientParams) docClient = new AWS.DynamoDB.DocumentClient(docClientParams)
} }
@ -119,10 +119,10 @@ exports.init = endpoint => {
exports.apiKeyTable = new Table(TableInfo.API_KEYS) exports.apiKeyTable = new Table(TableInfo.API_KEYS)
exports.userTable = new Table(TableInfo.USERS) exports.userTable = new Table(TableInfo.USERS)
if (environment.CLOUD) { if (env.CLOUD) {
exports.init(`https://dynamodb.${AWS_REGION}.amazonaws.com`) exports.init(`https://dynamodb.${AWS_REGION}.amazonaws.com`)
} else { } else {
process.env.AWS_ACCESS_KEY_ID = "KEY_ID" env._set("AWS_ACCESS_KEY_ID", "KEY_ID")
process.env.AWS_SECRET_ACCESS_KEY = "SECRET_KEY" env._set("AWS_SECRET_ACCESS_KEY", "SECRET_KEY")
exports.init("http://localhost:8333") exports.init("http://localhost:8333")
} }

View File

@ -40,9 +40,9 @@ function LinkDocument(
} }
class LinkController { class LinkController {
constructor({ instanceId, tableId, row, table, oldTable }) { constructor({ appId, tableId, row, table, oldTable }) {
this._instanceId = instanceId this._appId = appId
this._db = new CouchDB(instanceId) this._db = new CouchDB(appId)
this._tableId = tableId this._tableId = tableId
this._row = row this._row = row
this._table = table this._table = table
@ -87,7 +87,7 @@ class LinkController {
*/ */
getRowLinkDocs(rowId) { getRowLinkDocs(rowId) {
return getLinkDocuments({ return getLinkDocuments({
instanceId: this._instanceId, appId: this._appId,
tableId: this._tableId, tableId: this._tableId,
rowId, rowId,
includeDocs: IncludeDocs.INCLUDE, includeDocs: IncludeDocs.INCLUDE,
@ -99,7 +99,7 @@ class LinkController {
*/ */
getTableLinkDocs() { getTableLinkDocs() {
return getLinkDocuments({ return getLinkDocuments({
instanceId: this._instanceId, appId: this._appId,
tableId: this._tableId, tableId: this._tableId,
includeDocs: IncludeDocs.INCLUDE, includeDocs: IncludeDocs.INCLUDE,
}) })

View File

@ -31,7 +31,7 @@ exports.createLinkView = createLinkView
* Update link documents for a row or table - this is to be called by the API controller when a change is occurring. * Update link documents for a row or table - this is to be called by the API controller when a change is occurring.
* @param {string} eventType states what type of change which is occurring, means this can be expanded upon in the * @param {string} eventType states what type of change which is occurring, means this can be expanded upon in the
* future quite easily (all updates go through one function). * future quite easily (all updates go through one function).
* @param {string} instanceId The ID of the instance in which the change is occurring. * @param {string} appId The ID of the instance in which the change is occurring.
* @param {string} tableId The ID of the of the table which is being changed. * @param {string} tableId The ID of the of the table which is being changed.
* @param {object|null} row The row which is changing, e.g. created, updated or deleted. * @param {object|null} row The row which is changing, e.g. created, updated or deleted.
* @param {object|null} table If the table has already been retrieved this can be used to reduce database gets. * @param {object|null} table If the table has already been retrieved this can be used to reduce database gets.
@ -41,14 +41,14 @@ exports.createLinkView = createLinkView
*/ */
exports.updateLinks = async function({ exports.updateLinks = async function({
eventType, eventType,
instanceId, appId,
row, row,
tableId, tableId,
table, table,
oldTable, oldTable,
}) { }) {
const baseReturnObj = row == null ? table : row const baseReturnObj = row == null ? table : row
if (instanceId == null) { if (appId == null) {
throw "Cannot operate without an instance ID." throw "Cannot operate without an instance ID."
} }
// make sure table ID is set // make sure table ID is set
@ -86,13 +86,13 @@ exports.updateLinks = async function({
/** /**
* Update a row with information about the links that pertain to it. * Update a row with information about the links that pertain to it.
* @param {string} instanceId The instance in which this row has been created. * @param {string} appId The instance in which this row has been created.
* @param {object} rows The row(s) themselves which is to be updated with info (if applicable). This can be * @param {object} rows The row(s) themselves which is to be updated with info (if applicable). This can be
* a single row object or an array of rows - both will be handled. * a single row object or an array of rows - both will be handled.
* @returns {Promise<object>} The updated row (this may be the same if no links were found). If an array was input * @returns {Promise<object>} The updated row (this may be the same if no links were found). If an array was input
* then an array will be output, object input -> object output. * then an array will be output, object input -> object output.
*/ */
exports.attachLinkInfo = async (instanceId, rows) => { exports.attachLinkInfo = async (appId, rows) => {
// handle a single row as well as multiple // handle a single row as well as multiple
let wasArray = true let wasArray = true
if (!(rows instanceof Array)) { if (!(rows instanceof Array)) {
@ -105,7 +105,7 @@ exports.attachLinkInfo = async (instanceId, rows) => {
await Promise.all( await Promise.all(
tableIds.map(tableId => tableIds.map(tableId =>
getLinkDocuments({ getLinkDocuments({
instanceId, appId,
tableId: tableId, tableId: tableId,
includeDocs: IncludeDocs.EXCLUDE, includeDocs: IncludeDocs.EXCLUDE,
}) })

View File

@ -13,12 +13,12 @@ exports.IncludeDocs = {
/** /**
* Creates the link view for the instance, this will overwrite the existing one, but this should only * Creates the link view for the instance, this will overwrite the existing one, but this should only
* be called if it is found that the view does not exist. * be called if it is found that the view does not exist.
* @param {string} instanceId The instance to which the view should be added. * @param {string} appId The instance to which the view should be added.
* @returns {Promise<void>} The view now exists, please note that the next view of this query will actually build it, * @returns {Promise<void>} The view now exists, please note that the next view of this query will actually build it,
* so it may be slow. * so it may be slow.
*/ */
exports.createLinkView = async instanceId => { exports.createLinkView = async appId => {
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
const designDoc = await db.get("_design/database") const designDoc = await db.get("_design/database")
const view = { const view = {
map: function(doc) { map: function(doc) {
@ -47,7 +47,7 @@ exports.createLinkView = async instanceId => {
/** /**
* Gets the linking documents, not the linked documents themselves. * Gets the linking documents, not the linked documents themselves.
* @param {string} instanceId The instance in which we are searching for linked rows. * @param {string} appId The instance in which we are searching for linked rows.
* @param {string} tableId The table which we are searching for linked rows against. * @param {string} tableId The table which we are searching for linked rows against.
* @param {string|null} fieldName The name of column/field which is being altered, only looking for * @param {string|null} fieldName The name of column/field which is being altered, only looking for
* linking documents that are related to it. If this is not specified then the table level will be assumed. * linking documents that are related to it. If this is not specified then the table level will be assumed.
@ -60,12 +60,12 @@ exports.createLinkView = async instanceId => {
* (if any). * (if any).
*/ */
exports.getLinkDocuments = async function({ exports.getLinkDocuments = async function({
instanceId, appId,
tableId, tableId,
rowId, rowId,
includeDocs, includeDocs,
}) { }) {
const db = new CouchDB(instanceId) const db = new CouchDB(appId)
let params let params
if (rowId != null) { if (rowId != null) {
params = { key: [tableId, rowId] } params = { key: [tableId, rowId] }
@ -85,7 +85,7 @@ exports.getLinkDocuments = async function({
} catch (err) { } catch (err) {
// check if the view doesn't exist, it should for all new instances // check if the view doesn't exist, it should for all new instances
if (err != null && err.name === "not_found") { if (err != null && err.name === "not_found") {
await exports.createLinkView(instanceId) await exports.createLinkView(appId)
return exports.getLinkDocuments(arguments[0]) return exports.getLinkDocuments(arguments[0])
} else { } else {
Sentry.captureException(err) Sentry.captureException(err)

View File

@ -12,6 +12,7 @@ const DocumentTypes = {
APP: "app", APP: "app",
ACCESS_LEVEL: "ac", ACCESS_LEVEL: "ac",
WEBHOOK: "wh", WEBHOOK: "wh",
INSTANCE: "inst",
} }
exports.DocumentTypes = DocumentTypes exports.DocumentTypes = DocumentTypes

View File

@ -21,6 +21,7 @@ module.exports = {
COUCH_DB_URL: process.env.COUCH_DB_URL, COUCH_DB_URL: process.env.COUCH_DB_URL,
SALT_ROUNDS: process.env.SALT_ROUNDS, SALT_ROUNDS: process.env.SALT_ROUNDS,
LOGGER: process.env.LOGGER, LOGGER: process.env.LOGGER,
LOG_LEVEL: process.env.LOG_LEVEL,
AUTOMATION_DIRECTORY: process.env.AUTOMATION_DIRECTORY, AUTOMATION_DIRECTORY: process.env.AUTOMATION_DIRECTORY,
AUTOMATION_BUCKET: process.env.AUTOMATION_BUCKET, AUTOMATION_BUCKET: process.env.AUTOMATION_BUCKET,
BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT, BUDIBASE_ENVIRONMENT: process.env.BUDIBASE_ENVIRONMENT,
@ -30,4 +31,11 @@ module.exports = {
AWS_REGION: process.env.AWS_REGION, AWS_REGION: process.env.AWS_REGION,
DEPLOYMENT_CREDENTIALS_URL: process.env.DEPLOYMENT_CREDENTIALS_URL, DEPLOYMENT_CREDENTIALS_URL: process.env.DEPLOYMENT_CREDENTIALS_URL,
BUDIBASE_API_KEY: process.env.BUDIBASE_API_KEY, BUDIBASE_API_KEY: process.env.BUDIBASE_API_KEY,
USERID_API_KEY: process.env.USERID_API_KEY,
ENABLE_ANALYTICS: process.env.ENABLE_ANALYTICS,
DEPLOYMENT_DB_URL: process.env.DEPLOYMENT_DB_URL,
_set(key, value) {
process.env[key] = value
module.exports[key] = value
},
} }

View File

@ -11,10 +11,10 @@ const EventEmitter = require("events").EventEmitter
* This is specifically quite important for mustache used in automations. * This is specifically quite important for mustache used in automations.
*/ */
class BudibaseEmitter extends EventEmitter { class BudibaseEmitter extends EventEmitter {
emitRow(eventName, instanceId, row, table = null) { emitRow(eventName, appId, row, table = null) {
let event = { let event = {
row, row,
instanceId, appId,
tableId: row.tableId, tableId: row.tableId,
} }
if (table) { if (table) {
@ -27,14 +27,14 @@ class BudibaseEmitter extends EventEmitter {
this.emit(eventName, event) this.emit(eventName, event)
} }
emitTable(eventName, instanceId, table = null) { emitTable(eventName, appId, table = null) {
const tableId = table._id const tableId = table._id
let event = { let event = {
table: { table: {
...table, ...table,
tableId: tableId, tableId: tableId,
}, },
instanceId, appId,
tableId: tableId, tableId: tableId,
} }
event.id = tableId event.id = tableId

View File

@ -7,7 +7,7 @@ const {
BUILDER_LEVEL_ID, BUILDER_LEVEL_ID,
ANON_LEVEL_ID, ANON_LEVEL_ID,
} = require("../utilities/accessLevels") } = require("../utilities/accessLevels")
const environment = require("../environment") const env = require("../environment")
const { AuthTypes } = require("../constants") const { AuthTypes } = require("../constants")
module.exports = async (ctx, next) => { module.exports = async (ctx, next) => {
@ -21,7 +21,7 @@ module.exports = async (ctx, next) => {
let token let token
// if running locally in the builder itself // if running locally in the builder itself
if (!environment.CLOUD && !appToken) { if (!env.CLOUD && !appToken) {
token = builderToken token = builderToken
ctx.auth.authenticated = AuthTypes.BUILDER ctx.auth.authenticated = AuthTypes.BUILDER
} else { } else {
@ -32,7 +32,7 @@ module.exports = async (ctx, next) => {
if (!token) { if (!token) {
ctx.auth.authenticated = false ctx.auth.authenticated = false
let appId = process.env.CLOUD ? ctx.subdomains[1] : ctx.params.appId let appId = env.CLOUD ? ctx.subdomains[1] : ctx.params.appId
// if appId can't be determined from path param or subdomain // if appId can't be determined from path param or subdomain
if (!appId && ctx.request.headers.referer) { if (!appId && ctx.request.headers.referer) {
@ -53,9 +53,9 @@ module.exports = async (ctx, next) => {
ctx.auth.apiKey = jwtPayload.apiKey ctx.auth.apiKey = jwtPayload.apiKey
ctx.user = { ctx.user = {
...jwtPayload, ...jwtPayload,
instanceId: jwtPayload.instanceId, appId: jwtPayload.appId,
accessLevel: await getAccessLevel( accessLevel: await getAccessLevel(
jwtPayload.instanceId, jwtPayload.appId,
jwtPayload.accessLevelId jwtPayload.accessLevelId
), ),
} }
@ -70,10 +70,10 @@ module.exports = async (ctx, next) => {
* Return the full access level object either from constants * Return the full access level object either from constants
* or the database based on the access level ID passed. * or the database based on the access level ID passed.
* *
* @param {*} instanceId - instanceId of the user * @param {*} appId - appId of the user
* @param {*} accessLevelId - the id of the users access level * @param {*} accessLevelId - the id of the users access level
*/ */
const getAccessLevel = async (instanceId, accessLevelId) => { const getAccessLevel = async (appId, accessLevelId) => {
if ( if (
accessLevelId === POWERUSER_LEVEL_ID || accessLevelId === POWERUSER_LEVEL_ID ||
accessLevelId === ADMIN_LEVEL_ID || accessLevelId === ADMIN_LEVEL_ID ||
@ -92,7 +92,7 @@ const getAccessLevel = async (instanceId, accessLevelId) => {
levelId: accessLevelId, levelId: accessLevelId,
}, },
user: { user: {
instanceId, appId,
}, },
} }
await accessLevelController.find(findAccessContext) await accessLevelController.find(findAccessContext)

View File

@ -5,7 +5,7 @@ const {
BUILDER_LEVEL_ID, BUILDER_LEVEL_ID,
BUILDER, BUILDER,
} = require("../utilities/accessLevels") } = require("../utilities/accessLevels")
const environment = require("../environment") const env = require("../environment")
const { apiKeyTable } = require("../db/dynamoClient") const { apiKeyTable } = require("../db/dynamoClient")
const { AuthTypes } = require("../constants") const { AuthTypes } = require("../constants")
@ -13,14 +13,10 @@ const LOCAL_PASS = new RegExp(["webhooks/trigger", "webhooks/schema"].join("|"))
module.exports = (permName, getItemId) => async (ctx, next) => { module.exports = (permName, getItemId) => async (ctx, next) => {
// webhooks can pass locally // webhooks can pass locally
if (!environment.CLOUD && LOCAL_PASS.test(ctx.request.url)) { if (!env.CLOUD && LOCAL_PASS.test(ctx.request.url)) {
return next() return next()
} }
if ( if (env.CLOUD && ctx.headers["x-api-key"] && ctx.headers["x-instanceid"]) {
environment.CLOUD &&
ctx.headers["x-api-key"] &&
ctx.headers["x-instanceid"]
) {
// api key header passed by external webhook // api key header passed by external webhook
const apiKeyInfo = await apiKeyTable.get({ const apiKeyInfo = await apiKeyTable.get({
primary: ctx.headers["x-api-key"], primary: ctx.headers["x-api-key"],
@ -32,7 +28,7 @@ module.exports = (permName, getItemId) => async (ctx, next) => {
apiKey: ctx.headers["x-api-key"], apiKey: ctx.headers["x-api-key"],
} }
ctx.user = { ctx.user = {
instanceId: ctx.headers["x-instanceid"], appId: ctx.headers["x-instanceid"],
} }
return next() return next()
} }
@ -41,7 +37,7 @@ module.exports = (permName, getItemId) => async (ctx, next) => {
} }
// don't expose builder endpoints in the cloud // don't expose builder endpoints in the cloud
if (environment.CLOUD && permName === BUILDER) return if (env.CLOUD && permName === BUILDER) return
if (!ctx.auth.authenticated) { if (!ctx.auth.authenticated) {
ctx.throw(403, "Session not authenticated") ctx.throw(403, "Session not authenticated")

View File

@ -1,6 +1,6 @@
const CouchDB = require("../db") const CouchDB = require("../db")
const usageQuota = require("../utilities/usageQuota") const usageQuota = require("../utilities/usageQuota")
const environment = require("../environment") const env = require("../environment")
// currently only counting new writes and deletes // currently only counting new writes and deletes
const METHOD_MAP = { const METHOD_MAP = {
@ -27,7 +27,7 @@ function getProperty(url) {
} }
module.exports = async (ctx, next) => { module.exports = async (ctx, next) => {
const db = new CouchDB(ctx.user.instanceId) const db = new CouchDB(ctx.user.appId)
let usage = METHOD_MAP[ctx.req.method] let usage = METHOD_MAP[ctx.req.method]
const property = getProperty(ctx.req.url) const property = getProperty(ctx.req.url)
if (usage == null || property == null) { if (usage == null || property == null) {
@ -51,7 +51,7 @@ module.exports = async (ctx, next) => {
: [ctx.request.files.file] : [ctx.request.files.file]
usage = files.map(file => file.size).reduce((total, size) => total + size) usage = files.map(file => file.size).reduce((total, size) => total + size)
} }
if (!environment.CLOUD) { if (!env.CLOUD) {
return next() return next()
} }
try { try {

View File

@ -30,7 +30,7 @@
"className": "", "className": "",
"onLoad": [], "onLoad": [],
"type": "div", "type": "div",
"_instanceId": "inst_app_80b_f158d4057d2c4bedb0042d42fda8abaf", "_appId": "inst_app_80b_f158d4057d2c4bedb0042d42fda8abaf",
"_instanceName": "Header", "_instanceName": "Header",
"_children": [ "_children": [
{ {
@ -59,7 +59,7 @@
"borderWidth": "", "borderWidth": "",
"borderColor": "", "borderColor": "",
"borderStyle": "", "borderStyle": "",
"_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394",
"_instanceName": "Navigation", "_instanceName": "Navigation",
"_children": [ "_children": [
{ {
@ -88,7 +88,7 @@
"underline": false, "underline": false,
"fontSize": "", "fontSize": "",
"fontFamily": "initial", "fontFamily": "initial",
"_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394",
"_instanceName": "Home Link", "_instanceName": "Home Link",
"_children": [] "_children": []
} }

View File

@ -36,7 +36,7 @@
"className": "", "className": "",
"text": "Welcome to your Budibase App 👋", "text": "Welcome to your Budibase App 👋",
"type": "h2", "type": "h2",
"_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394",
"_instanceName": "Heading", "_instanceName": "Heading",
"_children": [] "_children": []
}, },
@ -62,7 +62,7 @@
"className": "", "className": "",
"onLoad": [], "onLoad": [],
"type": "div", "type": "div",
"_instanceId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", "_appId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c",
"_instanceName": "Video Container", "_instanceName": "Video Container",
"_children": [ "_children": [
{ {
@ -88,7 +88,7 @@
}, },
"_code": "", "_code": "",
"embed": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/dQw4w9WgXcQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>", "embed": "<iframe width=\"560\" height=\"315\" src=\"https://www.youtube.com/embed/dQw4w9WgXcQ\" frameborder=\"0\" allow=\"accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture\" allowfullscreen></iframe>",
"_instanceId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", "_appId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c",
"_instanceName": "Rick Astley Video", "_instanceName": "Rick Astley Video",
"_children": [] "_children": []
} }

View File

@ -10,7 +10,6 @@ const {
} = require("fs-extra") } = require("fs-extra")
const { join, resolve } = require("../centralPath") const { join, resolve } = require("../centralPath")
const { dirname } = require("path") const { dirname } = require("path")
const env = require("../../environment")
const buildPage = require("./buildPage") const buildPage = require("./buildPage")
const getPages = require("./getPages") const getPages = require("./getPages")
@ -30,10 +29,7 @@ module.exports.getPackageForBuilder = async (config, application) => {
return { return {
pages, pages,
application, application,
clientId: env.CLIENT_ID,
} }
} }

View File

@ -1,17 +1,16 @@
const { BUILDER_LEVEL_ID } = require("../accessLevels") const { BUILDER_LEVEL_ID } = require("../accessLevels")
const environment = require("../../environment") const env = require("../../environment")
const jwt = require("jsonwebtoken") const jwt = require("jsonwebtoken")
module.exports = (ctx, appId, instanceId, version) => { module.exports = (ctx, appId, version) => {
const builderUser = { const builderUser = {
userId: "BUILDER", userId: "BUILDER",
accessLevelId: BUILDER_LEVEL_ID, accessLevelId: BUILDER_LEVEL_ID,
instanceId,
appId, appId,
version, version,
} }
if (environment.BUDIBASE_API_KEY) { if (env.BUDIBASE_API_KEY) {
builderUser.apiKey = environment.BUDIBASE_API_KEY builderUser.apiKey = env.BUDIBASE_API_KEY
} }
const token = jwt.sign(builderUser, ctx.config.jwtSecret, { const token = jwt.sign(builderUser, ctx.config.jwtSecret, {
expiresIn: "30 days", expiresIn: "30 days",

View File

@ -1,10 +1,12 @@
const env = require("../environment")
exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms)) exports.wait = ms => new Promise(resolve => setTimeout(resolve, ms))
exports.isDev = () => { exports.isDev = () => {
return ( return (
!process.env.CLOUD && !env.CLOUD &&
process.env.NODE_ENV !== "production" && env.NODE_ENV !== "production" &&
process.env.NODE_ENV !== "jest" && env.NODE_ENV !== "jest" &&
process.env.NODE_ENV !== "cypress" env.NODE_ENV !== "cypress"
) )
} }

View File

@ -7,13 +7,7 @@ module.exports = async opts => {
await ensureDir(opts.dir) await ensureDir(opts.dir)
await setCouchDbUrl(opts) await setCouchDbUrl(opts)
// need an env file to create the client database // need an env file
await createDevEnvFile(opts)
await createClientDatabase(opts)
// need to recreate the env file, as we only now have a client id
// quiet flag will force overwrite of config
opts.quiet = true
await createDevEnvFile(opts) await createDevEnvFile(opts)
} }
@ -41,15 +35,3 @@ const createDevEnvFile = async opts => {
await writeFile(destConfigFile, config, { flag: "w+" }) await writeFile(destConfigFile, config, { flag: "w+" })
} }
} }
const createClientDatabase = async opts => {
// cannot be a top level require as it
// will cause environment module to be loaded prematurely
const clientDb = require("../db/clientDb")
if (!opts.clientId || opts.clientId === "new") {
opts.clientId = uuid.v4()
}
await clientDb.create(opts.clientId)
}

View File

@ -4,15 +4,15 @@ const automationController = require("../api/controllers/automation")
const accessLevels = require("./accessLevels") const accessLevels = require("./accessLevels")
// this has been broken out to reduce risk of circular dependency from utilities, no enums defined here // this has been broken out to reduce risk of circular dependency from utilities, no enums defined here
const generateAdminPermissions = async instanceId => [ const generateAdminPermissions = async appId => [
...accessLevels.adminPermissions, ...accessLevels.adminPermissions,
...(await generatePowerUserPermissions(instanceId)), ...(await generatePowerUserPermissions(appId)),
] ]
const generatePowerUserPermissions = async instanceId => { const generatePowerUserPermissions = async appId => {
const fetchTablesCtx = { const fetchTablesCtx = {
user: { user: {
instanceId, appId,
}, },
} }
await tableController.fetch(fetchTablesCtx) await tableController.fetch(fetchTablesCtx)
@ -20,7 +20,7 @@ const generatePowerUserPermissions = async instanceId => {
const fetchViewsCtx = { const fetchViewsCtx = {
user: { user: {
instanceId, appId,
}, },
} }
await viewController.fetch(fetchViewsCtx) await viewController.fetch(fetchViewsCtx)
@ -28,7 +28,7 @@ const generatePowerUserPermissions = async instanceId => {
const fetchAutomationsCtx = { const fetchAutomationsCtx = {
user: { user: {
instanceId, appId,
}, },
} }
await automationController.fetch(fetchAutomationsCtx) await automationController.fetch(fetchAutomationsCtx)

View File

@ -33,11 +33,7 @@ exports.downloadTemplate = async function(type, name) {
return join(budibaseAppsDir(), "templates", type, name) return join(budibaseAppsDir(), "templates", type, name)
} }
exports.exportTemplateFromApp = async function({ exports.exportTemplateFromApp = async function({ templateName, appId }) {
appId,
templateName,
instanceId,
}) {
// Copy frontend files // Copy frontend files
const appToExport = join(os.homedir(), ".budibase", appId, "pages") const appToExport = join(os.homedir(), ".budibase", appId, "pages")
const templatesDir = join(os.homedir(), ".budibase", "templates") const templatesDir = join(os.homedir(), ".budibase", "templates")
@ -52,7 +48,7 @@ exports.exportTemplateFromApp = async function({
) )
// perform couch dump // perform couch dump
const instanceDb = new CouchDB(instanceId) const instanceDb = new CouchDB(appId)
await instanceDb.dump(writeStream) await instanceDb.dump(writeStream)
return templateOutputPath return templateOutputPath

View File

@ -1,4 +1,4 @@
const environment = require("../environment") const env = require("../environment")
const { apiKeyTable } = require("../db/dynamoClient") const { apiKeyTable } = require("../db/dynamoClient")
const DEFAULT_USAGE = { const DEFAULT_USAGE = {
@ -63,7 +63,7 @@ exports.getAPIKey = async appId => {
*/ */
exports.update = async (apiKey, property, usage) => { exports.update = async (apiKey, property, usage) => {
// don't try validate in builder // don't try validate in builder
if (!environment.CLOUD) { if (!env.CLOUD) {
return return
} }
try { try {