Fixing issue with exporting CSV/JSON, also saving as right file type.

This commit is contained in:
mike12345567 2021-11-18 14:57:17 +00:00
parent 4385ee6fc2
commit 2f282e70a9
4 changed files with 35 additions and 8 deletions

View File

@ -1,6 +1,7 @@
<script>
import { Select, ModalContent } from "@budibase/bbui"
import { Select, ModalContent, notifications } from "@budibase/bbui"
import download from "downloadjs"
import { get } from "builderStore/api"
const FORMATS = [
{
@ -18,11 +19,18 @@
let exportFormat = FORMATS[0].key
async function exportView() {
download(
`/api/views/export?view=${encodeURIComponent(
view
)}&format=${exportFormat}`
const uri = encodeURIComponent(view)
const response = await get(
`/api/views/export?view=${uri}&format=${exportFormat}`
)
if (response.status === 200) {
const data = await response.text()
download(data, `export.${exportFormat}`)
} else {
notifications.error(
`Unable to export ${exportFormat.toUpperCase()} data.`
)
}
}
</script>

View File

@ -55,7 +55,8 @@ exports.save = async ctx => {
exports.fetchView = async ctx => {
// there are no views in external data sources, shouldn't ever be called
// for now just fetch
ctx.params.tableId = ctx.params.viewName.split("all_")[1]
const split = ctx.params.viewName.split("all_")
ctx.params.tableId = split[1] ? split[1] : split[0]
return exports.fetch(ctx)
}

View File

@ -16,3 +16,8 @@ exports.csv = function (headers, rows) {
exports.json = function (headers, rows) {
return JSON.stringify(rows, undefined, 2)
}
exports.ExportFormats = {
CSV: "csv",
JSON: "json",
}

View File

@ -4,6 +4,7 @@ const { apiFileReturn } = require("../../../utilities/fileSystem")
const exporters = require("./exporters")
const { saveView, getView, getViews, deleteView } = require("./utils")
const { fetchView } = require("../row")
const { getTable } = require("../table/utils")
exports.fetch = async ctx => {
const db = new CouchDB(ctx.appId)
@ -56,7 +57,7 @@ exports.exportView = async ctx => {
const view = await getView(db, viewName)
const format = ctx.query.format
if (!format) {
if (!format || !Object.values(exporters.ExportFormats).includes(format)) {
ctx.throw(400, "Format must be specified, either csv or json")
}
@ -80,10 +81,22 @@ exports.exportView = async ctx => {
let schema = view && view.meta && view.meta.schema
if (!schema) {
const tableId = ctx.params.tableId || view.meta.tableId
const table = await db.get(tableId)
const table = await getTable(ctx.appId, tableId)
schema = table.schema
}
// make sure no "undefined" entries appear in the CSV
if (format === exporters.ExportFormats.CSV) {
const schemaKeys = Object.keys(schema)
for (let key of schemaKeys) {
for (let row of ctx.body) {
if (row[key] == null) {
row[key] = ""
}
}
}
}
// Export part
let headers = Object.keys(schema)
const exporter = exporters[format]