diff --git a/packages/server/scripts/exportAppTemplate.js b/packages/server/scripts/exportAppTemplate.js index 95ab8c718c..4ae2f729ae 100755 --- a/packages/server/scripts/exportAppTemplate.js +++ b/packages/server/scripts/exportAppTemplate.js @@ -3,7 +3,7 @@ const { exportTemplateFromApp } = require("../src/utilities/templates") const yargs = require("yargs") // 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 .command( @@ -15,8 +15,8 @@ yargs alias: "n", type: "string", }, - instanceId: { - description: "The instanceId to dump the database for", + appId: { + description: "The appId to dump the database for", alias: "inst", type: "string", }, @@ -30,7 +30,6 @@ yargs console.log("Exporting app..") const exportPath = await exportTemplateFromApp({ templateName: args.name, - instanceId: args.instanceId, appId: args.appId, }) console.log(`Template ${args.name} exported to ${exportPath}`) diff --git a/packages/server/src/api/controllers/accesslevel.js b/packages/server/src/api/controllers/accesslevel.js index ab145a1b76..143c633e42 100644 --- a/packages/server/src/api/controllers/accesslevel.js +++ b/packages/server/src/api/controllers/accesslevel.js @@ -11,7 +11,7 @@ const { } = require("../../db/utils") exports.fetch = async function(ctx) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const body = await db.allDocs( getAccessLevelParams(null, { include_docs: true, @@ -23,12 +23,12 @@ exports.fetch = async function(ctx) { { _id: ADMIN_LEVEL_ID, name: "Admin", - permissions: await generateAdminPermissions(ctx.user.instanceId), + permissions: await generateAdminPermissions(ctx.user.appId), }, { _id: POWERUSER_LEVEL_ID, 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) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) ctx.body = await db.get(ctx.params.levelId) } 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) level.name = ctx.body.name level.permissions = ctx.request.body.permissions @@ -52,7 +52,7 @@ exports.update = 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 { removedPermissions, addedPermissions, _rev } = ctx.request.body @@ -88,7 +88,7 @@ exports.patch = async function(ctx) { } exports.create = async function(ctx) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const level = { name: ctx.request.body.name, @@ -105,7 +105,7 @@ exports.create = 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) ctx.message = `Access Level ${ctx.params.id} deleted successfully` ctx.status = 200 diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index 5e538eb38f..0765d50f6d 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -19,9 +19,9 @@ const { const APP_PREFIX = DocumentTypes.APP + SEPARATOR async function createInstance(template) { - const instanceId = generateAppID() + const appId = generateAppID() - const db = new CouchDB(instanceId) + const db = new CouchDB(appId) await db.put({ _id: "_design/database", // view collation information, read before writing any complex views: @@ -29,7 +29,7 @@ async function createInstance(template) { views: {}, }) // add view for linked rows - await createLinkView(instanceId) + await createLinkView(appId) // replicate the template data to the instance DB if (template) { @@ -43,7 +43,7 @@ async function createInstance(template) { } } - return { _id: instanceId } + return { _id: appId } } exports.fetch = async function(ctx) { @@ -62,17 +62,17 @@ exports.fetch = async function(ctx) { } exports.fetchAppPackage = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) - const application = await db.get(ctx.params.instanceId) + const db = new CouchDB(ctx.params.appId) + const application = await db.get(ctx.params.appId) ctx.body = await getPackageForBuilder(ctx.config, application) - setBuilderToken(ctx, ctx.params.instanceId, application.version) + setBuilderToken(ctx, ctx.params.appId, application.version) } exports.create = async function(ctx) { const instance = await createInstance(ctx.request.body.template) - const instanceId = instance._id + const appId = instance._id const newApplication = { - _id: instanceId, + _id: appId, type: "app", userInstanceMap: {}, version: packageJson.version, @@ -81,7 +81,7 @@ exports.create = async function(ctx) { template: ctx.request.body.template, instance: instance, } - const instanceDb = new CouchDB(instanceId) + const instanceDb = new CouchDB(appId) await instanceDb.put(newApplication) if (env.NODE_ENV !== "jest") { @@ -95,8 +95,8 @@ exports.create = async function(ctx) { } exports.update = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) - const application = await db.get(ctx.params.instanceId) + const db = new CouchDB(ctx.params.appId) + const application = await db.get(ctx.params.appId) const data = ctx.request.body const newData = { ...application, ...data } @@ -110,12 +110,12 @@ exports.update = async function(ctx) { } exports.delete = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) - const app = await db.get(ctx.params.instanceId) + const db = new CouchDB(ctx.params.appId) + const app = await db.get(ctx.params.appId) const result = await db.destroy() // remove top level directory - await fs.rmdir(join(budibaseAppsDir(), ctx.params.instanceId), { + await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), { recursive: true, }) diff --git a/packages/server/src/api/controllers/auth.js b/packages/server/src/api/controllers/auth.js index eb5fd8d11f..9576d22da1 100644 --- a/packages/server/src/api/controllers/auth.js +++ b/packages/server/src/api/controllers/auth.js @@ -6,8 +6,8 @@ const { getAPIKey } = require("../../utilities/usageQuota") const { generateUserID } = require("../../db/utils") exports.authenticate = async ctx => { - const instanceId = ctx.user.instanceId - if (!instanceId) ctx.throw(400, "No instanceId") + const appId = ctx.user.appId + if (!appId) ctx.throw(400, "No appId") const { username, password } = ctx.request.body @@ -15,8 +15,8 @@ exports.authenticate = async ctx => { if (!password) ctx.throw(400, "Password Required.") // Check the user exists in the instance DB by username - const db = new CouchDB(instanceId) - const app = await db.get(instanceId) + const db = new CouchDB(appId) + const app = await db.get(appId) let dbUser try { @@ -32,9 +32,8 @@ exports.authenticate = async ctx => { const payload = { userId: dbUser._id, accessLevelId: dbUser.accessLevelId, - appId: ctx.user.appId, version: app.version, - instanceId, + appId, } // if in cloud add the user api key if (env.CLOUD) { diff --git a/packages/server/src/api/controllers/automation.js b/packages/server/src/api/controllers/automation.js index 2127c0a661..5fa618654b 100644 --- a/packages/server/src/api/controllers/automation.js +++ b/packages/server/src/api/controllers/automation.js @@ -56,7 +56,7 @@ async function checkForWebhooks({ user, oldAuto, newAuto }) { !isWebhookTrigger(newAuto) && oldTrigger.webhookId ) { - let db = new CouchDB(user.instanceId) + let db = new CouchDB(user.appId) // need to get the webhook to get the rev const webhook = await db.get(oldTrigger.webhookId) const ctx = { @@ -86,15 +86,15 @@ async function checkForWebhooks({ user, oldAuto, newAuto }) { const id = ctx.body.webhook._id newTrigger.webhookId = id newTrigger.inputs = { - schemaUrl: `api/webhooks/schema/${user.instanceId}/${id}`, - triggerUrl: `api/webhooks/trigger/${user.instanceId}/${id}`, + schemaUrl: `api/webhooks/schema/${user.appId}/${id}`, + triggerUrl: `api/webhooks/trigger/${user.appId}/${id}`, } } return newAuto } exports.create = async function(ctx) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) let automation = ctx.request.body automation.appId = ctx.user.appId @@ -117,7 +117,7 @@ exports.create = 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 automation.appId = ctx.user.appId const oldAutomation = await db.get(automation._id) @@ -142,7 +142,7 @@ exports.update = 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( getAutomationParams(null, { include_docs: true, @@ -152,12 +152,12 @@ exports.fetch = 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) } 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) await checkForWebhooks({ user: ctx.user, oldAuto: oldAutomation }) 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) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) let automation = await db.get(ctx.params.id) await triggers.externalTrigger(automation, { ...ctx.request.body, - instanceId: ctx.user.instanceId, + appId: ctx.user.appId, }) ctx.status = 200 ctx.body = { diff --git a/packages/server/src/api/controllers/component.js b/packages/server/src/api/controllers/component.js index e6c25b2d83..c58e40eadc 100644 --- a/packages/server/src/api/controllers/component.js +++ b/packages/server/src/api/controllers/component.js @@ -6,13 +6,13 @@ const { } = require("../../utilities/budibaseDir") exports.fetchAppComponentDefinitions = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) - const app = await db.get(ctx.params.instanceId) + const db = new CouchDB(ctx.params.appId) + const app = await db.get(ctx.params.appId) ctx.body = app.componentLibraries.reduce((acc, componentLibrary) => { let appDirectory = resolve( budibaseAppsDir(), - ctx.params.instanceId, + ctx.params.appId, "node_modules" ) diff --git a/packages/server/src/api/controllers/deploy/aws.js b/packages/server/src/api/controllers/deploy/aws.js index eea7be052d..3e19812a00 100644 --- a/packages/server/src/api/controllers/deploy/aws.js +++ b/packages/server/src/api/controllers/deploy/aws.js @@ -62,24 +62,21 @@ exports.updateDeploymentQuota = async function(quota) { throw new Error(`Error updating deployment quota for API Key`) } - const json = await response.json() - - return json + return await response.json() } /** * Verifies the users API key and * 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 {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(env.DEPLOYMENT_CREDENTIALS_URL, { method: "POST", body: JSON.stringify({ apiKey: env.BUDIBASE_API_KEY, - instanceId, appId, quota, }), @@ -159,7 +156,6 @@ exports.prepareUploadForS3 = prepareUploadForS3 exports.uploadAppAssets = async function({ appId, - instanceId, bucket, cfDistribution, accountId, @@ -193,7 +189,7 @@ exports.uploadAppAssets = async function({ } // Upload file attachments - const db = new PouchDB(instanceId) + const db = new PouchDB(appId) let fileUploads try { fileUploads = await db.get("_local/fileuploads") diff --git a/packages/server/src/api/controllers/deploy/index.js b/packages/server/src/api/controllers/deploy/index.js index 0ab7a3791e..8b4bb003aa 100644 --- a/packages/server/src/api/controllers/deploy/index.js +++ b/packages/server/src/api/controllers/deploy/index.js @@ -81,9 +81,9 @@ function replicate(local, remote) { }) } -async function replicateCouch({ instanceId, session }) { - const localDb = new PouchDB(instanceId) - const remoteDb = new CouchDB(`${env.DEPLOYMENT_DB_URL}/${instanceId}`, { +async function replicateCouch({ appId, session }) { + const localDb = new PouchDB(appId) + const remoteDb = new CouchDB(`${env.DEPLOYMENT_DB_URL}/${appId}`, { fetch: function(url, opts) { opts.headers.set("Cookie", `${session};`) return CouchDB.fetch(url, opts) @@ -93,8 +93,8 @@ async function replicateCouch({ instanceId, session }) { return replicate(localDb, remoteDb) } -async function getCurrentInstanceQuota(instanceId) { - const db = new PouchDB(instanceId) +async function getCurrentInstanceQuota(appId) { + const db = new PouchDB(appId) const rows = await db.allDocs({ startkey: DocumentTypes.ROW + SEPARATOR, @@ -119,7 +119,7 @@ async function getCurrentInstanceQuota(instanceId) { } async function storeLocalDeploymentHistory(deployment) { - const db = new PouchDB(deployment.instanceId) + const db = new PouchDB(deployment.appId) let deploymentDoc try { @@ -147,11 +147,10 @@ async function storeLocalDeploymentHistory(deployment) { } } -async function deployApp({ instanceId, appId, deploymentId }) { +async function deployApp({ appId, deploymentId }) { try { - const instanceQuota = await getCurrentInstanceQuota(instanceId) + const instanceQuota = await getCurrentInstanceQuota(appId) const verification = await verifyDeployment({ - instanceId, appId, quota: instanceQuota, }) @@ -160,14 +159,13 @@ async function deployApp({ instanceId, appId, deploymentId }) { const invalidationId = await uploadAppAssets({ appId, - instanceId, ...verification, }) // replicate the DB to the couchDB cluster in prod console.log("Replicating local PouchDB to remote..") await replicateCouch({ - instanceId, + appId, session: verification.couchDbSession, }) @@ -175,7 +173,7 @@ async function deployApp({ instanceId, appId, deploymentId }) { await storeLocalDeploymentHistory({ _id: deploymentId, - instanceId, + appId, invalidationId, cfDistribution: verification.cfDistribution, quota: verification.quota, @@ -184,7 +182,7 @@ async function deployApp({ instanceId, appId, deploymentId }) { } catch (err) { await storeLocalDeploymentHistory({ _id: deploymentId, - instanceId, + appId, status: DeploymentStatus.FAILURE, err: err.message, }) @@ -194,7 +192,7 @@ async function deployApp({ instanceId, appId, deploymentId }) { exports.fetchDeployments = async function(ctx) { try { - const db = new PouchDB(ctx.user.instanceId) + const db = new PouchDB(ctx.user.appId) const deploymentDoc = await db.get("_local/deployments") const { updated, deployments } = await checkAllDeployments( deploymentDoc, @@ -211,7 +209,7 @@ exports.fetchDeployments = async function(ctx) { exports.deploymentProgress = async function(ctx) { try { - const db = new PouchDB(ctx.user.instanceId) + const db = new PouchDB(ctx.user.appId) const deploymentDoc = await db.get("_local/deployments") ctx.body = deploymentDoc[ctx.params.deploymentId] } catch (err) { @@ -224,7 +222,6 @@ exports.deploymentProgress = async function(ctx) { exports.deployApp = async function(ctx) { const deployment = await storeLocalDeploymentHistory({ - instanceId: ctx.user.instanceId, appId: ctx.user.appId, status: DeploymentStatus.PENDING, }) diff --git a/packages/server/src/api/controllers/row.js b/packages/server/src/api/controllers/row.js index 1e12bf7a78..c4a44581d1 100644 --- a/packages/server/src/api/controllers/row.js +++ b/packages/server/src/api/controllers/row.js @@ -28,8 +28,8 @@ validateJs.extend(validateJs.validators.datetime, { }) exports.patch = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) let row = await db.get(ctx.params.id) const table = await db.get(row.tableId) const patchfields = ctx.request.body @@ -56,7 +56,7 @@ exports.patch = async function(ctx) { // returned row is cleaned and prepared for writing to DB row = await linkRows.updateLinks({ - instanceId, + appId, eventType: linkRows.EventType.ROW_UPDATE, row, tableId: row.tableId, @@ -67,15 +67,15 @@ exports.patch = async function(ctx) { row.type = "row" ctx.eventEmitter && - ctx.eventEmitter.emitRow(`row:update`, instanceId, row, table) + ctx.eventEmitter.emitRow(`row:update`, appId, row, table) ctx.body = row ctx.status = 200 ctx.message = `${table.name} updated successfully.` } exports.save = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) let row = ctx.request.body row.tableId = ctx.params.tableId @@ -112,7 +112,7 @@ exports.save = async function(ctx) { // make sure link rows are up to date row = await linkRows.updateLinks({ - instanceId, + appId, eventType: linkRows.EventType.ROW_SAVE, row, tableId: row.tableId, @@ -134,15 +134,15 @@ exports.save = async function(ctx) { row._rev = response.rev ctx.eventEmitter && - ctx.eventEmitter.emitRow(`row:save`, instanceId, row, table) + ctx.eventEmitter.emitRow(`row:save`, appId, row, table) ctx.body = row ctx.status = 200 ctx.message = `${table.name} created successfully` } exports.fetchView = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const { calculation, group, field } = ctx.query const viewName = ctx.params.viewName @@ -160,7 +160,7 @@ exports.fetchView = async function(ctx) { if (!calculation) { 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) { @@ -186,8 +186,8 @@ exports.fetchView = async function(ctx) { } exports.fetchTableRows = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const response = await db.allDocs( getRowParams(ctx.params.tableId, null, { include_docs: true, @@ -195,45 +195,45 @@ exports.fetchTableRows = async function(ctx) { ) ctx.body = response.rows.map(row => row.doc) ctx.body = await linkRows.attachLinkInfo( - instanceId, + appId, response.rows.map(row => row.doc) ) } exports.search = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const response = await db.allDocs({ include_docs: true, ...ctx.request.body, }) ctx.body = await linkRows.attachLinkInfo( - instanceId, + appId, response.rows.map(row => row.doc) ) } exports.find = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const row = await db.get(ctx.params.rowId) if (row.tableId !== ctx.params.tableId) { ctx.throw(400, "Supplied tableId does not match the rows tableId") return } - ctx.body = await linkRows.attachLinkInfo(instanceId, row) + ctx.body = await linkRows.attachLinkInfo(appId, row) } exports.destroy = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const row = await db.get(ctx.params.rowId) if (row.tableId !== ctx.params.tableId) { ctx.throw(400, "Supplied tableId doesn't match the row's tableId") return } await linkRows.updateLinks({ - instanceId, + appId, eventType: linkRows.EventType.ROW_DELETE, row, tableId: row.tableId, @@ -243,12 +243,12 @@ exports.destroy = async function(ctx) { // for automations include the row that was deleted 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) { const errors = await validate({ - instanceId: ctx.user.instanceId, + appId: ctx.user.appId, tableId: ctx.params.tableId, row: ctx.request.body, }) @@ -256,9 +256,9 @@ exports.validate = async function(ctx) { ctx.body = errors } -async function validate({ instanceId, tableId, row, table }) { +async function validate({ appId, tableId, row, table }) { if (!table) { - const db = new CouchDB(instanceId) + const db = new CouchDB(appId) table = await db.get(tableId) } const errors = {} @@ -273,11 +273,11 @@ async function validate({ instanceId, tableId, row, table }) { } exports.fetchEnrichedRow = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const tableId = ctx.params.tableId const rowId = ctx.params.rowId - if (instanceId == null || tableId == null || rowId == null) { + if (appId == null || tableId == null || rowId == null) { ctx.status = 400 ctx.body = { status: 400, @@ -290,7 +290,7 @@ exports.fetchEnrichedRow = async function(ctx) { const [table, row] = await Promise.all([db.get(tableId), db.get(rowId)]) // get the link docs const linkVals = await linkRows.getLinkDocuments({ - instanceId, + appId, tableId, rowId, }) @@ -301,7 +301,7 @@ exports.fetchEnrichedRow = async function(ctx) { }) // need to include the IDs in these rows for any links they may have let linkedRows = await linkRows.attachLinkInfo( - instanceId, + appId, response.rows.map(row => row.doc) ) // insert the link rows in the correct place throughout the main row @@ -375,13 +375,13 @@ const TYPE_TRANSFORM_MAP = { } async function bulkDelete(ctx) { - const instanceId = ctx.user.instanceId + const appId = ctx.user.appId const { rows } = ctx.request.body - const db = new CouchDB(instanceId) + const db = new CouchDB(appId) const linkUpdates = rows.map(row => linkRows.updateLinks({ - instanceId, + appId, eventType: linkRows.EventType.ROW_DELETE, row, tableId: row.tableId, @@ -392,6 +392,6 @@ async function bulkDelete(ctx) { await Promise.all(linkUpdates) rows.forEach(row => { - ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, instanceId, row) + ctx.eventEmitter && ctx.eventEmitter.emitRow(`row:delete`, appId, row) }) } diff --git a/packages/server/src/api/controllers/static.js b/packages/server/src/api/controllers/static.js index 19eb90dc42..1ebe169417 100644 --- a/packages/server/src/api/controllers/static.js +++ b/packages/server/src/api/controllers/static.js @@ -65,11 +65,11 @@ exports.uploadFile = async function(ctx) { ctx.body = await processLocalFileUploads({ files, 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 !fs.existsSync(outputPath) && fs.mkdirSync(outputPath, { recursive: true }) @@ -98,7 +98,7 @@ async function processLocalFileUploads({ files, outputPath, instanceId }) { // local document used to track which files need to be uploaded // db.get throws an error if the document doesn't exist // need to use a promise to default - const db = new CouchDB(instanceId) + const db = new CouchDB(appId) await db .get("_local/fileuploads") .then(data => { @@ -130,7 +130,7 @@ exports.performLocalFileProcessing = async function(ctx) { ctx.body = await processLocalFileUploads({ files, outputPath: processedFileOutputPath, - instanceId: ctx.user.instanceId, + appId: ctx.user.appId, }) } catch (err) { ctx.throw(500, err) @@ -188,7 +188,7 @@ exports.serveAppAsset = async function(ctx) { const appPath = resolve( budibaseAppsDir(), - ctx.user.instanceId, + ctx.user.appId, "public", mainOrAuth ) diff --git a/packages/server/src/api/controllers/table.js b/packages/server/src/api/controllers/table.js index 61726d591a..f7021974c5 100644 --- a/packages/server/src/api/controllers/table.js +++ b/packages/server/src/api/controllers/table.js @@ -9,7 +9,7 @@ const { } = require("../../db/utils") exports.fetch = async function(ctx) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const body = await db.allDocs( getTableParams(null, { include_docs: true, @@ -19,13 +19,13 @@ exports.fetch = 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) } exports.save = async function(ctx) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const { dataImport, ...rest } = ctx.request.body const tableToSave = { type: "table", @@ -90,7 +90,7 @@ exports.save = async function(ctx) { // update linked rows await linkRows.updateLinks({ - instanceId, + appId, eventType: oldTable ? linkRows.EventType.TABLE_UPDATED : linkRows.EventType.TABLE_SAVE, @@ -107,7 +107,7 @@ exports.save = async function(ctx) { tableToSave._rev = result.rev ctx.eventEmitter && - ctx.eventEmitter.emitTable(`table:save`, instanceId, tableToSave) + ctx.eventEmitter.emitTable(`table:save`, appId, tableToSave) if (dataImport && dataImport.csvString) { // 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) { - const instanceId = ctx.user.instanceId - const db = new CouchDB(instanceId) + const appId = ctx.user.appId + const db = new CouchDB(appId) const tableToDelete = await db.get(ctx.params.tableId) // Delete all rows for that table @@ -141,7 +141,7 @@ exports.destroy = async function(ctx) { // update linked rows await linkRows.updateLinks({ - instanceId, + appId, eventType: linkRows.EventType.TABLE_DELETE, table: tableToDelete, }) @@ -150,7 +150,7 @@ exports.destroy = async function(ctx) { await db.remove(tableToDelete) ctx.eventEmitter && - ctx.eventEmitter.emitTable(`table:delete`, instanceId, tableToDelete) + ctx.eventEmitter.emitTable(`table:delete`, appId, tableToDelete) ctx.status = 200 ctx.message = `Table ${ctx.params.tableId} deleted.` } diff --git a/packages/server/src/api/controllers/templates.js b/packages/server/src/api/controllers/templates.js index c7be43b4e3..8e21b3c18b 100644 --- a/packages/server/src/api/controllers/templates.js +++ b/packages/server/src/api/controllers/templates.js @@ -28,12 +28,11 @@ exports.downloadTemplate = async function(ctx) { } exports.exportTemplateFromApp = async function(ctx) { - const { appId, instanceId } = ctx.user + const { appId } = ctx.user const { templateName } = ctx.request.body await exportTemplateFromApp({ appId, - instanceId, templateName, }) diff --git a/packages/server/src/api/controllers/user.js b/packages/server/src/api/controllers/user.js index 192f56180e..da0adff895 100644 --- a/packages/server/src/api/controllers/user.js +++ b/packages/server/src/api/controllers/user.js @@ -7,7 +7,7 @@ const { } = require("../../utilities/accessLevels") exports.fetch = async function(ctx) { - const database = new CouchDB(ctx.user.instanceId) + const database = new CouchDB(ctx.user.appId) const data = await database.allDocs( getUserParams(null, { include_docs: true, @@ -17,7 +17,7 @@ exports.fetch = async function(ctx) { } exports.create = async function(ctx) { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const { username, password, name, accessLevelId } = ctx.request.body if (!username || !password) { @@ -39,10 +39,10 @@ exports.create = async function(ctx) { const response = await db.post(user) - const app = await db.get(ctx.user.instanceId) + const app = await db.get(ctx.user.appId) app.userInstanceMap = { ...app.userInstanceMap, - [username]: ctx.user.instanceId, + [username]: ctx.user.appId, } await db.put(app) @@ -57,7 +57,7 @@ exports.create = 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 dbUser = db.get(ctx.request.body._id) const newData = { ...dbUser, ...user } @@ -71,14 +71,14 @@ exports.update = 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)) ctx.message = `User ${ctx.params.username} deleted.` ctx.status = 200 } 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)) ctx.body = { username: user.username, diff --git a/packages/server/src/api/controllers/view/index.js b/packages/server/src/api/controllers/view/index.js index b0d21b42d1..57d4862b7e 100644 --- a/packages/server/src/api/controllers/view/index.js +++ b/packages/server/src/api/controllers/view/index.js @@ -8,7 +8,7 @@ const { fetchView } = require("../row") const controller = { 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 response = [] @@ -26,7 +26,7 @@ const controller = { ctx.body = response }, save: async ctx => { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const { originalName, ...viewToSave } = ctx.request.body const designDoc = await db.get("_design/database") @@ -63,7 +63,7 @@ const controller = { ctx.message = `View ${viewToSave.name} saved successfully.` }, 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 viewName = decodeURI(ctx.params.viewName) diff --git a/packages/server/src/api/controllers/webhook.js b/packages/server/src/api/controllers/webhook.js index 3259a89341..7a343b1b07 100644 --- a/packages/server/src/api/controllers/webhook.js +++ b/packages/server/src/api/controllers/webhook.js @@ -22,7 +22,7 @@ exports.WebhookType = { } exports.fetch = async ctx => { - const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB(ctx.user.appId) const response = await db.allDocs( getWebhookParams(null, { include_docs: true, @@ -32,7 +32,7 @@ exports.fetch = 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 webhook.appId = ctx.user.appId @@ -53,7 +53,7 @@ exports.save = 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) } @@ -91,7 +91,7 @@ exports.trigger = async ctx => { await triggers.externalTrigger(target, { body: ctx.request.body, ...ctx.request.body, - instanceId: ctx.params.instance, + appId: ctx.params.instance, }) } ctx.status = 200 diff --git a/packages/server/src/api/routes/application.js b/packages/server/src/api/routes/application.js index f600eee333..aeb815d38c 100644 --- a/packages/server/src/api/routes/application.js +++ b/packages/server/src/api/routes/application.js @@ -8,12 +8,12 @@ const router = Router() router .get("/api/applications", authorized(BUILDER), controller.fetch) .get( - "/api/:instanceId/appPackage", + "/api/:appId/appPackage", authorized(BUILDER), controller.fetchAppPackage ) - .put("/api/:instanceId", authorized(BUILDER), controller.update) + .put("/api/:appId", authorized(BUILDER), controller.update) .post("/api/applications", authorized(BUILDER), controller.create) - .delete("/api/:instanceId", authorized(BUILDER), controller.delete) + .delete("/api/:appId", authorized(BUILDER), controller.delete) module.exports = router diff --git a/packages/server/src/api/routes/component.js b/packages/server/src/api/routes/component.js index f9dfff331c..8fbe7ac41a 100644 --- a/packages/server/src/api/routes/component.js +++ b/packages/server/src/api/routes/component.js @@ -6,7 +6,7 @@ const { BUILDER } = require("../../utilities/accessLevels") const router = Router() router.get( - "/:instanceId/components/definitions", + "/:appId/components/definitions", authorized(BUILDER), controller.fetchAppComponentDefinitions ) diff --git a/packages/server/src/api/routes/tests/accesslevel.spec.js b/packages/server/src/api/routes/tests/accesslevel.spec.js index d7958c8f32..3362cbd713 100644 --- a/packages/server/src/api/routes/tests/accesslevel.spec.js +++ b/packages/server/src/api/routes/tests/accesslevel.spec.js @@ -17,7 +17,7 @@ const { describe("/accesslevels", () => { let server let request - let instanceId + let appId let table let view @@ -31,9 +31,9 @@ describe("/accesslevels", () => { beforeEach(async () => { let app = await createApplication(request) - instanceId = app.instance._id - table = await createTable(request, instanceId) - view = await createView(request, instanceId, table._id) + appId = app.instance._id + table = await createTable(request, appId) + view = await createView(request, appId, table._id) }) describe("create", () => { @@ -42,7 +42,7 @@ describe("/accesslevels", () => { const res = await request .post(`/api/accesslevels`) .send({ name: "user" }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -60,7 +60,7 @@ describe("/accesslevels", () => { const createRes = await request .post(`/api/accesslevels`) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -68,7 +68,7 @@ describe("/accesslevels", () => { const res = await request .get(`/api/accesslevels`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -76,11 +76,11 @@ describe("/accesslevels", () => { const adminLevel = res.body.find(r => r._id === ADMIN_LEVEL_ID) 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) 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) expect(customLevelFetched.permissions).toEqual(customLevel.permissions) @@ -93,7 +93,7 @@ describe("/accesslevels", () => { const createRes = await request .post(`/api/accesslevels`) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE } ] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -101,12 +101,12 @@ describe("/accesslevels", () => { await request .delete(`/api/accesslevels/${customLevel._id}/${customLevel._rev}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect(200) await request .get(`/api/accesslevels/${customLevel._id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect(404) }) }) @@ -116,7 +116,7 @@ describe("/accesslevels", () => { const createRes = await request .post(`/api/accesslevels`) .send({ name: "user", permissions: [ { itemId: table._id, name: READ_TABLE }] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -128,13 +128,13 @@ describe("/accesslevels", () => { _rev: customLevel._rev, addedPermissions: [ { itemId: table._id, name: WRITE_TABLE } ] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) const finalRes = await request .get(`/api/accesslevels/${customLevel._id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect(200) expect(finalRes.body.permissions.length).toBe(2) @@ -152,7 +152,7 @@ describe("/accesslevels", () => { { itemId: table._id, name: WRITE_TABLE }, ] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -164,13 +164,13 @@ describe("/accesslevels", () => { _rev: customLevel._rev, removedPermissions: [ { itemId: table._id, name: WRITE_TABLE }] }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) const finalRes = await request .get(`/api/accesslevels/${customLevel._id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect(200) expect(finalRes.body.permissions.length).toBe(1) diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index 6d30701a29..6068d783cb 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -36,12 +36,12 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { const otherApplication = await createApplication(request) - const instanceId = otherApplication.instance._id + const appId = otherApplication.instance._id await builderEndpointShouldBlockNormalUsers({ request, method: "POST", url: `/api/applications`, - instanceId: instanceId, + appId: appId, body: { name: "My App" } }) }) @@ -64,12 +64,12 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { const otherApplication = await createApplication(request) - const instanceId = otherApplication.instance._id + const appId = otherApplication.instance._id await builderEndpointShouldBlockNormalUsers({ request, method: "GET", url: `/api/applications`, - instanceId: instanceId, + appId: appId, }) }) }) diff --git a/packages/server/src/api/routes/tests/automation.spec.js b/packages/server/src/api/routes/tests/automation.spec.js index fa78cccf0d..226bbad226 100644 --- a/packages/server/src/api/routes/tests/automation.spec.js +++ b/packages/server/src/api/routes/tests/automation.spec.js @@ -39,7 +39,7 @@ describe("/automations", () => { let request let server let app - let instanceId + let appId let automation let automationId @@ -49,7 +49,7 @@ describe("/automations", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instance._id + appId = app.instance._id if (automation) await destroyDocument(automation.id) }) @@ -58,7 +58,7 @@ describe("/automations", () => { }) const createAutomation = async () => { - automation = await insertDocument(instanceId, { + automation = await insertDocument(appId, { type: "automation", ...TEST_AUTOMATION }) @@ -69,7 +69,7 @@ describe("/automations", () => { return await request .post(`/api/automations/${automationId}/trigger`) .send({ name: "Test", description: "TEST" }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) } @@ -78,7 +78,7 @@ describe("/automations", () => { it("returns a list of definitions for actions", async () => { const res = await request .get(`/api/automations/action/list`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -89,7 +89,7 @@ describe("/automations", () => { it("returns a list of definitions for triggers", async () => { const res = await request .get(`/api/automations/trigger/list`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -100,7 +100,7 @@ describe("/automations", () => { it("returns a list of definitions for actions", async () => { const res = await request .get(`/api/automations/logic/list`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -111,7 +111,7 @@ describe("/automations", () => { it("returns all of the definitions in one", async () => { const res = await request .get(`/api/automations/definitions/list`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -139,7 +139,7 @@ describe("/automations", () => { it("returns a success message when the automation is successfully created", async () => { const res = await request .post(`/api/automations`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .send(TEST_AUTOMATION) .expect('Content-Type', /json/) .expect(200) @@ -155,7 +155,7 @@ describe("/automations", () => { request, method: "POST", url: `/api/automations`, - instanceId: instanceId, + appId: appId, body: TEST_AUTOMATION }) }) @@ -163,7 +163,7 @@ describe("/automations", () => { describe("trigger", () => { it("trigger the automation successfully", async () => { - let table = await createTable(request, instanceId) + let table = await createTable(request, appId) TEST_AUTOMATION.definition.trigger.inputs.tableId = table._id TEST_AUTOMATION.definition.steps[0].inputs.row.tableId = table._id await createAutomation() @@ -176,7 +176,7 @@ describe("/automations", () => { expect(res.body.message).toEqual(`Automation ${automation._id} has been triggered.`) expect(res.body.automation.name).toEqual(TEST_AUTOMATION.name) await delay(500) - let elements = await getAllFromTable(request, instanceId, table._id) + let elements = await getAllFromTable(request, appId, table._id) // don't test it unless there are values to test if (elements.length === 1) { expect(elements.length).toEqual(1) @@ -199,7 +199,7 @@ describe("/automations", () => { const res = await request .put(`/api/automations`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .send(automation) .expect('Content-Type', /json/) .expect(200) @@ -214,7 +214,7 @@ describe("/automations", () => { await createAutomation() const res = await request .get(`/api/automations`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -226,7 +226,7 @@ describe("/automations", () => { request, method: "GET", url: `/api/automations`, - instanceId: instanceId, + appId: appId, }) }) }) @@ -236,7 +236,7 @@ describe("/automations", () => { await createAutomation() const res = await request .delete(`/api/automations/${automation.id}/${automation.rev}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -249,7 +249,7 @@ describe("/automations", () => { request, method: "DELETE", url: `/api/automations/${automation.id}/${automation._rev}`, - instanceId: instanceId, + appId: appId, }) }) }) diff --git a/packages/server/src/api/routes/tests/couchTestUtils.js b/packages/server/src/api/routes/tests/couchTestUtils.js index 25b8dd964e..83c968323d 100644 --- a/packages/server/src/api/routes/tests/couchTestUtils.js +++ b/packages/server/src/api/routes/tests/couchTestUtils.js @@ -23,11 +23,11 @@ exports.supertest = async () => { return { request, server } } -exports.defaultHeaders = instanceId => { +exports.defaultHeaders = appId => { const builderUser = { userId: "BUILDER", accessLevelId: BUILDER_LEVEL_ID, - instanceId, + appId, } const builderToken = jwt.sign(builderUser, env.JWT_SECRET) @@ -38,7 +38,7 @@ exports.defaultHeaders = instanceId => { } } -exports.createTable = async (request, instanceId, table) => { +exports.createTable = async (request, appId, table) => { if (table != null && table._id) { delete table._id } @@ -64,19 +64,19 @@ exports.createTable = async (request, instanceId, table) => { const res = await request .post(`/api/tables`) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) .send(table) return res.body } -exports.getAllFromTable = async (request, instanceId, tableId) => { +exports.getAllFromTable = async (request, appId, tableId) => { const res = await request .get(`/api/${tableId}/rows`) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) return res.body } -exports.createView = async (request, instanceId, tableId, view) => { +exports.createView = async (request, appId, tableId, view) => { view = view || { map: "function(doc) { emit(doc[doc.key], doc._id); } ", tableId: tableId, @@ -84,7 +84,7 @@ exports.createView = async (request, instanceId, tableId, view) => { const res = await request .post(`/api/views`) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) .send(view) return res.body } @@ -111,13 +111,13 @@ exports.clearApplications = async request => { exports.createUser = async ( request, - instanceId, + appId, username = "babs", password = "babs_password" ) => { const res = await request .post(`/api/users`) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) .send({ name: "Bill", username, @@ -129,29 +129,29 @@ exports.createUser = async ( const createUserWithOnePermission = async ( request, - instanceId, + appId, permName, itemId ) => { - let permissions = await generateAdminPermissions(instanceId) + let permissions = await generateAdminPermissions(appId) permissions = permissions.filter( p => p.name === permName && p.itemId === itemId ) return await createUserWithPermissions( request, - instanceId, + appId, permissions, "onePermOnlyUser" ) } -const createUserWithAdminPermissions = async (request, instanceId) => { - let permissions = await generateAdminPermissions(instanceId) +const createUserWithAdminPermissions = async (request, appId) => { + let permissions = await generateAdminPermissions(appId) return await createUserWithPermissions( request, - instanceId, + appId, permissions, "adminUser" ) @@ -159,18 +159,18 @@ const createUserWithAdminPermissions = async (request, instanceId) => { const createUserWithAllPermissionExceptOne = async ( request, - instanceId, + appId, permName, itemId ) => { - let permissions = await generateAdminPermissions(instanceId) + let permissions = await generateAdminPermissions(appId) permissions = permissions.filter( p => !(p.name === permName && p.itemId === itemId) ) return await createUserWithPermissions( request, - instanceId, + appId, permissions, "allPermsExceptOneUser" ) @@ -178,19 +178,19 @@ const createUserWithAllPermissionExceptOne = async ( const createUserWithPermissions = async ( request, - instanceId, + appId, permissions, username ) => { const accessRes = await request .post(`/api/accesslevels`) .send({ name: "TestLevel", permissions }) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) const password = `password_${username}` await request .post(`/api/users`) - .set(exports.defaultHeaders(instanceId)) + .set(exports.defaultHeaders(appId)) .send({ name: username, username, @@ -201,7 +201,7 @@ const createUserWithPermissions = async ( const anonUser = { userId: "ANON", accessLevelId: ANON_LEVEL_ID, - instanceId: instanceId, + appId: appId, version: packageJson.version, } @@ -224,13 +224,13 @@ exports.testPermissionsForEndpoint = async ({ method, url, body, - instanceId, + appId, permissionName, itemId, }) => { const headers = await createUserWithOnePermission( request, - instanceId, + appId, permissionName, itemId ) @@ -241,7 +241,7 @@ exports.testPermissionsForEndpoint = async ({ const noPermsHeaders = await createUserWithAllPermissionExceptOne( request, - instanceId, + appId, permissionName, itemId ) @@ -256,9 +256,9 @@ exports.builderEndpointShouldBlockNormalUsers = async ({ method, url, body, - instanceId, + appId, }) => { - const headers = await createUserWithAdminPermissions(request, instanceId) + const headers = await createUserWithAdminPermissions(request, appId) await createRequest(request, method, url, body) .set(headers) diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index 31f273216f..507f05cf6c 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -8,7 +8,7 @@ const { describe("/rows", () => { let request let server - let instanceId + let appId let table let row let app @@ -24,8 +24,8 @@ describe("/rows", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instance._id - table = await createTable(request, instanceId) + appId = app.instance._id + table = await createTable(request, appId) row = { name: "Test Contact", description: "original description", @@ -38,14 +38,14 @@ describe("/rows", () => { await request .post(`/api/${r ? r.tableId : row.tableId}/rows`) .send(r || row) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) const loadRow = async id => await request .get(`/api/${table._id}/rows/${id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -72,7 +72,7 @@ describe("/rows", () => { tableId: table._id, name: "Updated Name", }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -86,7 +86,7 @@ describe("/rows", () => { const res = await request .get(`/api/${table._id}/rows/${existing._id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -109,7 +109,7 @@ describe("/rows", () => { const res = await request .get(`/api/${table._id}/rows`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -131,7 +131,7 @@ describe("/rows", () => { const res = await request .post(`/api/rows/search`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .send({ keys: rowIds }) @@ -146,7 +146,7 @@ describe("/rows", () => { await createRow() await request .get(`/api/${table._id}/rows/not-a-valid-id`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(404) }) @@ -158,7 +158,7 @@ describe("/rows", () => { const number = {type:"number", constraints: { type: "number", presence: false }} const datetime = {type:"datetime", constraints: { type: "string", presence: false, datetime: {earliest:"", latest: ""} }} - table = await createTable(request, instanceId, { + table = await createTable(request, appId, { name: "TestTable2", type: "table", key: "name", @@ -254,7 +254,7 @@ describe("/rows", () => { tableId: table._id, name: "Updated Name", }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -275,7 +275,7 @@ describe("/rows", () => { const result = await request .post(`/api/${table._id}/rows/validate`) .send({ name: "ivan" }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -287,7 +287,7 @@ describe("/rows", () => { const result = await request .post(`/api/${table._id}/rows/validate`) .send({ name: 1 }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index 193bfb50a6..67d53a489b 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -11,7 +11,7 @@ describe("/tables", () => { let request let server let app - let instanceId + let appId beforeAll(async () => { ({ request, server } = await supertest()) @@ -23,7 +23,7 @@ describe("/tables", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instance._id + appId = app.instance._id }); describe("create", () => { @@ -37,7 +37,7 @@ describe("/tables", () => { name: { type: "string" } } }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) .end(async (err, res) => { @@ -48,14 +48,14 @@ describe("/tables", () => { }) it("renames all the row fields for a table when a schema key is renamed", async () => { - const testTable = await createTable(request, instanceId); + const testTable = await createTable(request, appId); const testRow = await request .post(`/api/${testTable._id}/rows`) .send({ name: "test" }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -74,7 +74,7 @@ describe("/tables", () => { updatedName: { type: "string" } } }) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -83,7 +83,7 @@ describe("/tables", () => { const res = await request .get(`/api/${testTable._id}/rows/${testRow.body._id}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -96,7 +96,7 @@ describe("/tables", () => { request, method: "POST", url: `/api/tables`, - instanceId: instanceId, + appId: appId, body: { name: "TestTable", key: "name", @@ -112,7 +112,7 @@ describe("/tables", () => { let testTable beforeEach(async () => { - testTable = await createTable(request, instanceId, testTable) + testTable = await createTable(request, appId, testTable) }); afterEach(() => { @@ -122,7 +122,7 @@ describe("/tables", () => { it("returns all the tables for that instance in the response body", done => { request .get(`/api/tables`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { @@ -138,7 +138,7 @@ describe("/tables", () => { request, method: "GET", url: `/api/tables`, - instanceId: instanceId, + appId: appId, }) }) }); @@ -147,7 +147,7 @@ describe("/tables", () => { let testTable; beforeEach(async () => { - testTable = await createTable(request, instanceId, testTable) + testTable = await createTable(request, appId, testTable) }); afterEach(() => { @@ -157,7 +157,7 @@ describe("/tables", () => { it("returns a success response when a table is deleted.", async done => { request .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { @@ -167,7 +167,7 @@ describe("/tables", () => { }) it("deletes linked references to the table after deletion", async done => { - const linkedTable = await createTable(request, instanceId, { + const linkedTable = await createTable(request, appId, { name: "LinkedTable", type: "table", key: "name", @@ -190,12 +190,12 @@ describe("/tables", () => { request .delete(`/api/tables/${testTable._id}/${testTable._rev}`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) .end(async (_, res) => { expect(res.res.statusMessage).toEqual(`Table ${testTable._id} deleted.`); - const dependentTable = await getDocument(instanceId, linkedTable._id) + const dependentTable = await getDocument(appId, linkedTable._id) expect(dependentTable.schema.TestTable).not.toBeDefined(); done(); }); @@ -206,7 +206,7 @@ describe("/tables", () => { request, method: "DELETE", url: `/api/tables/${testTable._id}/${testTable._rev}`, - instanceId: instanceId, + appId: appId, }) }) diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 497a654fc3..a569902bae 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -15,7 +15,7 @@ describe("/users", () => { let request let server let app - let instanceId + let appId beforeAll(async () => { ({ request, server } = await supertest(server)) @@ -23,7 +23,7 @@ describe("/users", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instance._id + appId = app.instance._id }); afterAll(() => { @@ -33,11 +33,11 @@ describe("/users", () => { describe("fetch", () => { it("returns a list of users from an instance db", async () => { - await createUser(request, instanceId, "brenda", "brendas_password") - await createUser(request, instanceId, "pam", "pam_password") + await createUser(request, appId, "brenda", "brendas_password") + await createUser(request, appId, "pam", "pam_password") const res = await request .get(`/api/users`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -47,12 +47,12 @@ describe("/users", () => { }) it("should apply authorization to endpoint", async () => { - await createUser(request, instanceId, "brenda", "brendas_password") + await createUser(request, appId, "brenda", "brendas_password") await testPermissionsForEndpoint({ request, method: "GET", url: `/api/users`, - instanceId: instanceId, + appId: appId, permissionName: LIST_USERS, }) }) @@ -64,7 +64,7 @@ describe("/users", () => { it("returns a success message when a user is successfully created", async () => { const res = await request .post(`/api/users`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .send({ name: "Bill", username: "bill", password: "bills_password", accessLevelId: POWERUSER_LEVEL_ID }) .expect(200) .expect('Content-Type', /json/) @@ -79,7 +79,7 @@ describe("/users", () => { method: "POST", body: { name: "brandNewUser", username: "brandNewUser", password: "yeeooo", accessLevelId: POWERUSER_LEVEL_ID }, url: `/api/users`, - instanceId: instanceId, + appId: appId, permissionName: USER_MANAGEMENT, }) }) diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.js index 0419e7326c..f1ee5d8218 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.js @@ -10,7 +10,7 @@ describe("/views", () => { let request let server let app - let instanceId + let appId let table const createView = async (config = { @@ -22,14 +22,14 @@ describe("/views", () => { await request .post(`/api/views`) .send(config) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) const createRow = async row => request .post(`/api/${table._id}/rows`) .send(row) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) @@ -39,7 +39,7 @@ describe("/views", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instance._id + appId = app.instance._id }) afterAll(() => { @@ -48,7 +48,7 @@ describe("/views", () => { describe("create", () => { beforeEach(async () => { - table = await createTable(request, instanceId); + table = await createTable(request, appId); }) it("returns a success message when the view is successfully created", async () => { @@ -59,7 +59,7 @@ describe("/views", () => { it("updates the table row with the new view metadata", async () => { const res = await createView() expect(res.res.statusMessage).toEqual("View TestView saved successfully."); - const updatedTable = await getDocument(instanceId, table._id) + const updatedTable = await getDocument(appId, table._id) expect(updatedTable.views).toEqual({ TestView: { field: "Price", @@ -96,14 +96,14 @@ describe("/views", () => { describe("fetch", () => { beforeEach(async () => { - table = await createTable(request, instanceId); + table = await createTable(request, appId); }); it("returns only custom views", async () => { await createView() const res = await request .get(`/api/views`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) expect(res.body.length).toBe(1) @@ -113,7 +113,7 @@ describe("/views", () => { describe("query", () => { beforeEach(async () => { - table = await createTable(request, instanceId); + table = await createTable(request, appId); }); it("returns data for the created view", async () => { @@ -132,7 +132,7 @@ describe("/views", () => { }) const res = await request .get(`/api/views/TestView?calculation=stats`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) expect(res.body.length).toBe(1) @@ -164,7 +164,7 @@ describe("/views", () => { }) const res = await request .get(`/api/views/TestView?calculation=stats&group=Category`) - .set(defaultHeaders(instanceId)) + .set(defaultHeaders(appId)) .expect('Content-Type', /json/) .expect(200) diff --git a/packages/server/src/automations/automationUtils.js b/packages/server/src/automations/automationUtils.js index c6284ea3bd..3a66420dd0 100644 --- a/packages/server/src/automations/automationUtils.js +++ b/packages/server/src/automations/automationUtils.js @@ -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 * 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 {object} row The input row structure which requires clean-up after having been through mustache statements. * @returns {Promise} The cleaned up rows object, will should now have all the required primitive types. */ -module.exports.cleanUpRow = async (instanceId, tableId, row) => { - const db = new CouchDB(instanceId) +module.exports.cleanUpRow = async (appId, tableId, row) => { + const db = new CouchDB(appId) const table = await db.get(tableId) 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 * 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 {object} row The input row structure which requires clean-up after having been through mustache statements. * @returns {Promise} The cleaned up rows object, which will now have all the required primitive types. */ -module.exports.cleanUpRowById = async (instanceId, rowId, row) => { - const db = new CouchDB(instanceId) +module.exports.cleanUpRowById = async (appId, rowId, row) => { + const db = new CouchDB(appId) const foundRow = await db.get(rowId) - return module.exports.cleanUpRow(instanceId, foundRow.tableId, row) + return module.exports.cleanUpRow(appId, foundRow.tableId, row) } diff --git a/packages/server/src/automations/steps/createRow.js b/packages/server/src/automations/steps/createRow.js index afc3d018ee..0b7897809a 100644 --- a/packages/server/src/automations/steps/createRow.js +++ b/packages/server/src/automations/steps/createRow.js @@ -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 if (inputs.row == null || inputs.row.tableId == null) { return } inputs.row = await automationUtils.cleanUpRow( - instanceId, + appId, inputs.row.tableId, inputs.row ) @@ -76,7 +76,7 @@ module.exports.run = async function({ inputs, instanceId, apiKey }) { request: { body: inputs.row, }, - user: { instanceId }, + user: { appId }, } try { diff --git a/packages/server/src/automations/steps/createUser.js b/packages/server/src/automations/steps/createUser.js index 38eeb5ae1e..07d9f05316 100644 --- a/packages/server/src/automations/steps/createUser.js +++ b/packages/server/src/automations/steps/createUser.js @@ -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 ctx = { user: { - instanceId: instanceId, + appId: appId, }, request: { body: { username, password, accessLevelId }, diff --git a/packages/server/src/automations/steps/deleteRow.js b/packages/server/src/automations/steps/deleteRow.js index 9bece74fa2..f4884caad3 100644 --- a/packages/server/src/automations/steps/deleteRow.js +++ b/packages/server/src/automations/steps/deleteRow.js @@ -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 if (inputs.id == null || inputs.revision == null) { return @@ -61,7 +61,7 @@ module.exports.run = async function({ inputs, instanceId, apiKey }) { rowId: inputs.id, revId: inputs.revision, }, - user: { instanceId }, + user: { appId }, } try { diff --git a/packages/server/src/automations/steps/updateRow.js b/packages/server/src/automations/steps/updateRow.js index e443f7e745..b134ebfa9c 100644 --- a/packages/server/src/automations/steps/updateRow.js +++ b/packages/server/src/automations/steps/updateRow.js @@ -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) { return } inputs.row = await automationUtils.cleanUpRowById( - instanceId, + appId, inputs.rowId, inputs.row ) @@ -78,7 +78,7 @@ module.exports.run = async function({ inputs, instanceId }) { request: { body: inputs.row, }, - user: { instanceId }, + user: { appId }, } try { diff --git a/packages/server/src/automations/thread.js b/packages/server/src/automations/thread.js index 6b29fce1c1..26ca8f04a3 100644 --- a/packages/server/src/automations/thread.js +++ b/packages/server/src/automations/thread.js @@ -32,9 +32,9 @@ function recurseMustache(inputs, context) { */ class Orchestrator { constructor(automation, triggerOutput) { - this._instanceId = triggerOutput.instanceId + this._appId = triggerOutput.appId // remove from context - delete triggerOutput.instanceId + delete triggerOutput.appId // step zero is never used as the mustache is zero indexed for customer facing this._context = { steps: [{}], trigger: triggerOutput } this._automation = automation @@ -62,11 +62,11 @@ class Orchestrator { step.inputs, step.schema.inputs ) - // instanceId is always passed + // appId is always passed try { const outputs = await stepFn({ inputs: step.inputs, - instanceId: this._instanceId, + appId: this._appId, apiKey: automation.apiKey, }) if (step.stepId === FILTER_STEP_ID && !outputs.success) { diff --git a/packages/server/src/automations/triggers.js b/packages/server/src/automations/triggers.js index 6dd163ea8e..5d2121df7b 100644 --- a/packages/server/src/automations/triggers.js +++ b/packages/server/src/automations/triggers.js @@ -122,10 +122,10 @@ const BUILTIN_DEFINITIONS = { } async function queueRelevantRowAutomations(event, eventType) { - if (event.instanceId == null) { - throw `No instanceId specified for ${eventType} - check event emitters.` + if (event.appId == null) { + 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( getAutomationParams(null, { include_docs: true }) ) @@ -169,7 +169,7 @@ emitter.on("row:delete", async function(event) { async function fillRowOutput(automation, params) { let triggerSchema = automation.definition.trigger let tableId = triggerSchema.inputs.tableId - const db = new CouchDB(params.instanceId) + const db = new CouchDB(params.appId) try { let table = await db.get(tableId) let row = {} diff --git a/packages/server/src/db/linkedRows/LinkController.js b/packages/server/src/db/linkedRows/LinkController.js index 5eed0bb4a8..d6116b76f6 100644 --- a/packages/server/src/db/linkedRows/LinkController.js +++ b/packages/server/src/db/linkedRows/LinkController.js @@ -40,9 +40,9 @@ function LinkDocument( } class LinkController { - constructor({ instanceId, tableId, row, table, oldTable }) { - this._instanceId = instanceId - this._db = new CouchDB(instanceId) + constructor({ appId, tableId, row, table, oldTable }) { + this._appId = appId + this._db = new CouchDB(appId) this._tableId = tableId this._row = row this._table = table @@ -87,7 +87,7 @@ class LinkController { */ getRowLinkDocs(rowId) { return getLinkDocuments({ - instanceId: this._instanceId, + appId: this._appId, tableId: this._tableId, rowId, includeDocs: IncludeDocs.INCLUDE, @@ -99,7 +99,7 @@ class LinkController { */ getTableLinkDocs() { return getLinkDocuments({ - instanceId: this._instanceId, + appId: this._appId, tableId: this._tableId, includeDocs: IncludeDocs.INCLUDE, }) diff --git a/packages/server/src/db/linkedRows/index.js b/packages/server/src/db/linkedRows/index.js index 6f745b6e7c..02af98eef9 100644 --- a/packages/server/src/db/linkedRows/index.js +++ b/packages/server/src/db/linkedRows/index.js @@ -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. * @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). - * @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 {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. @@ -41,14 +41,14 @@ exports.createLinkView = createLinkView */ exports.updateLinks = async function({ eventType, - instanceId, + appId, row, tableId, table, oldTable, }) { const baseReturnObj = row == null ? table : row - if (instanceId == null) { + if (appId == null) { throw "Cannot operate without an instance ID." } // 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. - * @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 * a single row object or an array of rows - both will be handled. * @returns {Promise} 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. */ -exports.attachLinkInfo = async (instanceId, rows) => { +exports.attachLinkInfo = async (appId, rows) => { // handle a single row as well as multiple let wasArray = true if (!(rows instanceof Array)) { @@ -105,7 +105,7 @@ exports.attachLinkInfo = async (instanceId, rows) => { await Promise.all( tableIds.map(tableId => getLinkDocuments({ - instanceId, + appId, tableId: tableId, includeDocs: IncludeDocs.EXCLUDE, }) diff --git a/packages/server/src/db/linkedRows/linkUtils.js b/packages/server/src/db/linkedRows/linkUtils.js index 5f600cc3cb..dc9d8d3f1e 100644 --- a/packages/server/src/db/linkedRows/linkUtils.js +++ b/packages/server/src/db/linkedRows/linkUtils.js @@ -13,12 +13,12 @@ exports.IncludeDocs = { /** * 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. - * @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} The view now exists, please note that the next view of this query will actually build it, * so it may be slow. */ -exports.createLinkView = async instanceId => { - const db = new CouchDB(instanceId) +exports.createLinkView = async appId => { + const db = new CouchDB(appId) const designDoc = await db.get("_design/database") const view = { map: function(doc) { @@ -47,7 +47,7 @@ exports.createLinkView = async instanceId => { /** * 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|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. @@ -60,12 +60,12 @@ exports.createLinkView = async instanceId => { * (if any). */ exports.getLinkDocuments = async function({ - instanceId, + appId, tableId, rowId, includeDocs, }) { - const db = new CouchDB(instanceId) + const db = new CouchDB(appId) let params if (rowId != null) { params = { key: [tableId, rowId] } @@ -85,7 +85,7 @@ exports.getLinkDocuments = async function({ } catch (err) { // check if the view doesn't exist, it should for all new instances if (err != null && err.name === "not_found") { - await exports.createLinkView(instanceId) + await exports.createLinkView(appId) return exports.getLinkDocuments(arguments[0]) } else { Sentry.captureException(err) diff --git a/packages/server/src/events/index.js b/packages/server/src/events/index.js index 4c4541741c..237e212293 100644 --- a/packages/server/src/events/index.js +++ b/packages/server/src/events/index.js @@ -11,10 +11,10 @@ const EventEmitter = require("events").EventEmitter * This is specifically quite important for mustache used in automations. */ class BudibaseEmitter extends EventEmitter { - emitRow(eventName, instanceId, row, table = null) { + emitRow(eventName, appId, row, table = null) { let event = { row, - instanceId, + appId, tableId: row.tableId, } if (table) { @@ -27,14 +27,14 @@ class BudibaseEmitter extends EventEmitter { this.emit(eventName, event) } - emitTable(eventName, instanceId, table = null) { + emitTable(eventName, appId, table = null) { const tableId = table._id let event = { table: { ...table, tableId: tableId, }, - instanceId, + appId, tableId: tableId, } event.id = tableId diff --git a/packages/server/src/middleware/authenticated.js b/packages/server/src/middleware/authenticated.js index b62517e0f9..74f133cbde 100644 --- a/packages/server/src/middleware/authenticated.js +++ b/packages/server/src/middleware/authenticated.js @@ -53,9 +53,9 @@ module.exports = async (ctx, next) => { ctx.auth.apiKey = jwtPayload.apiKey ctx.user = { ...jwtPayload, - instanceId: jwtPayload.instanceId, + appId: jwtPayload.appId, accessLevel: await getAccessLevel( - jwtPayload.instanceId, + jwtPayload.appId, jwtPayload.accessLevelId ), } @@ -70,10 +70,10 @@ module.exports = async (ctx, next) => { * Return the full access level object either from constants * 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 */ -const getAccessLevel = async (instanceId, accessLevelId) => { +const getAccessLevel = async (appId, accessLevelId) => { if ( accessLevelId === POWERUSER_LEVEL_ID || accessLevelId === ADMIN_LEVEL_ID || @@ -92,7 +92,7 @@ const getAccessLevel = async (instanceId, accessLevelId) => { levelId: accessLevelId, }, user: { - instanceId, + appId, }, } await accessLevelController.find(findAccessContext) diff --git a/packages/server/src/middleware/authorized.js b/packages/server/src/middleware/authorized.js index 71ff5cc1e1..34758903f3 100644 --- a/packages/server/src/middleware/authorized.js +++ b/packages/server/src/middleware/authorized.js @@ -28,7 +28,7 @@ module.exports = (permName, getItemId) => async (ctx, next) => { apiKey: ctx.headers["x-api-key"], } ctx.user = { - instanceId: ctx.headers["x-instanceid"], + appId: ctx.headers["x-instanceid"], } return next() } diff --git a/packages/server/src/middleware/usageQuota.js b/packages/server/src/middleware/usageQuota.js index 58e9c7eddc..39e387bd5a 100644 --- a/packages/server/src/middleware/usageQuota.js +++ b/packages/server/src/middleware/usageQuota.js @@ -27,7 +27,7 @@ function getProperty(url) { } 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] const property = getProperty(ctx.req.url) if (usage == null || property == null) { diff --git a/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json b/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json index 44d30a65f1..ecd63a6284 100644 --- a/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json +++ b/packages/server/src/utilities/appDirectoryTemplate/pages/main/page.json @@ -30,7 +30,7 @@ "className": "", "onLoad": [], "type": "div", - "_instanceId": "inst_app_80b_f158d4057d2c4bedb0042d42fda8abaf", + "_appId": "inst_app_80b_f158d4057d2c4bedb0042d42fda8abaf", "_instanceName": "Header", "_children": [ { @@ -59,7 +59,7 @@ "borderWidth": "", "borderColor": "", "borderStyle": "", - "_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", + "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_instanceName": "Navigation", "_children": [ { @@ -88,7 +88,7 @@ "underline": false, "fontSize": "", "fontFamily": "initial", - "_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", + "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_instanceName": "Home Link", "_children": [] } diff --git a/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/d834fea2-1b3e-4320-ab34-f9009f5ecc59.json b/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/d834fea2-1b3e-4320-ab34-f9009f5ecc59.json index 5afcf244f2..4104dcdb1e 100644 --- a/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/d834fea2-1b3e-4320-ab34-f9009f5ecc59.json +++ b/packages/server/src/utilities/appDirectoryTemplate/pages/main/screens/d834fea2-1b3e-4320-ab34-f9009f5ecc59.json @@ -36,7 +36,7 @@ "className": "", "text": "Welcome to your Budibase App 👋", "type": "h2", - "_instanceId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", + "_appId": "inst_cf8ace4_69efc0d72e6f443db2d4c902c14d9394", "_instanceName": "Heading", "_children": [] }, @@ -62,7 +62,7 @@ "className": "", "onLoad": [], "type": "div", - "_instanceId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", + "_appId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", "_instanceName": "Video Container", "_children": [ { @@ -88,7 +88,7 @@ }, "_code": "", "embed": "", - "_instanceId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", + "_appId": "inst_app_2cc_ca3383f896034e9295345c05f7dfca0c", "_instanceName": "Rick Astley Video", "_children": [] } diff --git a/packages/server/src/utilities/builder/setBuilderToken.js b/packages/server/src/utilities/builder/setBuilderToken.js index ff057a90eb..fca3e0724f 100644 --- a/packages/server/src/utilities/builder/setBuilderToken.js +++ b/packages/server/src/utilities/builder/setBuilderToken.js @@ -2,11 +2,11 @@ const { BUILDER_LEVEL_ID } = require("../accessLevels") const env = require("../../environment") const jwt = require("jsonwebtoken") -module.exports = (ctx, instanceId, version) => { +module.exports = (ctx, appId, version) => { const builderUser = { userId: "BUILDER", accessLevelId: BUILDER_LEVEL_ID, - instanceId, + appId, version, } if (env.BUDIBASE_API_KEY) { diff --git a/packages/server/src/utilities/permissions.js b/packages/server/src/utilities/permissions.js index 8cd30c9ff8..e1513fd0fa 100644 --- a/packages/server/src/utilities/permissions.js +++ b/packages/server/src/utilities/permissions.js @@ -4,15 +4,15 @@ const automationController = require("../api/controllers/automation") const accessLevels = require("./accessLevels") // 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, - ...(await generatePowerUserPermissions(instanceId)), + ...(await generatePowerUserPermissions(appId)), ] -const generatePowerUserPermissions = async instanceId => { +const generatePowerUserPermissions = async appId => { const fetchTablesCtx = { user: { - instanceId, + appId, }, } await tableController.fetch(fetchTablesCtx) @@ -20,7 +20,7 @@ const generatePowerUserPermissions = async instanceId => { const fetchViewsCtx = { user: { - instanceId, + appId, }, } await viewController.fetch(fetchViewsCtx) @@ -28,7 +28,7 @@ const generatePowerUserPermissions = async instanceId => { const fetchAutomationsCtx = { user: { - instanceId, + appId, }, } await automationController.fetch(fetchAutomationsCtx) diff --git a/packages/server/src/utilities/templates.js b/packages/server/src/utilities/templates.js index 626f1d003b..11150261ec 100644 --- a/packages/server/src/utilities/templates.js +++ b/packages/server/src/utilities/templates.js @@ -33,11 +33,7 @@ exports.downloadTemplate = async function(type, name) { return join(budibaseAppsDir(), "templates", type, name) } -exports.exportTemplateFromApp = async function({ - appId, - templateName, - instanceId, -}) { +exports.exportTemplateFromApp = async function({ templateName, appId }) { // Copy frontend files const appToExport = join(os.homedir(), ".budibase", appId, "pages") const templatesDir = join(os.homedir(), ".budibase", "templates") @@ -52,7 +48,7 @@ exports.exportTemplateFromApp = async function({ ) // perform couch dump - const instanceDb = new CouchDB(instanceId) + const instanceDb = new CouchDB(appId) await instanceDb.dump(writeStream) return templateOutputPath