How to make a UUID in DynamoDB?

后端 未结 10 1296
佛祖请我去吃肉
佛祖请我去吃肉 2020-12-01 18:17

In my db scheme, I need a autoincrement primary key. How I can realize this feature?

PS For access to DynamoDB, I use dynode, module for Node.js.

10条回答
  •  旧巷少年郎
    2020-12-01 18:43

    Addition to @yadutaf's answer

    AWS supports Atomic Counters.

    Create a separate table (order_id) with a row holding the latest order_number:

    +----+--------------+
    | id | order_number |
    +----+--------------+
    |  0 |         5000 |
    +----+--------------+
    

    This will allow to increment order_number by 1 and get the incremented result in a callback from AWS DynamoDB:

    config={
      region: 'us-east-1',
      endpoint: "http://localhost:8000"
    };
    const docClient = new AWS.DynamoDB.DocumentClient(config); 
    
    let param = {
                TableName: 'order_id',
                Key: {
                    "id": 0
                },
                UpdateExpression: "set order_number = order_number + :val",
                ExpressionAttributeValues:{
                    ":val": 1
                },
                ReturnValues: "UPDATED_NEW"
            };
            
           
    docClient.update(params, function(err, data) {
       if (err) {
                    console.log("Unable to update the table. Error JSON:", JSON.stringify(err, null, 2));
       } else {
                    console.log(data);
                    console.log(data.Attributes.order_number); // <= here is our incremented result
        }
      });
    

提交回复
热议问题