2020-04-23 15:37:08 +02:00
|
|
|
const CouchDB = require("../../db");
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
exports.create = async function(ctx) {
|
|
|
|
const instanceName = ctx.request.body.name;
|
2020-04-23 15:37:08 +02:00
|
|
|
// await couchdb.db.create(instanceName);
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
const { clientId, applicationId } = ctx.params;
|
2020-04-24 10:54:20 +02:00
|
|
|
const db = new CouchDB(ctx.config)(instanceName);
|
2020-04-23 15:37:08 +02:00
|
|
|
await db.put({
|
|
|
|
_id: "_design/database",
|
2020-04-20 17:17:11 +02:00
|
|
|
metadata: {
|
|
|
|
clientId,
|
|
|
|
applicationId
|
|
|
|
},
|
|
|
|
views: {
|
|
|
|
by_type: {
|
|
|
|
map: function(doc) {
|
|
|
|
emit([doc.type], doc._id);
|
2020-04-23 18:05:33 +02:00
|
|
|
}.toString()
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
}
|
2020-04-23 15:37:08 +02:00
|
|
|
});
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
// Add the new instance under the app clientDB
|
|
|
|
const clientDatabaseId = `client-${clientId}`
|
2020-04-24 10:54:20 +02:00
|
|
|
const clientDb = new CouchDB(ctx.config)(clientDatabaseId);
|
2020-04-20 17:17:11 +02:00
|
|
|
const budibaseApp = await clientDb.get(applicationId);
|
2020-04-23 15:37:08 +02:00
|
|
|
const instance = { id: instanceName, name: instanceName };
|
|
|
|
budibaseApp.instances.push(instance);
|
|
|
|
await clientDb.put(budibaseApp);
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
message: `Instance Database ${instanceName} successfully provisioned.`,
|
2020-04-23 15:37:08 +02:00
|
|
|
status: 200,
|
|
|
|
instance
|
2020-04-20 17:17:11 +02:00
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
exports.destroy = async function(ctx) {
|
2020-04-24 10:54:20 +02:00
|
|
|
const db = new CouchDB(ctx.config)(ctx.params.instanceId);
|
2020-04-20 17:17:11 +02:00
|
|
|
const designDoc = await db.get("_design/database");
|
2020-04-23 15:37:08 +02:00
|
|
|
await db.destroy();
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
// remove instance from client application document
|
|
|
|
const { metadata } = designDoc;
|
2020-04-24 10:54:20 +02:00
|
|
|
const clientDb = new CouchDB(ctx.config)(metadata.clientId);
|
2020-04-20 17:17:11 +02:00
|
|
|
const budibaseApp = await clientDb.get(metadata.applicationId);
|
|
|
|
budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId);
|
2020-04-23 15:37:08 +02:00
|
|
|
await clientDb.put(budibaseApp);
|
2020-04-20 17:17:11 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
|
|
|
message: `Instance Database ${ctx.params.instanceId} successfully destroyed.`,
|
|
|
|
status: 200
|
|
|
|
}
|
|
|
|
};
|