Aws-sdk-js: aws dynamodb DocumentClient is not supporting transactions ?

Created on 1 Dec 2018  路  10Comments  路  Source: aws/aws-sdk-js

AWS dynamodb document client is not supporting transaction as of date DEC 1 2018 however DynamoDB supporting transactions

let aws=require("aws-sdk");
aws.config.update(
    {
        region:"us-east-1",
        endpoint: "dynamodb.us-east-1.amazonaws.com"    

    }
);
var dynamodb = new  aws.DynamoDB.DocumentClient();


dynamodb.transactWriteItems();
TypeError: dynamodb.transactWriteItems is not a function
    at Object.<anonymous> (/home/varnit/projects/aws_support/index.js:12:10)
    at Module._compile (module.js:652:30)
    at Object.Module._extensions..js (module.js:663:10)
    at Module.load (module.js:565:32)
    at tryModuleLoad (module.js:505:12)
    at Function.Module._load (module.js:497:3)
    at Function.Module.runMain (module.js:693:10)
    at startup (bootstrap_node.js:188:16)
    at bootstrap_node.js:609:3

however when i create an instance of only dynamodb it works
var dynamodb = new aws.DynamoDB();

feature-request

Most helpful comment

SOLUTION
Update the aws-sdk package (>2.365)
use-
var dynamodb = new aws.DynamoDB();
instead of
var dynamodb = new aws.DynamoDB.DocumentClient();

All 10 comments

@varnitgoyal95

Thanks for opening this issue.

You are correct, DynamoDB API was recently updated with added support for transactions and those operations were made available in the SDK in version 2.365.0.

The DynamoDB DocumentClient has not been updated to use transactions with that style of interaction with DynamoDB (namely, abstracting away the notion of attribute values).

Marking this issue as a feature request.

As I temporary solution, I use some private methods of AWS.DynamoDB.DocumentClient. I know it's a bad practice, but works fine for me.

  1. I collect all transaction items in an array:
....
transactions.push({ Put: params });
....
transactions.push({ Delete: params })

where params are AWS.DynamoDB.DocumentClient params.

  1. I map these transactions to AWS.DynamoDB array
const operationsMapping = {
  Put: 'putItem',
  Delete: 'deleteItem',
  Update: 'updateItem',
};

const dynamoClient = new Dynamo.DocumentClient({ service: dynamoInstance });

// Danger zone - using private methods!
const translator = dynamoClient.getTranslator();
const transactionItems = this.transaction.map((transaction) => {
  const operation = Object.keys(transaction)[0];
  const params = Object.values(transaction)[0];
  const operationType = operationsMapping[operation];

  const request = dynamoClient.service[operationType](params);
  const inputShape = request.service.api.operations[operationType].input;
  const translated = translator.translateInput(params, inputShape);

  return {
    [operation]: translated
  }
});
  1. I execute all as a transaction:
const dynamo = new Dynamo();
const result = await dynamo.transactWriteItems({ TransactItems: transactionItems }).promise();

@varnitgoyal95

A change to support this has been merged. It will be available with the next release of the SDK.

thankyou

SOLUTION
Update the aws-sdk package (>2.365)
use-
var dynamodb = new aws.DynamoDB();
instead of
var dynamodb = new aws.DynamoDB.DocumentClient();

Does anyone know when this will be supported in lambdas? I've tried to use it but it seems the package used by Amazon is older than the newest one (as described here https://stackoverflow.com/questions/53902670/dynamodb-transactwriteitems-is-not-a-function-error-on-lambda-but-not-when-using)
Quite ironic.

@MartinMasek

The version of the SDK provided by Lambda does lag behind the latest version.

The best practice is to bundle your own version of the SDK anyways. That will allow you control over the upgrade cycle for all of your dependencies.

@srchase

That's something I try to avoid. The whole reason why I started with lambdas was to get rid of taking care of versions and dependencies myself. However, you have a good point that my approach may introduce some problems later.

I'd be helpful to get roadmap info from Amazon (so I would know whether it's worth waiting) but I was not able to find such thing.

This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs and link to relevant comments in this thread.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

bhishp picture bhishp  路  4Comments

hashans picture hashans  路  3Comments

ataraxus picture ataraxus  路  4Comments

Sunil6591 picture Sunil6591  路  4Comments

jaggu07 picture jaggu07  路  3Comments