2020-05-07 21:53:34 +12:00
|
|
|
const CouchDB = require("../../db")
|
2020-05-18 17:40:29 +12:00
|
|
|
const client = require("../../db/clientDb")
|
2020-05-19 03:22:09 +12:00
|
|
|
const newid = require("../../db/newid")
|
2020-05-15 02:12:30 +12:00
|
|
|
const env = require("../../environment")
|
|
|
|
|
2020-04-21 03:17:11 +12:00
|
|
|
exports.create = async function(ctx) {
|
2020-05-07 21:53:34 +12:00
|
|
|
const instanceName = ctx.request.body.name
|
2020-05-19 03:33:41 +12:00
|
|
|
const appShortId = ctx.params.applicationId.substring(0, 7)
|
|
|
|
const instanceId = `inst_${appShortId}_${newid()}`
|
2020-05-15 02:12:30 +12:00
|
|
|
const { applicationId } = ctx.params
|
|
|
|
const clientId = env.CLIENT_ID
|
2020-05-12 03:14:31 +12:00
|
|
|
const db = new CouchDB(instanceId)
|
2020-04-24 01:37:08 +12:00
|
|
|
await db.put({
|
|
|
|
_id: "_design/database",
|
2020-04-21 03:17:11 +12:00
|
|
|
metadata: {
|
|
|
|
clientId,
|
2020-05-07 21:53:34 +12:00
|
|
|
applicationId,
|
2020-04-21 03:17:11 +12:00
|
|
|
},
|
2020-05-07 21:53:34 +12:00
|
|
|
views: {
|
|
|
|
by_username: {
|
|
|
|
map: function(doc) {
|
2020-05-06 21:33:30 +12:00
|
|
|
if (doc.type === "user") {
|
2020-05-07 21:53:34 +12:00
|
|
|
emit([doc.username], doc._id)
|
2020-05-06 21:33:30 +12:00
|
|
|
}
|
2020-05-07 21:53:34 +12:00
|
|
|
}.toString(),
|
2020-05-06 21:33:30 +12:00
|
|
|
},
|
2020-05-07 21:53:34 +12:00
|
|
|
by_type: {
|
|
|
|
map: function(doc) {
|
|
|
|
emit([doc.type], doc._id)
|
|
|
|
}.toString(),
|
|
|
|
},
|
2020-06-01 04:12:52 +12:00
|
|
|
by_workflow_trigger: {
|
|
|
|
map: function(doc) {
|
|
|
|
if (doc.type === "workflow") {
|
2020-06-02 03:22:13 +12:00
|
|
|
const trigger = doc.definition.trigger
|
2020-06-01 04:12:52 +12:00
|
|
|
if (trigger) {
|
|
|
|
emit([trigger.event], trigger)
|
|
|
|
}
|
|
|
|
}
|
2020-06-01 21:41:28 +12:00
|
|
|
}.toString(),
|
|
|
|
},
|
2020-05-07 21:53:34 +12:00
|
|
|
},
|
|
|
|
})
|
2020-04-21 03:17:11 +12:00
|
|
|
|
|
|
|
// Add the new instance under the app clientDB
|
2020-05-18 17:40:29 +12:00
|
|
|
const clientDb = new CouchDB(client.name(clientId))
|
2020-05-07 21:53:34 +12:00
|
|
|
const budibaseApp = await clientDb.get(applicationId)
|
2020-05-15 02:12:30 +12:00
|
|
|
const instance = { _id: instanceId, name: instanceName }
|
2020-05-07 21:53:34 +12:00
|
|
|
budibaseApp.instances.push(instance)
|
|
|
|
await clientDb.put(budibaseApp)
|
2020-04-21 03:17:11 +12:00
|
|
|
|
2020-05-15 02:12:30 +12:00
|
|
|
ctx.status = 200
|
|
|
|
ctx.message = `Instance Database ${instanceName} successfully provisioned.`
|
|
|
|
ctx.body = instance
|
2020-05-07 21:53:34 +12:00
|
|
|
}
|
2020-04-21 03:17:11 +12:00
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
2020-05-07 21:53:34 +12:00
|
|
|
const db = new CouchDB(ctx.params.instanceId)
|
|
|
|
const designDoc = await db.get("_design/database")
|
|
|
|
await db.destroy()
|
2020-04-21 03:17:11 +12:00
|
|
|
|
|
|
|
// remove instance from client application document
|
2020-05-07 21:53:34 +12:00
|
|
|
const { metadata } = designDoc
|
2020-05-18 17:40:29 +12:00
|
|
|
const clientDb = new CouchDB(client.name(metadata.clientId))
|
2020-05-07 21:53:34 +12:00
|
|
|
const budibaseApp = await clientDb.get(metadata.applicationId)
|
|
|
|
budibaseApp.instances = budibaseApp.instances.filter(
|
|
|
|
instance => instance !== ctx.params.instanceId
|
|
|
|
)
|
2020-05-08 00:52:24 +12:00
|
|
|
await clientDb.put(budibaseApp)
|
2020-04-21 03:17:11 +12:00
|
|
|
|
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
|
|
|
}
|