From b9f1babc0ed5e3b8a7b5461318e0e6d7d949cdd9 Mon Sep 17 00:00:00 2001 From: mike12345567 Date: Thu, 29 Oct 2020 10:21:06 +0000 Subject: [PATCH] Removing instance API as it was un-used and simplifying the nature of an instance. --- .../builder/src/builderStore/store/index.js | 4 +- .../pages/[application]/design/_layout.svelte | 6 +-- .../server/src/api/controllers/application.js | 46 ++++++++++++----- .../server/src/api/controllers/instance.js | 46 ----------------- packages/server/src/api/index.js | 4 -- packages/server/src/api/routes/index.js | 2 - packages/server/src/api/routes/instance.js | 12 ----- .../src/api/routes/tests/accesslevel.spec.js | 2 +- .../src/api/routes/tests/application.spec.js | 4 +- .../src/api/routes/tests/automation.spec.js | 2 +- .../src/api/routes/tests/couchTestUtils.js | 10 ---- .../src/api/routes/tests/instance.spec.js | 49 ------------------- .../server/src/api/routes/tests/row.spec.js | 2 +- .../server/src/api/routes/tests/table.spec.js | 2 +- .../server/src/api/routes/tests/user.spec.js | 2 +- .../server/src/api/routes/tests/view.spec.js | 2 +- 16 files changed, 47 insertions(+), 148 deletions(-) delete mode 100644 packages/server/src/api/controllers/instance.js delete mode 100644 packages/server/src/api/routes/instance.js delete mode 100644 packages/server/src/api/routes/tests/instance.spec.js diff --git a/packages/builder/src/builderStore/store/index.js b/packages/builder/src/builderStore/store/index.js index 9069682304..0aa3d69ecd 100644 --- a/packages/builder/src/builderStore/store/index.js +++ b/packages/builder/src/builderStore/store/index.js @@ -137,10 +137,10 @@ const setPackage = (store, initial) => async pkg => { ...Object.values(unauth_screens), ] initial.builtins = [getBuiltin("##builtin/screenslot")] - initial.appInstances = pkg.application.instances + initial.appInstance = pkg.application.instance initial.appId = pkg.application._id store.set(initial) - await backendUiStore.actions.database.select(initial.appInstances[0]) + await backendUiStore.actions.database.select(initial.appInstance) return initial } diff --git a/packages/builder/src/pages/[application]/design/_layout.svelte b/packages/builder/src/pages/[application]/design/_layout.svelte index 13f2b4ee53..c4c67c796e 100644 --- a/packages/builder/src/pages/[application]/design/_layout.svelte +++ b/packages/builder/src/pages/[application]/design/_layout.svelte @@ -7,15 +7,15 @@ import { last } from "lodash/fp" import FrontendNavigatePane from "components/userInterface/FrontendNavigatePane.svelte" - $: instances = $store.appInstances + $: instance = $store.appInstance async function selectDatabase(database) { backendUiStore.actions.database.select(database) } onMount(async () => { - if ($store.appInstances.length > 0 && !$backendUiStore.database) { - await selectDatabase($store.appInstances[0]) + if ($store.appInstance && !$backendUiStore.database) { + await selectDatabase($store.appInstance) } }) diff --git a/packages/server/src/api/controllers/application.js b/packages/server/src/api/controllers/application.js index 9f0fdbc524..5e538eb38f 100644 --- a/packages/server/src/api/controllers/application.js +++ b/packages/server/src/api/controllers/application.js @@ -1,7 +1,6 @@ const CouchDB = require("../../db") const { getPackageForBuilder, buildPage } = require("../../utilities/builder") const env = require("../../environment") -const instanceController = require("./instance") const { copy, existsSync, readFile, writeFile } = require("fs-extra") const { budibaseAppsDir } = require("../../utilities/budibaseDir") const sqrl = require("squirrelly") @@ -11,12 +10,42 @@ const { join, resolve } = require("../../utilities/centralPath") const { promisify } = require("util") const chmodr = require("chmodr") 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 { downloadExtractComponentLibraries, } = require("../../utilities/createAppPackage") 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) { let allDbs = await CouchDB.allDbs() const appDbNames = allDbs.filter(dbName => dbName.startsWith(APP_PREFIX)) @@ -40,15 +69,8 @@ exports.fetchAppPackage = async function(ctx) { } exports.create = async function(ctx) { - const createInstCtx = { - request: { - body: { - template: ctx.request.body.template, - }, - }, - } - await instanceController.create(createInstCtx) - const instanceId = createInstCtx.body._id + const instance = await createInstance(ctx.request.body.template) + const instanceId = instance._id const newApplication = { _id: instanceId, type: "app", @@ -57,7 +79,7 @@ exports.create = async function(ctx) { componentLibraries: ["@budibase/standard-components"], name: ctx.request.body.name, template: ctx.request.body.template, - instances: [createInstCtx.body], + instance: instance, } const instanceDb = new CouchDB(instanceId) await instanceDb.put(newApplication) diff --git a/packages/server/src/api/controllers/instance.js b/packages/server/src/api/controllers/instance.js deleted file mode 100644 index 9879919540..0000000000 --- a/packages/server/src/api/controllers/instance.js +++ /dev/null @@ -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.` -} diff --git a/packages/server/src/api/index.js b/packages/server/src/api/index.js index e9418c518a..0fbd65501b 100644 --- a/packages/server/src/api/index.js +++ b/packages/server/src/api/index.js @@ -9,7 +9,6 @@ const { pageRoutes, userRoutes, deployRoutes, - instanceRoutes, applicationRoutes, rowRoutes, tableRoutes, @@ -82,9 +81,6 @@ router.use(rowRoutes.allowedMethods()) router.use(userRoutes.routes()) router.use(userRoutes.allowedMethods()) -router.use(instanceRoutes.routes()) -router.use(instanceRoutes.allowedMethods()) - router.use(automationRoutes.routes()) router.use(automationRoutes.allowedMethods()) diff --git a/packages/server/src/api/routes/index.js b/packages/server/src/api/routes/index.js index 434def06b5..0025c5fabf 100644 --- a/packages/server/src/api/routes/index.js +++ b/packages/server/src/api/routes/index.js @@ -1,7 +1,6 @@ const authRoutes = require("./auth") const pageRoutes = require("./pages") const userRoutes = require("./user") -const instanceRoutes = require("./instance") const applicationRoutes = require("./application") const tableRoutes = require("./table") const rowRoutes = require("./row") @@ -21,7 +20,6 @@ module.exports = { authRoutes, pageRoutes, userRoutes, - instanceRoutes, applicationRoutes, rowRoutes, tableRoutes, diff --git a/packages/server/src/api/routes/instance.js b/packages/server/src/api/routes/instance.js deleted file mode 100644 index 00fce6bb12..0000000000 --- a/packages/server/src/api/routes/instance.js +++ /dev/null @@ -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 diff --git a/packages/server/src/api/routes/tests/accesslevel.spec.js b/packages/server/src/api/routes/tests/accesslevel.spec.js index 1bc540c046..d7958c8f32 100644 --- a/packages/server/src/api/routes/tests/accesslevel.spec.js +++ b/packages/server/src/api/routes/tests/accesslevel.spec.js @@ -31,7 +31,7 @@ describe("/accesslevels", () => { beforeEach(async () => { let app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id table = await createTable(request, instanceId) view = await createView(request, instanceId, table._id) }) diff --git a/packages/server/src/api/routes/tests/application.spec.js b/packages/server/src/api/routes/tests/application.spec.js index 179aa1b3ca..6d30701a29 100644 --- a/packages/server/src/api/routes/tests/application.spec.js +++ b/packages/server/src/api/routes/tests/application.spec.js @@ -36,7 +36,7 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { const otherApplication = await createApplication(request) - const instanceId = otherApplication.instances[0]._id + const instanceId = otherApplication.instance._id await builderEndpointShouldBlockNormalUsers({ request, method: "POST", @@ -64,7 +64,7 @@ describe("/applications", () => { it("should apply authorization to endpoint", async () => { const otherApplication = await createApplication(request) - const instanceId = otherApplication.instances[0]._id + const instanceId = otherApplication.instance._id await builderEndpointShouldBlockNormalUsers({ request, method: "GET", diff --git a/packages/server/src/api/routes/tests/automation.spec.js b/packages/server/src/api/routes/tests/automation.spec.js index 6bda1b7708..fa78cccf0d 100644 --- a/packages/server/src/api/routes/tests/automation.spec.js +++ b/packages/server/src/api/routes/tests/automation.spec.js @@ -49,7 +49,7 @@ describe("/automations", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id if (automation) await destroyDocument(automation.id) }) diff --git a/packages/server/src/api/routes/tests/couchTestUtils.js b/packages/server/src/api/routes/tests/couchTestUtils.js index df50e3cd7c..25b8dd964e 100644 --- a/packages/server/src/api/routes/tests/couchTestUtils.js +++ b/packages/server/src/api/routes/tests/couchTestUtils.js @@ -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 ( request, instanceId, diff --git a/packages/server/src/api/routes/tests/instance.spec.js b/packages/server/src/api/routes/tests/instance.spec.js deleted file mode 100644 index 180d7f44e6..0000000000 --- a/packages/server/src/api/routes/tests/instance.spec.js +++ /dev/null @@ -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.`); - }) - }); -}); diff --git a/packages/server/src/api/routes/tests/row.spec.js b/packages/server/src/api/routes/tests/row.spec.js index bd447513f3..31f273216f 100644 --- a/packages/server/src/api/routes/tests/row.spec.js +++ b/packages/server/src/api/routes/tests/row.spec.js @@ -24,7 +24,7 @@ describe("/rows", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id table = await createTable(request, instanceId) row = { name: "Test Contact", diff --git a/packages/server/src/api/routes/tests/table.spec.js b/packages/server/src/api/routes/tests/table.spec.js index 0c0f64050f..193bfb50a6 100644 --- a/packages/server/src/api/routes/tests/table.spec.js +++ b/packages/server/src/api/routes/tests/table.spec.js @@ -23,7 +23,7 @@ describe("/tables", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id }); describe("create", () => { diff --git a/packages/server/src/api/routes/tests/user.spec.js b/packages/server/src/api/routes/tests/user.spec.js index 010f735c4a..497a654fc3 100644 --- a/packages/server/src/api/routes/tests/user.spec.js +++ b/packages/server/src/api/routes/tests/user.spec.js @@ -23,7 +23,7 @@ describe("/users", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id }); afterAll(() => { diff --git a/packages/server/src/api/routes/tests/view.spec.js b/packages/server/src/api/routes/tests/view.spec.js index ec30f10f3f..0419e7326c 100644 --- a/packages/server/src/api/routes/tests/view.spec.js +++ b/packages/server/src/api/routes/tests/view.spec.js @@ -39,7 +39,7 @@ describe("/views", () => { beforeEach(async () => { app = await createApplication(request) - instanceId = app.instances[0]._id + instanceId = app.instance._id }) afterAll(() => {