Dynamoose .update 不像 AWS javascript SDK 那样工作。我需要使用 SDK 来更新项目吗?

Dynamoose .update not working like the AWS javascript SDK. Do I need to use the SDK to update items?

这是架构和模型

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);
        }
    });
}

这是我认为在 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 SDK 进行的更新采用主键和新的键值对,并添加到 dynamo 中的条目。这种行为可以用 dynamoose 语法实现吗?

编辑 2 - Dynamoose 将向 dynamo 中的现有条目添加一个键值,当且仅当该键在传递给模型的模式中定义时。无论 saveUnkown: true 是什么,任何在传递给模型的 dynamoose 模式中保存到 dynamo 的键值对都不能被 dynamoose 使用 update 修改。 添加: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 模型必须至少包含要用作散列、范围、全局二级索引、全局二级索引范围的键,以及令我惊讶的任何在初始输入后可能需要更改的键。

目前您的问题有三种变通方法。

  1. 将 属性 添加到您的架构中
  2. 获取项目,更新属性,然后使用 model.save() 将项目保存回 DynamoDB
  3. 为此直接使用 AWS SDK

第二个选项有其缺点,因为它实际上并未使用 DynamoDB 的更新功能,但可能是最干净的方法,具体取决于您的设置和目标。

第四个选项就是你在做什么。您的代码 应该 有效。因为您将 saveUnknown 设置为 true 我 100% 期望您的代码能够正常工作。虽然,我刚刚测试了它,但它不起作用。因此这是 Dynamoose 中的一个错误。


由于它看起来是 Dynamoose 中的一个错误,我已经提交了一个 issue and PR 并且会尽快解决这个问题。请随时关注该问题和 PR 以获取有关此的更新。我也会尝试 post 在这里更新任何主要更新。 此外,如果您想尝试修复它,那也很棒,只需从我的 PR 分支中创建一个分支,尝试修复它并提交新的 PR 并提及旧问题和新 PR 中的 PR!!

从技术上讲,最佳做法是将所有内容都包含在您的架构和模型中。这是 Dynamoose 的主要目标之一,它旨在成为 DynamoDB 的建模工具。因此,拥有一个必须定义模型结构的更严格的系统有点像 Dynamoose 的 objective。但由于您已将 saveUnknown 设置为 true,因此应该保存这些属性。


更新 这已在 Dynamoose PR #431 中修复。在撰写本文时,PR 已合并到 master 中,并将包含在 Dynamoose (v1.0.0) 的下一个版本中。