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())
|
table = await config.api.table.save(defaultTable())
|
||||||
})
|
})
|
||||||
|
|
||||||
it("should allow exporting all columns", async () => {
|
isInternal &&
|
||||||
const existing = await config.api.row.save(table._id!, {})
|
it("should not export internal couchdb fields", async () => {
|
||||||
const res = await config.api.row.exportRows(table._id!, {
|
const existing = await config.api.row.save(table._id!, {
|
||||||
rows: [existing._id!],
|
name: generator.guid(),
|
||||||
})
|
description: generator.paragraph(),
|
||||||
const results = JSON.parse(res)
|
})
|
||||||
expect(results.length).toEqual(1)
|
const res = await config.api.row.exportRows(table._id!, {
|
||||||
const row = results[0]
|
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)).toEqual(["_id", "name", "description"])
|
||||||
expect(Object.keys(row).length).toBeGreaterThanOrEqual(
|
})
|
||||||
Object.keys(existing).length
|
|
||||||
)
|
!isInternal &&
|
||||||
Object.keys(existing).forEach(key => {
|
it("should allow exporting all columns", async () => {
|
||||||
expect(row[key]).toEqual(existing[key])
|
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 () => {
|
it("should allow exporting only certain columns", async () => {
|
||||||
const existing = await config.api.row.save(table._id!, {})
|
const existing = await config.api.row.save(table._id!, {})
|
||||||
|
|
|
@ -11,6 +11,7 @@ import {
|
||||||
SearchResponse,
|
SearchResponse,
|
||||||
SortType,
|
SortType,
|
||||||
Table,
|
Table,
|
||||||
|
TableSchema,
|
||||||
User,
|
User,
|
||||||
} from "@budibase/types"
|
} from "@budibase/types"
|
||||||
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
|
import { getGlobalUsersFromMetadata } from "../../../../utilities/global"
|
||||||
|
@ -137,6 +138,9 @@ export async function exportRows(
|
||||||
let rows: Row[] = []
|
let rows: Row[] = []
|
||||||
let schema = table.schema
|
let schema = table.schema
|
||||||
let headers
|
let headers
|
||||||
|
|
||||||
|
result = trimFields(result, schema)
|
||||||
|
|
||||||
// Filter data to only specified columns if required
|
// Filter data to only specified columns if required
|
||||||
if (columns && columns.length) {
|
if (columns && columns.length) {
|
||||||
for (let i = 0; i < result.length; i++) {
|
for (let i = 0; i < result.length; i++) {
|
||||||
|
@ -299,3 +303,13 @@ async function getView(db: Database, viewName: string) {
|
||||||
}
|
}
|
||||||
return viewInfo
|
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(
|
export function cleanExportRows(
|
||||||
rows: any[],
|
rows: Row[],
|
||||||
schema: TableSchema,
|
schema: TableSchema,
|
||||||
format: string,
|
format: string,
|
||||||
columns?: string[],
|
columns?: string[],
|
||||||
|
|
Loading…
Reference in New Issue