fetch users from couch, create design document on DB creation

This commit is contained in:
Martin McKeaveney 2020-04-08 11:18:30 +01:00
parent 1341f6cbcf
commit 6b77b2825f
11 changed files with 96 additions and 3 deletions

View File

@ -1,6 +1,10 @@
const couchdb = require("../../db"); const couchdb = require("../../db");
const controller = { const controller = {
fetch: async ctx => {
const clientDatabase = couchdb.db.use(ctx.params.clientId);
ctx.body = await clientDatabase.list({ type: "app" });
},
create: async ctx => { create: async ctx => {
const clientDatabase = couchdb.db.use(ctx.params.clientId); const clientDatabase = couchdb.db.use(ctx.params.clientId);
ctx.body = await clientDatabase.insert(ctx.request.body); ctx.body = await clientDatabase.insert(ctx.request.body);

View File

@ -0,0 +1,18 @@
const couchdb = require("../../db");
const controller = {
save: async ctx => {
},
fetch: async ctx => {
const databaseId = ctx.params.databaseId;
const database = couchdb.db.use(databaseId)
ctx.body = await database.list({});
},
destroy: async ctx => {
const databaseId = ctx.params.databaseId;
const database = couchdb.db.use(databaseId)
ctx.body = await database.destroy(ctx.params.recordId);
},
}
module.exports = controller;

View File

@ -1,8 +1,25 @@
const couchdb = require("../../db"); const couchdb = require("../../db");
const controller = { const controller = {
fetch: async ctx => {
},
create: async ctx => { create: async ctx => {
ctx.body = await couchdb.db.create(ctx.request.body.databaseName); const databaseName = ctx.request.body.databaseName;
await couchdb.db.create(databaseName);
const db = couchdb.db.use(databaseName)
await db.insert({
views: {
by_type: {
map: function(doc) {
emit([doc.type], doc._id);
}
}
}
}, '_design/database');
ctx.body = {
message: `Database ${databaseName} successfully provisioned.`
}
}, },
destroy: async ctx => { destroy: async ctx => {
ctx.body = await couchdb.db.destroy(ctx.params.databaseName) ctx.body = await couchdb.db.destroy(ctx.params.databaseName)

View File

@ -0,0 +1,27 @@
const couchdb = require("../../db");
const fs = require("fs");
const controller = {
download: async ctx => {
// const appDatabase = couchdb.db.use(ctx.params.appId)
// appDatabase.attachment.get('rabbit', 'rabbit.png', function(err, body) {
// if (!err) {
// fs.writeFile('rabbit.png', body);
// }
// });
},
upload: async ctx => {
// const appDatabase = couchdb.db.use(ctx.params.appId)
// fs.readFile('rabbit.png', function(err, data) {
// if (!err) {
// alice.attachment.insert('rabbit', 'rabbit.png', data, 'image/png',
// { rev: '12-150985a725ec88be471921a54ce91452' }, function(err, body) {
// if (!err)
// console.log(body);
// });
// }
// });
}
}
module.exports = controller;

View File

@ -3,7 +3,10 @@ const couchdb = require("../../db");
const controller = { const controller = {
fetch: async ctx => { fetch: async ctx => {
const database = couchdb.db.use(ctx.params.databaseId); const database = couchdb.db.use(ctx.params.databaseId);
ctx.body = await database.list({ include_docs: true }); ctx.body = await database.view("database", "by_type", {
include_docs: true,
key: ["user"]
})
}, },
create: async ctx => { create: async ctx => {
const database = couchdb.db.use(ctx.params.databaseId); const database = couchdb.db.use(ctx.params.databaseId);

View File

@ -3,6 +3,8 @@ const controller = require("../../controllers/application");
const router = Router(); const router = Router();
router.post("/api/:clientId/applications", controller.create) router
.get("/api/:clientId/applications", controller.fetch)
.post("/api/:clientId/applications", controller.create)
module.exports = router; module.exports = router;

View File

@ -0,0 +1,10 @@
const Router = require("@koa/router");
const controller = require("../../controllers/client");
const router = Router();
router
.post("/api/clients", controller.create)
.delete("/api/clients/:clientId", controller.destroy);
module.exports = router;

View File

@ -4,6 +4,7 @@ const controller = require("../../controllers/database");
const router = Router(); const router = Router();
router router
.get("/api/databases", controller.fetch)
.post("/api/databases", controller.create) .post("/api/databases", controller.create)
.delete("/api/databases/:databaseId", controller.destroy); .delete("/api/databases/:databaseId", controller.destroy);

View File

@ -0,0 +1,11 @@
const Router = require("@koa/router");
const controller = require("../../controllers/view");
const router = Router();
router
.get("/api/:databaseId/views", controller.fetch)
.post("/api/:databaseId/views", controller.create)
.patch("/api/:databaseId/views", controller.update);
module.exports = router;