Skip to content

Commit

Permalink
feat: how to implement auto increment counter in DynamoDB
Browse files Browse the repository at this point in the history
  • Loading branch information
trmaphi committed Sep 23, 2020
1 parent a8c6a8a commit d569b02
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 0 deletions.
19 changes: 19 additions & 0 deletions dynamodb-incremental-counter/incremental-counter.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"TableName": "Counter",
"KeySchema": [
{
"AttributeName": "CounterEntity",
"KeyType": "HASH"
}
],
"AttributeDefinitions": [
{
"AttributeName": "CounterEntity",
"AttributeType": "S"
}
],
"ProvisionedThroughput": {
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
}
}
39 changes: 39 additions & 0 deletions dynamodb-incremental-counter/readme.md
Original file line number Diff line number Diff line change
@@ -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"
}
}
}
```
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
{
"TableName": "Counter",
"Key": {
"CounterEntity": {
"S": "Entity"
}
},
"UpdateExpression": "ADD currentValue :count",
"ExpressionAttributeValues": {
":count": {
"N": "1"
}
},
"ReturnValues": "ALL_NEW"
}

0 comments on commit d569b02

Please sign in to comment.