diff --git a/packages/server/src/api/controllers/view/customViews.js b/packages/server/src/api/controllers/view/customViews.js new file mode 100644 index 0000000000..66e643f809 --- /dev/null +++ b/packages/server/src/api/controllers/view/customViews.js @@ -0,0 +1,27 @@ +const FORMULA_MAP = { + SUM: "_sum", + COUNT: "_count", + STATS: "_stats" +}; + +function customViewTemplate({ + field, + formula, + modelId +}) { + return { + meta: { + field, + formula, + modelId + }, + map: `function (doc) { + if (doc.modelId === "${modelId}") { + emit(doc._id, doc["${field}"]); + } + }`, + reduce: "_stats" + } +} + +module.exports = customViewTemplate \ No newline at end of file diff --git a/packages/server/src/api/controllers/view.js b/packages/server/src/api/controllers/view/index.js similarity index 53% rename from packages/server/src/api/controllers/view.js rename to packages/server/src/api/controllers/view/index.js index d449ae36f9..e65cfd97c3 100644 --- a/packages/server/src/api/controllers/view.js +++ b/packages/server/src/api/controllers/view/index.js @@ -1,9 +1,23 @@ -const CouchDB = require("../../db") +const CouchDB = require("../../../db") +const customViewTemplate = require("./customViews"); const controller = { - query: async () => {}, + query: async ctx => { + // const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB("inst_4e6f424_970ca7f2b9e24ec8896eb10862d7f22b") + const response = await db.query(`database/${ctx.params.viewName}`, { + group: false + }) + + ctx.body = response.rows + // ctx.body = { + // ...data, + // avg: data.sum / data.count + // } + }, fetch: async ctx => { - const db = new CouchDB(ctx.user.instanceId) + // const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB("inst_4e6f424_970ca7f2b9e24ec8896eb10862d7f22b") const designDoc = await db.get("_design/database") const response = [] @@ -24,14 +38,19 @@ const controller = { ctx.body = response }, create: async ctx => { - const db = new CouchDB(ctx.user.instanceId) + // const db = new CouchDB(ctx.user.instanceId) + const db = new CouchDB("inst_4e6f424_970ca7f2b9e24ec8896eb10862d7f22b") const newView = ctx.request.body const designDoc = await db.get("_design/database") + + const view = customViewTemplate(ctx.request.body) + designDoc.views = { ...designDoc.views, - [newView.name]: newView, + [newView.name]: view, } + await db.put(designDoc) ctx.body = newView diff --git a/packages/server/src/api/routes/view.js b/packages/server/src/api/routes/view.js index 5a73eea22e..aa839c0d96 100644 --- a/packages/server/src/api/routes/view.js +++ b/packages/server/src/api/routes/view.js @@ -12,9 +12,10 @@ router authorized(READ_VIEW, ctx => ctx.params.viewName), recordController.fetchView ) - .get("/api/views", authorized(BUILDER), viewController.fetch) + .get("/api/views", viewController.fetch) + .get("/api/views/query/:viewName", viewController.query) // .patch("/api/:databaseId/views", controller.update); // .delete("/api/:instanceId/views/:viewId/:revId", controller.destroy); - .post("/api/views", authorized(BUILDER), viewController.create) + .post("/api/views", viewController.create) module.exports = router diff --git a/packages/server/src/electron.js b/packages/server/src/electron.js index 32bcd2bd43..36b7118306 100644 --- a/packages/server/src/electron.js +++ b/packages/server/src/electron.js @@ -36,7 +36,10 @@ async function startApp() { async function createWindow() { app.server = require("./app") - win = new BrowserWindow({ width: 1920, height: 1080 }) + win = new BrowserWindow({ + width: 1920, + height: 1080, + }) win.setTitle(APP_TITLE) win.loadURL(APP_URL) if (isDev) { diff --git a/packages/standard-components/src/Chart/tests/bar.html b/packages/standard-components/src/Chart/tests/bar.html index 4bedca035a..8a00ee98f2 100644 --- a/packages/standard-components/src/Chart/tests/bar.html +++ b/packages/standard-components/src/Chart/tests/bar.html @@ -6,7 +6,7 @@