2020-05-07 11:53:34 +02:00
|
|
|
const CouchDB = require("../../db")
|
2020-11-06 14:46:19 +01:00
|
|
|
const compileStaticAssetsForPage = require("../../utilities/builder/compileStaticAssetsForPage")
|
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,
|
|
|
|
getPageParams,
|
2020-11-18 19:24:12 +01:00
|
|
|
getScreenParams,
|
2020-11-02 15:53:51 +01:00
|
|
|
generatePageID,
|
2020-11-03 18:42:54 +01:00
|
|
|
generateScreenID,
|
2020-11-02 15:53:51 +01:00
|
|
|
} = require("../../db/utils")
|
2020-11-18 19:24:12 +01:00
|
|
|
const { BUILTIN_LEVEL_IDS } = require("../../utilities/security/accessLevels")
|
2020-07-14 22:10:51 +02:00
|
|
|
const {
|
|
|
|
downloadExtractComponentLibraries,
|
|
|
|
} = require("../../utilities/createAppPackage")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { MAIN, UNAUTHENTICATED, PageTypes } = require("../../constants/pages")
|
2020-11-03 17:27:28 +01:00
|
|
|
const { HOME_SCREEN } = require("../../constants/screens")
|
2020-11-03 18:42:54 +01:00
|
|
|
const { cloneDeep } = require("lodash/fp")
|
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
|
|
|
|
async function getMainAndUnauthPage(db) {
|
|
|
|
let pages = await db.allDocs(
|
|
|
|
getPageParams(null, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
pages = pages.rows.map(row => row.doc)
|
|
|
|
|
|
|
|
const mainPage = pages.find(page => page.name === PageTypes.MAIN)
|
|
|
|
const unauthPage = pages.find(page => page.name === PageTypes.UNAUTHENTICATED)
|
|
|
|
return { mainPage, unauthPage }
|
|
|
|
}
|
|
|
|
|
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-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 {
|
|
|
|
ctx.body = await Promise.all(apps)
|
|
|
|
}
|
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)
|
|
|
|
// TODO: need to get rid of pages here, they shouldn't be needed anymore
|
|
|
|
const { mainPage, unauthPage } = await getMainAndUnauthPage(db)
|
|
|
|
const userAccessLevelId =
|
|
|
|
!ctx.user.accessLevel || !ctx.user.accessLevel._id
|
|
|
|
? BUILTIN_LEVEL_IDS.PUBLIC
|
|
|
|
: ctx.user.accessLevel._id
|
|
|
|
const correctPage =
|
|
|
|
userAccessLevelId === BUILTIN_LEVEL_IDS.PUBLIC ? unauthPage : mainPage
|
|
|
|
const screens = (
|
|
|
|
await db.allDocs(
|
|
|
|
getScreenParams(correctPage._id, {
|
|
|
|
include_docs: true,
|
|
|
|
})
|
|
|
|
)
|
|
|
|
).rows.map(row => row.doc)
|
|
|
|
// TODO: need to handle access control here, limit screens to user access level
|
|
|
|
ctx.body = {
|
|
|
|
page: correctPage,
|
|
|
|
screens: screens,
|
|
|
|
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-02 15:53:51 +01:00
|
|
|
|
2020-11-18 19:24:12 +01:00
|
|
|
const { mainPage, unauthPage } = await getMainAndUnauthPage(db)
|
2020-11-02 15:53:51 +01:00
|
|
|
ctx.body = {
|
|
|
|
application,
|
2020-11-03 18:42:54 +01:00
|
|
|
pages: {
|
|
|
|
main: mainPage,
|
|
|
|
unauthenticated: unauthPage,
|
|
|
|
},
|
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-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-03 18:42:54 +01:00
|
|
|
const mainPage = cloneDeep(MAIN)
|
|
|
|
mainPage._id = generatePageID()
|
|
|
|
mainPage.title = app.name
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
const unauthPage = cloneDeep(UNAUTHENTICATED)
|
|
|
|
unauthPage._id = generatePageID()
|
|
|
|
unauthPage.title = app.name
|
2020-11-05 15:38:44 +01:00
|
|
|
unauthPage.props._children[0].title = `Log in to ${app.name}`
|
2020-11-06 14:40:00 +01:00
|
|
|
|
2020-11-03 18:42:54 +01:00
|
|
|
const homeScreen = cloneDeep(HOME_SCREEN)
|
|
|
|
homeScreen._id = generateScreenID(mainPage._id)
|
2020-11-05 14:41:16 +01:00
|
|
|
await db.bulkDocs([mainPage, unauthPage, homeScreen])
|
2020-11-02 15:53:51 +01:00
|
|
|
|
2020-11-05 14:41:16 +01:00
|
|
|
await compileStaticAssetsForPage(app._id, "main", {
|
2020-11-03 18:42:54 +01:00
|
|
|
page: mainPage,
|
|
|
|
screens: [homeScreen],
|
|
|
|
})
|
2020-11-05 14:41:16 +01:00
|
|
|
await compileStaticAssetsForPage(app._id, "unauthenticated", {
|
2020-11-03 18:42:54 +01:00
|
|
|
page: unauthPage,
|
|
|
|
screens: [],
|
2020-06-22 22:16:51 +02:00
|
|
|
})
|
|
|
|
|
2020-05-26 17:29:16 +02:00
|
|
|
return newAppFolder
|
|
|
|
}
|