new view foundations

This commit is contained in:
Martin McKeaveney 2020-08-13 15:04:19 +01:00
parent 28473c4cd4
commit bf46067fe1
5 changed files with 59 additions and 9 deletions

View File

@ -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

View File

@ -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

View File

@ -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

View File

@ -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) {

View File

@ -6,7 +6,7 @@
<title>Document</title>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3-selection/1.2.0/d3-selection.js"></script>
<script src="https://cdn.jsdelivr.net/npm/britecharts@2.10.0/dist/umd/bar.min.js"
<script src="https://cdn.jsdelivr.net/npm/britecharts@2.10.0/dist/umd/bar.min.js"
type="text/javascript"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/britecharts/dist/css/britecharts.min.css" type="text/css" />