Skip to content

Instantly share code, notes, and snippets.

@ivarprudnikov
Created March 17, 2020 10:59
Show Gist options
  • Save ivarprudnikov/c6c0b0b5293db806812146e04b5be12d to your computer and use it in GitHub Desktop.
Save ivarprudnikov/c6c0b0b5293db806812146e04b5be12d to your computer and use it in GitHub Desktop.
Insert rows into dynamo table
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