javascript - 如何创建 lambda 函数来更新 amplify 上的新记录?

标签 javascript reactjs aws-lambda database-trigger aws-amplify

如何使用 GraphQL 创建 lambda 函数来更新 amplify 项目上的新记录?

我有一个使用 api GraphQL 作为 api(也是 dynamoDB)的 amplify 项目。

我的应用程序是一个简单的待办事项列表,只是为了学习,所以我将待办事项保存在我的 dynamoDB 上,这就是我的架构:

type Todo @model @auth(rules: [{ allow: owner }]) {
    id: ID!
    title: String!
    status: String
}

当我创建一个新的 Todo 时,我只设置标题,而不是状态,我想使用 lambda 触发器函数使用默认值更新状态(我知道我不必这样做,但我'我正在尝试学习更多有关 amplify 的 lambda 函数)。

所以我按照 amplify docs 中的步骤操作,但现在我不知道下一步是什么(我不熟悉 aws lambda 函数)。

我想做的事情是:

for each new record
newRecord.status = 'To do'
newRecord.update()

最佳答案

当您首次创建 Todo 项目时,它会作为项目存储在 DynamodDB 表中。要更新待办事项项目,您需要更新 DynamoDB 表中的项目。

当您更新 DynamoDB 中的项目时,您需要使用 AWS 开发工具包来处理此问题。最简单的 sdk 使用是使用 AWS DynamoDB Document Client如果您在 lambda 函数中使用 nodejs。

// Load the AWS SDK for Node.js
var AWS = require('aws-sdk');
// Set the region
AWS.config.update({region: 'REGION'}); //replace Region with the region you are using ('EU-WEST-1')

// Create DynamoDB document client
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});


// put the the following code in the exports.handler in your lambda function
var params = {
  TableName: 'Todo-SOMERANDOMSTRING-ENV',//The name of your dynamodb table
  Key: {
    'id' : 'asasd123-asdsa1-12sdasads-12', // the id of your todo item
  },
  UpdateExpression: 'set status = :s',
  ExpressionAttributeValues: {
    ':s' : 'To do' // what the new status should be
  }
};
// and run the update function
docClient.update(params, function(err, data) {
  if (err) {
    console.log("Error", err);
  } else {
    console.log("Success", data);
  }
});

编辑

根据您的评论,我认为您可能位于 DynamoDB Lambda 触发器部分(?),并且您的样板 lambda 函数可能如下所示:

exports.handler = function (event, context) {
  console.log(JSON.stringify(event, null, 2));
  event.Records.forEach((record) => {
    console.log(record.eventID);
    console.log(record.eventName);
    console.log('DynamoDB Record: %j', record.dynamodb);
  });
  context.done(null, 'Successfully processed DynamoDB record');
};

我自己以前没有做过这种类型的触发器,所以我不完全确定记录中的数据是如何构造的,但我认为它可能可以像这样访问:

record.data.id
//or
record.Item.id

您可以通过以下方式找到这一点:转到 lambda 控制台,找到并打开您的 lambda 函数,转到“监控”,然后在 AWS 中打开“查看 CloudWatch 中的日志”,并在创建项目后检查 CloudWatch 日志。

假设它是 record.Item.id 您的 lambda 代码可能如下所示(未经测试):

var AWS = require('aws-sdk');
AWS.config.update({region: 'REGION'});
var docClient = new AWS.DynamoDB.DocumentClient({apiVersion: '2012-08-10'});

exports.handler = function (event, context) {
  console.log(JSON.stringify(event, null, 2));
     event.Records.forEach((record) => {
        var params = {
           TableName: 'YOUR-DB-TABLE-NAME',
           Key: {
              'id' : record.Item.id,
           },
           UpdateExpression: 'set status = :status',
           ExpressionAttributeValues: {
              ':status' : 'To do' // what the new status should be
           }
        };
        docClient.update(params, function(err, data) {
           if (err) {
              console.log("Error", err);
           } else {
              console.log("Success", data);
           }
        });
      });
   context.done(null, 'Successfully processed DynamoDB record');
 };

我认为这段代码并不完全完整,您可能必须更改“context.done”函数的工作方式/时间(因为我认为它将在代码完成更新项目之前运行),但它可能让你朝着正确的方向前进。

关于javascript - 如何创建 lambda 函数来更新 amplify 上的新记录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61325333/

相关文章:

javascript - 如何点击显示 :none element with puppeteer?

javascript - 通过键从JavaScript中的字典中获取值

javascript - 使 Alexa Skill Loop 成为语音功能

javascript - 子组件在 POST 请求后不重新渲染父组件

node.js - 是否可以从AWS Lambda调用Azure队列

javascript - 添加过滤器以在 Nunjucks 中动态格式化日期值

javascript - 终极版 : Accessing current state - best practice?

javascript - 使用 React 聚焦 div 元素

go - 从 Golang 中的 AWS Lambda 函数读取参数

mysql - Nodejs lambda函数超时