Last active
December 3, 2021 13:08
-
-
Save diegofcornejo/d6c71085d5e4685e3e3a4e4ce959dfc4 to your computer and use it in GitHub Desktop.
Node DynamoDB Get all records
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const json2xls = require('json2xls'); | |
const { | |
convertArrayToCSV | |
} = require('convert-array-to-csv'); | |
var fs = require('fs'); | |
var AWS = require('aws-sdk'); | |
AWS.config.update({ | |
region: "us-east-1", | |
}); | |
var events = new AWS.DynamoDB.DocumentClient(); | |
var params = { | |
TableName: 'ses-event' | |
}; | |
events.scan(params, onScan); | |
// var count = 1; | |
var logger = fs.createWriteStream(Date.now() + '.log', { | |
flags: 'a' // 'a' means appending (old data will be preserved) | |
}) | |
var arr = []; | |
function onScan(err, data) { | |
if (err) { | |
console.error("Unable to scan the table. Error JSON:", JSON.stringify(err, null, 2)); | |
} else { | |
console.log("Scan succeeded."); | |
//esto es para generar un archivo plano | |
// console.log(data) | |
// data.Items.forEach(function (item) { | |
// // logger.write(count++ + '-' + JSON.stringify(itemdata) + '\r\n'); | |
// let from = item.from; | |
// let subject = item.subject; | |
// if (from.includes('[email protected]')) { | |
// // console.log(string); | |
// let obj = { | |
// "from": JSON.parse(item.from), | |
// "to": JSON.parse(item.to), | |
// "subject": item.subject, | |
// "timestamp": item.timestamp | |
// } | |
// console.log(obj); | |
// arr.push(obj); | |
// } | |
// // logger.write(JSON.stringify(itemdata)); | |
// }); | |
arr.push.apply(arr, data.Items); | |
// continue scanning if we have more items | |
if (typeof data.LastEvaluatedKey != "undefined") { | |
console.log("Scanning for more..."); | |
params.ExclusiveStartKey = data.LastEvaluatedKey; | |
events.scan(params, onScan); | |
} else { | |
// console.log('last'); | |
// logger.end(); | |
//ESTO ES PARA OBTENER UN SOLO ARCHIVO | |
var xls = json2xls(arr); | |
var path = './' + Date.now() + '.xlsx'; | |
fs.writeFile(path, xls, 'binary', function (err, data) { | |
console.log('create file') | |
if (err) { | |
console.log('error') | |
throw err; | |
} else { | |
console.log('file Created'); | |
} | |
}); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment