DynamoDB Document SDK
This SDK abstracts away the typing of attribute values in the low level SDK in order to provide a simpler developing experience.
JS datatypes like string
or number
can be passed directly into DynamoDB requests and the wrapping will be handled for you; similarly for responses, datatypes will be unwrapped.
For those DynamoDB types that do not have direct mappings to JS datatypes, a wrapper Object is provided to handle type ambiguities (i.e. StrSet, NumSet, BinSet).
Lastly, a Condition Object is being introduced to simplify the use of the KeyCondition and Expected portion of the request params. Note: Condition Object serves to simplify previous api (NOT new expressions)
Getting Started
In order to instantiate the client, you still need the AWS JS SDK to store your region/credentials.
var AWS = ;var DOC = ;AWSconfig;var docClient = ;
Alternatively if you already have the existing DynamoDB Client, you can pass it in order to instantiate the client.
// assumes AWS.config is set up alreadyvar awsClient = AWS;var docClient = awsClient;
After this, you can make requests and receive responses with JS datatypes!
JS datatypes that can be used in place of DynamoDB Datatypes:
Javascript | DynamoDB |
---|---|
string | S |
number | N |
boolean | BOOL |
null | NULL |
array | L |
object | M |
For Sets, the client will provide object for you:
docClient
Refer to the Basic Usage and Nested DataTypes and More sections down below to see examples of the updated API.
In addition, the SDK also introduces a special kind of Object in order to simplify conditions.
docClient
Refer to the section down below on Condition Objects to see an example of the usage.
NOTE: To build the node js files for the browser yourself, run
npm install; uglifyjs lib/* | sed 's/\"use strict\";//' > dynamodb-doc.min.js# sed portion is optional depending on your use case
For each example assume we have these variables available to us.
// Basic Client creationAWSconfig;docClient = ;// Basic Callbackvar {if errconsole;elseconsole;}
Basic Usage:
// Basic Scalar Datatypesvar params = {};paramsTableName = "Users";paramsItem = UserId : "John"Age : 21Pic : docClient;docClient;params = {};paramsTableName = "Users";paramsKey = UserId : "John"docClient;/* Response{Item: {UserId : "John",Age : 21,Pic : Bin}}*/
NOTE: StrToBin returns either a Buffer
for NodeJS or Uint8Array
for the browser.
Nested DataTypes and More:
var params = {};paramsTableName = "Shopping Cart";// Compatible is a Map of Part to List of PartId's// OnSale is a BOOL type// Discount is a NULL typeparamsItem = PartId : "CPU1"OnSale : falseDiscount : nullCompatible : Motherboards : "MB1" "MB2"RAM : "RAM1";docClient;params = {};paramsKey = PartId : "CPU1";paramsTableName = "Shopping Cart";docClient;/*Response{Item: {PartId : "CPU1",OnSale : false,Discount : null,Compatible : {Motherboards : ["MB1", "MB2"],RAM : ["RAM1"]}}};*/
Condition Object:
var params = {};paramsTableName = "Houses";// Note: This is a query on the Key Schema of the table.// For queries on secondary indexes, specify params.IndexName = "index-name"// use an array of Condition Objects for multiple conditionsparamsKeyConditions = docClientdocClient;// use a Condition Object for just a single conditionparamsQueryFilter = docClient;docClient;/*Reponse{Count: 3,Items: [ { HouseId : "123 amzn way",YearBuilt : 2001,Price : 450000},{ HouseId : "321 dynamo st",YearBuilt : 2012,Price : 100000},{ HouseId : "213 JS ave",YearBuilt : 2014,Price : 1}],ScannedCount: 3}*/
Expressions (NEW!!):
var params = {};paramsTableName = "SomeTable";paramsKey = Some : "Key";// Use the #(variable) to substitute in place of attribute Names// Use the :(variable) to subsitute in place of attribute ValuesparamsUpdateExpression = "set #a = :x + :y";paramsConditionExpression = "#a < :MAX and Price = :correct";paramsExpressionAttributeNames = "#a" : "Description";paramsExpressionAttributeValues = ":x" : 20":y" : 45":MAX" : 100":correct" : "is right!!";docClient;