Created
December 20, 2022 18:44
-
-
Save jitunayak/64937cb23ffc5caf56c43c2dd40c77cf to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
const AWS = require("aws-sdk"); | |
const dynamo = new AWS.DynamoDB.DocumentClient({ apiVersion: '2012-08-10', region: 'ap-south-1' }); | |
async function insert(id, key) { | |
const param = { | |
TableName: 'event', | |
Item: { | |
PartKey: id, | |
SortKey: "v" + key, | |
version: key | |
} | |
} | |
const result = await dynamo.put(param).promise() | |
console.log(result) | |
} | |
async function incrementVersion(id) { | |
const param = { | |
TableName: 'event', | |
Key: { | |
PartKey: id, | |
SortKey: "v0" | |
}, | |
UpdateExpression: "SET version = if_not_exists(version, :initial) + :num", | |
ExpressionAttributeValues: { | |
":num": 1, | |
":initial": 0, | |
}, | |
ReturnValues: 'UPDATED_NEW' | |
} | |
const result = await dynamo.update(param).promise() | |
await insert(id, result.Attributes.version) | |
console.log(result) | |
} | |
// insert('j-1') | |
incrementVersion('j-2') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment