0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

AWS SDK for JavaScript v3に色々あるDynamoDBクライアントの違い

Last updated at Posted at 2025-06-16

AWS SDK for JavaScript v3のDynamoDBクライアントはなんか色々あって紛らわしいので、GetItemする処理をそれぞれで書いて違いを確認してみた。

結果

No クラス ライブラリ サイズ "S": "a"形式 書き方
1 DynamoDBClient @aws-sdk/client-dynamodb 軽量 である client.send(new GetItemCommand(params))
2 AWS.DynamoDB @aws-sdk/client-dynamodb 重量 である client.getItem(params)
3 DynamoDBDocumentClient @aws-sdk/lib-dynamodb 軽量 でない docClient.send(new GetCommand(params))
4 DynamoDBDocument @aws-sdk/lib-dynamodb 重量 でない docClient.get(params)

1は一番基本的。
2はv2互換スタイルで、v4では削除される可能性があるとのこと
3は1のラッパーで、paramsや戻り値が"S": "a"形式でなくなる。インポート元が1, 2と異なることに注意。
4は3に加えてcommandを使わなくてよくなるが、全commandをインポートするため重くなる。3を継承している

  • Document がつくやつは"S":"a"形式でなくて良い
  • Client がつくやつは .send()

と覚えよう。

1. DynamoDBClient

import { DynamoDBClient, GetItemCommand } from "@aws-sdk/client-dynamodb";
const client = new DynamoDBClient({});

async function main() {
    const params = {
        TableName: "test1",
        Key: {
            id: {
                S: "a"
            }
        },
    };
    const res = await client.send(new GetItemCommand(params));
    console.log(res.Item); // { id: { S: 'a' }, num: { N: '1' } }
}

main();

2. AWS.DynamoDB

import * as AWS from "@aws-sdk/client-dynamodb";
const client = new AWS.DynamoDB({});

async function main() {
    const params = {
        TableName: "test1",
        Key: {
            id: {
                S: "a"
            }
        },
    };
    const res = await client.getItem(params);
    console.log(res.Item); // { id: { S: 'a' }, num: { N: '1' } }
}

main();

3. DynamoDBDocumentClient

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocumentClient, GetCommand } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const docClient = DynamoDBDocumentClient.from(client);

async function main() {
    const params = {
        TableName: "test1",
        Key: {
            id: "a"
        },
    };
    const res = await docClient.send(new GetCommand(params));
    console.log(res.Item); // { id: 'a', num: 1 }
}

main();

4. DynamoDBDocument

import { DynamoDBClient } from '@aws-sdk/client-dynamodb';
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
const client = new DynamoDBClient({});
const docClient = DynamoDBDocument.from(client);

async function main() {
    const params = {
        TableName: "test1",
        Key: {
            id: "a"
        },
    };
    const res = await docClient.get(params);
    console.log(res.Item); // { id: 'a', num: 1 }
}

main();
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?