budibase/packages/server/middleware/controllers/view.js

32 lines
831 B
JavaScript
Raw Normal View History

const CouchDB = require("../../db");
2020-04-09 17:53:48 +02:00
const controller = {
fetch: async ctx => {
const db = new CouchDB(ctx.params.instanceId);
2020-04-13 17:22:30 +02:00
const designDoc = await db.get("_design/database");
ctx.body = designDoc.views;
2020-04-09 17:53:48 +02:00
},
create: async ctx => {
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
};
const newView = await db.put(designDoc);
2020-04-13 17:22:30 +02:00
ctx.body = {
...newView,
message: `View ${name} created successfully.`,
status: 200,
}
2020-04-09 17:53:48 +02:00
},
destroy: async ctx => {
const db = new CouchDB(ctx.params.instanceId);
ctx.body = await db.destroy(ctx.params.userId)
2020-04-09 17:53:48 +02:00
}
}
module.exports = controller;