📜  查找 dynamodb lambda 中的所有数据 - Javascript 代码示例

📅  最后修改于: 2022-03-11 15:03:33.303000             🧑  作者: Mango

代码示例1
//NODE JS
const AWS = require('aws-sdk')
const dynamoDB = new AWS.DynamoDB({ region: 'ap-south-1', apiVersion: '2019.11. 21' })
var docClient = new AWS.DynamoDB.DocumentClient()

exports.handler = async (event, context, cb) => {
  //Getting all data
     let params = {
         TableName: 'TABLE_NAME'
     }
     let results = await docClient.scan(params).promise()
      const response = {
          "statusCode": 200,
          "success": true,
          "message": results.Items,
          "headers": {
          "Access-Control-Allow-Origin": "*",
          "Access-Control-Allow-Credentials": true
          }
      };
      return cb(null, response)
  
  //Query in dynamoDB
  let params = {
    TableName: 'TABLE_NAME',
    FilterExpression: "#someKEY = :someKEY_val", // can use 'AND' 'OR' here
    ExpressionAttributeNames: {
      "#someKEY": "userId", // its value should be key you want to lookup
    },
    ExpressionAttributeValues: { ":someKEY_val": "1" } // Value to match
  }
  let results = await docClient.scan(params).promise()
  const response = {
    "statusCode": 200,
    "success": true,
    "message": results.Items,
    "headers": {
      "Access-Control-Allow-Origin": "*",
      "Access-Control-Allow-Credentials": true
    }
  };
  return cb(null, response)
}