AWS SDK DynamoDB 客户端和 DocumentClient 之间的区别?

Difference between AWS SDK DynamoDB client and DocumentClient?

我想知道 AWS SDK DynamoDB 客户端和 DynamoDB DocumentClient 的区别?在哪种用例中我们应该使用 DynamoDB 客户端而不是 DocumentClient?

const dynamoClient = new AWS.DynamoDB.DocumentClient();

vs 

const dynamo = new AWS.DynamoDB();

根据 DocumentClient 的 announcement

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.

它基本上是在 SDK 中调用 dynamoDB 的一种更简单的方法,它还将带注释的响应数据转换为原生 JS 类型。通常,在对数据库执行更多 "special" 操作(例如创建表等)时,您应该只使用常规 DynamoDB 客户端。这些通常在 CRUD 范围之外。

我认为通过比较两个执行相同操作的代码示例可以最好地回答这个问题。

以下是使用 dynamoDB 客户端放置项目的方法:

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);           
});

以下是使用 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);
});

正如您在 DocumentClient 中看到的那样,Item 以更自然的方式指定。更新 DDB 的所有其他操作(update()delete())和读取操作返回的项目(get()query()scan())也存在类似差异.

简单来说,DocumentClient 只是 DynamoDB 客户端的包装器。正如下面其他评论和 aws 文档中提到的,它具有使用方便的特点,将带注释的响应数据转换为原生 JS 类型并抽象出属性值的概念。

另一个明显的区别是 documentclient 的范围仅限于项目级操作,但 dynamodb 客户端除了项目级操作外还提供更广泛的操作。

来自AWS document client documentation

The document client simplifies working with items in Amazon DynamoDB by abstracting away the notion of attribute values. This abstraction annotates native JavaScript types supplied as input parameters, as well as converts annotated response data to native JavaScript types.