DynamoDB DocumentClient typescript definitions seem to be incorrect
See original GitHub issueHi,
I am using the DocumentClient
with typescript.
The type definitions seem to be incorrect:
- The
DocumentClient
always returns JSON instead of anAttributeMap
.
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 AttributeValue
s (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:
- Created 5 years ago
- Comments:5 (2 by maintainers)
Top GitHub Comments
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:
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.
@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.
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.