Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Implemented ES6 and ES7 features to improve code readability #53

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
4 changes: 1 addition & 3 deletions config.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
{
"accessKeyId": "REPLACE",
"secretAccessKey": "REPLACE",
"region": "REPLACE"
"region": "sa-east-1"
}
224 changes: 119 additions & 105 deletions dynamoDBtoCSV.js
Original file line number Diff line number Diff line change
@@ -1,129 +1,143 @@
var program = require('commander');
var AWS = require('aws-sdk');
var unmarshalItem = require('dynamodb-marshaler').unmarshalItem;
var unmarshal = require('dynamodb-marshaler').unmarshal;
var Papa = require('papaparse');
var fs = require('fs');
var headers = [];
var unMarshalledArray = [];
const program = require('commander');
const AWS = require('aws-sdk');
const unmarshal = require('dynamodb-marshaler').unmarshal;
const Papa = require('papaparse');
const fs = require('fs');
const headers = [];
const unMarshalledArray = [];
const util = require('util');

program
.version('0.0.1')
.option('-t, --table [tablename]', 'Add the table you want to output to csv')
.option("-d, --describe")
.option("-r, --region [regionname]")
.option("-e, --endpoint [url]", 'Endpoint URL, can be used to dump from local DynamoDB')
.option("-p, --profile [profile]", 'Use profile from your credentials file')
.option("-f, --file [file]", "Name of the file to be created")
.parse(process.argv);
const fsWrite = util.promisify(fs.writeFile);

if (!program.table) {
console.log("You must specify a table");
program.outputHelp();
process.exit(1);
program
.version('0.0.1')
.option('-t, --table [tablename]', 'Add the table you want to output to csv')
.option("-d, --describe")
.option("-r, --region [regionname]")
.option("-e, --endpoint [url]", 'Endpoint URL, can be used to dump from local DynamoDB')
.option("-p, --profile [profile]", 'Use profile from your credentials file')
.option("-f, --file [file]", "Name of the file to be created")
.parse(process.argv);

if (!program.table)
{
console.error("You must specify a table");
program.outputHelp();
process.exit(1);
}

if (program.region && AWS.config.credentials) {
AWS.config.update({region: program.region});
} else {
AWS.config.loadFromPath(__dirname + '/config.json');
if (program.region && AWS.config.credentials)
{
AWS.config.update({region: program.region});
} else
{
AWS.config.loadFromPath(__dirname + '/config.json');
}

if (program.endpoint) {
AWS.config.update({endpoint: program.endpoint})
if (program.endpoint)
{
AWS.config.update({endpoint: program.endpoint})
}

if (program.profile) {
var newCreds = AWS.config.credentials;
newCreds.profile = program.profile;
AWS.config.update({credentials: newCreds});
if (program.profile)
{
const newCreds = AWS.config.credentials;
newCreds.profile = program.profile;
AWS.config.update({credentials: newCreds});
}

var dynamoDB = new AWS.DynamoDB();
const dynamoDB = new AWS.DynamoDB();

var query = {
"TableName": program.table,
"Limit": 1000
const query = {
"TableName": program.table,
"FilterExpression": "attribute_exists(acertou)",
"Limit": 500
};

var describeTable = function(query) {

dynamoDB.describeTable({
"TableName": program.table
}, function(err, data) {

if (!err) {

console.dir(data.Table);

} else console.dir(err);
});
const describeTable = async function()
{
try
{
let data = await dynamoDB.describeTable({
"TableName": program.table
}).promise();
console.dir(data.Table);
} catch (err)
{
console.dir(err);
}
}


var scanDynamoDB = function ( query ) {

dynamoDB.scan( query, function ( err, data ) {

if ( !err ) {
unMarshalIntoArray( data.Items ); // Print out the subset of results.
if ( data.LastEvaluatedKey ) { // Result is incomplete; there is more to come.
query.ExclusiveStartKey = data.LastEvaluatedKey;
scanDynamoDB(query);
}
else {
let endData = Papa.unparse( { fields: [ ...headers ], data: unMarshalledArray } );
if(program.file){
writeData(endData)
}else{
console.log(endData);
const scanDynamoDB = async function(query)
{
try
{
let data = await dynamoDB.scan(query).promise();
unMarshalIntoArray(data.Items); // Print out the subset of results.
if (data.LastEvaluatedKey)
{ // Result is incomplete; there is more to come.
console.warn('Buscando mais dados')
console.warn(data.LastEvaluatedKey)
query.ExclusiveStartKey = data.LastEvaluatedKey;
setTimeout( () => scanDynamoDB(query), 2000);
return;
}
}
}
else {
console.dir(err);
}
});
let endData = Papa.unparse({fields: [...headers], data: unMarshalledArray});
if (program.file)
{
await writeData(endData)
return;
}
console.log(endData);
} catch (err)
{
console.dir(err);
}
};

var writeData = function(data)
const writeData = async function(data)
{
fs.writeFile(program.file, data, (err) => {
if(err) throw err;
try
{
await fsWrite(program.file, data);
console.log('File Saved');
});
} catch (err)
{
console.error(err);
}
}

function unMarshalIntoArray( items ) {
if ( items.length === 0 )
return;

items.forEach( function ( row ) {
let newRow = {};

// console.log( 'Row: ' + JSON.stringify( row ));
Object.keys( row ).forEach( function ( key ) {
if ( headers.indexOf( key.trim() ) === -1 ) {
// console.log( 'putting new key ' + key.trim() + ' into headers ' + headers.toString());
headers.push( key.trim() );
}
let newValue = unmarshal( row[key] );

if ( typeof newValue === 'object' ) {
newRow[key] = JSON.stringify( newValue );
}
else {
newRow[key] = newValue;
}
});

// console.log( newRow );
unMarshalledArray.push( newRow );

});

const unMarshalIntoArray = function(items)
{
if (items.length === 0)
return;

items.forEach(row =>
{
let newRow = {};
// console.log( 'Row: ' + JSON.stringify( row ));
Object.keys(row).forEach(function(key)
{
if (!headers.includes(key.trim()))
{
// console.log( 'putting new key ' + key.trim() + ' into headers ' + headers.toString());
headers.push(key.trim());
}
let newValue = unmarshal(row[key]);

if (typeof newValue === 'object')
{
newRow[key] = JSON.stringify(newValue);
}
else
{
newRow[key] = newValue;
}
});
// console.log( newRow );
unMarshalledArray.push(newRow);
});
}

if ( program.describe ) describeTable( query );
else scanDynamoDB( query );

if (program.describe) describeTable();
else scanDynamoDB(query);