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..9786529 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; }, {});