Update data export with new DB view structure and linked records
This commit is contained in:
parent
261c3da459
commit
7a51963ac5
|
@ -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,
|
||||
|
|
Loading…
Reference in New Issue