From efe08672da7ad9bc9e084530d9161bc0f7558eb1 Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Thu, 9 Jun 2022 11:43:54 +0200 Subject: [PATCH 1/2] Add DynamoDB type information in CSV --- README.md | 6 ++++++ index.js | 13 +++++++++---- 2 files changed, 15 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 5532c22..1838a6f 100644 --- a/README.md +++ b/README.md @@ -4,6 +4,12 @@ A utility to import CSV files generated by the AWS DynamoDB Console `Export to c ## Usage +Export the CSV file via AWS Console and add the DynamoDB type separated by | for each column. Must be one of "B","BOOL","BS","L","M","N","NS","NULL","S","SS" + +``` +"S|id","S|__typename","N|_lastChangedAt","N|_version","N|color","S|createdAt","M|description","L|medias","S|name","N|sortPosition","S|status","M|subTitle","S|updatedAt" +``` + ```sh # Set the region to use export AWS_DEFAULT_REGION=us-east-1 diff --git a/index.js b/index.js index 4393544..653ade1 100755 --- a/index.js +++ b/index.js @@ -7,7 +7,7 @@ const DynamoDB = require('aws-sdk/clients/dynamodb'); const stream = require('stream'); const util = require('util'); const debug = require('debug')('dynamodb-csv-export-import'); - +const dynamoDbTypes = ["B","BOOL","BS","L","M","N","NS","NULL","S","SS"]; const pipeline = util.promisify(stream.pipeline); async function writeBatch(ddb, tableName, batch) { @@ -25,13 +25,18 @@ async function writeBatch(ddb, tableName, batch) { const transformRecord = record => Object.entries(record) .reduce((output, [key, value]) => { - const [, name, type] = /(\w+) \((\w+)\)/.exec(key); + const [type,name] = key.split("|"); + if(!name) { + throw new Error(`DynamoDB type information is missing for ${key} - must be one of ${dynamoDbTypes.join(",")}. Example: S|id`) + } + if(!dynamoDbTypes.includes(type)) { + throw new Error(`DynamoDB type information is incompatible for ${key} - must be one of ${dynamoDbTypes.join(",")}.`) + } if (!value) { return output; } - const contents = (['L', 'M', 'BOOL'].includes(type)) ? JSON.parse(value) : value; output[name] = { - [type]: contents, + [type]: ["L","M","NS","SS","BS"].includes(type) ? JSON.parse(value) : value, }; return output; }, {}); From 5763b552465bdd4d71352c9fd3cd6a249ba4d293 Mon Sep 17 00:00:00 2001 From: Benjamin Schulz Date: Thu, 9 Jun 2022 11:45:43 +0200 Subject: [PATCH 2/2] Formatting --- index.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/index.js b/index.js index 653ade1..9786529 100755 --- a/index.js +++ b/index.js @@ -27,10 +27,10 @@ const transformRecord = record => Object.entries(record) .reduce((output, [key, value]) => { const [type,name] = key.split("|"); if(!name) { - throw new Error(`DynamoDB type information is missing for ${key} - must be one of ${dynamoDbTypes.join(",")}. Example: S|id`) + throw new Error(`DynamoDB type information is missing for ${key} - must be one of ${dynamoDbTypes.join(",")}. Example: S|id`); } if(!dynamoDbTypes.includes(type)) { - throw new Error(`DynamoDB type information is incompatible for ${key} - must be one of ${dynamoDbTypes.join(",")}.`) + throw new Error(`DynamoDB type information is incompatible for ${key} - must be one of ${dynamoDbTypes.join(",")}.`); } if (!value) { return output;