copy-dynamodb-table
Advanced tools
Comparing version 2.0.20 to 2.1.0
@@ -22,15 +22,17 @@ var copy = require('./index').copy | ||
copy({ | ||
config: globalAWSConfig, | ||
source: { | ||
tableName: 'source_table_name', // required | ||
config: sourceAWSConfig // optional , leave blank to use globalAWSConfig | ||
}, | ||
destination: { | ||
tableName: 'destination_table_name', // required | ||
config: destinationAWSConfig // optional , leave blank to use globalAWSConfig | ||
}, | ||
log: true, // default false | ||
create : false , // create table if not exist | ||
schemaOnly: false // make it true and it will copy schema only | ||
config: globalAWSConfig, | ||
source: { | ||
tableName: 'source_table_name', // required | ||
config: sourceAWSConfig // optional , leave blank to use globalAWSConfig | ||
}, | ||
destination: { | ||
tableName: 'destination_table_name', // required | ||
config: destinationAWSConfig // optional , leave blank to use globalAWSConfig | ||
}, | ||
log: true, // default false | ||
create: false, // create table if not exist | ||
schemaOnly: false, // make it true and it will copy schema only | ||
continuousBackups: true, // default 'copy', true will always enable backups, 'copy' copies the behaviour from the source and false does not enable them | ||
transformDataFn: function(item){ return item } // function to transform data | ||
}, | ||
function (err, result) { | ||
@@ -37,0 +39,0 @@ if (err) { |
61
index.js
@@ -38,9 +38,18 @@ 'use strict' | ||
data: {}, | ||
transformDataFn: values.transformDataFn, | ||
log: values.log, | ||
create: values.create, | ||
schemaOnly: values.schemaOnly | ||
schemaOnly: values.schemaOnly, | ||
continuousBackups: values.continuousBackups | ||
} | ||
if (options.source.active && options.destination.active) { // both tables are active | ||
return startCopying(options, fn) | ||
return startCopying(options, function (err, data) { | ||
if (err) { | ||
return fn(err, data) | ||
} | ||
if (options.continuousBackups) { | ||
setContinuousBackups(options, fn) | ||
} | ||
}) | ||
} | ||
@@ -69,3 +78,11 @@ | ||
} | ||
startCopying(options, fn) | ||
startCopying(options, function (err, data) { | ||
if (err) { | ||
return fn(err, data) | ||
} | ||
if (options.continuousBackups) { | ||
setContinuousBackups(options, fn) | ||
} | ||
}) | ||
}) | ||
@@ -75,2 +92,34 @@ | ||
function setContinuousBackups(options, fn) { | ||
function enableBackups() { | ||
var params = { | ||
PointInTimeRecoverySpecification: { | ||
PointInTimeRecoveryEnabled: true | ||
}, | ||
TableName: options.destination.tableName | ||
}; | ||
options.destination.dynamodb.updateContinuousBackups(params, function (err, data) { | ||
return fn(err, data); | ||
}) | ||
} | ||
if (options.continuousBackups !== 'copy') { | ||
enableBackups(); | ||
} else { | ||
options.source.dynamodb.describeContinuousBackups({ TableName: options.source.tableName }, function (err, data) { | ||
if (err) { | ||
return fn(err, data); | ||
} | ||
var backupStatus = data.ContinuousBackupsDescription.ContinuousBackupsStatus; | ||
if (backupStatus === 'ENABLED') { | ||
enableBackups(); | ||
} | ||
}) | ||
} | ||
} | ||
function clearTableSchema(table) { | ||
@@ -228,3 +277,3 @@ | ||
} | ||
fn(err, mapItems(data)) | ||
fn(err, mapItems(options, data)) | ||
}) | ||
@@ -242,7 +291,7 @@ } | ||
function mapItems(data) { | ||
function mapItems(options, data) { | ||
data.Items = data.Items.map(function (item) { | ||
return { | ||
PutRequest: { | ||
Item: item | ||
Item: !!options.transformDataFn ? options.transformDataFn(item) : item | ||
} | ||
@@ -249,0 +298,0 @@ } |
{ | ||
"name": "copy-dynamodb-table", | ||
"version": "2.0.20", | ||
"version": "2.1.0", | ||
"description": "Copy Dynamodb table to another in the same or different zone , It is 100% safe , and speed depends on your destination table user defined write provisioned throughput", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -26,4 +26,7 @@ Safe Copy Dynamodb Table | ||
log: true, // default false | ||
create : true // create destination table if not exist | ||
schemaOnly : false // if true it will copy schema only -- optional | ||
create : true, // create destination table if not exist | ||
continuousBackups: true, // if true will enable point in time backups | ||
schemaOnly : false, // if true it will copy schema only -- optional | ||
transformDataFn: function(item){ return item } // function to transform data | ||
}, | ||
@@ -57,3 +60,4 @@ function (err, result) { | ||
log: true, // default false | ||
create : true // create destination table if not exist | ||
create : true, // create destination table if not exist | ||
continuousBackups: 'copy' // if 'copy' it will match the point in time backups from the source | ||
}, | ||
@@ -118,2 +122,34 @@ function (err, result) { | ||
## Promise version : | ||
Use this if you want to copy using promises, or async / await . | ||
```javascript | ||
function promiseCopy(data) { | ||
return new Promise((resolve, reject) => { | ||
copy(data, function (err, result) { | ||
if (err) { | ||
return reject(err) | ||
} | ||
resolve(result) | ||
}) | ||
}) | ||
} | ||
promiseCopy({ | ||
source: { | ||
tableName: 'source_table_name', // required | ||
}, | ||
destination: { | ||
tableName: 'destination_table_name', // required | ||
}, | ||
log: true, // default false | ||
create: true // create destination table if not exist | ||
}).then(function (results) { | ||
// do stuff | ||
}).catch(function (err) { | ||
//handle error | ||
}) | ||
``` | ||
## Use Case : | ||
@@ -130,2 +166,4 @@ With source table read capacity units = 100 & destination table write capacity units = 1000 , I managed to copy ~100,000 items from source to destination within ~175 seconds , with avarage item size of 4 KB. | ||
- [Simon Li](https://github.com/siutsin) | ||
- [Janusz Slota](https://github.com/nixilla) | ||
- [Kyle Watson](https://github.com/kylejwatson) | ||
@@ -132,0 +170,0 @@ ## License : |
17213
344
169