1
0
Fork 0
mirror of synced 2024-10-01 01:28:51 +13:00

Merge pull request #3440 from Budibase/fix/export-sql

Fixing some issues with exporting CSV/JSON
This commit is contained in:
Michael Drury 2021-11-18 15:26:23 +00:00 committed by GitHub
commit 3dcec7671e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
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]