2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-11-23 15:07:18 +01:00
|
|
|
const compileStaticAssets = require("../../utilities/builder/compileStaticAssets")
|
2020-05-14 16:12:30 +02:00
|
|
|
const env = require("../../environment")
|
2020-11-06 13:30:30 +01:00
|
|
|
const { existsSync } = require("fs-extra")
|
2020-06-03 22:23:56 +02:00
|
|
|
const { budibaseAppsDir } = require("../../utilities/budibaseDir")
|
2020-06-18 17:59:31 +02:00
|
|
|
const setBuilderToken = require("../../utilities/builder/setBuilderToken")
|
2020-07-07 14:44:05 +02:00
|
|
|
const fs = require("fs-extra")
|
2020-10-09 11:00:57 +02:00
|
|
|
const { join, resolve } = require("../../utilities/centralPath")
|
2020-10-21 16:28:30 +02:00
|
|
|
const packageJson = require("../../../package.json")
|
2020-10-29 11:21:06 +01:00
|
|
|
const { createLinkView } = require("../../db/linkedRows")
|
2020-11-16 19:04:44 +01:00
|
|
|
const { createRoutingView } = require("../../utilities/routing")
|
2020-10-29 11:21:06 +01:00
|
|
|
const { downloadTemplate } = require("../../utilities/templates")
|
2020-11-02 15:53:51 +01:00
|
|
|
const {
|
|
|
|
generateAppID,
|
|
|
|
DocumentTypes,
|
|
|
|
SEPARATOR,
|
2020-11-23 15:07:18 +01:00
|
|
|
getLayoutParams,
|
2020-11-18 19:24:12 +01:00
|
|
|
getScreenParams,
|
2020-11-03 18:42:54 +01:00
|
|
|
generateScreenID,
|
2020-11-02 15:53:51 +01:00
|
|
|
} = require("../../db/utils")
|
2020-11-23 16:46:26 +01:00
|
|
|
const {
|
2020-12-02 14:26:57 +01:00
|
|
|
BUILTIN_ROLE_IDS,
|
2020-11-23 16:46:26 +01:00
|
|
|
AccessController,
|
2020-12-02 14:26:57 +01:00
|
|
|
} = require("../../utilities/security/roles")
|
2020-07-14 22:10:51 +02:00
|
|
|
const {
|
|
|
|
downloadExtractComponentLibraries,
|
|
|
|
} = require("../../utilities/createAppPackage")
|
2020-11-23 16:46:26 +01:00
|
|
|
const { BASE_LAYOUTS } = require("../../constants/layouts")
|
2020-12-04 15:02:58 +01:00
|
|
|
const {
|
|
|
|
createHomeScreen,
|
|
|
|
createLoginScreen,
|
|
|
|
} = require("../../constants/screens")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
2021-01-19 16:29:49 +01:00
|
|
|
const { objectHandlebars } = require("../../utilities/handlebars")
|
2020-11-25 16:03:19 +01:00
|
|
|
const { USERS_TABLE_SCHEMA } = require("../../constants")
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
const APP_PREFIX = DocumentTypes.APP + SEPARATOR
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-11-18 19:24:12 +01:00
|
|
|
// utility function, need to do away with this
|
2020-11-23 16:46:26 +01:00
|
|
|
async function getLayouts(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getLayoutParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
}
|
|
|
|
|
|
|
|
async function getScreens(db) {
|
|
|
|
return (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
}
|
2020-11-18 19:24:12 +01:00
|
|
|
|
2020-12-02 14:26:57 +01:00
|
|
|
function getUserRoleId(ctx) {
|
|
|
|
return !ctx.user.role || !ctx.user.role._id
|
|
|
|
? BUILTIN_ROLE_IDS.PUBLIC
|
|
|
|
: ctx.user.role._id
|
2020-11-18 19:24:12 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:21:06 +01:00
|
|
|
async function createInstance(template) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const appId = generateAppID()
|
2020-10-29 11:21:06 +01:00
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(appId)
|
2020-10-29 11:21:06 +01:00
|
|
|
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
|
2020-10-29 11:28:27 +01:00
|
|
|
await createLinkView(appId)
|
2020-11-13 16:35:20 +01:00
|
|
|
await createRoutingView(appId)
|
2020-10-29 11:21:06 +01:00
|
|
|
|
|
|
|
// 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."
|
|
|
|
}
|
2020-11-24 15:04:14 +01:00
|
|
|
} else {
|
|
|
|
// create the users table
|
2020-11-25 16:03:19 +01:00
|
|
|
await db.put(USERS_TABLE_SCHEMA)
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2020-10-29 11:28:27 +01:00
|
|
|
return { _id: appId }
|
2020-10-29 11:21:06 +01:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetch = async function(ctx) {
|
2020-10-28 21:35:06 +01:00
|
|
|
let allDbs = await CouchDB.allDbs()
|
|
|
|
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
|
2020-10-29 12:48:01 +01:00
|
|
|
const apps = appDbNames.map(db => new CouchDB(db).get(db))
|
2020-10-28 21:35:06 +01:00
|
|
|
if (apps.length === 0) {
|
|
|
|
ctx.body = []
|
|
|
|
} else {
|
2020-11-23 16:34:23 +01:00
|
|
|
const response = await Promise.allSettled(apps)
|
2020-11-23 17:42:28 +01:00
|
|
|
ctx.body = response
|
|
|
|
.filter(result => result.status === "fulfilled")
|
|
|
|
.map(({ value }) => value)
|
2020-10-28 21:35:06 +01:00
|
|
|
}
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-04-07 18:25:09 +02:00
|
|
|
|
2020-11-18 19:24:12 +01:00
|
|
|
exports.fetchAppDefinition = async function(ctx) {
|
|
|
|
const db = new CouchDB(ctx.params.appId)
|
2020-11-23 16:46:26 +01:00
|
|
|
const layouts = await getLayouts(db)
|
2020-12-02 14:26:57 +01:00
|
|
|
const userRoleId = getUserRoleId(ctx)
|
2020-11-23 16:46:26 +01:00
|
|
|
const accessController = new AccessController(ctx.params.appId)
|
2020-12-04 15:02:58 +01:00
|
|
|
const screens = await accessController.checkScreensAccess(
|
2020-11-23 16:46:26 +01:00
|
|
|
await getScreens(db),
|
2020-12-02 14:26:57 +01:00
|
|
|
userRoleId
|
2020-11-23 16:46:26 +01:00
|
|
|
)
|
2020-11-18 19:24:12 +01:00
|
|
|
ctx.body = {
|
2020-11-23 16:46:26 +01:00
|
|
|
layouts,
|
|
|
|
screens,
|
2020-11-18 19:24:12 +01:00
|
|
|
libraries: ["@budibase/standard-components"],
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.fetchAppPackage = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-11-24 19:11:18 +01:00
|
|
|
const [layouts, screens] = await Promise.all([getLayouts(db), getScreens(db)])
|
2020-11-02 15:53:51 +01:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-23 16:46:26 +01:00
|
|
|
screens,
|
|
|
|
layouts,
|
2020-11-02 15:53:51 +01:00
|
|
|
}
|
2020-11-02 16:46:08 +01:00
|
|
|
await setBuilderToken(ctx, ctx.params.appId, application.version)
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.create = async function(ctx) {
|
2020-10-29 11:21:06 +01:00
|
|
|
const instance = await createInstance(ctx.request.body.template)
|
2020-10-29 11:28:27 +01:00
|
|
|
const appId = instance._id
|
2020-11-05 13:43:03 +01:00
|
|
|
const version = packageJson.version
|
2020-05-14 16:12:30 +02:00
|
|
|
const newApplication = {
|
2020-10-29 11:28:27 +01:00
|
|
|
_id: appId,
|
2020-04-20 17:17:11 +02:00
|
|
|
type: "app",
|
2020-10-21 16:28:30 +02:00
|
|
|
version: packageJson.version,
|
2020-08-14 12:21:52 +02:00
|
|
|
componentLibraries: ["@budibase/standard-components"],
|
2020-05-26 17:29:16 +02:00
|
|
|
name: ctx.request.body.name,
|
2020-09-25 15:47:42 +02:00
|
|
|
template: ctx.request.body.template,
|
2020-10-29 11:21:06 +01:00
|
|
|
instance: instance,
|
2020-12-14 19:31:48 +01:00
|
|
|
deployment: {
|
|
|
|
type: "cloud",
|
|
|
|
},
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
2020-10-29 11:28:27 +01:00
|
|
|
const instanceDb = new CouchDB(appId)
|
2020-10-28 21:35:06 +01:00
|
|
|
await instanceDb.put(newApplication)
|
2020-05-14 16:12:30 +02:00
|
|
|
|
2020-10-28 21:35:06 +01:00
|
|
|
if (env.NODE_ENV !== "jest") {
|
2020-05-26 17:29:16 +02:00
|
|
|
const newAppFolder = await createEmptyAppPackage(ctx, newApplication)
|
2020-07-14 22:07:53 +02:00
|
|
|
await downloadExtractComponentLibraries(newAppFolder)
|
2020-05-26 17:29:16 +02:00
|
|
|
}
|
|
|
|
|
2020-11-05 13:43:03 +01:00
|
|
|
await setBuilderToken(ctx, appId, version)
|
2020-06-10 22:39:30 +02:00
|
|
|
ctx.status = 200
|
2020-05-14 16:12:30 +02:00
|
|
|
ctx.body = newApplication
|
|
|
|
ctx.message = `Application ${ctx.request.body.name} created successfully`
|
2020-05-07 11:53:34 +02:00
|
|
|
}
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-06-29 17:49:16 +02:00
|
|
|
exports.update = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const application = await db.get(ctx.params.appId)
|
2020-06-29 17:18:43 +02:00
|
|
|
|
|
|
|
const data = ctx.request.body
|
|
|
|
const newData = { ...application, ...data }
|
|
|
|
|
|
|
|
const response = await db.put(newData)
|
|
|
|
data._rev = response.rev
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Application ${application.name} updated successfully.`
|
|
|
|
ctx.body = response
|
|
|
|
}
|
|
|
|
|
2020-07-07 14:44:05 +02:00
|
|
|
exports.delete = async function(ctx) {
|
2020-10-29 11:28:27 +01:00
|
|
|
const db = new CouchDB(ctx.params.appId)
|
|
|
|
const app = await db.get(ctx.params.appId)
|
2020-10-28 21:35:06 +01:00
|
|
|
const result = await db.destroy()
|
2020-10-13 17:31:14 +02:00
|
|
|
|
|
|
|
// remove top level directory
|
2020-10-29 11:28:27 +01:00
|
|
|
await fs.rmdir(join(budibaseAppsDir(), ctx.params.appId), {
|
2020-07-07 14:44:05 +02:00
|
|
|
recursive: true,
|
|
|
|
})
|
|
|
|
|
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Application ${app.name} deleted successfully.`
|
|
|
|
ctx.body = result
|
|
|
|
}
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
const createEmptyAppPackage = async (ctx, app) => {
|
2020-06-03 22:23:56 +02:00
|
|
|
const appsFolder = budibaseAppsDir()
|
2020-10-07 10:58:32 +02:00
|
|
|
const newAppFolder = resolve(appsFolder, app._id)
|
2020-05-26 17:29:16 +02:00
|
|
|
|
2020-11-02 15:53:51 +01:00
|
|
|
const db = new CouchDB(app._id)
|
|
|
|
|
2020-10-13 18:02:59 +02:00
|
|
|
if (existsSync(newAppFolder)) {
|
2020-05-26 17:29:16 +02:00
|
|
|
ctx.throw(400, "App folder already exists for this application")
|
|
|
|
}
|
|
|
|
|
2020-11-05 15:38:44 +01:00
|
|
|
fs.mkdirpSync(newAppFolder)
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2020-11-23 17:56:35 +01:00
|
|
|
let screensAndLayouts = []
|
2020-11-23 16:46:26 +01:00
|
|
|
for (let layout of BASE_LAYOUTS) {
|
|
|
|
const cloned = cloneDeep(layout)
|
2021-01-19 16:29:49 +01:00
|
|
|
screensAndLayouts.push(objectHandlebars(cloned, app))
|
2020-11-23 16:46:26 +01:00
|
|
|
}
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2020-12-04 15:02:58 +01:00
|
|
|
const homeScreen = createHomeScreen(app)
|
2020-11-23 16:46:26 +01:00
|
|
|
homeScreen._id = generateScreenID()
|
2020-11-23 17:56:35 +01:00
|
|
|
screensAndLayouts.push(homeScreen)
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-12-04 15:02:58 +01:00
|
|
|
const loginScreen = createLoginScreen(app)
|
2020-12-01 17:22:06 +01:00
|
|
|
loginScreen._id = generateScreenID()
|
|
|
|
screensAndLayouts.push(loginScreen)
|
|
|
|
|
2020-11-23 17:56:35 +01:00
|
|
|
await db.bulkDocs(screensAndLayouts)
|
2020-12-09 13:30:21 +01:00
|
|
|
await compileStaticAssets(app._id)
|
2020-05-26 17:29:16 +02:00
|
|
|
return newAppFolder
|
|
|
|
}
|