diff --git a/packages/server/db/client.js b/packages/server/db/client.js index 371b55221e..410392897b 100644 --- a/packages/server/db/client.js +++ b/packages/server/db/client.js @@ -1,11 +1,19 @@ // const nano = require("nano") -const PouchDB = require("pouchdb"); +const PouchDB = require("pouchdb") const COUCH_DB_URL = process.env.COUCH_DB_URL || "http://admin:password@localhost:5984" -const CouchDB = PouchDB.defaults({ - prefix: COUCH_DB_URL -}); +// database can be "pouch" or "couch" +const CouchDB = ({ database, couchDbConnectionString }) => { + database = database || "couch" + couchDbConnectionString = couchDbConnectionString || COUCH_DB_URL + if (database === "couch") { + return PouchDB.defaults({ + prefix: couchDbConnectionString, + }) + } + // else setup for leveldb +} -module.exports = CouchDB; +module.exports = CouchDB diff --git a/packages/server/middleware/controllers/application.js b/packages/server/middleware/controllers/application.js index 1b52eb3cf1..4f308ee08a 100644 --- a/packages/server/middleware/controllers/application.js +++ b/packages/server/middleware/controllers/application.js @@ -4,7 +4,7 @@ const { } = require("../../utilities/builder") exports.fetch = async function(ctx) { - const clientDb = new CouchDB(`client-${ctx.params.clientId}`); + const clientDb = new CouchDB(ctx.config)(`client-${ctx.params.clientId}`); const body = await clientDb.query("client/by_type", { include_docs: true, key: ["app"] diff --git a/packages/server/middleware/controllers/auth.js b/packages/server/middleware/controllers/auth.js index 2edad7f06c..6c122fffd2 100644 --- a/packages/server/middleware/controllers/auth.js +++ b/packages/server/middleware/controllers/auth.js @@ -18,7 +18,7 @@ exports.authenticate = async ctx => { if (!password) ctx.throw(400, "Password Required"); // query couch for their username - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); const dbUser = await db.query("database/by_username", { include_docs: true, key: username diff --git a/packages/server/middleware/controllers/client.js b/packages/server/middleware/controllers/client.js index 981b541441..186d68b60d 100644 --- a/packages/server/middleware/controllers/client.js +++ b/packages/server/middleware/controllers/client.js @@ -23,7 +23,7 @@ exports.create = async function(ctx) { exports.destroy = async function(ctx) { const dbId = `client-${ctx.params.clientId}`; - await new CouchDB(dbId).destroy(); + await new CouchDB(ctx.config)(dbId).destroy(); ctx.body = { status: 200, diff --git a/packages/server/middleware/controllers/instance.js b/packages/server/middleware/controllers/instance.js index 557e565785..3b2e4d1871 100644 --- a/packages/server/middleware/controllers/instance.js +++ b/packages/server/middleware/controllers/instance.js @@ -5,7 +5,7 @@ exports.create = async function(ctx) { // await couchdb.db.create(instanceName); const { clientId, applicationId } = ctx.params; - const db = new CouchDB(instanceName); + const db = new CouchDB(ctx.config)(instanceName); await db.put({ _id: "_design/database", metadata: { @@ -23,7 +23,7 @@ exports.create = async function(ctx) { // Add the new instance under the app clientDB const clientDatabaseId = `client-${clientId}` - const clientDb = new CouchDB(clientDatabaseId); + const clientDb = new CouchDB(ctx.config)(clientDatabaseId); const budibaseApp = await clientDb.get(applicationId); const instance = { id: instanceName, name: instanceName }; budibaseApp.instances.push(instance); @@ -37,13 +37,13 @@ exports.create = async function(ctx) { }; exports.destroy = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); const designDoc = await db.get("_design/database"); await db.destroy(); // remove instance from client application document const { metadata } = designDoc; - const clientDb = new CouchDB(metadata.clientId); + const clientDb = new CouchDB(ctx.config)(metadata.clientId); const budibaseApp = await clientDb.get(metadata.applicationId); budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId); await clientDb.put(budibaseApp); diff --git a/packages/server/middleware/controllers/model.js b/packages/server/middleware/controllers/model.js index d9c8fd36c1..d676dfa76a 100644 --- a/packages/server/middleware/controllers/model.js +++ b/packages/server/middleware/controllers/model.js @@ -10,7 +10,7 @@ exports.fetch = async function(ctx) { } exports.create = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); const newModel = await db.post({ type: "model", ...ctx.request.body @@ -44,7 +44,7 @@ exports.update = async function(ctx) { } exports.destroy = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) + const db = new CouchDB(ctx.config)(ctx.params.instanceId) const model = await db.remove(ctx.params.modelId, ctx.params.revId); const modelViewId = `all_${model.id}` diff --git a/packages/server/middleware/controllers/record.js b/packages/server/middleware/controllers/record.js index 5669227f0f..5f02ac6bf3 100644 --- a/packages/server/middleware/controllers/record.js +++ b/packages/server/middleware/controllers/record.js @@ -44,7 +44,7 @@ exports.save = async function(ctx) { } exports.fetch = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) + const db = new CouchDB(ctx.config)(ctx.params.instanceId) const response = await db.query( `database/${ctx.params.viewName}`, { @@ -55,12 +55,12 @@ exports.fetch = async function(ctx) { } exports.find = async function(ctx) { - const db = new CouchDB(ctx.params.instanceId) + const db = new CouchDB(ctx.config)(ctx.params.instanceId) ctx.body = await db.get(ctx.params.recordId) } exports.destroy = async function(ctx) { const databaseId = ctx.params.instanceId; - const db = new CouchDB(databaseId) + const db = new CouchDB(ctx.config)(databaseId) ctx.body = await db.destroy(ctx.params.recordId, ctx.params.revId); }; diff --git a/packages/server/middleware/controllers/user.js b/packages/server/middleware/controllers/user.js index 6164197dc2..b8f6be4c15 100644 --- a/packages/server/middleware/controllers/user.js +++ b/packages/server/middleware/controllers/user.js @@ -2,7 +2,7 @@ const CouchDB = require("../../db"); const bcrypt = require("../../utilities/bcrypt"); exports.fetch = async function(ctx) { - const database = new CouchDB(ctx.params.instanceId); + const database = new CouchDB(ctx.config)(ctx.params.instanceId); const data = await database.query("database/by_type", { include_docs: true, key: ["user"] @@ -12,7 +12,7 @@ exports.fetch = async function(ctx) { }; exports.create = async function(ctx) { - const database = new CouchDB(ctx.params.instanceId); + const database = new CouchDB(ctx.config)(ctx.params.instanceId); const { username, password, name } = ctx.request.body; if (!username || !password) ctx.throw(400, "Username and Password Required."); @@ -37,7 +37,7 @@ exports.create = async function(ctx) { }; exports.destroy = async function(ctx) { - const database = new CouchDB(ctx.params.instanceId); + const database = new CouchDB(ctx.config)(ctx.params.instanceId); const response = await database.destroy(ctx.params.userId) ctx.body = { ...response, diff --git a/packages/server/middleware/controllers/view.js b/packages/server/middleware/controllers/view.js index 69acb758c5..daef504d7e 100644 --- a/packages/server/middleware/controllers/view.js +++ b/packages/server/middleware/controllers/view.js @@ -2,12 +2,12 @@ const CouchDB = require("../../db"); const controller = { fetch: async ctx => { - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); const designDoc = await db.get("_design/database"); ctx.body = designDoc.views; }, create: async ctx => { - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); const { name, ...viewDefinition } = ctx.request.body; const designDoc = await db.get("_design/database"); @@ -24,7 +24,7 @@ const controller = { } }, destroy: async ctx => { - const db = new CouchDB(ctx.params.instanceId); + const db = new CouchDB(ctx.config)(ctx.params.instanceId); ctx.body = await db.destroy(ctx.params.userId) } } diff --git a/packages/server/middleware/routes/neo/tests/couchTestUtils.js b/packages/server/middleware/routes/neo/tests/couchTestUtils.js index cf2f5803b1..4892f11678 100644 --- a/packages/server/middleware/routes/neo/tests/couchTestUtils.js +++ b/packages/server/middleware/routes/neo/tests/couchTestUtils.js @@ -1,5 +1,5 @@ -const couchdb = require("../../../../db"); - +const couchdb = require("../../../../db")({ database: "couch" }) +const createClientDb = require("../../../../db/createClientDb") const CLIENT_DB_ID = "client-testing"; const TEST_APP_ID = "test-app"; @@ -36,29 +36,8 @@ exports.createModel = async (instanceId, model) => { }; } -exports.createClientDatabase = async () => { - await couchdb.db.create(CLIENT_DB_ID); - - const db = couchdb.db.use(CLIENT_DB_ID); - - await db.insert({ - views: { - by_type: { - map: function(doc) { - emit([doc.type], doc._id); - } - } - } - }, '_design/client'); - - await db.insert({ - _id: TEST_APP_ID, - type: "app", - instances: [] - }); - - return CLIENT_DB_ID; -} +exports.createClientDatabase = async () => + await createClientDb(CLIENT_DB_ID) exports.destroyClientDatabase = async () => await couchdb.db.destroy(CLIENT_DB_ID);