1
0
Fork 0
mirror of synced 2024-07-02 13:01:09 +12:00
budibase/packages/server/api/controllers/view.js

49 lines
1.1 KiB
JavaScript
Raw Normal View History

const CouchDB = require("../../db");
2020-04-10 03:53:48 +12:00
const controller = {
query: async ctx => {
},
2020-04-10 03:53:48 +12:00
fetch: async ctx => {
2020-04-25 04:28:32 +12:00
const db = new CouchDB(ctx.params.instanceId);
2020-04-14 03:22:30 +12:00
const designDoc = await db.get("_design/database");
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-10 03:53:48 +12:00
},
create: async ctx => {
2020-04-25 04:28:32 +12:00
const db = new CouchDB(ctx.params.instanceId);
2020-04-14 03:22:30 +12: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-14 03:22:30 +12:00
ctx.body = {
view: {
...ctx.request.body,
...newView
},
2020-04-14 03:22:30 +12:00
message: `View ${name} created successfully.`,
status: 200,
}
2020-04-10 03:53:48 +12:00
},
destroy: async ctx => {
2020-04-25 04:28:32 +12:00
const db = new CouchDB(ctx.params.instanceId);
ctx.body = await db.destroy(ctx.params.userId)
2020-04-10 03:53:48 +12:00
}
}
module.exports = controller;