Created
March 17, 2020 10:59
-
-
Save ivarprudnikov/c6c0b0b5293db806812146e04b5be12d to your computer and use it in GitHub Desktop.
Insert rows into dynamo table
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 config = { | |
region: 'eu-west-1', | |
table: 'MyTableName' | |
}; | |
const AWS = require('aws-sdk'); | |
const ddb = new AWS.DynamoDB({ | |
apiVersion: '2012-08-10', | |
region: config.region | |
}); | |
/** | |
* Insert data into DynamoDB | |
* @param item {object} | |
* @return {Promise} | |
*/ | |
async function insertRow (item) { | |
return new Promise((resolve, reject) => { | |
try { | |
ddb.putItem({ | |
TableName: config.table, | |
Item: AWS.DynamoDB.Converter.marshall(item) | |
}, function (err) { | |
if (err) { | |
reject(err); | |
} else { | |
resolve(); | |
} | |
}); | |
} catch (e) { | |
reject(e); | |
} | |
}); | |
} | |
const operations = []; | |
[ | |
{foo:"bar"}, | |
{baz:"baf"} | |
].forEach(item => { | |
operations.push(Promise.resolve() | |
.then(() => insertRow(item)) | |
); | |
}); | |
Promise.all(operations) | |
.catch(e => { | |
console.error(e); | |
process.exit(0); | |
}).finally(() => { | |
process.exit(0); | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment