Node.js 更新不像AWS javascript SDK那样工作。我是否需要使用SDK更新项目?
下面是模式和模型Node.js 更新不像AWS javascript SDK那样工作。我是否需要使用SDK更新项目?,node.js,aws-sdk-js,dynamoose,Node.js,Aws Sdk Js,Dynamoose,下面是模式和模型 const orderSchema = new Schema({ orderId: { type: String, required: true, hashKey: true, }, serialNumberEnd: { type: Number, }, productName: { type: String, required: true,
const orderSchema = new Schema({
orderId: {
type: String,
required: true,
hashKey: true,
},
serialNumberEnd: {
type: Number,
},
productName: {
type: String,
required: true,
index: {
global: true,
rangeKey: 'serialNumberEnd',
name: 'productNameIndex',
project: false,
throughput: 1,
},
},
}, {
throughput: {
read: 1,
write: 1
},
timestamps: true,
saveUnknown: true,
}, );
const Order = dynamoose.model('Order-inv-dev', orderSchema, {
update: true
});
module.exports = Order;
以下是AWS SDK版本的更新
function updateItem() {
var table = "Order-inv-dev";
var params = {
TableName: table,
Key: {
"orderId": "1161a35c-afd7-4523-91c0-9ee397d89058",
},
UpdateExpression: "set fulfilled = :fulfilled",
ExpressionAttributeValues: {
":fulfilled": true,
},
ReturnValues: "UPDATED_NEW"
};
dynamodb.update(params, function(err, data) {
if (err) {
console.log(err);
} else {
console.log(data);
}
});
}
这是我认为在发电机中的等效作用
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
fulfilled: true
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
使用dynamoose实现这一点的唯一方法是重新创建整个条目并使用newOrder(replacementObject).save()。对我可能缺少的东西有什么想法吗?使用流畅的dynamoose语法获取orderId,然后将其放入AWS SDK语法中以添加布尔键值对,这感觉很愚蠢。感谢您的阅读和任何帮助
编辑:
当我使用相同的语法,而不是尝试更新现有的字符串时,它可以工作
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
productName: 'blue tags'
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
但当我试图修改现有的布尔值时,它不会从true变为false
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
tag: false
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
它也不会添加字符串类型的新键
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
tag: false
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
为了通过dynamoose语法进行更新,它似乎只能更改dynamo中存储的现有字符串。它不会将新键添加为字符串或布尔值,也不会更改现有的布尔值。通过AWS SDK进行的更新采用主键和新的键值对,并添加到dynamo中的条目中。这种行为可以通过dynamoose语法实现吗
edit2-Dynamoose将向dynamo中的现有条目添加一个键值,当且仅当该键值在传递给模型的模式中定义时。任何保存到dynamo的键值对,如果不在传递给模型的dynamoose架构中,则dynamoose不能使用update修改该键值对,而不管saveUnknown:true。
添加:completed:{type:Boolean},
到orederSchema,下面的代码将{completed:true}添加到dynamo条目
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
fulfilled: true
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
这是预期的行为吗?我觉得奇怪的是,模型中没有明确定义的数据不能用update命令修改,特别是因为SDK没有这个限制
要点:dynamoose模型必须至少包含要用作哈希、范围、全局二级索引、全局二级索引范围的键,以及在初始输入后可能需要更改的任何键。目前有三种解决方案
model.save()
将项目保存回DynamoDBsaveUnknown
设置为true
I 100%希望您的代码正常工作。虽然,我刚刚测试了它,但它不起作用。因此,它是发电机中的一只虫子
由于它看起来是Dynamoose中的一个bug,我已经提交了一个和,并将尝试尽快解决这个问题。请随时关注该问题和PR,以获取此方面的更新。我还将尝试在这里发布一个更新,其中包含任何主要的更新。另外,如果你想尝试修复它,那也太棒了,只需在我的公关分支上做一个分支,尝试修复它并提交一份新的公关,在你的新公关中提及旧问题和公关 从技术上讲,将所有内容都包含在模式和模型中是最佳实践。这是Dynamoose的主要目标之一,它是DynamoDB的建模工具。因此,有一个更严格的系统来定义模型的结构是Dynamoose的目标。但是,由于您已将
saveUnknown
设置为true
,因此应该保存这些属性
更新此问题已在中修复。在撰写本文时,PR已合并到master中,并将包含在下一版本的Dynamoose(v1.0.0)中。到底是什么问题?它是给你一个错误信息还是什么?同样在格式化你的代码之后,我假设最后一个代码块中函数中
result
后面的最后一个逗号是一个拼写错误,但我不想编辑它,以防你的代码就是这样的。您能确认代码中是否存在逗号吗?如果没有,请编辑您的问题以删除:)删除逗号。我不认为这是脚本执行的结果。问题是,在运行dynamoose版本的updateThere之后,dynamo中的条目保持不变。没有错误消息。这不会改变dynamo中的条目。感谢您的回复和您采取的其他操作。我已经查看了版本7的npmdoc.github上的代码,没有看到在第一次通过时造成这种限制的原因。这个周末,我会考虑一个更为及时的回购协议,看看我是否能出资。再次感谢你@布莱斯,这个问题已经解决了。该PR已合并到master中,并将包含在下一个Dynamoose版本中。