Skip to content
This repository has been archived by the owner on Jan 15, 2025. It is now read-only.

DynmoDB Types in CSV File #3

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 9 additions & 4 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand All @@ -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;
}, {});
Expand Down