@@ -4,8 +4,6 @@ const aws = require('aws-sdk');
44const path = require ( 'path' ) ;
55const csvToJson = require ( 'csvtojson' ) ;
66
7- let dynamoDB ;
8-
97function generateDataDynamoDB ( tableName , data ) {
108 const params = {
119 RequestItems : {
@@ -24,15 +22,9 @@ function generateDataDynamoDB(tableName, data) {
2422}
2523
2624function bulkData ( tableName , data ) {
25+ const dynamoDB = new aws . DynamoDB ( ) ;
2726 const params = generateDataDynamoDB ( tableName , data ) ;
28- console . log ( params ) ;
29- dynamoDB . batchWriteItem ( params , ( err ) => {
30- if ( err ) {
31- throw err ;
32- } else {
33- console . log ( 'Imported' ) ;
34- }
35- } ) ;
27+ return dynamoDB . batchWriteItem ( params ) . promise ( ) ;
3628}
3729
3830function createFile ( locaFile , csv ) {
@@ -44,49 +36,38 @@ function createFile(locaFile, csv) {
4436 fs . mkdirSync ( path . dirname ( locationFile ) ) ;
4537 fs . writeFileSync ( locationFile , csv ) ;
4638 }
39+ return Promise . resolve ( true ) ;
4740 } catch ( err ) {
48- throw err ;
41+ return Promise . reject ( err ) ;
4942 }
5043}
5144
52- module . exports = {
53- configByFile ( pathConfig ) {
54- aws . config . loadFromPath ( pathConfig ) ;
55- dynamoDB = new aws . DynamoDB ( ) ;
56- } ,
57- config ( accessKeyId , secretAccessKey , region ) {
58- aws . config . update ( { accessKeyId, secretAccessKey, region } ) ;
59- dynamoDB = new aws . DynamoDB ( ) ;
60- } ,
61- export ( tableName , locationFile ) {
62- const params = {
63- TableName : tableName ,
64- } ;
65- dynamoDB . scan ( params , ( error , data ) => {
66- if ( error ) {
67- throw error ;
68- } else {
69- const { Items : items } = data ;
70- try {
71- const records = [ ] ;
72- items . forEach ( ( element ) => {
73- records . push ( aws . DynamoDB . Converter . unmarshall ( element ) ) ;
74- } ) ;
75- const fields = Object . keys ( records [ 0 ] ) ;
76- const parser = new Json2csvParser ( { fields } ) ;
77- const csv = parser . parse ( records ) ;
78- createFile ( locationFile , csv ) ;
79- } catch ( err ) {
80- throw err ;
81- }
82- }
45+ function csvFileToJson ( data ) {
46+ const { Items : items } = data ;
47+ try {
48+ const records = [ ] ;
49+ items . forEach ( ( element ) => {
50+ records . push ( aws . DynamoDB . Converter . unmarshall ( element ) ) ;
8351 } ) ;
52+ const fields = Object . keys ( records [ 0 ] ) ;
53+ const parser = new Json2csvParser ( { fields } ) ;
54+ const csv = parser . parse ( records ) ;
55+ return Promise . resolve ( csv ) ;
56+ } catch ( err ) {
57+ return Promise . reject ( err ) ;
58+ }
59+ }
60+
61+ module . exports = {
62+ exportCSV ( params , locationFile ) {
63+ const dynamoDB = new aws . DynamoDB ( ) ;
64+ return dynamoDB . scan ( params ) . promise ( )
65+ . then ( response => csvFileToJson ( response ) )
66+ . then ( csv => createFile ( locationFile , csv ) ) ;
8467 } ,
85- import ( tableName , csvFilePath ) {
86- csvToJson ( )
68+ importCSV ( tableName , csvFilePath ) {
69+ return csvToJson ( )
8770 . fromFile ( csvFilePath )
88- . then ( ( jsonObj ) => {
89- bulkData ( tableName , jsonObj ) ;
90- } ) ;
71+ . then ( jsonObj => bulkData ( tableName , jsonObj ) ) ;
9172 } ,
9273} ;
0 commit comments