| Forum

Put items in batch to DynamoDB with NodeJS

Is it possible to insert multiple items to DynamoDB in one go? Like the batch insert/update provided by other RDBS like Oracle/MySQL?

Use this.

 * Function to insert entries to dynamodb
 * @param tableName {String} 
 * @param items {Array} 
 * @returns {Promise} Promise to insert 'items' in 'tableName'
 * */
function putItemsToDB(table, items) {
	const promise = new Promise(function(resolve, reject){
		var i,j,itemsChunk,chunkSize = 25;
		for (i=0,j=items.length; i<j; i+=chunkSize) {
    		itemsChunk = items.slice(i,i+chunkSize);
    		let params = {
				RequestItems: {
        			[table]: itemsChunk
			ddb.batchWriteItem(params, function(err, data) {
			  	if (err) {
				    console.log("Error", err);

There is a limit on the number of items you can insert into DynamoDB table, which is 25. So to fix this, the above code splits the list of items to the inserted into chunks of size 25 and calls the batchWriteItem method multiple times.

The method returns a promise, so the right way to invoke this method is

function putStatesInTable(table, states){
    putItemsToDB(table, states);

The items are expected to be in a special format. Here is an example of how I inserted a list of states.

function insertStatesToDB(states){
	let statesItems = [];
		console.log(state.state_id +":"+ state.state_name);
    		PutRequest: {
      			Item: {
	        		id: AWS.DynamoDB.Converter.input(state.state_id, true),
	        		state_name: AWS.DynamoDB.Converter.input(state.state_name, true)
	putStatesInTable('STATES', statesItems);
1 Like