Merge pull request #3440 from Budibase/fix/export-sql
Fixing some issues with exporting CSV/JSON
This commit is contained in:
commit
773bc06f7f
|
@ -1,6 +1,7 @@
|
||||||
<script>
|
<script>
|
||||||
import { Select, ModalContent } from "@budibase/bbui"
|
import { Select, ModalContent, notifications } from "@budibase/bbui"
|
||||||
import download from "downloadjs"
|
import download from "downloadjs"
|
||||||
|
import { get } from "builderStore/api"
|
||||||
|
|
||||||
const FORMATS = [
|
const FORMATS = [
|
||||||
{
|
{
|
||||||
|
@ -18,11 +19,18 @@
|
||||||
let exportFormat = FORMATS[0].key
|
let exportFormat = FORMATS[0].key
|
||||||
|
|
||||||
async function exportView() {
|
async function exportView() {
|
||||||
download(
|
const uri = encodeURIComponent(view)
|
||||||
`/api/views/export?view=${encodeURIComponent(
|
const response = await get(
|
||||||
view
|
`/api/views/export?view=${uri}&format=${exportFormat}`
|
||||||
)}&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>
|
</script>
|
||||||
|
|
||||||
|
|
|
@ -55,7 +55,8 @@ exports.save = async ctx => {
|
||||||
exports.fetchView = async ctx => {
|
exports.fetchView = async ctx => {
|
||||||
// there are no views in external data sources, shouldn't ever be called
|
// there are no views in external data sources, shouldn't ever be called
|
||||||
// for now just fetch
|
// 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)
|
return exports.fetch(ctx)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -16,3 +16,8 @@ exports.csv = function (headers, rows) {
|
||||||
exports.json = function (headers, rows) {
|
exports.json = function (headers, rows) {
|
||||||
return JSON.stringify(rows, undefined, 2)
|
return JSON.stringify(rows, undefined, 2)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
exports.ExportFormats = {
|
||||||
|
CSV: "csv",
|
||||||
|
JSON: "json",
|
||||||
|
}
|
||||||
|
|
|
@ -4,6 +4,7 @@ const { apiFileReturn } = require("../../../utilities/fileSystem")
|
||||||
const exporters = require("./exporters")
|
const exporters = require("./exporters")
|
||||||
const { saveView, getView, getViews, deleteView } = require("./utils")
|
const { saveView, getView, getViews, deleteView } = require("./utils")
|
||||||
const { fetchView } = require("../row")
|
const { fetchView } = require("../row")
|
||||||
|
const { getTable } = require("../table/utils")
|
||||||
|
|
||||||
exports.fetch = async ctx => {
|
exports.fetch = async ctx => {
|
||||||
const db = new CouchDB(ctx.appId)
|
const db = new CouchDB(ctx.appId)
|
||||||
|
@ -56,7 +57,7 @@ exports.exportView = async ctx => {
|
||||||
const view = await getView(db, viewName)
|
const view = await getView(db, viewName)
|
||||||
|
|
||||||
const format = ctx.query.format
|
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")
|
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
|
let schema = view && view.meta && view.meta.schema
|
||||||
if (!schema) {
|
if (!schema) {
|
||||||
const tableId = ctx.params.tableId || view.meta.tableId
|
const tableId = ctx.params.tableId || view.meta.tableId
|
||||||
const table = await db.get(tableId)
|
const table = await getTable(ctx.appId, tableId)
|
||||||
schema = table.schema
|
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
|
// Export part
|
||||||
let headers = Object.keys(schema)
|
let headers = Object.keys(schema)
|
||||||
const exporter = exporters[format]
|
const exporter = exporters[format]
|
||||||
|
|
Loading…
Reference in New Issue