Merge pull request #14289 from Budibase/fix/import-column-issues
Trim couchdb fields while exporting rows
This commit is contained in:
commit
19c4305034
|
@ -1640,23 +1640,38 @@ describe.each([
|
|||
table = await config.api.table.save(defaultTable())
|
||||
})
|
||||
|
||||
it("should allow exporting all columns", async () => {
|
||||
const existing = await config.api.row.save(table._id!, {})
|
||||
const res = await config.api.row.exportRows(table._id!, {
|
||||
rows: [existing._id!],
|
||||
})
|
||||
const results = JSON.parse(res)
|
||||
expect(results.length).toEqual(1)
|
||||
const row = results[0]
|
||||
isInternal &&
|
||||
it("should not export internal couchdb fields", async () => {
|
||||
const existing = await config.api.row.save(table._id!, {
|
||||
name: generator.guid(),
|
||||
description: generator.paragraph(),
|
||||
})
|
||||
const res = await config.api.row.exportRows(table._id!, {
|
||||
rows: [existing._id!],
|
||||
})
|
||||
const results = JSON.parse(res)
|
||||
expect(results.length).toEqual(1)
|
||||
const row = results[0]
|
||||
|
||||
// Ensure all original columns were exported
|
||||
expect(Object.keys(row).length).toBeGreaterThanOrEqual(
|
||||
Object.keys(existing).length
|
||||
)
|
||||
Object.keys(existing).forEach(key => {
|
||||
expect(row[key]).toEqual(existing[key])
|
||||
expect(Object.keys(row)).toEqual(["_id", "name", "description"])
|
||||
})
|
||||
|
||||
!isInternal &&
|
||||
it("should allow exporting all columns", async () => {
|
||||
const existing = await config.api.row.save(table._id!, {})
|
||||
const res = await config.api.row.exportRows(table._id!, {
|
||||
rows: [existing._id!],
|
||||
})
|
||||
const results = JSON.parse(res)
|
||||
expect(results.length).toEqual(1)
|
||||
const row = results[0]
|
||||
|
||||
// Ensure all original columns were exported
|
||||
expect(Object.keys(row).length).toBe(Object.keys(existing).length)
|
||||
Object.keys(existing).forEach(key => {
|
||||
expect(row[key]).toEqual(existing[key])
|
||||
})
|
||||
})
|
||||
})
|
||||
|
||||
it("should allow exporting only certain columns", async () => {
|
||||
const existing = await config.api.row.save(table._id!, {})
|
||||
|
|
|
@ -11,6 +11,7 @@ import {
|
|||
SearchResponse,
|
||||
SortType,
|
||||
Table,
|
||||
TableSchema,
|
||||
User,
|
||||
} from "@budibase/types"
|
||||
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
|
||||
|
@ -137,6 +138,9 @@ export async function exportRows(
|
|||
let rows: Row[] = []
|
||||
let schema = table.schema
|
||||
let headers
|
||||
|
||||
result = trimFields(result, schema)
|
||||
|
||||
// Filter data to only specified columns if required
|
||||
if (columns && columns.length) {
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
|
@ -299,3 +303,13 @@ async function getView(db: Database, viewName: string) {
|
|||
}
|
||||
return viewInfo
|
||||
}
|
||||
|
||||
function trimFields(rows: Row[], schema: TableSchema) {
|
||||
const allowedFields = ["_id", ...Object.keys(schema)]
|
||||
const result = rows.map(row =>
|
||||
Object.keys(row)
|
||||
.filter(key => allowedFields.includes(key))
|
||||
.reduce((acc, key) => ({ ...acc, [key]: row[key] }), {} as Row)
|
||||
)
|
||||
return result
|
||||
}
|
||||
|
|
|
@ -76,7 +76,7 @@ export async function getDatasourceAndQuery(
|
|||
}
|
||||
|
||||
export function cleanExportRows(
|
||||
rows: any[],
|
||||
rows: Row[],
|
||||
schema: TableSchema,
|
||||
format: string,
|
||||
columns?: string[],
|
||||
|
|
Loading…
Reference in New Issue