View a markdown version of this page

移轉至 DAX Node.js SDK V3 - Amazon DynamoDB

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

移轉至 DAX Node.js SDK V3

此移轉指南將協助您轉換現有的 DAX Node.js 應用程式。新的 SDK 需要 Node.js 18 或更高版本,並在建構 DynamoDB Accelerator 程式碼方面引入幾項重要變更。本指南將逐步解說其中的主要差異,包括語法變更、新的匯入方法,以及更新的非同步程式設計模式。

使用 V2 Node.js DAX

const AmazonDaxClient = require('amazon-dax-client'); const AWS = require('aws-sdk'); var region = "us-west-2"; AWS.config.update({ region: region, }); var client = new AWS.DynamoDB.DocumentClient(); if (process.argv.length > 2) { var dax = new AmazonDaxClient({ endpoints: [process.argv[2]], region: region, }); client = new AWS.DynamoDB.DocumentClient({ service: dax }); } // Make Get Call using Dax var params = { TableName: 'TryDaxTable', pk: 1, sk: 1 } client.get(params, function (err, data) { if (err) { console.error( "Unable to read item. Error JSON:", JSON.stringify(err, null, 2) ); } else { console.log(data); } });

使用 V3 Node.js DAX

使用 DAX 時建議採用 Node.js V3 Node 版本 18 或更高版本。若要移至 Node 18,請使用下列項目:

import { DaxDocument } from '@amazon-dax-sdk/lib-dax'; import { DynamoDBDocument } from '@aws-sdk/lib-dynamodb'; import { DynamoDBClient } from '@aws-sdk/client-dynamodb'; let client: DynamoDBDocument | DaxDocument = DynamoDBDocument.from( new DynamoDBClient({ region: 'us-west-2' }) ); if (process.argv.length > 2) { client = new DaxDocument({ endpoints: [process.argv[2]], region: 'us-west-2', }); } const params = { TableName: 'TryDaxTable', Key: { pk: 1, sk: 1 }, }; try { const results = await client.get(params); console.log(results); } catch (err) { console.error(err); }