AWS DynamoDB

AWS DynamoDB

為一個NoSQL的結構,其中primary key是唯一的,也是查詢條件。

可以新增其他key為sort key。

但查詢時就必須提供所有key。

如果想再在未來新增查詢的Key可參考: GSI (Table點進去後按Indexes新增)

Global Secondary Indexes (GSI) allow you to query efficiently over any field (attribute) in your DynamoDB table. GSIs can treat any table attribute as a key, even attributes not present in all items.

步驟:

1.先設定好AMI,然後參考此文章,將AMI的key設定到電腦檔案中。

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/loading-node-credentials-shared.html

2.安裝aws-sdk

yarn add aws-sdk

3.對照下面表格記下自己的AWS服務位置

Code    Name
ap-northeast-1    Asia Pacific (Tokyo)
ap-southeast-1    Asia Pacific (Singapore)
ap-southeast-2    Asia Pacific (Sydney)
eu-central-1    EU (Frankfurt)
eu-west-1    EU (Ireland)
sa-east-1    South America (Sao Paulo)
us-east-1    US East (N. Virginia)
us-west-1    US West (N. California)
us-west-2    US West (Oregon)

4.建立一個Dynamo Table

新增

參考官方範例:

https://docs.aws.amazon.com/sdk-for-javascript/v2/developer-guide/dynamodb-example-table-read-write.html

會看到 putItem 而非 put,兩者的差別可參考:

https://stackoverflow.com/questions/33942945/error-invalidparametertype-expected-params-itempid-to-be-a-structure-in-dyn?utm_medium=organic&utm_source=google_rich_qa&utm_campaign=google_rich_qa

查詢

docClient.put改成 docClient.get 即可,並且把params的Item改為Key

查詢全部

使用Scan,並且不用指定Key

更新

需要加上幾個欄位在參數: UpdateExpression, ExpressionAttributeNames, ExpressionAttributeValues, ReturnValues

ReturnValues代表:

範例:

刪除

參數與新增相同

不錯文章:

https://medium.com/@kinvilhsiao/dynamodb-使用-nodejs-4a24c99ea751

Last updated

Was this helpful?