Skip to content

Commit

Permalink
feat: use csv-stringify library
Browse files Browse the repository at this point in the history
  • Loading branch information
edelwud committed Mar 16, 2023
1 parent 2c5c4ae commit f5a24c6
Show file tree
Hide file tree
Showing 5 changed files with 25 additions and 33 deletions.
2 changes: 1 addition & 1 deletion .projen/deps.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion .projenrc.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const project = new awscdk.AwsCdkTypeScriptApp({
"@aws-sdk/client-dynamodb",
"@aws-sdk/client-s3",
"@aws-sdk/lib-storage",
"stream-transform",
"csv-stringify",
"@aws-prototyping-sdk/pdk-nag",
"cdk-nag",
"cdk-pipelines-github",
Expand Down
2 changes: 1 addition & 1 deletion package.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

42 changes: 17 additions & 25 deletions src/services/export.lambda.ts
Original file line number Diff line number Diff line change
@@ -1,44 +1,36 @@
import { PassThrough } from "stream";
import {
AttributeValue,
DynamoDBClient,
paginateScan,
} from "@aws-sdk/client-dynamodb";
import { DynamoDBClient, paginateScan } from "@aws-sdk/client-dynamodb";
import { S3Client } from "@aws-sdk/client-s3";
import { Upload } from "@aws-sdk/lib-storage";
import { transform } from "stream-transform";
import { stringify } from "csv-stringify";

const ddbClient = new DynamoDBClient({});
const s3Client = new S3Client({});
const ddbItemsPaginator = paginateScan(
{
client: new DynamoDBClient({}),
},
{
TableName: process.env.DATA_TABLE_NAME,
}
);

export const handler = async () => {
const ddbItemsPaginator = paginateScan(
{
client: ddbClient,
},
{
TableName: process.env.DATA_TABLE_NAME,
}
);

const csvTransform = transform((record: Record<string, AttributeValue>) => {
return Object.keys(record)
.map((field) => JSON.stringify(record[field]))
.join(",");
});

const stringifier = stringify();
const upload = new Upload({
client: s3Client,
params: {
Bucket: process.env.DESTINATION_BUCKET_NAME,
Key: new Date().toISOString().split("T")[0] + ".csv",
Body: csvTransform.pipe(new PassThrough()),
Body: stringifier,
},
leavePartsOnError: false,
});

for await (const page of ddbItemsPaginator) {
page.Items?.forEach((item) => csvTransform.write(item));
page.Items?.forEach((item) =>
stringifier.write(
Object.keys(item).map((field) => JSON.stringify(item[field]))
)
);
}

return upload.done();
Expand Down
10 changes: 5 additions & 5 deletions yarn.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

0 comments on commit f5a24c6

Please sign in to comment.