Created
September 28, 2022 12:21
-
-
Save SodaDev/ebc0d4f879a254923d5f8587420a41f2 to your computer and use it in GitHub Desktop.
This file contains 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
const AWS = require("aws-sdk") | |
const _ = require("lodash") | |
const dynamo = new AWS.DynamoDB.DocumentClient({ | |
apiVersion: "2012-08-10", | |
region: "eu-west-1" | |
}) | |
const BATCH_SIZE = 25; | |
const sourceTable = "<put-value-here>"; | |
const destinationTable = "<put-value-here>"; | |
loadItems({TableName: sourceTable, Limit: 100, }) | |
.then( | |
result => console.log("Done", result), | |
err => console.error("Loading items failed", err) | |
) | |
function processItems(res) { | |
const batches = _.chunk(res.Items, BATCH_SIZE); | |
return Promise.all(batches | |
.map(bachItems => { | |
console.log("Persisting batch", bachItems) | |
return dynamo.batchWrite({ | |
RequestItems: { | |
[destinationTable]: bachItems | |
.map(dynamoItem => ({ | |
PutRequest: { | |
Item: dynamoItem | |
} | |
})) | |
} | |
}).promise() | |
}) | |
) | |
} | |
async function loadItems(params) { | |
let results = [] | |
console.log("Scanning", params) | |
const scanResult = await dynamo.scan(params, (err, data) => { | |
if (err) { | |
console.error("Scanning failed", JSON.stringify(err, null, 2)) | |
} else { | |
results = data.Items | |
} | |
}).promise(); | |
await processItems(scanResult) | |
if (typeof scanResult.LastEvaluatedKey != "undefined") { | |
params.ExclusiveStartKey = scanResult.LastEvaluatedKey | |
return loadItems(params) | |
} | |
return results | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment