【问题标题】:Dynamoose .update not working like the AWS javascript SDK. Do I need to use the SDK to update items?Dynamoose .update 不像 AWS javascript SDK 那样工作。我需要使用 SDK 来更新项目吗?
【发布时间】:2018-09-29 04:58:13
【问题描述】:

这是架构和模型

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 模型必须至少包含用作哈希、范围、全局二级索引、全局二级索引范围的键,并且令我惊讶的是,在初始输入后可能需要更改的任何键。

【问题讨论】:

  • 到底是什么问题?是给你一个错误信息还是什么?
  • 同样在格式化代码之后,我假设最后一个代码块中函数中result 之后的最后一个逗号是一个错字,但我不想编辑它以防万一实际上就是您的代码。您能否确认该逗号是否存在于您的代码中?如果不是,请编辑您的问题以将其删除:)
  • 删除了逗号。我认为这与脚本执行无关。问题是运行更新的dynamoose版本后dynamo中的条目保持不变
  • 没有错误信息。它只是不会改变发电机中的条目。

标签: node.js aws-sdk-js dynamoose


【解决方案1】:

目前有三种解决方法可以解决您的问题。

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

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

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


由于它看起来是 Dynamoose 中的一个错误,我已经提交了 issuePR 并将尝试尽快解决此问题。请随时关注该问题和 PR 以获取有关此问题的更新。我还将尝试在此处发布更新,其中包含任何重大更新。 此外,如果您想尝试修复它,那也很棒,只需从我的 PR 分支创建一个分支,尝试修复它并提交新 PR 并提及旧问题并在您的新 PR 中进行 PR!

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


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

【讨论】:

  • 感谢您的回复和您采取的其他措施。我查看了 npmdoc.github 上版本 7 的代码,并没有看到可能导致首次通过此限制的原因。这个周末我会深入研究一个更新的回购,看看我是否可以做出贡献。再次感谢您!
  • @Bryce 这已在Dynamoose PR #431 中修复。该 PR 已合并到 master 中,并将包含在下一个 Dynamoose 版本中。
猜你喜欢
  • 2022-09-26
  • 2012-11-24
  • 1970-01-01
  • 1970-01-01
  • 2021-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多