couchDb client - accepts couchdb config
This commit is contained in:
parent
b2e801a77b
commit
bfe8d045f5
|
@ -1,11 +1,19 @@
|
||||||
// const nano = require("nano")
|
// const nano = require("nano")
|
||||||
const PouchDB = require("pouchdb");
|
const PouchDB = require("pouchdb")
|
||||||
|
|
||||||
const COUCH_DB_URL =
|
const COUCH_DB_URL =
|
||||||
process.env.COUCH_DB_URL || "http://admin:password@localhost:5984"
|
process.env.COUCH_DB_URL || "http://admin:password@localhost:5984"
|
||||||
|
|
||||||
const CouchDB = PouchDB.defaults({
|
// database can be "pouch" or "couch"
|
||||||
prefix: COUCH_DB_URL
|
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
|
||||||
|
|
|
@ -4,7 +4,7 @@ const {
|
||||||
} = require("../../utilities/builder")
|
} = require("../../utilities/builder")
|
||||||
|
|
||||||
exports.fetch = async function(ctx) {
|
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", {
|
const body = await clientDb.query("client/by_type", {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
key: ["app"]
|
key: ["app"]
|
||||||
|
|
|
@ -18,7 +18,7 @@ exports.authenticate = async ctx => {
|
||||||
if (!password) ctx.throw(400, "Password Required");
|
if (!password) ctx.throw(400, "Password Required");
|
||||||
|
|
||||||
// query couch for their username
|
// 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", {
|
const dbUser = await db.query("database/by_username", {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
key: username
|
key: username
|
||||||
|
|
|
@ -23,7 +23,7 @@ exports.create = async function(ctx) {
|
||||||
exports.destroy = async function(ctx) {
|
exports.destroy = async function(ctx) {
|
||||||
const dbId = `client-${ctx.params.clientId}`;
|
const dbId = `client-${ctx.params.clientId}`;
|
||||||
|
|
||||||
await new CouchDB(dbId).destroy();
|
await new CouchDB(ctx.config)(dbId).destroy();
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
status: 200,
|
status: 200,
|
||||||
|
|
|
@ -5,7 +5,7 @@ exports.create = async function(ctx) {
|
||||||
// await couchdb.db.create(instanceName);
|
// await couchdb.db.create(instanceName);
|
||||||
|
|
||||||
const { clientId, applicationId } = ctx.params;
|
const { clientId, applicationId } = ctx.params;
|
||||||
const db = new CouchDB(instanceName);
|
const db = new CouchDB(ctx.config)(instanceName);
|
||||||
await db.put({
|
await db.put({
|
||||||
_id: "_design/database",
|
_id: "_design/database",
|
||||||
metadata: {
|
metadata: {
|
||||||
|
@ -23,7 +23,7 @@ exports.create = async function(ctx) {
|
||||||
|
|
||||||
// Add the new instance under the app clientDB
|
// Add the new instance under the app clientDB
|
||||||
const clientDatabaseId = `client-${clientId}`
|
const clientDatabaseId = `client-${clientId}`
|
||||||
const clientDb = new CouchDB(clientDatabaseId);
|
const clientDb = new CouchDB(ctx.config)(clientDatabaseId);
|
||||||
const budibaseApp = await clientDb.get(applicationId);
|
const budibaseApp = await clientDb.get(applicationId);
|
||||||
const instance = { id: instanceName, name: instanceName };
|
const instance = { id: instanceName, name: instanceName };
|
||||||
budibaseApp.instances.push(instance);
|
budibaseApp.instances.push(instance);
|
||||||
|
@ -37,13 +37,13 @@ exports.create = async function(ctx) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.destroy = 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");
|
const designDoc = await db.get("_design/database");
|
||||||
await db.destroy();
|
await db.destroy();
|
||||||
|
|
||||||
// remove instance from client application document
|
// remove instance from client application document
|
||||||
const { metadata } = designDoc;
|
const { metadata } = designDoc;
|
||||||
const clientDb = new CouchDB(metadata.clientId);
|
const clientDb = new CouchDB(ctx.config)(metadata.clientId);
|
||||||
const budibaseApp = await clientDb.get(metadata.applicationId);
|
const budibaseApp = await clientDb.get(metadata.applicationId);
|
||||||
budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId);
|
budibaseApp.instances = budibaseApp.instances.filter(instance => instance !== ctx.params.instanceId);
|
||||||
await clientDb.put(budibaseApp);
|
await clientDb.put(budibaseApp);
|
||||||
|
|
|
@ -10,7 +10,7 @@ exports.fetch = async function(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.create = 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({
|
const newModel = await db.post({
|
||||||
type: "model",
|
type: "model",
|
||||||
...ctx.request.body
|
...ctx.request.body
|
||||||
|
@ -44,7 +44,7 @@ exports.update = async function(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.destroy = 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 model = await db.remove(ctx.params.modelId, ctx.params.revId);
|
||||||
const modelViewId = `all_${model.id}`
|
const modelViewId = `all_${model.id}`
|
||||||
|
|
|
@ -44,7 +44,7 @@ exports.save = async function(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.fetch = 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(
|
const response = await db.query(
|
||||||
`database/${ctx.params.viewName}`,
|
`database/${ctx.params.viewName}`,
|
||||||
{
|
{
|
||||||
|
@ -55,12 +55,12 @@ exports.fetch = async function(ctx) {
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.find = 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)
|
ctx.body = await db.get(ctx.params.recordId)
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.destroy = async function(ctx) {
|
exports.destroy = async function(ctx) {
|
||||||
const databaseId = ctx.params.instanceId;
|
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);
|
ctx.body = await db.destroy(ctx.params.recordId, ctx.params.revId);
|
||||||
};
|
};
|
||||||
|
|
|
@ -2,7 +2,7 @@ const CouchDB = require("../../db");
|
||||||
const bcrypt = require("../../utilities/bcrypt");
|
const bcrypt = require("../../utilities/bcrypt");
|
||||||
|
|
||||||
exports.fetch = async function(ctx) {
|
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", {
|
const data = await database.query("database/by_type", {
|
||||||
include_docs: true,
|
include_docs: true,
|
||||||
key: ["user"]
|
key: ["user"]
|
||||||
|
@ -12,7 +12,7 @@ exports.fetch = async function(ctx) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.create = 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;
|
const { username, password, name } = ctx.request.body;
|
||||||
|
|
||||||
if (!username || !password) ctx.throw(400, "Username and Password Required.");
|
if (!username || !password) ctx.throw(400, "Username and Password Required.");
|
||||||
|
@ -37,7 +37,7 @@ exports.create = async function(ctx) {
|
||||||
};
|
};
|
||||||
|
|
||||||
exports.destroy = 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)
|
const response = await database.destroy(ctx.params.userId)
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
...response,
|
...response,
|
||||||
|
|
|
@ -2,12 +2,12 @@ const CouchDB = require("../../db");
|
||||||
|
|
||||||
const controller = {
|
const controller = {
|
||||||
fetch: async ctx => {
|
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");
|
const designDoc = await db.get("_design/database");
|
||||||
ctx.body = designDoc.views;
|
ctx.body = designDoc.views;
|
||||||
},
|
},
|
||||||
create: async ctx => {
|
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 { name, ...viewDefinition } = ctx.request.body;
|
||||||
|
|
||||||
const designDoc = await db.get("_design/database");
|
const designDoc = await db.get("_design/database");
|
||||||
|
@ -24,7 +24,7 @@ const controller = {
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
destroy: async ctx => {
|
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)
|
ctx.body = await db.destroy(ctx.params.userId)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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 CLIENT_DB_ID = "client-testing";
|
||||||
const TEST_APP_ID = "test-app";
|
const TEST_APP_ID = "test-app";
|
||||||
|
|
||||||
|
@ -36,29 +36,8 @@ exports.createModel = async (instanceId, model) => {
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
exports.createClientDatabase = async () => {
|
exports.createClientDatabase = async () =>
|
||||||
await couchdb.db.create(CLIENT_DB_ID);
|
await createClientDb(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.destroyClientDatabase = async () => await couchdb.db.destroy(CLIENT_DB_ID);
|
exports.destroyClientDatabase = async () => await couchdb.db.destroy(CLIENT_DB_ID);
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue