Skip to content

Instantly share code, notes, and snippets.

@jitunayak
Created December 20, 2022 18:44
Show Gist options
  • Save jitunayak/64937cb23ffc5caf56c43c2dd40c77cf to your computer and use it in GitHub Desktop.
Save jitunayak/64937cb23ffc5caf56c43c2dd40c77cf to your computer and use it in GitHub Desktop.
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