2020-04-23 15:37:08 +02:00
|
|
|
const CouchDB = require("../../db");
|
2020-04-09 17:53:48 +02:00
|
|
|
|
|
|
|
const controller = {
|
2020-04-28 15:39:35 +02:00
|
|
|
query: async ctx => {
|
|
|
|
|
|
|
|
},
|
2020-04-09 17:53:48 +02:00
|
|
|
fetch: async ctx => {
|
2020-04-24 18:28:32 +02:00
|
|
|
const db = new CouchDB(ctx.params.instanceId);
|
2020-04-13 17:22:30 +02:00
|
|
|
const designDoc = await db.get("_design/database");
|
2020-04-28 15:39:35 +02:00
|
|
|
const response = [];
|
|
|
|
|
|
|
|
for (let name in designDoc.views) {
|
|
|
|
if (!name.startsWith("all") && name !== "by_type") {
|
|
|
|
response.push({
|
|
|
|
name,
|
|
|
|
...designDoc.views[name]
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
ctx.body = response
|
2020-04-09 17:53:48 +02:00
|
|
|
},
|
|
|
|
create: async ctx => {
|
2020-04-24 18:28:32 +02:00
|
|
|
const db = new CouchDB(ctx.params.instanceId);
|
2020-04-13 17:22:30 +02:00
|
|
|
const { name, ...viewDefinition } = ctx.request.body;
|
|
|
|
|
|
|
|
const designDoc = await db.get("_design/database");
|
|
|
|
designDoc.views = {
|
|
|
|
...designDoc.views,
|
|
|
|
[name]: viewDefinition
|
|
|
|
};
|
2020-04-23 15:37:08 +02:00
|
|
|
const newView = await db.put(designDoc);
|
2020-04-13 17:22:30 +02:00
|
|
|
|
|
|
|
ctx.body = {
|
2020-04-28 15:39:35 +02:00
|
|
|
view: {
|
|
|
|
...ctx.request.body,
|
|
|
|
...newView
|
|
|
|
},
|
2020-04-13 17:22:30 +02:00
|
|
|
message: `View ${name} created successfully.`,
|
|
|
|
status: 200,
|
|
|
|
}
|
2020-04-09 17:53:48 +02:00
|
|
|
},
|
|
|
|
destroy: async ctx => {
|
2020-04-24 18:28:32 +02:00
|
|
|
const db = new CouchDB(ctx.params.instanceId);
|
2020-04-23 15:37:08 +02:00
|
|
|
ctx.body = await db.destroy(ctx.params.userId)
|
2020-04-09 17:53:48 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
module.exports = controller;
|