Created
February 20, 2019 03:46
-
-
Save diegofcornejo/31c8f6fb135f7e8430a1ce81e5ac8800 to your computer and use it in GitHub Desktop.
Create XLSX based on DynamoDB scan
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
var express = require('express'); | |
var bodyParser = require('body-parser'); | |
var app = express(); | |
const json2xls = require('json2xls'); | |
var fs = require('fs'); | |
var AWS = require('aws-sdk'); | |
AWS.config.update({ | |
region: "us-east-1", | |
}); | |
var events = new AWS.DynamoDB.DocumentClient(); | |
app.use(bodyParser.json({ | |
limit: '50mb' | |
})); // parse application/vnd.api+json as json | |
app.use(bodyParser.urlencoded({ | |
limit: '50mb', | |
extended: true, | |
parameterLimit: 50000 | |
})); // parse application/x-www-form-urlencoded | |
app.use(function (req, res, next) { | |
res.header("Access-Control-Allow-Origin", "*"); | |
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS'); | |
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept"); | |
next(); | |
}); | |
app.set('port', process.env.PORT || 4000); | |
var server = app.listen(app.get('port'), function () { | |
console.log('Express server listening on port ' + server.address().port); | |
var params = { | |
TableName: 'ses-events' | |
}; | |
events.scan(params, function (err, data) { | |
if (err) { | |
console.log("Error", err); | |
} else { | |
var xls = json2xls(data.Items); | |
var path = './' + Date.now() + '.xlsx'; | |
fs.writeFile(path, xls, 'binary', function (err, data) { | |
if (err) { | |
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