Difference between AWS SDK DynamoDB client and DocumentClient?

前端 未结 2 974
心在旅途
心在旅途 2021-02-18 21:33

I want to know the difference between the AWS SDK DynamoDB client and the DynamoDB DocumentClient? In which use case should we use the DynamoDB client over the DocumentClient? <

相关标签:
2条回答
  • 2021-02-18 22:00

    I think this can be best answered by comparing two code samples which do the same thing.

    Here's how you put an item using the dynamoDB client:

    var params = {
        Item: {
            "AlbumTitle": {
                S: "Somewhat Famous"
            },
            "Artist": {
                S: "No One You Know"
            },
            "SongTitle": {
                S: "Call Me Today"
            }
        },
        TableName: "Music"
    };
    dynamodb.putItem(params, function (err, data) {
        if (err) console.log(err)
        else console.log(data);           
    });
    

    Here's how you put the same item using the DocumentClient API:

    var params = {
        Item: {
            "AlbumTitle": "Somewhat Famous",
            "Artist": "No One You Know",
            "SongTitle": "Call Me Today"
        },
        TableName: "Music"
    };
    
    var documentClient = new AWS.DynamoDB.DocumentClient();
    
    documentClient.put(params, function (err, data) {
        if (err) console.log(err);
        else console.log(data);
    });
    

    As you can see in the DocumentClient the Item is specified in a more natural way. Similar differences exist in all other operations that update DDB (update(), delete()) and in the items returned from read operations (get(), query(), scan()).

    0 讨论(0)
  • 2021-02-18 22:04

    As per the announcement of the DocumentClient:

    The document client abstraction makes it easier to read and write data to Amazon DynamoDB with the AWS SDK for JavaScript. Now you can use native JavaScript objects without annotating them as AttributeValue types.

    It's basically a simpler way of calling dynamoDB in the SDK and it also converts annotated response data to native JS types. Generally you should only use the regular DynamoDB client when doing more "special" operations on your database like creating tables etc. That's stuff usually outside of the CRUD-scope.

    0 讨论(0)
提交回复
热议问题