amazon-dynamodbseedamazon-dynamodb-local

DynamoDB table seed works in cli but not AWS-SDK


I have a table that has more than 25 items and wrote a basic script to break them into sub arrays of 25 items each then loops thru that collection of sub arrays to run a batch write item command in the AWS DynamoDB Client. The issue I am getting is a returned validation error. When I run the same seed file via the aws-cli it seeds the table perfectly. This makes me think it has something to do with my script. See anything I am missing? Thanks in advance!

var { DynamoDB } = require('aws-sdk');
var db = new DynamoDB.DocumentClient({
  region: 'localhost',
  endpoint: 'http://localhost:8000',
});
const allItems = require('./allItems.json');
const tableName = 'some-table-name';
console.log({ tableName, allItems });
var batches = [];
var currentBatch = [];
var count = 0;
for (let i = 0; i < allItems.length; i++) {
  //push item to the current batch
  count++;
  currentBatch.push(allItems[i]);
  if (count === 25) {
    batches.push(currentBatch);
    currentBatch = [];
  }
}
//if there are still items left in the curr batch, add to the collection of batches
if (currentBatch.length > 0 && currentBatch.length !== 25) {
  batches.push(currentBatch);
}

var completedRequests = 0;
var errors = false;
//request handler for DynamoDB
function requestHandler(err, data) {
  console.log('In the request handler...');
  return function (err, data) {
    completedRequests++;
    errors = errors ? true : err;
    //log error
    if (errors) {
      console.error('Request caused a DB error.');
      console.error('ERROR: ' + err);
      console.error(JSON.stringify(err, null, 2));
    } else {
      var res = {
        statusCode: 200,
        headers: {
          'Content-Type': 'application/json',
          'Access-Control-Allow-Methods': 'GET,POST,OPTIONS',
          'Access-Control-Allow-Origin': '*',
          'Access-Control-Allow-Credentials': true,
        },
        body: JSON.stringify(data),
        isBase64Encoded: false,
      };
      console.log(`Success: returned ${data}`);
      return res;
    }
    if (completedRequests == batches.length) {
      return errors;
    }
  };
}

//Make request
var params;
for (let j = 0; j < batches.length; j++) {
  //items go in params.RequestedItems.id array
  //format for the items is {PutRequest : {Item: ITEM_OBJECT}}
  params = '{"RequestItems": {"' + tableName + '": []}}';
  params = JSON.parse(params);
  params.RequestItems[tableName] = batches[j];

  console.log('before db.batchWriteItem: ', params);

    try {
        //send to db
        db.batchWrite(params, requestHandler(params));
    } catch{
        console.error(err)
    }
}

Here is the formatted request object and the error:

before db.batchWriteItem:  
{ RequestItems:
   { 'some-table-name': [ [Object], [Object], [Object], [Object] ] } 
}
In the request handler...
Request caused a DB error.
ERROR: ValidationException: Invalid attribute value type
{
  "message": "Invalid attribute value type",
  "code": "ValidationException",
  "time": "2020-08-04T10:51:13.751Z",
  "requestId": "dd49628c-6ee9-4275-9349-6edca29636fd",
  "statusCode": 400,
  "retryable": false,
  "retryDelay": 47.94198279972915
}

Solution

  • You are using the DocumentClient in the nodejs code. This will automatically convert the data format used by DynamoDB to a more easily consumable format.

    e.g.

      {
         "id": {
           "S": "A string value"
         }
      }
    

    would become

      {
         "id": "A string value"
      }
    

    The CLI does not perform this data conversion. You can use the regular DynamoDB client to not perform this conversion in Nodejs. e.g. const db = new Dynamodb()