我正在处理一个dynamoDB更新的用例:
- 动态更新(如果存在则更新,如果不存在则插入)项,而不硬编码项的组件。
- 使用DynamoDB文档客户端简化
- 在同一个原子操作中,更新一个简单的计数器
我从Daniel Barrel在https://stackoverflow.com/a/63511693/15369972上提供的一个很好的实用方法开始,该方法提供了一个使用动态值进行更新的通用实用方法,但没有原子计数器。
我尝试通过在加载动态值后将计数器及其递增器添加到参数对象中来添加原子计数器功能,但是在更新时计数器中获得静态值,而不是每次调用递增1的值。
哪里出了问题?我用一个表名、一个动态javascript对象和一个包含哈希值和排序键的数组调用修改后的更新函数:
await update(tableName, jsonObjectToStore, ['myHashKey', 'mySortKey'])
修改后的更新方法不是递增的,是:
async function update (tableName, item, idAttributeNames) {
var params = {
TableName: tableName,
Key: {},
ExpressionAttributeValues: {},
ExpressionAttributeNames: {},
UpdateExpression: "",
ReturnValues: "UPDATED_NEW"
};
for (const attname of idAttributeNames) {
params["Key"][attname] = item[attname];
}
let prefix = "set ";
let attributes = Object.keys(item);
for (let i=0; i<attributes.length; i++) {
let attribute = attributes[i];
if (!idAttributeNames.includes(attribute)) {
params["UpdateExpression"] += prefix + "#" + attribute + " = :" + attribute;
params["ExpressionAttributeValues"][":" + attribute] = item[attribute];
params["ExpressionAttributeNames"]["#" + attribute] = attribute;
prefix = ", ";
}
}
// Add the counter
params["UpdateExpression"] += ", #nImports = :nImports + :incr";
console.log(params["UpdateExpression"])
console.log(params["ExpressionAttributeValues"])
params["ExpressionAttributeValues"][":incr"] = 1;
params["ExpressionAttributeValues"][":nImports"] = 0;
console.log(params["ExpressionAttributeValues"])
console.log(params["ExpressionAttributeNames"])
params["ExpressionAttributeNames"]["#nImports"] = 'nImports'
console.log(params["ExpressionAttributeNames"])
await docClient.update
return await docClient.update(params).promise();
}
与AWS支持部门合作寻找合理的解决方案。他们也不确定如何使用ddb文档客户端(与具有许多文档示例的低级客户端相反)执行原子计数器,但建议使用ADD命令,该命令具有对数字字段进行原子更新的副作用。
因此,在下面的示例中,我们从要存储的对象构造动态更新,然后在更新表达式中附加ADD语句(不带逗号!),并向nImports的ExpressionAttributeValues添加实际上是数字增量的东西。像这样,这应该是一个完整的工作lambda示例。这里有一些console.log语句来显示正在发生的事情:const AWS = require('aws-sdk');
const docClient = new AWS.DynamoDB.DocumentClient();
async function update (tableName, item, idAttributeNames) {
var params = {
TableName: tableName,
Key: {},
ExpressionAttributeValues: {},
ExpressionAttributeNames: {},
UpdateExpression: "",
ReturnValues: "UPDATED_NEW"
};
for (const attname of idAttributeNames) {
params["Key"][attname] = item[attname];
}
let prefix = "set ";
let attributes = Object.keys(item);
for (let i=0; i<attributes.length; i++) {
let attribute = attributes[i];
if (!idAttributeNames.includes(attribute)) {
params["UpdateExpression"] += prefix + "#" + attribute + " = :" + attribute;
params["ExpressionAttributeValues"][":" + attribute] = item[attribute];
params["ExpressionAttributeNames"]["#" + attribute] = attribute;
prefix = ", ";
}
}
console.log('params before adding atomic counter is:', params)
// Add the counter using the ADD syntax
params["UpdateExpression"] += " ADD #nImports :nImports"
params["ExpressionAttributeValues"][":nImports"] = 1;
params["ExpressionAttributeNames"]["#nImports"] = 'nImports'
console.log('params after adding atomic counter is:', params)
try {
const result = await docClient.update(params).promise();
console.log('after await, result is ', result);
return result;
} catch (err) {
console.log('err is ', err)
}
};
exports.handler = async (event) => {
const item = {title: 'sometitle', site_url: "www.amazon.com", key: "G"};
const body = await update('test_table', item, ['title', 'site_url']);
const response = {
statusCode: 200,
body: JSON.stringify(body),
};
return response;
}
好心的AWS工作人员做了更多的挖掘,并指出了初始代码中的一个错误,在纠正后,应该使用SET操作符按预期增加。
基本上,原始代码没有正确地针对增量变量。因此,我们添加自增变量的正确版本应该是:
console.log('params before adding atomic counter is:', params)
// Add the counter
params["UpdateExpression"] += ", #nImports = #nImports + :incr";
params["ExpressionAttributeValues"][":incr"] = 1;
//params["ExpressionAttributeValues"][":nImports"] = 0;
params["ExpressionAttributeNames"]["#nImports"] = 'nImports'
console.log('params after adding atomic counter is:', params)```
I'm sticking with the original ADD answer because I like the differentiation it gives over the properties inserted by the SET, but both seem valid and I wanted to include the correction as well