balmbees / dynamo-types

Typescript AWS DynamoDB ORM
231 stars 18 forks source link

Increment value in DynamoDB table #71

Open besasch88 opened 4 years ago

besasch88 commented 4 years ago

Hi! Thanks for the library! I've a question: Is it possible to update an item stored in the dynamoDB table incrementing a value without read it, update the value and finally write again.

Using the aws-sdk you can do something like it: dynamoDB.updateItem({ TableName: "Users", Key: { "UserId": { S: "c6af9ac6-7b61" } }, ExpressionAttributeValues: { ":inc": {N: "1"} }, UpdateExpression: "ADD loginCount :inc" }) Thanks!

mooyoul commented 4 years ago

You can use update query to increment attribute value: (To decrement value, Simply supply negative value)

// for HashPrimaryKey based Table
Table.primaryKey.update(hashKey, { attr: ["ADD", 1] }); // equivalent to "ADD attr 1"
// for FullPrimaryKey based Table
Table.primaryKey.update(hashKey, rangeKey, { attr: ["ADD", 1] }); // equivalent to "ADD attr 1"

FYI, All of maintainers of dynamo-types left Vingle, Inc. (which is owner of this repository.) Unfortunately, We couldn't get permissions from Company - We lost all permissions about this repository (including access credentials for accessing NPM).

So, We've forked this repo to serverless-seoul/dynamorm.

We won't make any further updates on this repository. Please leave an issue to forked repository if you have further questions. Thanks.

cc/ @breath103