mirror of https://github.com/Budibase/budibase.git
16 changed files with 47 additions and 148 deletions
@ -1,46 +0,0 @@ |
|||
const fs = require("fs") |
|||
const CouchDB = require("../../db") |
|||
const { createLinkView } = require("../../db/linkedRows") |
|||
const { join } = require("../../utilities/centralPath") |
|||
const { downloadTemplate } = require("../../utilities/templates") |
|||
const { generateAppID } = require("../../db/utils") |
|||
|
|||
exports.create = async function(ctx) { |
|||
const instanceName = ctx.request.body.name |
|||
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
|
|||
views: {}, |
|||
}) |
|||
// add view for linked rows
|
|||
await createLinkView(instanceId) |
|||
|
|||
// 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) { |
|||
ctx.throw(500, "Error loading database dump from template.") |
|||
} |
|||
} |
|||
|
|||
ctx.status = 200 |
|||
ctx.message = `Instance Database ${instanceName} successfully provisioned.` |
|||
ctx.body = { _id: instanceId, name: instanceName } |
|||
} |
|||
|
|||
exports.destroy = async function(ctx) { |
|||
const db = new CouchDB(ctx.params.instanceId) |
|||
await db.destroy() |
|||
|
|||
ctx.status = 200 |
|||
ctx.message = `Instance Database ${ctx.params.instanceId} successfully destroyed.` |
|||
} |
|||
@ -1,12 +0,0 @@ |
|||
const Router = require("@koa/router") |
|||
const controller = require("../controllers/instance") |
|||
const authorized = require("../../middleware/authorized") |
|||
const { BUILDER } = require("../../utilities/accessLevels") |
|||
|
|||
const router = Router() |
|||
|
|||
router |
|||
.post("/api/instances", authorized(BUILDER), controller.create) |
|||
.delete("/api/instances/:instanceId", authorized(BUILDER), controller.destroy) |
|||
|
|||
module.exports = router |
|||
@ -1,49 +0,0 @@ |
|||
const { |
|||
createInstance, |
|||
createApplication, |
|||
supertest, |
|||
defaultHeaders |
|||
} = require("./couchTestUtils"); |
|||
|
|||
describe("/instances", () => { |
|||
let TEST_APP_ID; |
|||
let server |
|||
let request |
|||
beforeAll(async () => { |
|||
({ request, server } = await supertest()) |
|||
TEST_APP_ID = (await createApplication(request))._id |
|||
}); |
|||
|
|||
afterAll(() => { |
|||
server.close() |
|||
}) |
|||
|
|||
describe("create", () => { |
|||
|
|||
it("returns a success message when the instance database is successfully created", async () => { |
|||
const res = await request |
|||
.post(`/api/instances`) |
|||
.send({ name: "test-instance" }) |
|||
.set(defaultHeaders()) |
|||
.expect('Content-Type', /json/) |
|||
.expect(200) |
|||
|
|||
expect(res.res.statusMessage).toEqual("Instance Database test-instance successfully provisioned."); |
|||
expect(res.body._id).toBeDefined(); |
|||
|
|||
}) |
|||
}); |
|||
|
|||
describe("destroy", () => { |
|||
|
|||
it("returns a success message when the instance database is successfully deleted", async () => { |
|||
const instance = await createInstance(request, TEST_APP_ID); |
|||
const res = await request |
|||
.delete(`/api/instances/${instance._id}`) |
|||
.set(defaultHeaders()) |
|||
.expect(200) |
|||
|
|||
expect(res.res.statusMessage).toEqual(`Instance Database ${instance._id} successfully destroyed.`); |
|||
}) |
|||
}); |
|||
}); |
|||
Loading…
Reference in new issue