Auto increment number (non primary key) with AppSync and DynamoDB
See original GitHub issue** Which Category is your question related to? ** API
** What AWS Services are you utilizing? ** AppSync, DynamoDB
** Provide additional details e.g. code snippets ** Let’s say I have below schema:
type Item @model {
category: String!
name: String!
tagNumber: String!
}
What I want to implement:
id | category | name | tagNumber |
---|---|---|---|
3c174388-054b-431f-a0f4-d835acdd273a | food | cheese cake | 1.1 |
63b189d0-597e-47f3-8dcf-996da87101a8 | book | harry porter | 2.1 |
87749278-acae-4de1-b0ff-84d6b86506cc | food | steak | 1.2 |
fd2334a9-ff76-4ae2-9ca5-834d998f91e7 | book | bad blood | 2.2 |
If I want to add a new item, which is a book called Joke, it should have a tagNumber 2.3, because it belongs to the 2nd category in the table, and it is the 3rd item in that category.
Why do I want to implement this? Because the tagNumber is easiest for people, not computers, to look up items.
Is there a way to implement this with AppSync and DynamoDB? I can think of the hard way to do it: use pipeline resolver. If the category specified exists, find an item with the latest createdAt
timestamp in that category with tagNumber x.y, and assign x.<y+1> to the new item. If the category specified doesn’t exist, count categories N, and assign <N+1>.1 to the new item.
Issue Analytics
- State:
- Created 5 years ago
- Comments:9 (5 by maintainers)
Coming back to this, if anyone wants a working example of an atomic counter (increment, decrement) with DynamoDB and AppSync, here’s something I’m using:
@YikSanChan We are working on designs that will make working with pipeline resolvers a lot easier. Please see aws-amplify/amplify-category-api#430 and offer feedback. It is going to go through another round of review and design and it would be great to cover this use case.