DynamoDB K for SDK for SDKJavaScript のSDK のv3) - AWSSDK コードサンプル

AWSDocAWS SDKGitHub サンプルリポジトリには、さらに多くの SDK サンプルがあります

翻訳は機械翻訳により提供されています。提供された翻訳内容と英語版の間で齟齬、不一致または矛盾がある場合、英語版が優先します。

DynamoDB K for SDK for SDKJavaScript のSDK のv3)

次のコード例は、AWS SDK for JavaScript (v3 でアクションを実行DynamoDB。

アクション」は、個々のサービス関数の呼び出し方法を示すコードの抜粋です。

シナリオ」は、同じサービス内で複数の関数を呼び出して、特定のタスクを実行する方法を示すコード例です。

それぞれの例にはGitHub、へのリンクがあり、コンテキストでコードを設定および実行する方法についての説明が記載されています。

アクション

次のコード例は、DynamoDB テーブルを作成する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

テーブルを作成します。

// Import required AWS SDK clients and commands for Node.js import { CreateTableCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { AttributeDefinitions: [ { AttributeName: "Season", //ATTRIBUTE_NAME_1 AttributeType: "N", //ATTRIBUTE_TYPE }, { AttributeName: "Episode", //ATTRIBUTE_NAME_2 AttributeType: "N", //ATTRIBUTE_TYPE }, ], KeySchema: [ { AttributeName: "Season", //ATTRIBUTE_NAME_1 KeyType: "HASH", }, { AttributeName: "Episode", //ATTRIBUTE_NAME_2 KeyType: "RANGE", }, ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, TableName: "TEST_TABLE", //TABLE_NAME StreamSpecification: { StreamEnabled: false, }, }; export const run = async () => { try { const data = await ddbClient.send(new CreateTableCommand(params)); console.log("Table Created", data); return data; } catch (err) { console.log("Error", err); } }; run();
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI CreateTableリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { AttributeDefinitions: [ { AttributeName: 'CUSTOMER_ID', AttributeType: 'N' }, { AttributeName: 'CUSTOMER_NAME', AttributeType: 'S' } ], KeySchema: [ { AttributeName: 'CUSTOMER_ID', KeyType: 'HASH' }, { AttributeName: 'CUSTOMER_NAME', KeyType: 'RANGE' } ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1 }, TableName: 'CUSTOMER_LIST', StreamSpecification: { StreamEnabled: false } }; // Call DynamoDB to create the table ddb.createTable(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Table Created", data); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI CreateTableリファレンスのを参照してください

次のコード例は、DynamoDB テーブルを削除する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

テーブルを削除します。

// Import required AWS SDK clients and commands for Node.js import { DeleteTableCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { TableName: "CUSTOMER_LIST_NEW", }; export const run = async () => { try { const data = await ddbClient.send(new DeleteTableCommand(params)); console.log("Success, table deleted", data); return data; } catch (err) { console.log("Error", err); } }; run();
  • API の詳細については、AWS SDK for JavaScriptAPI DeleteTableリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: process.argv[2] }; // Call DynamoDB to delete the specified table ddb.deleteTable(params, function(err, data) { if (err && err.code === 'ResourceNotFoundException') { console.log("Error: Table not found"); } else if (err && err.code === 'ResourceInUseException') { console.log("Error: Table in use"); } else { console.log("Success", data); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI DeleteTableリファレンスのを参照してください

次のコード例は、DynamoDB テーブルから項目を削除する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

DynamoDB ドキュメントクライアントを使用して、テーブルから項目を削除します。

import { DeleteCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; // Set the parameters. export const params = { TableName: "TABLE_NAME", Key: { PRIMARY_KEY: "VALUE_1", //e.g. title: "Rush" SORT_KEY: "VALUE_2", // e.g. year: "2013" }, }; export const deleteItem = async () => { try { await ddbDocClient.send(new DeleteCommand(params)); console.log("Success - item deleted"); } catch (err) { console.log("Error", err); } }; deleteItem();

PartiQL を使用してテーブルから項目を削除します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; export const run = async (tableName, movieYear1, movieTitle1) => { const params = { Statement: "DELETE FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item deleted."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1);

PartiQL を使用してバッチごとにテーブルから項目を削除します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { try { const params = { Statements: [ { Statement: "DELETE FROM " + tableName + " where year=? and title=?", Parameters: [{ N: movieYear1 }, { S: movieTitle1 }], }, { Statement: "DELETE FROM " + tableName + " where year=? and title=?", Parameters: [{ N: movieYear2 }, { S: movieTitle2 }], }, ], }; const data = await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items deleted.", data); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI DeleteItemリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

テーブルから項目を削除します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: 'TABLE', Key: { 'KEY_NAME': {N: 'VALUE'} } }; // Call DynamoDB to delete the item from the table ddb.deleteItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

DynamoDB ドキュメントクライアントを使用して、テーブルから項目を削除します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); var params = { Key: { 'HASH_KEY': VALUE }, TableName: 'TABLE' }; docClient.delete(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI DeleteItemリファレンスのを参照してください

DynamoDB 項目のバッチを取得する方法を、次のコード例により示します。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

項目を取得します。

// Import required AWS SDK clients and commands for Node.js import { BatchGetItemCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { RequestItems: { TABLE_NAME: { Keys: [ { KEY_NAME_1: { N: "KEY_VALUE" }, KEY_NAME_2: { N: "KEY_VALUE" }, KEY_NAME_3: { N: "KEY_VALUE" }, }, ], ProjectionExpression: "ATTRIBUTE_NAME", }, }, }; export const run = async () => { try { const data = await ddbClient.send(new BatchGetItemCommand(params)); console.log("Success, items retrieved", data); return data; } catch (err) { console.log("Error", err); } }; run();
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI BatchGetItemリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { RequestItems: { 'TABLE_NAME': { Keys: [ {'KEY_NAME': {N: 'KEY_VALUE_1'}}, {'KEY_NAME': {N: 'KEY_VALUE_2'}}, {'KEY_NAME': {N: 'KEY_VALUE_3'}} ], ProjectionExpression: 'KEY_NAME, ATTRIBUTE' } } }; ddb.batchGetItem(params, function(err, data) { if (err) { console.log("Error", err); } else { data.Responses.TABLE_NAME.forEach(function(element, index, array) { console.log(element); }); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI BatchGetItemリファレンスのを参照してください

次のコード例は、DynamoDB テーブルから項目を取得する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

テーブルから項目を取得します。

import { GetCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; // Set the parameters. export const params = { TableName: "TABLE_NAME", Key: { PRIMARY_KEY: "VALUE_1", //e.g. title: "Rush" SORT_KEY: "VALUE_2", // e.g. year: "2013" }, }; export const getItem = async () => { try { const data = await ddbDocClient.send(new GetCommand(params)); console.log("Success :", data.Item); } catch (err) { console.log("Error", err); } }; getItem();

PartiQL を使用してテーブルから項目を取得します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient"; const tableName = process.argv[2]; const movieTitle1 = process.argv[3]; export const run = async (tableName, movieTitle1) => { const params = { Statement: "SELECT * FROM " + tableName + " where title=?", Parameters: [{ S: movieTitle1 }], }; try { const data = await ddbDocClient.send(new ExecuteStatementCommand(params)); for (let i = 0; i < data.Items.length; i++) { console.log( "Success. The query return the following data. Item " + i, data.Items[i].year, data.Items[i].title, data.Items[i].info ); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieTitle1);

PartiQL を使用してテーブルからバッチで項目を取得します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { const params = { Statements: [ { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { const data = await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); for (let i = 0; i < data.Responses.length; i++) { console.log(data.Responses[i].Item.year); console.log(data.Responses[i].Item.title); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);
  • API の詳細については、AWS SDK for JavaScriptAPI GetItemリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

テーブルから項目を取得します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: 'TABLE', Key: { 'KEY_NAME': {N: '001'} }, ProjectionExpression: 'ATTRIBUTE_NAME' }; // Call DynamoDB to read the item from the table ddb.getItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Item); } });

DynamoDB ドキュメントクライアントを使用して、テーブルから項目を取得します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); var params = { TableName: 'EPISODES_TABLE', Key: {'KEY_NAME': VALUE} }; docClient.get(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Item); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI GetItemリファレンスのを参照してください

次のコード例は、DynamoDB テーブルに関する情報を取得する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

テーブルを記述します。

// Import required AWS SDK clients and commands for Node.js import { DescribeTableCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { TableName: "TABLE_NAME" }; //TABLE_NAME export const run = async () => { try { const data = await ddbClient.send(new DescribeTableCommand(params)); console.log("Success", data); // console.log("Success", data.Table.KeySchema); return data; } catch (err) { console.log("Error", err); } }; run();
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI DescribeTableリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: process.argv[2] }; // Call DynamoDB to retrieve the selected table descriptions ddb.describeTable(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Table.KeySchema); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI DescribeTableリファレンスのを参照してください

次のコード例は、DynamoDB テーブルを一覧表示する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

テーブルを一覧表示します。

// Import required AWS SDK clients and commands for Node.js import { ListTablesCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; export const run = async () => { try { const data = await ddbClient.send(new ListTablesCommand({})); console.log(data); // console.log(data.TableNames.join("\n")); return data; } catch (err) { console.error(err); } }; run();
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI ListTablesリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); // Call DynamoDB to retrieve the list of tables ddb.listTables({Limit: 10}, function(err, data) { if (err) { console.log("Error", err.code); } else { console.log("Table names are ", data.TableNames); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI ListTablesリファレンスのを参照してください

次のコード例は、DynamoDB テーブルで項目を配置する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

DynamoDB ドキュメントクライアントを使用して、テーブルに項目を配置します。

import { PutCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; export const putItem = async () => { // Set the parameters. export const params = { TableName: "TABLE_NAME", Item: { PRIMARY_KEY: "VALUE_1", //e.g. title: "Rush" SORT_KEY: "VALUE_2", // e.g. year: "2013" }, }; try { const data = await ddbDocClient.send(new PutCommand(params)); console.log("Success - item added or updated", data); } catch (err) { console.log("Error", err.stack); } }; putItem();

PartiQL を使用してテーブルに項目を配置します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; export const run = async (tableName, movieTitle1, movieYear1) => { const params = { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item added."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieTitle1, movieYear1);

PartiQL を使用して、バッチでテーブルに項目を配置します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { const params = { Statements: [ { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items added."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);
  • API の詳細については、AWS SDK for JavaScriptAPI PutItemリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

テーブルに項目を配置します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create the DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { TableName: 'CUSTOMER_LIST', Item: { 'CUSTOMER_ID' : {N: '001'}, 'CUSTOMER_NAME' : {S: 'Richard Roe'} } }; // Call DynamoDB to add the item to the table ddb.putItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });

DynamoDB ドキュメントクライアントを使用して、テーブルに項目を配置します。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); var params = { TableName: 'TABLE', Item: { 'HASHKEY': VALUE, 'ATTRIBUTE_1': 'STRING_VALUE', 'ATTRIBUTE_2': VALUE_2 } }; docClient.put(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI PutItemリファレンスのを参照してください

次のコード例は、DynamoDB テーブルに対してバッチでクエリを実行する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. const REGION = "REGION"; //e.g. "us-east-1" // Create an Amazon DynamoDB service client object. const ddbClient = new DynamoDBClient({ region: REGION }); export { ddbClient };

テーブルに対してクエリを実行します。

// Import required AWS SDK clients and commands for Node.js import { QueryCommand } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "./libs/ddbClient.js"; // Set the parameters export const params = { KeyConditionExpression: "Season = :s and Episode > :e", FilterExpression: "contains (Subtitle, :topic)", ExpressionAttributeValues: { ":s": { N: "1" }, ":e": { N: "2" }, ":topic": { S: "SubTitle" }, }, ProjectionExpression: "Episode, Title, Subtitle", TableName: "EPISODES_TABLE", }; export const run = async () => { try { const data = await ddbClient.send(new QueryCommand(params)); data.Items.forEach(function (element) { console.log(element.Title.S + " (" + element.Subtitle.S + ")"); }); return data; } catch (err) { console.error(err); } }; run();

DynamoDB ドキュメントクライアントのクライアントを作成します。

// Create service client module using ES6 syntax. import { DynamoDBDocumentClient} from "@aws-sdk/lib-dynamodb"; import {ddbClient} from "./ddbClient"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: false, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; const translateConfig = { marshallOptions, unmarshallOptions }; // Create the DynamoDB Document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, translateConfig); export { ddbDocClient };

DynamoDB ドキュメントクライアントを使用して、テーブルに対してクエリを実行します。

import { QueryCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; // Set the parameters. export const params = { ExpressionAttributeNames: { "#r": "rank", "#y": "year" }, ProjectionExpression: "#r, #y, title", TableName: "TABLE_NAME", ExpressionAttributeValues: { ":t": "MOVIE_NAME", ":y": "MOVIE_YEAR", ":r": "MOVIE_RANK", }, KeyConditionExpression: "title = :t and #y = :y", FilterExpression: "info.#r = :r", }; export const queryTable = async () => { try { const data = await ddbDocClient.send(new QueryCommand(params)); for (let i = 0; i < data.Items.length; i++) { console.log( "Success. Items with rank of " + "MOVIE_RANK" + " include\n" + "Year = " + data.Items[i].year + " Title = " + data.Items[i].title ); } } catch (err) { console.log("Error", err); } }; queryTable();
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScript API リファレンスの「Query」を参照してください。

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB document client var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'}); var params = { ExpressionAttributeValues: { ':s': 2, ':e': 9, ':topic': 'PHRASE' }, KeyConditionExpression: 'Season = :s and Episode > :e', FilterExpression: 'contains (Subtitle, :topic)', TableName: 'EPISODES_TABLE' }; docClient.query(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data.Items); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScript API リファレンスの「Query」を参照してください。

次のコード例は、DynamoDB テーブルで PartiQL ステートメントを実行する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. export const REGION = "eu-west-1"; // For example, "us-east-1". // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: false, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; const translateConfig = { marshallOptions, unmarshallOptions }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, translateConfig); export { ddbDocClient };

PartiQL を使用して項目を作成します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; export const run = async (tableName, movieTitle1, movieYear1) => { const params = { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item added."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieTitle1, movieYear1);

PartiQL を使用して項目を取得します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient"; const tableName = process.argv[2]; const movieTitle1 = process.argv[3]; export const run = async (tableName, movieTitle1) => { const params = { Statement: "SELECT * FROM " + tableName + " where title=?", Parameters: [{ S: movieTitle1 }], }; try { const data = await ddbDocClient.send(new ExecuteStatementCommand(params)); for (let i = 0; i < data.Items.length; i++) { console.log( "Success. The query return the following data. Item " + i, data.Items[i].year, data.Items[i].title, data.Items[i].info ); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieTitle1);

PartiQL を使用して項目を更新します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const producer1 = process.argv[5]; export const run = async (tableName, movieYear1, movieTitle1, producer1) => { const params = { Statement: "UPDATE " + tableName + " SET Producer=? where title=? and year=?", Parameters: [{ S: producer1 }, { S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item updated."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, producer1);

PartiQL を使用して項目を削除します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; export const run = async (tableName, movieYear1, movieTitle1) => { const params = { Statement: "DELETE FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item deleted."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1);
  • API の詳細については、AWS SDK for JavaScriptAPI ExecuteStatementリファレンスのを参照してください

次のコード例は、DynamoDB テーブルで PartiQL ステートメントのバッチを実行する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; // Set the AWS Region. export const REGION = "eu-west-1"; // For example, "us-east-1". // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: false, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; const translateConfig = { marshallOptions, unmarshallOptions }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, translateConfig); export { ddbDocClient };

PartiQL を使用して項目のバッチを作成します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { const params = { Statements: [ { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [{ S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items added."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);

PartiQL を使用して項目のバッチを取得します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { const params = { Statements: [ { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [{ S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { const data = await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); for (let i = 0; i < data.Responses.length; i++) { console.log(data.Responses[i].Item.year); console.log(data.Responses[i].Item.title); } return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);

PartiQL を使用して項目のバッチを更新します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[6]; const movieTitle2 = process.argv[7]; const producer1 = process.argv[5]; const producer2 = process.argv[8]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2, producer1, producer2 ) => { const params = { Statements: [ { Statement: "UPDATE " + tableName + " SET producer=? where title=? and year=?", Parameters: [{ S: producer1 }, { S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "UPDATE " + tableName + " SET producer=? where title=? and year=?", Parameters: [{ S: producer2 }, { S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items updated."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2, producer1, producer2 );

PartiQL を使用して項目のバッチを削除します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[5]; const movieTitle2 = process.argv[6]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2 ) => { try { const params = { Statements: [ { Statement: "DELETE FROM " + tableName + " where year=? and title=?", Parameters: [{ N: movieYear1 }, { S: movieTitle1 }], }, { Statement: "DELETE FROM " + tableName + " where year=? and title=?", Parameters: [{ N: movieYear2 }, { S: movieTitle2 }], }, ], }; const data = await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items deleted.", data); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, movieYear2, movieTitle2);
  • API の詳細については、AWS SDK for JavaScriptAPI BatchExecuteStatementリファレンスのを参照してください

次のコード例は、DynamoDB テーブルをスキャンする方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

DynamoDB ドキュメントクライアントを使用してテーブルをスキャンします。

// Import required AWS SDK clients and commands for Node.js. import { ddbDocClient } from "../libs/ddbDocClient.js"; import { ScanCommand } from "@aws-sdk/lib-dynamodb"; // Set the parameters. export const params = { TableName: "TABLE_NAME", ProjectionExpression: "#r, #y, title", ExpressionAttributeNames: { "#r": "rank", "#y": "year" }, FilterExpression: "title = :t and #y = :y and info.#r = :r", ExpressionAttributeValues: { ":r": "MOVIE_RANK", ":y": "MOVIE_YEAR", ":t": "MOVIE_NAME", }, }; export const scanTable = async () => { try { const data = await ddbDocClient.send(new ScanCommand(params)); console.log("success", data.Items); } catch (err) { console.log("Error", err); } }; scanTable();
  • API の詳細については、「AWS SDK for JavaScript API リファレンス」の「Scan」を参照してください。

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js. var AWS = require("aws-sdk"); // Set the AWS Region. AWS.config.update({ region: "REGION" }); // Create DynamoDB service object. var ddb = new AWS.DynamoDB({ apiVersion: "2012-08-10" }); const params = { // Specify which items in the results are returned. FilterExpression: "Subtitle = :topic AND Season = :s AND Episode = :e", // Define the expression attribute value, which are substitutes for the values you want to compare. ExpressionAttributeValues: { ":topic": {S: "SubTitle2"}, ":s": {N: 1}, ":e": {N: 2}, }, // Set the projection expression, which are the attributes that you want. ProjectionExpression: "Season, Episode, Title, Subtitle", TableName: "EPISODES_TABLE", }; ddb.scan(params, function (err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); data.Items.forEach(function (element, index, array) { console.log( "printing", element.Title.S + " (" + element.Subtitle.S + ")" ); }); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScript API リファレンスの「Scan」を参照してください。

次のコード例は、DynamoDB テーブルで項目を更新する方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

DynamoDB ドキュメントクライアントを使用して、テーブル内の項目を更新します。

import { UpdateCommand } from "@aws-sdk/lib-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; export const updateItem = async () => { // Set the parameters. const params = { TableName: "TABLE_NAME", Key: { PRIMARY_KEY: "VALUE_1", //e.g. title: "Rush" SORT_KEY: "VALUE_2", // e.g. year: "2013" }, ProjectionExpression: "#r", ExpressionAttributeNames: { "#r": "rank" }, UpdateExpression: "set info.plot = :p, info.#r = :r", ExpressionAttributeValues: { ":p": "MOVIE_PLOT", ":r": "MOVIE_RANK", }, }; try { const data = await ddbDocClient.send(new UpdateCommand(params)); console.log("Success - item added or updated", data); return data; } catch (err) { console.log("Error", err); } }; updateItem();

PartiQL を使用してテーブルの項目を更新します。

// Import required AWS SDK clients and commands for Node.js. import { ExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const producer1 = process.argv[5]; export const run = async (tableName, movieYear1, movieTitle1, producer1) => { const params = { Statement: "UPDATE " + tableName + " SET Producer=? where title=? and year=?", Parameters: [{ S: producer1 }, { S: movieTitle1 }, { N: movieYear1 }], }; try { await ddbDocClient.send(new ExecuteStatementCommand(params)); console.log("Success. Item updated."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run(tableName, movieYear1, movieTitle1, producer1);

PartiQL を使用して、バッチでテーブルの項目を更新します。

// Import required AWS SDK clients and commands for Node.js. import { BatchExecuteStatementCommand } from "@aws-sdk/client-dynamodb"; import { ddbDocClient } from "../libs/ddbDocClient.js"; const tableName = process.argv[2]; const movieYear1 = process.argv[3]; const movieTitle1 = process.argv[4]; const movieYear2 = process.argv[6]; const movieTitle2 = process.argv[7]; const producer1 = process.argv[5]; const producer2 = process.argv[8]; export const run = async ( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2, producer1, producer2 ) => { const params = { Statements: [ { Statement: "UPDATE " + tableName + " SET producer=? where title=? and year=?", Parameters: [{ S: producer1 }, { S: movieTitle1 }, { N: movieYear1 }], }, { Statement: "UPDATE " + tableName + " SET producer=? where title=? and year=?", Parameters: [{ S: producer2 }, { S: movieTitle2 }, { N: movieYear2 }], }, ], }; try { await ddbDocClient.send( new BatchExecuteStatementCommand(params) ); console.log("Success. Items updated."); return "Run successfully"; // For unit tests. } catch (err) { console.error(err); } }; run( tableName, movieYear1, movieTitle1, movieYear2, movieTitle2, producer1, producer2 );
  • API の詳細については、AWS SDK for JavaScriptAPI UpdateItemリファレンスのを参照してください

次のコード例は、DynamoDB 項目のバッチを書き込む方法を示しています。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

テーブルに項目を追加します。

import fs from "fs"; import * as R from "ramda"; import { ddbDocClient } from "../libs/ddbDocClient.js"; import { BatchWriteCommand } from "@aws-sdk/lib-dynamodb"; export const writeData = async () => { // Before you run this example, download 'movies.json' from https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/GettingStarted.Js.02.html, // and put it in the same folder as the example. // Get the movie data parse to convert into a JSON object. const allMovies = JSON.parse(fs.readFileSync("moviedata.json", "utf8")); // Split the table into segments of 25. const dataSegments = R.splitEvery(25, allMovies); const TABLE_NAME = "TABLE_NAME" try { // Loop batch write operation 10 times to upload 250 items. for (let i = 0; i < 10; i++) { const segment = dataSegments[i]; for (let j = 0; j < 25; j++) { const params = { RequestItems: { [TABLE_NAME]: [ { // Destination Amazon DynamoDB table name. PutRequest: { Item: { year: segment[j].year, title: segment[j].title, info: segment[j].info, }, }, }, ], }, }; ddbDocClient.send(new BatchWriteCommand(params)); } console.log("Success, table updated."); } } catch (error) { console.log("Error", error); } }; writeData();
  • API の詳細については、AWS SDK for JavaScriptAPI BatchWriteItemリファレンスのを参照してください

SDK forJavaScript (v2)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

// Load the AWS SDK for Node.js var AWS = require('aws-sdk'); // Set the region AWS.config.update({region: 'REGION'}); // Create DynamoDB service object var ddb = new AWS.DynamoDB({apiVersion: '2012-08-10'}); var params = { RequestItems: { "TABLE_NAME": [ { PutRequest: { Item: { "KEY": { "N": "KEY_VALUE" }, "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }, "ATTRIBUTE_2": { "N": "ATTRIBUTE_2_VALUE" } } } }, { PutRequest: { Item: { "KEY": { "N": "KEY_VALUE" }, "ATTRIBUTE_1": { "S": "ATTRIBUTE_1_VALUE" }, "ATTRIBUTE_2": { "N": "ATTRIBUTE_2_VALUE" } } } } ] } }; ddb.batchWriteItem(params, function(err, data) { if (err) { console.log("Error", err); } else { console.log("Success", data); } });
  • 詳細については、AWS SDK for JavaScript デベロッパーガイドを参照してください。

  • API の詳細については、AWS SDK for JavaScriptAPI BatchWriteItemリファレンスのを参照してください

シナリオ

次のコード例は、以下の操作方法を示しています。

  • 映画データを保持できるテーブルを作成する。

  • テーブルに 1 つの映画を入れ、取得して更新する。

  • サンプル JSON ファイルから映画データをテーブルに書き込む。

  • 特定の年にリリースされた映画を照会する。

  • 何年もの間にリリースされた映画をスキャンする。

  • テーブルからムービーを削除し、テーブルを削除します。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

DynamoDB クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

DynamoDB ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

シナリオを実行します。

import fs from "fs"; import { splitEvery } from "ramda"; import { PutCommand, GetCommand, UpdateCommand, BatchWriteCommand, DeleteCommand, ScanCommand, QueryCommand, } from "@aws-sdk/lib-dynamodb"; import { CreateTableCommand, DeleteTableCommand, waitUntilTableExists, waitUntilTableNotExists, } from "@aws-sdk/client-dynamodb"; import { ddbClient } from "../libs/ddbClient.js"; import { ddbDocClient } from "../libs/ddbDocClient.js"; /** * @param {string} tableName */ const createTable = async (tableName) => { await ddbClient.send( new CreateTableCommand({ AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N", }, { AttributeName: "title", AttributeType: "S", }, ], KeySchema: [ { AttributeName: "year", KeyType: "HASH", }, { AttributeName: "title", KeyType: "RANGE", }, ], // Enables "on-demand capacity mode". BillingMode: "PAY_PER_REQUEST", TableName: tableName, }) ); await waitUntilTableExists( { client: ddbClient, maxWaitTime: 15, maxDelay: 2, minDelay: 1 }, { TableName: tableName } ); }; /** * * @param {string} tableName * @param {Record<string, any> | undefined} attributes */ const putItem = async (tableName, attributes) => { const command = new PutCommand({ TableName: tableName, Item: attributes, }); await ddbDocClient.send(command); }; /** * @typedef {{ title: string, info: { plot: string, rank: number }, year: number }} Movie */ /** * @param {unknown} movies * @returns {Movie[]} */ const validateMovies = (input) => { const movies = JSON.parse(input, "utf8"); if (!Array.isArray(movies)) { throw new Error("File contents are not an array."); } if (movies.length === 0) { throw new Error("File contents are empty."); } if ( movies.some( ( /** @type {unknown} */ m ) => { if (typeof m !== "object") { return true; } if (typeof m.year !== "number") { return true; } if (typeof m.title !== "string") { return true; } if (typeof m.info !== "object") { return true; } return false; } ) ) { throw new Error("File contents contain invalid data."); } else { return movies; } }; /** * * @param {string} tableName * @param {string} filePath * @returns { { movieCount: number } } The number of movies written to the database. */ const batchWriteMoviesFromFile = async (tableName, filePath) => { const fileContents = fs.readFileSync(filePath); const movies = validateMovies(fileContents); // Map movies to RequestItems. const putMovieRequestItems = movies.map(({ year, title, info }) => ({ PutRequest: { Item: { year, title, info } }, })); // Organize RequestItems into batches of 25. 25 is the max number of items in a batch request. const putMovieBatches = splitEvery(25, putMovieRequestItems); const batchCount = putMovieBatches.length; // Map batches to promises. const batchRequests = putMovieBatches.map(async (batch, i) => { const command = new BatchWriteCommand({ RequestItems: { [tableName]: batch, }, }); await ddbDocClient.send(command).then(() => { console.log( `Wrote batch ${i + 1} of ${batchCount} with ${batch.length} items.` ); }); }); // Wait for all batch requests to resolve. await Promise.all(batchRequests); return { movieCount: movies.length }; }; /** * * @param {string} tableName * @param {{ * existingMovieName: string, * existingMovieYear: string, * newMoviePlot: string, * newMovieRank: string}} keyUpdate */ const updateMovie = async ( tableName, { existingMovieName, existingMovieYear, newMoviePlot, newMovieRank } ) => { await ddbClient.send( new UpdateCommand({ TableName: tableName, Key: { title: existingMovieName, year: existingMovieYear, }, // Define expressions for the new or updated attributes. ExpressionAttributeNames: { "#r": "rank" }, UpdateExpression: "set info.plot = :p, info.#r = :r", ExpressionAttributeValues: { ":p": newMoviePlot, ":r": newMovieRank, }, ReturnValues: "ALL_NEW", }) ); }; /** * @param {Movie} movie */ const logMovie = (movie) => { console.log(` | Title: "${movie.title}".`); console.log(` | Plot: "${movie.info.plot}`); console.log(` | Year: ${movie.year}`); console.log(` | Rank: ${movie.info.rank}`); }; /** * * @param {{ title: string, info: { plot: string, rank: number }, year: number }[]} movies */ const logMovies = (movies) => { console.log("\n"); movies.forEach((movie, i) => { if (i > 0) { console.log("-".repeat(80)); } logMovie(movie); }); }; /** * * @param {string} tableName * @param {string} title * @param {number} year * @returns */ const getMovie = async (tableName, title, year) => { const { Item } = await ddbDocClient.send( new GetCommand({ TableName: tableName, Key: { title, year, }, // By default, reads are eventually consistent. "ConsistentRead: true" represents // a strongly consistent read. This guarantees that the most up-to-date data is returned. It // can also result in higher latency and a potential for server errors. ConsistentRead: true, }) ); return Item; }; /** * * @param {string} tableName * @param {{ title: string, year: number }} key */ const deleteMovie = async (tableName, key) => { await ddbDocClient.send( new DeleteCommand({ TableName: tableName, Key: key, }) ); }; /** * * @param {string} tableName * @param {number} startYear * @param {number} endYear * @param {Record<string, any>} startKey * @returns {Promise<{}[]>} */ const findMoviesBetweenYears = async ( tableName, startYear, endYear, startKey = undefined ) => { const { Items, LastEvaluatedKey } = await ddbClient.send( new ScanCommand({ ConsistentRead: true, TableName: tableName, ExpressionAttributeNames: { "#y": "year" }, FilterExpression: "#y BETWEEN :y1 AND :y2", ExpressionAttributeValues: { ":y1": startYear, ":y2": endYear }, ExclusiveStartKey: startKey, }) ); if (LastEvaluatedKey && Array.isArray(Items)) { return Items.concat( await findMoviesBetweenYears( tableName, startYear, endYear, LastEvaluatedKey ) ); } else { return Items; } }; /** * * @param {string} tableName * @param {number} year * @returns */ const queryMoviesByYear = async (tableName, year) => { const command = new QueryCommand({ ConsistentRead: true, ExpressionAttributeNames: { "#y": "year" }, TableName: tableName, ExpressionAttributeValues: { ":y": year, }, KeyConditionExpression: "#y = :y", }); const { Items } = await ddbDocClient.send(command); return Items; }; /** * * @param {*} tableName */ const deleteTable = async (tableName) => { await ddbDocClient.send(new DeleteTableCommand({ TableName: tableName })); await waitUntilTableNotExists( { client: ddbClient, maxWaitTime: 10, maxDelay: 2, minDelay: 1, }, { TableName: tableName } ); }; export const runScenario = async ({ tableName, newMovieName, newMovieYear, existingMovieName, existingMovieYear, newMovieRank, newMoviePlot, moviesPath, }) => { console.log(`Creating table named: ${tableName}`); await createTable(tableName); console.log(`\nTable created.`); console.log(`\nAdding "${newMovieName}" to ${tableName}.`); await putItem(tableName, { title: newMovieName, year: newMovieYear }); console.log("\nSuccess - single movie added."); console.log("\nWriting hundreds of movies in batches."); const { movieCount } = await batchWriteMoviesFromFile(tableName, moviesPath); console.log(`\nWrote ${movieCount} movies to database.`); console.log(`\nGetting "${existingMovieName}."`); const originalMovie = await getMovie( tableName, existingMovieName, existingMovieYear ); logMovie(originalMovie); console.log(`\nUpdating "${existingMovieName}" with a new plot and rank.`); await updateMovie(tableName, { existingMovieName, existingMovieYear, newMoviePlot, newMovieRank, }); console.log(`\n"${existingMovieName}" updated.`); console.log(`\nGetting latest info for "${existingMovieName}"`); const updatedMovie = await getMovie( tableName, existingMovieName, existingMovieYear ); logMovie(updatedMovie); console.log(`\nDeleting "${newMovieName}."`); await deleteMovie(tableName, { title: newMovieName, year: newMovieYear }); console.log(`\n"${newMovieName} deleted.`); const [scanY1, scanY2] = [1985, 2003]; console.log( `\nScanning ${tableName} for movies that premiered between ${scanY1} and ${scanY2}.` ); const scannedMovies = await findMoviesBetweenYears(tableName, scanY1, scanY1); logMovies(scannedMovies); const queryY = 2003; console.log(`Querying ${tableName} for movies that premiered in ${queryY}.`); const queriedMovies = await queryMoviesByYear(tableName, queryY); logMovies(queriedMovies); console.log(`Deleting ${tableName}.`); await deleteTable(tableName); console.log(`${tableName} deleted.`); }; const main = async () => { const args = { tableName: "myNewTable", newMovieName: "myMovieName", newMovieYear: 2022, existingMovieName: "This Is the End", existingMovieYear: 2013, newMovieRank: 200, newMoviePlot: "A coder cracks code...", moviesPath: "../../../../../../resources/sample_files/movies.json", }; try { await runScenario(args); } catch (err) { // Some extra error handling here to be sure the table is cleaned up if something // goes wrong during the scenario run. console.error(err); const tableName = args.tableName; if (tableName) { console.log(`Attempting to delete ${tableName}`); await ddbClient .send(new DeleteTableCommand({ TableName: tableName })) .then(() => console.log(`\n${tableName} deleted.`)) .catch((err) => console.error(`\nFailed to delete ${tableName}.`, err)); } } }; export { main };

次のコード例は、以下の操作方法を示しています。

  • 複数の SELECT ステートメントを実行して、項目のバッチを取得する。

  • 複数の INSERT ステートメントを実行して、項目のバッチを追加する。

  • 複数の UPDATE ステートメントを実行して、項目のバッチを更新する。

  • 複数の DELETE ステートメントを実行して、項目のバッチを削除する。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

バッチで項目にクエリを実行します。

*/ import fs from "fs"; import {splitEvery} from "ramda"; import { BatchExecuteStatementCommand, BatchWriteCommand } from "@aws-sdk/lib-dynamodb"; import { CreateTableCommand, DeleteTableCommand, waitUntilTableExists, waitUntilTableNotExists, } from "@aws-sdk/client-dynamodb"; import {ddbClient} from "../libs/ddbClient.js"; import {ddbDocClient} from "../libs/ddbDocClient.js"; /** * @param {string} tableName */ const createTable = async (tableName) => { await ddbClient.send( new CreateTableCommand({ TableName: tableName, AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N", }, { AttributeName: "title", AttributeType: "S", } ], KeySchema: [ { AttributeName: "year", KeyType: "HASH", }, { AttributeName: "title", KeyType: "RANGE", }, ], // Enables "on-demand capacity mode". BillingMode: "PAY_PER_REQUEST" }) ); await waitUntilTableExists( {client: ddbClient, maxWaitTime: 15, maxDelay: 2, minDelay: 1}, {TableName: tableName} ); }; /** * * @param {string} tableName * @param {string} filePath * @returns { { movieCount: number } } The number of movies written to the database. */ const batchWriteMoviesFromFile = async (tableName, filePath) => { const fileContents = fs.readFileSync(filePath); const movies = JSON.parse(fileContents, "utf8"); // Map movies to RequestItems. const putMovieRequestItems = movies.map(({year, title, info}) => ({ PutRequest: {Item: {year, title, info}}, })); // Organize RequestItems into batches of 25. 25 is the max number of items in a batch request. const putMovieBatches = splitEvery(25, putMovieRequestItems); const batchCount = putMovieBatches.length; // Map batches to promises. const batchRequests = putMovieBatches.map(async (batch, i) => { const command = new BatchWriteCommand({ RequestItems: { [tableName]: batch, }, }); await ddbDocClient.send(command).then(() => { console.log( `Wrote batch ${i + 1} of ${batchCount} with ${batch.length} items.` ); }); }); // Wait for all batch requests to resolve. await Promise.all(batchRequests); return {movieCount: movies.length}; }; /** * * @param {string} tableName * @param {{ * existingMovieName1: string, * existingMovieYear1: number }} keyUpdate1 * @param {{ * existingMovieName2: string, * existingMovieYear2: number }} keyUpdate2 */ const batchGetMovies = async (tableName, keyUpdate1, keyUpdate2) => { const Items = await ddbDocClient.send( new BatchExecuteStatementCommand({ Statements: [ { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [keyUpdate1.existingMovieName1, keyUpdate1.existingMovieYear1] }, { Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [keyUpdate2.existingMovieName2, keyUpdate2.existingMovieYear2] } ] } ) ) return Items }; /** * * @param {string} tableName * @param {{ * existingMovieName1: string, * existingMovieYear1: number, * newProducer1: string }} keyUpdate1 * @param {{ * existingMovieName2: string, * existingMovieYear2: number, * newProducer2: string }} keyUpdate2 */ const batchUpdateMovies = async ( tableName, keyUpdate1, keyUpdate2 ) => { await ddbDocClient.send( new BatchExecuteStatementCommand({ Statements: [ { Statement: "UPDATE " + tableName + " SET Producer=? where title=? and year=?", Parameters: [keyUpdate1.newProducer1, keyUpdate1.existingMovieName1, keyUpdate1.existingMovieYear1 ], }, { Statement: "UPDATE " + tableName + " SET Producer=? where title=? and year=?", Parameters: [ keyUpdate2.newProducer2, keyUpdate2.existingMovieName2, keyUpdate2.existingMovieYear2 ], } ], }) ); }; /** * * @param {string} tableName * @param {{ existingMovieName1: string, existingMovieYear1: number }} key1, * @param {{ existingMovieName2: string, existingMovieYear2: number}} key2 */ const batchDeleteMovies = async (tableName, key1, key2) => { await ddbDocClient.send( new BatchExecuteStatementCommand({ Statements: [ { Statement: "DELETE FROM " + tableName + " where title=? and year=?", Parameters: [key1.existingMovieName1, key1.existingMovieYear1], }, { Statement: "DELETE FROM " + tableName + " where title=? and year=?", Parameters: [key2.existingMovieName2, key2.existingMovieYear2], }, ], })) }; /** * * @param {string} tableName * @param {{ newMovieName1: string, newMovieYear1: number }} key1, * @param {{ newMovieName2: string, newMovieYear2: number }} key2 */ const batchPutItems = async (tableName, key1, key2) => { const command = new BatchExecuteStatementCommand({ Statements: [ { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [key1.newMovieName1, key1.newMovieYear1], }, { Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [key2.newMovieName2, key2.newMovieYear2], } ] }) await ddbDocClient.send(command); }; /** * * @param {{ title: string, info: { plot: string, rank: number }, year: number }[]} movies */ const logMovies = (Items) => { console.log("Success. The query return the following data."); for (let i = 0; i < Items.Responses.length; i++) { console.log(Items.Responses[i].Item); } }; /** * * @param {*} tableName */ const deleteTable = async (tableName) => { await ddbDocClient.send(new DeleteTableCommand({TableName: tableName})); await waitUntilTableNotExists( { client: ddbClient, maxWaitTime: 10, maxDelay: 2, minDelay: 1, }, {TableName: tableName} ); }; export const runScenario = async ({ tableName, newMovieName1, newMovieYear1, newMovieName2, newMovieYear2, existingMovieName1, existingMovieYear1, existingMovieName2, existingMovieYear2, newProducer1, newProducer2, moviesPath }) => { await createTable(tableName); console.log(`Creating table named: ${tableName}`); console.log(`\nTable created.`); console.log("\nWriting hundreds of movies in batches."); const {movieCount} = await batchWriteMoviesFromFile(tableName, moviesPath); console.log(`\nWrote ${movieCount} movies to database.`); console.log(`\nGetting "${existingMovieName1}" and "${existingMovieName2}"`); const originalMovies = await batchGetMovies( tableName, { existingMovieName1, existingMovieYear1 }, { existingMovieName2, existingMovieYear2 } ); logMovies(originalMovies); console.log(`\nUpdating "${existingMovieName1} and ${existingMovieName2} with new producers.`); await batchUpdateMovies(tableName, { existingMovieName1, existingMovieYear1, newProducer1 }, { existingMovieName2, existingMovieYear2, newProducer2 } ); //console.log(`\n"${existingMovieName1}" and ${existingMovieName2}" updated.`); console.log(`\nDeleting "${existingMovieName1}."`); await batchDeleteMovies(tableName, { existingMovieName1, existingMovieYear1 }, { existingMovieName2, existingMovieYear2 } ); console.log(`\n"${existingMovieName1} and ${existingMovieName2} deleted.`); console.log(`\nAdding "${newMovieName1}" and "${newMovieName2}" to ${tableName}.`); await batchPutItems(tableName, {newMovieName1, newMovieYear1}, {newMovieName2, newMovieYear2}); console.log("\nSuccess - single movie added."); console.log(`Deleting ${tableName}.`); await deleteTable(tableName); console.log(`${tableName} deleted.`); }; const main = async () => { const args = { tableName: "myNewTable", newMovieName1: "myMovieName1", newMovieYear1: 2022, newMovieName2: "myMovieName2", newMovieYear2: 2023, existingMovieName1: "This Is the End", existingMovieYear1: 2013, existingMovieName2: "Deep Impact", existingMovieYear2: 1998, newProducer1: "Amazon Movies", newProducer2: "Amazon Movies2", moviesPath: "../../../../../../resources/sample_files/movies.json", }; try { await runScenario(args); } catch (err) { // Some extra error handling here to be sure the table is cleaned up if something // goes wrong during the scenario run. console.error(err); const tableName = args.tableName; if (tableName) { console.log(`Attempting to delete ${tableName}`); await ddbClient .send(new DeleteTableCommand({TableName: tableName})) .then(() => console.log(`\n${tableName} deleted.`)) .catch((err) => console.error(`\nFailed to delete ${tableName}.`, err)); } } }; export {main};
  • API の詳細については、AWS SDK for JavaScriptAPI BatchExecuteStatementリファレンスのを参照してください

次のコード例は、以下の操作方法を示しています。

  • SELECT ステートメントを実行して項目を取得する。

  • INSERT 文を実行して項目を追加する。

  • UPDATE ステートメントを使用して項目を更新する。

  • DELETE ステートメントを実行して項目を削除する。

SDK forJavaScript (v3)
注記

他にもありますGitHub。用例一覧を検索し、AWS コード例リポジトリでの設定と実行の方法を確認してください。

クライアントを作成します。

// Create the DynamoDB service client module using ES6 syntax. import { DynamoDBClient } from "@aws-sdk/client-dynamodb"; import { DEFAULT_REGION } from "../../../../libs/utils/util-aws-sdk.js"; // Create an Amazon DynamoDB service client object. export const ddbClient = new DynamoDBClient({ region: DEFAULT_REGION });

ドキュメントクライアントを作成します。

// Create a service client module using ES6 syntax. import { DynamoDBDocumentClient } from "@aws-sdk/lib-dynamodb"; import { ddbClient } from "./ddbClient.js"; const marshallOptions = { // Whether to automatically convert empty strings, blobs, and sets to `null`. convertEmptyValues: false, // false, by default. // Whether to remove undefined values while marshalling. removeUndefinedValues: true, // false, by default. // Whether to convert typeof object to map attribute. convertClassInstanceToMap: false, // false, by default. }; const unmarshallOptions = { // Whether to return numbers as a string instead of converting them to native JavaScript numbers. wrapNumbers: false, // false, by default. }; // Create the DynamoDB document client. const ddbDocClient = DynamoDBDocumentClient.from(ddbClient, { marshallOptions, unmarshallOptions, }); export { ddbDocClient };

1 つの項目をクエリします。

*/ import fs from "fs"; import {splitEvery} from "ramda"; import { ExecuteStatementCommand, BatchWriteCommand } from "@aws-sdk/lib-dynamodb"; import { CreateTableCommand, DeleteTableCommand, waitUntilTableExists, waitUntilTableNotExists, } from "@aws-sdk/client-dynamodb"; import {ddbClient} from "../libs/ddbClient.js"; import {ddbDocClient} from "../libs/ddbDocClient.js"; /** * @param {string} tableName */ const createTable = async (tableName) => { await ddbClient.send( new CreateTableCommand({ TableName: tableName, AttributeDefinitions: [ { AttributeName: "year", AttributeType: "N", }, { AttributeName: "title", AttributeType: "S", } ], KeySchema: [ { AttributeName: "year", KeyType: "HASH", }, { AttributeName: "title", KeyType: "RANGE", }, ], // Enables "on-demand capacity mode". BillingMode: "PAY_PER_REQUEST" }) ); await waitUntilTableExists( {client: ddbClient, maxWaitTime: 15, maxDelay: 2, minDelay: 1}, {TableName: tableName} ); }; /** * * @param {string} tableName * @param {string} filePath * @returns { { movieCount: number } } The number of movies written to the database. */ const batchWriteMoviesFromFile = async (tableName, filePath) => { const fileContents = fs.readFileSync(filePath); const movies = JSON.parse(fileContents, "utf8"); // Map movies to RequestItems. const putMovieRequestItems = movies.map(({year, title, info}) => ({ PutRequest: {Item: {year, title, info}}, })); // Organize RequestItems into batches of 25. 25 is the max number of items in a batch request. const putMovieBatches = splitEvery(25, putMovieRequestItems); const batchCount = putMovieBatches.length; // Map batches to promises. const batchRequests = putMovieBatches.map(async (batch, i) => { const command = new BatchWriteCommand({ RequestItems: { [tableName]: batch, }, }); await ddbDocClient.send(command).then(() => { console.log( `Wrote batch ${i + 1} of ${batchCount} with ${batch.length} items.` ); }); }); // Wait for all batch requests to resolve. await Promise.all(batchRequests); return {movieCount: movies.length}; }; /** * * @param {string} tableName * @param {{ * existingMovieName: string, * existingMovieYear: number }} keyUpdate * @returns */ const getMovie = async (tableName, keyUpdate) => { const {Items} = await ddbDocClient.send( new ExecuteStatementCommand({ Statement: "SELECT * FROM " + tableName + " where title=? and year=?", Parameters: [keyUpdate.existingMovieName, keyUpdate.existingMovieYear] }) ) return Items }; /** * * @param {string} tableName * @param {{ * existingMovieName: string, * existingMovieYear: number, * newProducer: string }} keyUpdate */ const updateMovie = async ( tableName, keyUpdate ) => { await ddbClient.send( new ExecuteStatementCommand({ Statement: "UPDATE " + tableName + " SET Producer=? where title=? and year=?", Parameters: [ keyUpdate.newProducer, keyUpdate.existingMovieName, keyUpdate.existingMovieYear ], }) ); }; /** * * @param {string} tableName * @param {{ existingMovieName: string, existingMovieYear: number }} key */ const deleteMovie = async (tableName, key) => { await ddbDocClient.send( new ExecuteStatementCommand({ Statement: "DELETE FROM " + tableName + " where title=? and year=?", Parameters: [key.existingMovieName, key.existingMovieYear], }) ); }; /** * * @param {string} tableName * @param {{ newMovieName: string, newMovieYear: number }} key */ const putItem = async (tableName, key) => { const command = new ExecuteStatementCommand({ Statement: "INSERT INTO " + tableName + " value {'title':?, 'year':?}", Parameters: [key.newMovieName, key.newMovieYear], }) await ddbDocClient.send(command); }; /** * @param {{ title: string, info: { plot: string, rank: number }, year: number }} movie */ const logMovie = (movie) => { console.log(` | Title: "${movie.title}".`); console.log(` | Plot: "${movie.info.plot}`); console.log(` | Year: ${movie.year}`); console.log(` | Rank: ${movie.info.rank}`); }; /** * * @param {{ title: string, info: { plot: string, rank: number }, year: number }[]} movies */ const logMovies = (movies) => { console.log("\n"); movies.forEach((movie, i) => { if (i > 0) { console.log("-".repeat(80)); } logMovie(movie); }); }; /** * * @param {*} tableName */ const deleteTable = async (tableName) => { await ddbDocClient.send(new DeleteTableCommand({TableName: tableName})); await waitUntilTableNotExists( { client: ddbClient, maxWaitTime: 10, maxDelay: 2, minDelay: 1, }, {TableName: tableName} ); }; export const runScenario = async ({tableName, newMovieName, newMovieYear, existingMovieName, existingMovieYear, newProducer, moviesPath}) => { console.log(`Creating table named: ${tableName}`); await createTable(tableName); console.log(`\nTable created.`); console.log("\nWriting hundreds of movies in batches."); const {movieCount} = await batchWriteMoviesFromFile(tableName, moviesPath); console.log(`\nWrote ${movieCount} movies to database.`); console.log(`\nGetting "${existingMovieName}."`); const originalMovie = await getMovie( tableName, { existingMovieName, existingMovieYear } ); logMovies(originalMovie); console.log(`\nUpdating "${existingMovieName}" with a new producer.`); await updateMovie(tableName, { newProducer, existingMovieName, existingMovieYear }); console.log(`\n"${existingMovieName}" updated.`); console.log(`\nDeleting "${existingMovieName}."`); await deleteMovie(tableName, {existingMovieName, existingMovieYear}); console.log(`\n"${existingMovieName} deleted.`); console.log(`\nAdding "${newMovieName}" to ${tableName}.`); await putItem(tableName, {newMovieName, newMovieYear}); console.log("\nSuccess - single movie added."); console.log(`Deleting ${tableName}.`); await deleteTable(tableName); console.log(`${tableName} deleted.`); }; const main = async () => { const args = { tableName: "myNewTable", newMovieName: "myMovieName", newMovieYear: 2022, existingMovieName: "This Is the End", existingMovieYear: 2013, newProducer: "Amazon Movies", moviesPath: "../../../../../../resources/sample_files/movies.json", }; try { await runScenario(args); } catch (err) { // Some extra error handling here to be sure the table is cleaned up if something // goes wrong during the scenario run. console.error(err); const tableName = args.tableName; if (tableName) { console.log(`Attempting to delete ${tableName}`); await ddbClient .send(new DeleteTableCommand({TableName: tableName})) .then(() => console.log(`\n${tableName} deleted.`)) .catch((err) => console.error(`\nFailed to delete ${tableName}.`, err)); } } }; export {main};
  • API の詳細については、AWS SDK for JavaScriptAPI ExecuteStatementリファレンスのを参照してください