Update data export with new DB view structure and linked records
This commit is contained in:
parent
6bdbadbd61
commit
f19e60dcb1
|
@ -4,6 +4,7 @@ const fs = require("fs")
|
||||||
const path = require("path")
|
const path = require("path")
|
||||||
const os = require("os")
|
const os = require("os")
|
||||||
const exporters = require("./exporters")
|
const exporters = require("./exporters")
|
||||||
|
const { fetchView } = require("../record")
|
||||||
|
|
||||||
const controller = {
|
const controller = {
|
||||||
fetch: async ctx => {
|
fetch: async ctx => {
|
||||||
|
@ -77,36 +78,24 @@ const controller = {
|
||||||
ctx.message = `View ${ctx.params.viewName} saved successfully.`
|
ctx.message = `View ${ctx.params.viewName} saved successfully.`
|
||||||
},
|
},
|
||||||
exportView: async ctx => {
|
exportView: async ctx => {
|
||||||
const db = new CouchDB(ctx.user.instanceId)
|
|
||||||
const view = ctx.request.body
|
const view = ctx.request.body
|
||||||
const format = ctx.query.format
|
const format = ctx.query.format
|
||||||
|
|
||||||
// fetch records for the view
|
// Fetch view records
|
||||||
const response = await db.query(`database/${view.name}`, {
|
ctx.params.viewName = view.name
|
||||||
include_docs: !view.calculation,
|
ctx.query.group = view.groupBy
|
||||||
group: view.groupBy,
|
if (view.field) {
|
||||||
})
|
ctx.query.stats = true
|
||||||
|
ctx.query.field = view.field
|
||||||
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)
|
|
||||||
}
|
}
|
||||||
|
await fetchView(ctx)
|
||||||
|
|
||||||
|
// Export part
|
||||||
let headers = Object.keys(view.schema)
|
let headers = Object.keys(view.schema)
|
||||||
|
|
||||||
const exporter = exporters[format]
|
const exporter = exporters[format]
|
||||||
const exportedFile = exporter(headers, response.rows)
|
const exportedFile = exporter(headers, ctx.body)
|
||||||
|
|
||||||
const filename = `${view.name}.${format}`
|
const filename = `${view.name}.${format}`
|
||||||
|
|
||||||
fs.writeFileSync(path.join(os.tmpdir(), filename), exportedFile)
|
fs.writeFileSync(path.join(os.tmpdir(), filename), exportedFile)
|
||||||
|
|
||||||
ctx.body = {
|
ctx.body = {
|
||||||
url: `/api/views/export/download/${filename}`,
|
url: `/api/views/export/download/${filename}`,
|
||||||
name: view.name,
|
name: view.name,
|
||||||
|
|
Loading…
Reference in New Issue