0

I want the following table structure, to store an auto increment a row's pid* attribute

          | id                                   | timstamp    | pid*
          | 00000000-1111-2222-93bb-0371fcb45674 | 0           | 1
          | 00000000-1111-2222-93bb-ee742a825e88 | 1           | 2
          | 00000000-1111-2222-93bb-bfac8753c0ba | 2           | 3
PutItem ->  autoId()                             | timestamp() | max(pid) + 1 = 4 ??

For PutItem operation, Is something like the following 1) possible, and 2) acceptable in DynamoDB land?

"pid" : $util.dynamodb.toDynamoDBJson(records.findMax(pid) + 1) # just pseudo code

3) How might one implement the above using DyanmoDB resolver mapper template?

Use case:

I'm trying to use AWS DynamoDB to back GraphQL, managed by AWS AppSync, the following is the request mapping template for Mutation.createFoo

{
    "version" : "2018-05-29",
    "operation" : "PutItem",
    "key" : {
        "id": $util.dynamodb.toDynamoDBJson($util.autoId()),
    },
    "attributeValues" : {
        "timestamp" :  $util.dynamodb.toDynamoDBJson($util.time.nowEpochMilliSeconds()),
        "pid" :  $util.dynamodb.toDynamoDBJson($ctx.args.input.pid), # currently client provided but it is not acceptable
        ...
    }
}

The primary key id is an UUID auto-generated by DynamoDB which is fine. But our use-case requires a incrementing pid for each new Foo in our FooTable. The business model requires at least for show a unique pid, while under the hood, queries like GetItem the UUID and timestamp will be used instead and business as usual.

I'm also weary to call for a change in business model because of an implementation detail issue.

References:

Andrew Lam
  • 1,371
  • 17
  • 35
  • 1
    There are some good discussions in these threads: https://stackoverflow.com/questions/37072341/how-to-use-auto-increment-for-primary-key-id-in-dynamodb https://stackoverflow.com/questions/11721308/how-to-make-a-uuid-in-dynamodb https://stackoverflow.com/questions/13264236/dynamodb-auto-incremented-id-server-time-ios-sdk - in general, it's usually not a good idea, but see the responses on these questions for thoughts on how it can be done. – parkerfath Sep 04 '19 at 01:26
  • multiple questions: do you need it to be auto-incrementing strictly by one? can there be holes in the pid value, e.g. 1, 2, 3, 8? what happens when an item is deleted? are the `pid` values need to be adjusted to avoid "holes"? alternatively, if holes are allowed, could it just be a unique numerical value? how many items are you going to store there? I guess many of these questions boil down to "how is pid used by the application?" – Itay Maman Sep 05 '19 at 07:46

0 Answers0