From d569b02ef8248c7b3805b116b7307189ab6b1ac5 Mon Sep 17 00:00:00 2001 From: Truong Ma Phi Date: Wed, 23 Sep 2020 23:35:55 +0700 Subject: [PATCH] feat: how to implement auto increment counter in DynamoDB --- .../incremental-counter.json | 19 +++++++++ dynamodb-incremental-counter/readme.md | 39 +++++++++++++++++++ .../update-and-get-current-value-counter.json | 15 +++++++ 3 files changed, 73 insertions(+) create mode 100644 dynamodb-incremental-counter/incremental-counter.json create mode 100644 dynamodb-incremental-counter/readme.md create mode 100644 dynamodb-incremental-counter/update-and-get-current-value-counter.json diff --git a/dynamodb-incremental-counter/incremental-counter.json b/dynamodb-incremental-counter/incremental-counter.json new file mode 100644 index 0000000..4ea0be1 --- /dev/null +++ b/dynamodb-incremental-counter/incremental-counter.json @@ -0,0 +1,19 @@ +{ + "TableName": "Counter", + "KeySchema": [ + { + "AttributeName": "CounterEntity", + "KeyType": "HASH" + } + ], + "AttributeDefinitions": [ + { + "AttributeName": "CounterEntity", + "AttributeType": "S" + } + ], + "ProvisionedThroughput": { + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + } +} diff --git a/dynamodb-incremental-counter/readme.md b/dynamodb-incremental-counter/readme.md new file mode 100644 index 0000000..19ea122 --- /dev/null +++ b/dynamodb-incremental-counter/readme.md @@ -0,0 +1,39 @@ +# DynamoDB cloud native incremental counter +---------- + +## Create table for counter + +```bash +aws dynamodb create-table --cli-input-json file://dynamodb-incremental-counter/incremental-counter.json --endpoint-url http://localhost:8000 +{ + "Attributes": { + "CreationDateTime": "2020-09-23T23:35:02.649000+07:00", + "ProvisionedThroughput": { + "LastIncreaseDateTime": "1970-01-01T07:00:00+07:00", + "LastDecreaseDateTime": "1970-01-01T07:00:00+07:00", + "NumberOfDecreasesToday": 0, + "ReadCapacityUnits": 1, + "WriteCapacityUnits": 1 + }, + "TableSizeBytes": 0, + "ItemCount": 0, + "TableArn": "arn:aws:dynamodb:ddblocal:000000000000:table/Counter" + } +} +``` + +## Update and get the last counter item value + +```bash +aws dynamodb update-item --cli-input-json file://dynamodb-incremental-counter/update-and-get-current-value-counter.json --endpoint-url http://localhost:8000 +{ + "Attributes": { + "CounterEntity": { + "S": "Entity" + }, + "currentValue": { + "N": "1" + } + } +} +``` diff --git a/dynamodb-incremental-counter/update-and-get-current-value-counter.json b/dynamodb-incremental-counter/update-and-get-current-value-counter.json new file mode 100644 index 0000000..d229245 --- /dev/null +++ b/dynamodb-incremental-counter/update-and-get-current-value-counter.json @@ -0,0 +1,15 @@ +{ + "TableName": "Counter", + "Key": { + "CounterEntity": { + "S": "Entity" + } + }, + "UpdateExpression": "ADD currentValue :count", + "ExpressionAttributeValues": { + ":count": { + "N": "1" + } + }, + "ReturnValues": "ALL_NEW" +}