这是架构和模型
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 开发工具包版本的更新
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);
}
});
}
这是我认为在 Dynamoose 中的等效操作
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 开发工具包进行的更新采用主键和新键值对并添加到 dynamo 中的条目。这种行为可以通过 dynamoose 语法来实现吗?
编辑 2 - 当且仅当在传递给模型的模式中定义该键时,Dynamoose 才会将键值添加到 dynamo 中的现有条目。无论 saveUnkown: true 是什么,dynamoose 都不能使用 update 修改任何未在传递给模型的 dynamoose 模式中保存到 dynamo 的键值对。添加:fulfilled: { type: Boolean },
到 orederSchema 和以下代码添加 {fulfilled: true } 到 dynamo 条目。
Order.update({
orderId: 'e5aa37de-a4a9-456e-bea7-1471f404a424'
}, {
fulfilled: true
}, function(error, result) {
if (error) {
return console.log(error);
}
console.log(result);
});
这是预期的行为吗?对我来说,模型中未明确定义的数据将无法使用更新命令修改,这似乎很奇怪,特别是因为 SDK 没有此限制。
要点:dynamoose 模型必须至少包含用作哈希、范围、全局二级索引、全局二级索引范围的键,并且令我惊讶的是,在初始输入后可能需要更改的任何键。