在 DynamoDB 中建立和使用資料表 - AWS SDK for JavaScript

AWS SDK for JavaScript V3 API 參考指南詳細介紹了 AWS SDK for JavaScript 版本 3(V3)的所有 API 操作。

本文為英文版的機器翻譯版本,如內容有任何歧義或不一致之處,概以英文版為準。

在 DynamoDB 中建立和使用資料表

JavaScript code example that applies to Node.js execution

這個 Node.js 程式碼範例會說明:

  • 如何建立和管理用於從 DynamoDB 儲存和擷取資料的表格。

該方案

與其他資料庫系統類似,DynamoDB 會將資料儲存在表格中。DynamoDB 資料表是資料的集合,這些資料組織成類似於列的項目。若要在 DynamoDB 中儲存或存取資料,您可以建立和使用資料表。

在此範例中,您會使用一系列 Node.js 模組,透過 DynamoDB 表格執行基本作業。程式碼會使用 SDK JavaScript 來建立並使用用DynamoDB戶端類別的下列方法來處理資料表:

必要工作

若要設定和執行此範例,請先完成這些任務:

重要

這些示例使用電子信息密碼 6(ES6)。這需要 Node.js 版本 13.x 或更高版本。要下載並安裝最新版本的 Node.js,請參閱 Node.js 下載。

但是,如果您更喜歡使用 CommonJS 語法,請參閱JavaScript ES6/共同語法

注意

如需這些範例中使用之資料類型的詳細資訊,請參閱 Amazon DynamoDB 中支援的資料類型和命名規則

建立一個資料表

以檔名 create-table.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立 JSON 物件,其中包含建立資料表所需的參數,在此範例中包含每個屬性的名稱和資料類型、主要結構描述、資料表名稱和要佈建的傳輸量單位。呼叫 DynamoDB 服務物件的CreateTableCommand方法。

import { CreateTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new CreateTableCommand({ TableName: "EspressoDrinks", // For more information about data types, // see https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/HowItWorks.NamingRulesDataTypes.html#HowItWorks.DataTypes and // https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/Programming.LowLevelAPI.html#Programming.LowLevelAPI.DataTypeDescriptors AttributeDefinitions: [ { AttributeName: "DrinkName", AttributeType: "S", }, ], KeySchema: [ { AttributeName: "DrinkName", KeyType: "HASH", }, ], ProvisionedThroughput: { ReadCapacityUnits: 1, WriteCapacityUnits: 1, }, }); const response = await client.send(command); console.log(response); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node create-table.js

您可以在這裡找到這個範例程式碼 GitHub。

列出您的表

以檔名 list-tables.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立 JSON 物件,其中包含列出資料表所需的參數,在此範例中,列出的資料表數限制為 10。呼叫 DynamoDB 服務物件的ListTablesCommand方法。

import { ListTablesCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new ListTablesCommand({}); const response = await client.send(command); console.log(response); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node list-tables.js

您可以在這裡找到這個範例程式碼 GitHub。

說明資料表

以檔名 describe-table.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立 JSON 物件,其中包含描述 DynamoDB 服務物件DescribeTableCommand方法所需的參數。

import { DescribeTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new DescribeTableCommand({ TableName: "Pastries", }); const response = await client.send(command); console.log(`TABLE NAME: ${response.Table.TableName}`); console.log(`TABLE ITEM COUNT: ${response.Table.ItemCount}`); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node describe-table.js

您可以在這裡找到這個範例程式碼 GitHub。

刪除資料表

以檔名 delete-table.js 建立一個 Node.js 模組。請務必如先前所示設定 SDK,包括下載所需的用戶端和套件。若要存取 DynamoDB,請建立DynamoDB用戶端服務物件。建立 JSON 物件,其中包含刪除資料表所需的參數,在此範例中會包含要提供做為命令列參數的資料表名稱。呼叫 DynamoDB 服務物件的DeleteTableCommand方法。

import { DeleteTableCommand, DynamoDBClient } from "@aws-sdk/client-dynamodb"; const client = new DynamoDBClient({}); export const main = async () => { const command = new DeleteTableCommand({ TableName: "DecafCoffees", }); const response = await client.send(command); console.log(response); return response; };

若要執行範例,請在命令提示字元中輸入下列命令。

node delete-table.js

您可以在這裡找到這個範例程式碼 GitHub。