I want to get the value after the sequence is issued in DynamoDB.

Asked 2 years ago, Updated 2 years ago, 110 views

We are developing POST with atomic counter on Lambda+DynamoDB.
①There is a sequence table l license table, but I would like to put the value in the l license table after the sequence is issued.
"id": table.id defines it, but it doesn't seem to fit in.If you know how to do it, please let me know.

constaws=require('aws-sdk');
aws.config.update({region:'ap-norteast-1'});

const dynamicb=news.DynamoDB.DocumentClient({apiVersion:'2012-08-10'});
const table = 'license';
const sequenceTable='sequences';

// sequence processing
function sequence (license_id, callback) {
    const params = {
        TableName: "sequences",
        Key: {
            name: "license"
        },
        UpdateExpression: "ADD current_number:val",
        ExpressionAttributeValues: {
            ":val":1
        },
        ReturnValues: "UPDATED_NEW"
    };
    dynamicb.update(params, function(err,data){
        let id;
        if(err){
            console.error('Unable to update item.Error JSON:', JSON.stringify(err, null, 2));
        } else{
            console.log('UpdateItem succeeded:',JSON.stringify(data,null,2));
            id=data.Attributes.current_number;
        }
        callback (id);
    });
}

// POST processing
const params = {
    Item: {
        "licensekey": "dapfddd4daaaf",
        "status": "Not registered",
        "staff_mail": "[email protected]",
        "license_create_date": Date.now(),
        "id": table.id
    },
    ReturnConsumedCapacity: "TOTAL",
    TableName: "license"
};

//let responseData={};
exports.handler=function(event, context, callback){
    dynamicb.put(params, function(err,data){
        console.log("callback response" + JSON.stringify(event['body-json'));
        sequence(sequenceTable, function(id){
        console.log("sequence:"+id);
        });
        callback(null, {"message":"success"});
        
        if(err)console.log(err,err.stack);
        else return data;
    });
};

aws amazon-dynamodb

2022-09-30 19:52

1 Answers

Since you need to put an id in the data to put after getting the atomic counter value, calling sequence in the post-put function will not set the value.

sequence(sequenceTable, function(id){
  // Now set the id to Item.id in params and call put.

I think it would be better if


2022-09-30 19:52

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.