couchDb client - accepts couchdb config

This commit is contained in:
Michael Shanks 2020-04-24 09:54:20 +01:00
parent fdc950d221
commit 421d822aec
10 changed files with 35 additions and 48 deletions

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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

View File

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