@@ -32,6 +32,7 @@ function DynamoRestore(options) {
32
32
} ) ;
33
33
34
34
this . options = options ;
35
+ this . dynamodb = new AWS . DynamoDB ( ) ;
35
36
}
36
37
37
38
// Stick to prototypal inheritance. While this can be done differently
@@ -53,7 +54,7 @@ DynamoRestore.prototype.run = function(finishCallback) {
53
54
} ) . bind ( this ) ) ;
54
55
// Finish off by updating write capacity to end-state (if needed)
55
56
this . on ( 'finish' , ( function ( ) {
56
- var dynamodb = new AWS . DynamoDB ( ) ,
57
+ var dynamodb = this . dynamodb ,
57
58
options = this . options ;
58
59
// Do we need to update write capacity?
59
60
if ( options . writecapacity ) {
@@ -89,22 +90,23 @@ DynamoRestore.prototype._validateS3Backup = function(options) {
89
90
} ;
90
91
91
92
DynamoRestore . prototype . _validateTable = function ( options ) {
92
- var dynamodb = new AWS . DynamoDB ( ) ;
93
+ var dynamodb = this . dynamodb ;
93
94
if ( ! options . table ) {
94
95
return this . emit ( 'error' , 'Please provide a Dynamo DB table name to restore to.' ) ;
95
96
}
96
97
dynamodb . listTables ( { } , this . _checkTableExists . bind ( this ) ) ;
97
98
} ;
98
99
99
100
DynamoRestore . prototype . _checkTableExists = function ( error , data ) {
101
+ var dynamodb = this . dynamodb ;
100
102
if ( error || ! data || ! data . TableNames ) {
101
103
return this . emit ( 'error' , 'Fatal Error. Could not connect to AWS DynamoDB engine. Please check your credentials.' ) ;
102
104
}
103
105
if ( data . TableNames . indexOf ( this . options . table ) > - 1 ) {
104
106
// Table exists, should we overwrite it??
105
107
if ( this . options . overwrite ) {
106
- this . emit ( 'warning' , util . format ( 'WARN: table [%s] will be overwritten.' , options . table ) ) ;
107
- dynamodb . describeTable ( { TableName : this . options . table } , this . _checkTableReady . bind ( this ) ) ;
108
+ this . emit ( 'warning' , util . format ( 'WARN: table [%s] will be overwritten.' , this . options . table ) ) ;
109
+ setTimeout ( dynamodb . describeTable . bind ( dynamodb , { TableName : this . options . table } , this . _checkTableReady . bind ( this ) ) , 1000 ) ;
108
110
} else {
109
111
this . emit ( 'error' , 'Fatal Error. The destination table already exists! Exiting process..' ) ;
110
112
}
@@ -228,7 +230,7 @@ DynamoRestore.prototype._extractSchema = function(template) {
228
230
} ;
229
231
230
232
DynamoRestore . prototype . _createTable = function ( callback ) {
231
- var dynamodb = new AWS . DynamoDB ( ) ,
233
+ var dynamodb = this . dynamodb ,
232
234
options = this . options ;
233
235
if ( ! options . table || ! options . partitionkey ) {
234
236
return this . emit ( 'error' , 'Fatal Error. Could not create dynamo table. Not enough information provided.' ) ;
@@ -269,7 +271,7 @@ DynamoRestore.prototype._createTable = function(callback) {
269
271
} ;
270
272
271
273
DynamoRestore . prototype . _checkTableReady = function ( error , data ) {
272
- var dynamodb = new AWS . DynamoDB ( ) ;
274
+ var dynamodb = this . dynamodb ;
273
275
if ( error || ! data || ! data . Table ) {
274
276
return this . emit ( 'error' , 'Error creating table ' + this . options . table ) ;
275
277
}
@@ -288,7 +290,7 @@ DynamoRestore.prototype._checkTableReady = function(error, data) {
288
290
DynamoRestore . prototype . _sendBatch = function ( ) {
289
291
// Prepare
290
292
var params = { RequestItems : { } } ,
291
- dynamo = new AWS . DynamoDB ( ) ,
293
+ dynamo = this . dynamodb ,
292
294
options = this . options ;
293
295
batch = this . batches . shift ( ) ;
294
296
params . RequestItems [ options . table ] = batch . items ;
@@ -313,7 +315,7 @@ DynamoRestore.prototype._sendBatch = function() {
313
315
var unprocessedItems = data && data . UnprocessedItems && data . UnprocessedItems [ options . table ] || [ ] ;
314
316
if ( unprocessedItems . length ) {
315
317
// Retry unprocessed items
316
- this . emit ( 'warning' , 'Retrying ' + unprocessedItems . length + ' unprocessed items' ) ;
318
+ this . emit ( 'warning' , unprocessedItems . length + ' unprocessed items. Add to queue and back off a bit. ' ) ;
317
319
this . batches . push ( {
318
320
items : unprocessedItems ,
319
321
attempts : batch . attempts + 1
0 commit comments