From 7a51963ac51239f6e1f7529a0118dfea96156bbf Mon Sep 17 00:00:00 2001 From: Andrew Kingston Date: Sat, 3 Oct 2020 21:07:59 +0100 Subject: [PATCH] Update data export with new DB view structure and linked records --- .../server/src/api/controllers/view/index.js | 31 ++++++------------- 1 file changed, 10 insertions(+), 21 deletions(-) diff --git a/packages/server/src/api/controllers/view/index.js b/packages/server/src/api/controllers/view/index.js index d2ce3b0835..b7826a015c 100644 --- a/packages/server/src/api/controllers/view/index.js +++ b/packages/server/src/api/controllers/view/index.js @@ -4,6 +4,7 @@ const fs = require("fs") const path = require("path") const os = require("os") const exporters = require("./exporters") +const { fetchView } = require("../record") const controller = { fetch: async ctx => { @@ -77,36 +78,24 @@ const controller = { ctx.message = `View ${ctx.params.viewName} saved successfully.` }, exportView: async ctx => { - const db = new CouchDB(ctx.user.instanceId) const view = ctx.request.body const format = ctx.query.format - // fetch records for the view - const response = await db.query(`database/${view.name}`, { - include_docs: !view.calculation, - group: view.groupBy, - }) - - if (view.calculation === "stats") { - response.rows = response.rows.map(row => ({ - group: row.key, - field: view.field, - ...row.value, - avg: row.value.sum / row.value.count, - })) - } else { - response.rows = response.rows.map(row => row.doc) + // Fetch view records + ctx.params.viewName = view.name + ctx.query.group = view.groupBy + if (view.field) { + ctx.query.stats = true + ctx.query.field = view.field } + await fetchView(ctx) + // Export part let headers = Object.keys(view.schema) - const exporter = exporters[format] - const exportedFile = exporter(headers, response.rows) - + const exportedFile = exporter(headers, ctx.body) const filename = `${view.name}.${format}` - fs.writeFileSync(path.join(os.tmpdir(), filename), exportedFile) - ctx.body = { url: `/api/views/export/download/${filename}`, name: view.name,