question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

DynamoDB DocumentClient typescript definitions seem to be incorrect

See original GitHub issue

Hi, I am using the DocumentClient with typescript.

The type definitions seem to be incorrect:

  • The DocumentClient always returns JSON instead of an AttributeMap.

Here is the output of the type definitions document_client.d.ts for GetItem

export interface GetItemOutput {
    Item?: AttributeMap;
    ...
}
export type AttributeMap = {[key: string]: AttributeValue};
/**
   * A JavaScript object or native type.
   */
export type AttributeValue = any;

Note that the type definitions in dynamodb.d.ts of AttributeValue is different than in the document_client.d.ts (it also contains the same interfaces…):

export interface GetItemOutput {
   Item?: AttributeMap;
   ...
}
export interface AttributeValue {
    S?: StringAttributeValue;
   ...
}

As the documentation of the document client says,

A JavaScript object or native type. Should I cast it to JSON?

In my package.json I use "aws-sdk": "^2.341.0", which includes the type definitions.

Here is the hacky code (JavaScript code copied from the DDB js tutorial) to reproduce the result as JSON (DocumentClient) vs a result with AttributeValues (DynamoDB):

var AWS = require('aws-sdk');
AWS.config.update({
  region: 'eu-west-1'
});
const tableName = 'table-name'
const KEY = 'abc'
var ddb = new AWS.DynamoDB({
  apiVersion: '2012-08-10'
});
var params = {
  RequestItems: {
    'table-name': {
      Keys: [{
        'key_id': {
          S: KEY
        }
      }]
    }
  }
};

ddb.batchGetItem(params, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    data.Responses[tableName].forEach(function (element, index, array) {
      console.log(element);
    });
  }
});
const docClient = new AWS.DynamoDB.DocumentClient();

const params2 = {
  ConsistentRead: true,
  RequestItems: {
    [tableName]: {
      Keys: [{
        "key_id": KEY
      }]
    },
  },
}
docClient.batchGet(params2, function (err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    data.Responses[tableName].forEach(function (element, index, array) {
      console.log('----------------------------------------');
      console.log(' doc client output: ');
      console.log(element);
      console.log('----------------------------------------');
    });
  }
});

Clearly the doc client output does not contain the AttributeValue types (e.g. {"S": "foo"})

// raw DDB client
key_id: { S: 'abc' }

// DocumentClient output
key_id: 'abc'

It seems that the client is auto-generated. https://github.com/aws/aws-sdk-js/blob/master/apis/dynamodb-2012-08-10.normal.json

Are the definitions up-to-date?

Thx. Simon

Issue Analytics

  • State:closed
  • Created 5 years ago
  • Comments:5 (2 by maintainers)

github_iconTop GitHub Comments

3reactions
simonmitcommented, Nov 22, 2018

Thanks for the fast reply. The behaviour of the DocumentClient is correct. The issue is about the type definitions: There are type definitions with overlapping types (which differ slightly).

I found out that the the issue is in how VS Code lets me navigate to the respective type definition files. I was able to convince VS Code to use the correct type:

// import { GetItemOutput } from 'aws-sdk/clients/dynamodb';
import { DocumentClient } from 'aws-sdk/clients/dynamodb';

const resp: DocumentClient.GetItemOutput = ...

So this is not an issue of the type definitions. It was my fault to rely on VS Code to point me to the correct type definition. I will close the issue.

1reaction
Jefftopiacommented, May 21, 2019

@srchase I just went down a little rabbit hole on this. Would be very helpful to document and note this in the d.ts files.

// These types...
import {
    DocumentClient,
    PutItemInput
} from 'aws-sdk/clients/dynamodb';

// Do not align with these types
import {
    DocumentClient as DocClient
} from 'aws-sdk/lib/dynamodb/document_client';

// DocClient.PutItemInput vs  PutItemInput

Having the top import path export the class but not do much to note the two different sets of interfaces is confusing. I had to fish for the ones in the DocumentClient namespace.

Read more comments on GitHub >

github_iconTop Results From Across the Web

AWS DynamoDB Attempting to ADD to a Set - Incorrect Operand
When I update the user, I get the error, "Invalid UpdateExpression: Incorrect operand type for operator or function; operator: ADD, operand type: MAP"....
Read more >
Using Global Secondary Indexes in DynamoDB
Use global secondary indexes to perform alternate queries from the base DynamoDB table to model your application's various access patterns.
Read more >
AWS DynamoDB DocumentClient & Node.js - Dynobase
DynamoDB from AWS SDK for JavaScript/Typescript; AWS.DynamoDB.DocumentClient which simplifies working with DynamoDB items by abstracting ...
Read more >
Using PartiQL to query AWS DynamoDb in Javascript - abba.dev
Comparing PartiQL (SQL-compatible query language) to the Document Client API to query and insert items in an AWS DynamoDb table with the ...
Read more >
Type Inference | DynamoDB Toolbox
autoParse , parse : If the parse option is set to false (either in the Entity definition or the method options), the method...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found