1
0
Fork 0
mirror of synced 2024-09-29 16:51:33 +13:00
budibase/packages/server/src/api/controllers/instance.js

47 lines
1.5 KiB
JavaScript
Raw Normal View History

2020-09-26 01:47:42 +12:00
const fs = require("fs")
2020-05-07 21:53:34 +12:00
const CouchDB = require("../../db")
const { createLinkView } = require("../../db/linkedRows")
const { join } = require("../../utilities/centralPath")
2020-09-29 05:04:08 +13:00
const { downloadTemplate } = require("../../utilities/templates")
const { generateAppID } = require("../../db/utils")
2020-05-15 02:12:30 +12:00
exports.create = async function(ctx) {
2020-05-07 21:53:34 +12:00
const instanceName = ctx.request.body.name
2020-09-26 01:47:42 +12:00
const template = ctx.request.body.template
const instanceId = generateAppID()
const db = new CouchDB(instanceId)
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
2020-10-03 01:04:44 +13:00
views: {},
2020-05-07 21:53:34 +12:00
})
// add view for linked rows
await createLinkView(instanceId)
2020-09-26 01:47:42 +12:00
// replicate the template data to the instance DB
if (template) {
2020-09-29 05:04:08 +13:00
const templatePath = await downloadTemplate(...template.key.split("/"))
const dbDumpReadStream = fs.createReadStream(
join(templatePath, "db", "dump.txt")
)
2020-09-26 01:47:42 +12:00
const { ok } = await db.load(dbDumpReadStream)
if (!ok) {
ctx.throw(500, "Error loading database dump from template.")
}
}
2020-05-15 02:12:30 +12:00
ctx.status = 200
ctx.message = `Instance Database ${instanceName} successfully provisioned.`
ctx.body = { _id: instanceId, name: instanceName }
2020-05-07 21:53:34 +12:00
}
exports.destroy = async function(ctx) {
2020-05-07 21:53:34 +12:00
const db = new CouchDB(ctx.params.instanceId)
await db.destroy()
2020-05-15 02:12:30 +12:00
ctx.status = 200
ctx.message = `Instance Database ${ctx.params.instanceId} successfully destroyed.`
2020-05-07 21:53:34 +12:00
}