Skip to content

Commit

Permalink
fix(agent): handle json field in csv export (#746)
Browse files Browse the repository at this point in the history
  • Loading branch information
nbouliol authored Jun 23, 2023
1 parent b0617c1 commit 8437f02
Show file tree
Hide file tree
Showing 2 changed files with 57 additions and 1 deletion.
12 changes: 11 additions & 1 deletion packages/agent/src/utils/csv-generator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,17 @@ export default class CsvGenerator {

private static convert(records: RecordData[], projection: Projection): Promise<string> {
return writeToString(
records.map(record => projection.map(field => RecordUtils.getFieldValue(record, field))),
records.map(record =>
projection.map(field => {
const value = RecordUtils.getFieldValue(record, field);

if (value?.toString() === '[object Object]') {
return JSON.stringify(value);
}

return value;
}),
),
{ includeEndRowDelimiter: true },
);
}
Expand Down
46 changes: 46 additions & 0 deletions packages/agent/test/utils/csv-generator.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -177,5 +177,51 @@ describe('CsvGenerator', () => {
);
});
});

describe('when there are Json fields', () => {
const setupWithJsonField = () => {
const records = [{ data: { name: 'ab', price: 5 } }, { data: { name: 'abc', price: 5 } }];
const filter = factories.filter.build({
conditionTree: factories.conditionTreeLeaf.build(),
sort: new Sort(),
page: new Page(),
});
const collection = factories.collection.build({
name: 'books',
schema: factories.collectionSchema.build({
fields: {
data: factories.columnSchema.build({ columnType: 'Json' }),
},
}),
});
const projection = new Projection('data');

return { records, filter, collection, projection };
};

test('should return all the records by fetching several time the list', async () => {
const { records, filter, collection, projection } = setupWithJsonField();

collection.list = jest.fn().mockReturnValue(records);

const caller = factories.caller.build();
const generator = CsvGenerator.generate(
caller,
projection,
'data',
filter,
collection,
collection.list,
);
const csv = await readCsv(generator);

expect(collection.list).toHaveBeenCalledTimes(1);

expect(csv).toStrictEqual([
'data\n',
'"{""name"":""ab"",""price"":5}"\n"{""name"":""abc"",""price"":5}"\n',
]);
});
});
});
});

0 comments on commit 8437f02

Please sign in to comment.