Only export selected columns (#12438)
* Only export selected columns * Refactor and unit test
This commit is contained in:
parent
a5a3b12936
commit
37dc8ba6e4
|
@ -133,9 +133,14 @@ export async function exportRows(
|
|||
|
||||
let result = await search({ tableId, query: requestQuery, sort, sortOrder })
|
||||
let rows: Row[] = []
|
||||
let headers
|
||||
|
||||
if (!tableName) {
|
||||
throw new HTTPError("Could not find table name.", 400)
|
||||
}
|
||||
const schema = datasource.entities[tableName].schema
|
||||
|
||||
// Filter data to only specified columns if required
|
||||
|
||||
if (columns && columns.length) {
|
||||
for (let i = 0; i < result.rows.length; i++) {
|
||||
rows[i] = {}
|
||||
|
@ -143,22 +148,17 @@ export async function exportRows(
|
|||
rows[i][column] = result.rows[i][column]
|
||||
}
|
||||
}
|
||||
headers = columns
|
||||
} else {
|
||||
rows = result.rows
|
||||
}
|
||||
|
||||
if (!tableName) {
|
||||
throw new HTTPError("Could not find table name.", 400)
|
||||
}
|
||||
const schema = datasource.entities[tableName].schema
|
||||
let exportRows = cleanExportRows(rows, schema, format, columns)
|
||||
|
||||
let headers = Object.keys(schema)
|
||||
|
||||
let content: string
|
||||
switch (format) {
|
||||
case exporters.Format.CSV:
|
||||
content = exporters.csv(headers, exportRows)
|
||||
content = exporters.csv(headers ?? Object.keys(schema), exportRows)
|
||||
break
|
||||
case exporters.Format.JSON:
|
||||
content = exporters.json(exportRows)
|
||||
|
|
|
@ -110,7 +110,7 @@ export async function exportRows(
|
|||
|
||||
let rows: Row[] = []
|
||||
let schema = table.schema
|
||||
|
||||
let headers
|
||||
// Filter data to only specified columns if required
|
||||
if (columns && columns.length) {
|
||||
for (let i = 0; i < result.length; i++) {
|
||||
|
@ -119,6 +119,7 @@ export async function exportRows(
|
|||
rows[i][column] = result[i][column]
|
||||
}
|
||||
}
|
||||
headers = columns
|
||||
} else {
|
||||
rows = result
|
||||
}
|
||||
|
@ -127,7 +128,7 @@ export async function exportRows(
|
|||
if (format === Format.CSV) {
|
||||
return {
|
||||
fileName: "export.csv",
|
||||
content: csv(Object.keys(rows[0]), exportRows),
|
||||
content: csv(headers ?? Object.keys(rows[0]), exportRows),
|
||||
}
|
||||
} else if (format === Format.JSON) {
|
||||
return {
|
||||
|
|
|
@ -18,7 +18,6 @@ jest.mock("../../../utilities/rowProcessor", () => ({
|
|||
|
||||
jest.mock("../../../api/controllers/view/exporters", () => ({
|
||||
...jest.requireActual("../../../api/controllers/view/exporters"),
|
||||
csv: jest.fn(),
|
||||
Format: {
|
||||
CSV: "csv",
|
||||
},
|
||||
|
@ -102,5 +101,32 @@ describe("external row sdk", () => {
|
|||
new HTTPError("Could not find table name.", 400)
|
||||
)
|
||||
})
|
||||
|
||||
it("should only export specified columns", async () => {
|
||||
mockDatasourcesGet.mockImplementation(async () => ({
|
||||
entities: {
|
||||
tablename: {
|
||||
schema: {
|
||||
name: {},
|
||||
age: {},
|
||||
dob: {},
|
||||
},
|
||||
},
|
||||
},
|
||||
}))
|
||||
const headers = ["name", "dob"]
|
||||
|
||||
const result = await exportRows({
|
||||
tableId: "datasource__tablename",
|
||||
format: Format.CSV,
|
||||
query: {},
|
||||
columns: headers,
|
||||
})
|
||||
|
||||
expect(result).toEqual({
|
||||
fileName: "export.csv",
|
||||
content: `"name","dob"`,
|
||||
})
|
||||
})
|
||||
})
|
||||
})
|
||||
|
|
Loading…
Reference in New Issue