简体   繁体   English

在使用AWS.DynamoDB.DocumentClient更新第二个表之前,如何使用事务检查一个表中是否存在项?

[英]How can I use transactions to check if an item exists in one table before updating a second table using AWS.DynamoDB.DocumentClient?

I am new to DynamoDB. 我是DynamoDB的新手。 I have a DynamoDB table called 'kids-profiles' that lists child profiles for a user (primary partition key 'userId', primary sort key 'childId'). 我有一个名为'kids-profiles'的DynamoDB表,它列出了用户的子配置文件(主分区键'userId',主排序键'childId')。 I have a second table called 'kids-tasks' that lists tasks for a child (primary partition key 'childId', primary sort key 'taskId'). 我有一个名为'kids-tasks'的第二个表,它列出了一个孩子的任务(主分区键'childId',主分类键'taskId')。 I would like to atomically add an item to the 'kids-tasks' table only if the 'childId' exists in the 'kids-profiles' table. 我希望只有在'kids-profiles'表中存在'childId'时,才能将项目原子地添加到'kids-tasks'表中。

I am trying to use the transactWrite method of the AWS.DynamoDB.DocumentClient class ( https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html ) to achieve this, but I can't get it to work. 我正在尝试使用AWS.DynamoDB.DocumentClient类( https://docs.aws.amazon.com/AWSJavaScriptSDK/latest/AWS/DynamoDB/DocumentClient.html )的transactWrite方法来实现此目的,但我不能让它工作。 First, when I try to add a ConditionCheck to the TransactionItems, it says "TransactItems can only contain one of Check, Put, Update or Delete". 首先,当我尝试将ConditionCheck添加到TransactionItems时,它说“TransactItems只能包含Check,Put,Update或Delete之一”。 So I have changed "ConditionCheck" to "Check" instead, assuming that is correct even though it is not mentioned in the documentation. 所以我已经将“ConditionCheck”改为“Check”,假设即使文档中没有提到它也是正确的。 Second, when I do get it to execute, it adds the task to the 'kids-tasks' table regardless of whether or not the 'childId' exists in the 'kids-profiles' table. 其次,当我确实执行它时,它会将任务添加到'kids-tasks'表中,而不管'children-profiles'表中是否存在'childId'。

    const params = {
        TransactItems: [ 
            {
                Check: {
                    TableName: "kids-profiles",
                    Key: {
                        userId:   userId,
                        childId:  childId,
                    },
                    ConditionExpression: "attribute_exists(childId)",
                },
                Put: {
                    TableName: "kids-tasks",
                    Item: {
                        childId:  childId,
                        taskId:   uuid.v1(),
                        title:    title,
                    }
                }
            }
        ]
    };

    try 
    {
        await dynamoDb.transactWrite(params).promise();
        console.log("Success!");
    } 
    catch (error) 
    {
        console.log("Error");
    }

Divide objects into individual Operations. 将对象划分为单独的操作。

 const params = {
     TransactItems: [ 
         {
            ConditionCheck: {
                ...
            },
+        }, // <------ !!!!!
+        {
             Put: {
                ...
         }
     ]
 };

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 TypeError:AWS.DynamoDB.DocumentClient不是构造函数 - TypeError: AWS.DynamoDB.DocumentClient is not a constructor 在 NodeJS 中使用 AWS.DynamoDB.DocumentClient 的排序键以 substring 开头的 get() 条目的正确语法是什么? - What is the proper syntax to get() entries where the sort key begins with a substring using AWS.DynamoDB.DocumentClient in NodeJS? TypeError:使用 Jest 进行测试时,AWS.DynamoDB.DocumentClient 不是构造函数 - TypeError: AWS.DynamoDB.DocumentClient is not a constructor when testing with Jest PHP 在更新 SQL 表之前检查文件是否存在 - PHP check if file exists before updating SQL table 使用适用于 Node.js 的 AWS 开发工具包将项目放在 DynamoDB 表上 - Put item on DynamoDB table using AWS SDK for Node.js 我需要创建一个函数,在将项目添加到第一个表之前,该函数必须检查第二个表是否不包含相同的项目 - I need to create a function that before adding an item to the first table the function must check if the second table does not contain the same item AWS DynamoDB DocumentClient可以处理本机Javascript Date对象吗? - Can AWS DynamoDB DocumentClient handle native Javascript Date objects? AWS Lambda函数如何更新DynamoDB表中的所有记录? - How can an AWS Lambda function update all records in a DynamoDB table? 如何在函数外使用 DynamoDB DocumentClient().get() 数据 - How to use DynamoDB DocumentClient().get() data outside of function 如何使用 Javascript 将新的列表变量插入到现有的 DynamoDB 表项中? - How do I insert a new list variable into an existing DynamoDB Table Item using Javascript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM