Removing instance API as it was un-used and simplifying the nature of an instance.

This commit is contained in:
mike12345567 2020-10-29 10:21:06 +00:00
parent 66e3e3b7a4
commit 08b0834fe2
16 changed files with 47 additions and 148 deletions

View File

@ -137,10 +137,10 @@ const setPackage = (store, initial) => async pkg => {
...Object.values(unauth_screens), ...Object.values(unauth_screens),
] ]
initial.builtins = [getBuiltin("##builtin/screenslot")] initial.builtins = [getBuiltin("##builtin/screenslot")]
initial.appInstances = pkg.application.instances initial.appInstance = pkg.application.instance
initial.appId = pkg.application._id initial.appId = pkg.application._id
store.set(initial) store.set(initial)
await backendUiStore.actions.database.select(initial.appInstances[0]) await backendUiStore.actions.database.select(initial.appInstance)
return initial return initial
} }

View File

@ -7,15 +7,15 @@
import { last } from "lodash/fp" import { last } from "lodash/fp"
import FrontendNavigatePane from "components/userInterface/FrontendNavigatePane.svelte" import FrontendNavigatePane from "components/userInterface/FrontendNavigatePane.svelte"
$: instances = $store.appInstances $: instance = $store.appInstance
async function selectDatabase(database) { async function selectDatabase(database) {
backendUiStore.actions.database.select(database) backendUiStore.actions.database.select(database)
} }
onMount(async () => { onMount(async () => {
if ($store.appInstances.length > 0 && !$backendUiStore.database) { if ($store.appInstance && !$backendUiStore.database) {
await selectDatabase($store.appInstances[0]) await selectDatabase($store.appInstance)
} }
}) })

View File

@ -1,7 +1,6 @@
const CouchDB = require("../../db") const CouchDB = require("../../db")
const { getPackageForBuilder, buildPage } = require("../../utilities/builder") const { getPackageForBuilder, buildPage } = require("../../utilities/builder")
const env = require("../../environment") const env = require("../../environment")
const instanceController = require("./instance")
const { copy, existsSync, readFile, writeFile } = require("fs-extra") const { copy, existsSync, readFile, writeFile } = require("fs-extra")
const { budibaseAppsDir } = require("../../utilities/budibaseDir") const { budibaseAppsDir } = require("../../utilities/budibaseDir")
const sqrl = require("squirrelly") const sqrl = require("squirrelly")
@ -11,12 +10,42 @@ const { join, resolve } = require("../../utilities/centralPath")
const { promisify } = require("util") const { promisify } = require("util")
const chmodr = require("chmodr") const chmodr = require("chmodr")
const packageJson = require("../../../package.json") const packageJson = require("../../../package.json")
const { DocumentTypes, SEPARATOR } = require("../../db/utils") const { createLinkView } = require("../../db/linkedRows")
const { downloadTemplate } = require("../../utilities/templates")
const { generateAppID, DocumentTypes, SEPARATOR } = require("../../db/utils")
const { const {
downloadExtractComponentLibraries, downloadExtractComponentLibraries,
} = require("../../utilities/createAppPackage") } = require("../../utilities/createAppPackage")
const APP_PREFIX = DocumentTypes.APP + SEPARATOR const APP_PREFIX = DocumentTypes.APP + SEPARATOR
async function createInstance(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) {
throw "Error loading database dump from template."
}
}
return { _id: instanceId }
}
exports.fetch = async function(ctx) { exports.fetch = async function(ctx) {
let allDbs = await CouchDB.allDbs() let allDbs = await CouchDB.allDbs()
const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX)) const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX))
@ -40,15 +69,8 @@ exports.fetchAppPackage = async function(ctx) {
} }
exports.create = async function(ctx) { exports.create = async function(ctx) {
const createInstCtx = { const instance = await createInstance(ctx.request.body.template)
request: { const instanceId = instance._id
body: {
template: ctx.request.body.template,
},
},
}
await instanceController.create(createInstCtx)
const instanceId = createInstCtx.body._id
const newApplication = { const newApplication = {
_id: instanceId, _id: instanceId,
type: "app", type: "app",
@ -57,7 +79,7 @@ exports.create = async function(ctx) {
componentLibraries: ["@budibase/standard-components"], componentLibraries: ["@budibase/standard-components"],
name: ctx.request.body.name, name: ctx.request.body.name,
template: ctx.request.body.template, template: ctx.request.body.template,
instances: [createInstCtx.body], instance: instance,
} }
const instanceDb = new CouchDB(instanceId) const instanceDb = new CouchDB(instanceId)
await instanceDb.put(newApplication) await instanceDb.put(newApplication)

View File

@ -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.`
}

View File

@ -9,7 +9,6 @@ const {
pageRoutes, pageRoutes,
userRoutes, userRoutes,
deployRoutes, deployRoutes,
instanceRoutes,
applicationRoutes, applicationRoutes,
rowRoutes, rowRoutes,
tableRoutes, tableRoutes,
@ -82,9 +81,6 @@ router.use(rowRoutes.allowedMethods())
router.use(userRoutes.routes()) router.use(userRoutes.routes())
router.use(userRoutes.allowedMethods()) router.use(userRoutes.allowedMethods())
router.use(instanceRoutes.routes())
router.use(instanceRoutes.allowedMethods())
router.use(automationRoutes.routes()) router.use(automationRoutes.routes())
router.use(automationRoutes.allowedMethods()) router.use(automationRoutes.allowedMethods())

View File

@ -1,7 +1,6 @@
const authRoutes = require("./auth") const authRoutes = require("./auth")
const pageRoutes = require("./pages") const pageRoutes = require("./pages")
const userRoutes = require("./user") const userRoutes = require("./user")
const instanceRoutes = require("./instance")
const applicationRoutes = require("./application") const applicationRoutes = require("./application")
const tableRoutes = require("./table") const tableRoutes = require("./table")
const rowRoutes = require("./row") const rowRoutes = require("./row")
@ -21,7 +20,6 @@ module.exports = {
authRoutes, authRoutes,
pageRoutes, pageRoutes,
userRoutes, userRoutes,
instanceRoutes,
applicationRoutes, applicationRoutes,
rowRoutes, rowRoutes,
tableRoutes, tableRoutes,

View File

@ -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

View File

@ -31,7 +31,7 @@ describe("/accesslevels", () => {
beforeEach(async () => { beforeEach(async () => {
let app = await createApplication(request) let app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
table = await createTable(request, instanceId) table = await createTable(request, instanceId)
view = await createView(request, instanceId, table._id) view = await createView(request, instanceId, table._id)
}) })

View File

@ -36,7 +36,7 @@ describe("/applications", () => {
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request) const otherApplication = await createApplication(request)
const instanceId = otherApplication.instances[0]._id const instanceId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "POST", method: "POST",
@ -64,7 +64,7 @@ describe("/applications", () => {
it("should apply authorization to endpoint", async () => { it("should apply authorization to endpoint", async () => {
const otherApplication = await createApplication(request) const otherApplication = await createApplication(request)
const instanceId = otherApplication.instances[0]._id const instanceId = otherApplication.instance._id
await builderEndpointShouldBlockNormalUsers({ await builderEndpointShouldBlockNormalUsers({
request, request,
method: "GET", method: "GET",

View File

@ -49,7 +49,7 @@ describe("/automations", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
if (automation) await destroyDocument(automation.id) if (automation) await destroyDocument(automation.id)
}) })

View File

@ -109,16 +109,6 @@ exports.clearApplications = async request => {
} }
} }
exports.createInstance = async request => {
const res = await request
.post(`/api/instances`)
.send({
name: "test-instance2",
})
.set(exports.defaultHeaders())
return res.body
}
exports.createUser = async ( exports.createUser = async (
request, request,
instanceId, instanceId,

View File

@ -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.`);
})
});
});

View File

@ -24,7 +24,7 @@ describe("/rows", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
table = await createTable(request, instanceId) table = await createTable(request, instanceId)
row = { row = {
name: "Test Contact", name: "Test Contact",

View File

@ -23,7 +23,7 @@ describe("/tables", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
}); });
describe("create", () => { describe("create", () => {

View File

@ -23,7 +23,7 @@ describe("/users", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
}); });
afterAll(() => { afterAll(() => {

View File

@ -39,7 +39,7 @@ describe("/views", () => {
beforeEach(async () => { beforeEach(async () => {
app = await createApplication(request) app = await createApplication(request)
instanceId = app.instances[0]._id instanceId = app.instance._id
}) })
afterAll(() => { afterAll(() => {