From 24d46932cc68b20c6348fb21c6813aa919fdb073 Mon Sep 17 00:00:00 2001 From: Martin McKeaveney Date: Wed, 8 Apr 2020 11:18:30 +0100 Subject: [PATCH] fetch users from couch, create design document on DB creation --- .../middleware/controllers/application.js | 4 +++ .../server/middleware/controllers/client.js | 18 +++++++++++++ .../server/middleware/controllers/database.js | 19 ++++++++++++- .../server/middleware/controllers/static.js | 27 +++++++++++++++++++ .../server/middleware/controllers/user.js | 5 +++- .../server/middleware/controllers/view.js | 0 .../middleware/routes/neo/application.js | 4 ++- .../server/middleware/routes/neo/client.js | 10 +++++++ .../server/middleware/routes/neo/database.js | 1 + .../server/middleware/routes/neo/static.js | 0 packages/server/middleware/routes/neo/view.js | 11 ++++++++ 11 files changed, 96 insertions(+), 3 deletions(-) create mode 100644 packages/server/middleware/controllers/client.js create mode 100644 packages/server/middleware/controllers/static.js create mode 100644 packages/server/middleware/controllers/view.js create mode 100644 packages/server/middleware/routes/neo/client.js create mode 100644 packages/server/middleware/routes/neo/static.js create mode 100644 packages/server/middleware/routes/neo/view.js diff --git a/packages/server/middleware/controllers/application.js b/packages/server/middleware/controllers/application.js index 213af8768d..9eba621d77 100644 --- a/packages/server/middleware/controllers/application.js +++ b/packages/server/middleware/controllers/application.js @@ -1,6 +1,10 @@ const couchdb = require("../../db"); const controller = { + fetch: async ctx => { + const clientDatabase = couchdb.db.use(ctx.params.clientId); + ctx.body = await clientDatabase.list({ type: "app" }); + }, create: async ctx => { const clientDatabase = couchdb.db.use(ctx.params.clientId); ctx.body = await clientDatabase.insert(ctx.request.body); diff --git a/packages/server/middleware/controllers/client.js b/packages/server/middleware/controllers/client.js new file mode 100644 index 0000000000..0f8a550e9e --- /dev/null +++ b/packages/server/middleware/controllers/client.js @@ -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; \ No newline at end of file diff --git a/packages/server/middleware/controllers/database.js b/packages/server/middleware/controllers/database.js index 24093e8142..937576c1cf 100644 --- a/packages/server/middleware/controllers/database.js +++ b/packages/server/middleware/controllers/database.js @@ -1,8 +1,25 @@ const couchdb = require("../../db"); const controller = { + fetch: 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 => { ctx.body = await couchdb.db.destroy(ctx.params.databaseName) diff --git a/packages/server/middleware/controllers/static.js b/packages/server/middleware/controllers/static.js new file mode 100644 index 0000000000..573498eb6e --- /dev/null +++ b/packages/server/middleware/controllers/static.js @@ -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; \ No newline at end of file diff --git a/packages/server/middleware/controllers/user.js b/packages/server/middleware/controllers/user.js index b1b1e04a77..9d2c1778c5 100644 --- a/packages/server/middleware/controllers/user.js +++ b/packages/server/middleware/controllers/user.js @@ -3,7 +3,10 @@ const couchdb = require("../../db"); const controller = { fetch: async ctx => { 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 => { const database = couchdb.db.use(ctx.params.databaseId); diff --git a/packages/server/middleware/controllers/view.js b/packages/server/middleware/controllers/view.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/middleware/routes/neo/application.js b/packages/server/middleware/routes/neo/application.js index 4c600dc54e..4fe69e62e0 100644 --- a/packages/server/middleware/routes/neo/application.js +++ b/packages/server/middleware/routes/neo/application.js @@ -3,6 +3,8 @@ const controller = require("../../controllers/application"); 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; \ No newline at end of file diff --git a/packages/server/middleware/routes/neo/client.js b/packages/server/middleware/routes/neo/client.js new file mode 100644 index 0000000000..c537837838 --- /dev/null +++ b/packages/server/middleware/routes/neo/client.js @@ -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; \ No newline at end of file diff --git a/packages/server/middleware/routes/neo/database.js b/packages/server/middleware/routes/neo/database.js index 89b2f99fc6..e36163748c 100644 --- a/packages/server/middleware/routes/neo/database.js +++ b/packages/server/middleware/routes/neo/database.js @@ -4,6 +4,7 @@ const controller = require("../../controllers/database"); const router = Router(); router + .get("/api/databases", controller.fetch) .post("/api/databases", controller.create) .delete("/api/databases/:databaseId", controller.destroy); diff --git a/packages/server/middleware/routes/neo/static.js b/packages/server/middleware/routes/neo/static.js new file mode 100644 index 0000000000..e69de29bb2 diff --git a/packages/server/middleware/routes/neo/view.js b/packages/server/middleware/routes/neo/view.js new file mode 100644 index 0000000000..15e7911f1b --- /dev/null +++ b/packages/server/middleware/routes/neo/view.js @@ -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; \ No newline at end of file