question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

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:closed
  • Created 5 years ago
  • Comments:9 (5 by maintainers)

github_iconTop GitHub Comments

8reactions
dabit3commented, Apr 21, 2020

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:

{
    "version": "2018-05-29",
    "operation": "UpdateItem",
    "key" : {
      "id" : $util.dynamodb.toDynamoDBJson($context.arguments.id)
    },
    "update": {
      "expression" : "set #upvotes = #upvotes + :updateValue",
      "expressionNames" : {
           "#upvotes" : "upvotes"
       },
       "expressionValues" : {
           ":updateValue" : { "N" : 1 }
       }
    }
}
2reactions
mikeparisstuffcommented, May 17, 2022

@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.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Auto-increment integer in dynamodb - Stack Overflow
to atomically increment the integer stored in the CounterTable row designated by the partition key "auto-incrementing-counter". After the atomic ...
Read more >
How to Auto Increment In DynamoDB - Unbiased Coder
You can't really use auto incrementing for a primary key in DynamoDB simply because DynamoDB does not have any support for primary keys....
Read more >
Conflict Detection and Sync - AWS AppSync
Versions are automatically incremented by AppSync during write operations and should not be modified by clients or outside of a resolver configured with...
Read more >
Reliable Auto-Incrementing Integers in DynamoDB
DynamoDB does not support auto-incrementing IDs for items. They can be a performance bottleneck at scale, and most workloads are better off with...
Read more >
Autogenerate an ID Field in GraphQL with AppSync Resolvers
In this lesson we'll take a look at how resolvers work in AWS AppSync. We'll also update a resolver to autogenerate an ID...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found