@@ -12,20 +12,23 @@ module.exports.streambotBackup = streambot(incrementalBackup);
1212function replicate ( event , context , callback ) {
1313 var replicaConfig = {
1414 table : process . env . ReplicaTable ,
15- region : process . env . ReplicaRegion ,
1615 maxRetries : 1000 ,
1716 httpOptions : {
1817 timeout : 750 ,
1918 agent : streambot . agent
2019 }
2120 } ;
21+
22+ if ( ! process . env . ReplicaRegion ) replicaConfig . region = event . Records [ 0 ] . awsRegion ;
2223 if ( process . env . ReplicaEndpoint ) replicaConfig . endpoint = process . env . ReplicaEndpoint ;
24+
2325 var replica = new Dyno ( replicaConfig ) ;
2426
25- var keyAttrs = Object . keys ( event . Records [ 0 ] . dynamodb . Keys ) ;
27+ var keyAttrs = Object . keys ( event . Records [ 0 ] . dynamodb . Keys ) ;
2628
2729 var allRecords = event . Records . reduce ( function ( allRecords , change ) {
2830 var id = JSON . stringify ( change . dynamodb . Keys ) ;
31+ id = id . replace ( '"InstanceId":{"N"' , '"InstanceId":{"S"' ) ;
2932 allRecords [ id ] = allRecords [ id ] || [ ] ;
3033 allRecords [ id ] . push ( change ) ;
3134 return allRecords ;
@@ -36,11 +39,11 @@ function replicate(event, context, callback) {
3639 var change = allRecords [ key ] . pop ( ) ;
3740 if ( change . eventName === 'INSERT' || change . eventName === 'MODIFY' ) {
3841 return {
39- PutRequest : { Item : Dyno . deserialize ( JSON . stringify ( change . dynamodb . NewImage ) ) }
42+ PutRequest : { Item : Dyno . deserialize ( JSON . stringify ( change . dynamodb . NewImage ) . replace ( '"InstanceId":{"N"' , '"InstanceId":{"S"' ) ) }
4043 } ;
4144 } else if ( change . eventName === 'REMOVE' ) {
4245 return {
43- DeleteRequest : { Key : Dyno . deserialize ( JSON . stringify ( change . dynamodb . Keys ) ) }
46+ DeleteRequest : { Key : Dyno . deserialize ( JSON . stringify ( change . dynamodb . Keys ) . replace ( '"InstanceId":{"N"' , '"InstanceId":{"S"' ) ) }
4447 }
4548 }
4649 } ) ;
0 commit comments