Dynamo数据库查询过滤器Node.js [英] Dynamo DB Query Filter Node.js

查看:97
本文介绍了Dynamo数据库查询过滤器Node.js的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

通过AWS运行Node.js无服务器后端。

Running a Node.js serverless backend through AWS.

主要目标:过滤并列出所有本地作业(表项),其中包括提供给过滤器的可用服务和邮政编码。

Main objective: to filter and list all LOCAL jobs (table items) that included the available services and zip codes provided to the filter.

我传入了多个邮政编码和多个可用服务。

Im passing in multiple zip codes, and multiple available services.

data.radius 将是一个邮政编码数组,类似于以下内容: ['93901','93902','93905','93906','93907','93912' , 93933, 93942, 93944, 93950, 95377, 95378, 95385, 95387, 95391]

data.radius would be an array of zip codes = to something like this:[ '93901', '93902', '93905', '93906', '93907', '93912', '93933', '93942', '93944', '93950', '95377', '95378', '95385', '95387', '95391' ]

data.availableServices 也会是一个数组=类似于这样的 ['Snow除掉','Ice Removal ','Salting','Same Day Response']

data.availableServices would also be an array = to something like this ['Snow removal', 'Ice Removal', 'Salting', 'Same Day Response']

我正在尝试进行API调用,仅返回具有匹配项的项目从 data.radius packageSelected zipCode code>与提供的数组 data.availableServices 匹配。

I am trying to make an API call that returns only items that have a matching zipCode from the array of zip codes provided by data.radius, and the packageSelected has a match of the array data.availableServices provided.

API调用

API CALL

import * as dynamoDbLib from "./libs/dynamodb-lib";
import { success, failure } from "./libs/response-lib";

export async function main(event, context) {
  const data = JSON.parse(event.body);
  const params = {
    TableName: "jobs",
    FilterExpression: "zipCode = :radius, packageSelected = :availableServices",
    ExpressionAttributeValues: {
      ":radius": data.radius,
      ":availableServices": data.availableServices
    }
  };

  try {
    const result = await dynamoDbLib.call("query", params);
    // Return the matching list of items in response body
    return success(result.Items);
  } catch (e) {
    return failure({ status: false });
  }

是否需要为此首先映射邮政编码和可用服务的数组

Do I need to map the array of zip codes and available services first for this to work?

我应该使用比较运算符吗?
https://docs.aws.amazon。 com / amazondynamodb / latest / developerguide / LegacyConditionalParameters.QueryFilter.html

Should I be using comparison operators? https://docs.aws.amazon.com/amazondynamodb/latest/developerguide/LegacyConditionalParameters.QueryFilter.html

查询和过滤是否需要排序键值或分区键? (表中有一个排序键和分区键,但我想避免在此调用中使用它们)

Is a sort key value or partition key required to query and filter? (the table has a sort key and partition key but i would like to avoid using them in this call)

我不是100%不确定如何执行此操作,如果任何人都可以向我指出正确的方向,这将是美好的,非常感谢!!

Im not 100% sure on how to go about this so if anyone could point me in the right direction that would be wonderful and greatly appreciated!!

推荐答案

我不确定您的 dynamodb-lib 是指什么但这里有一个示例,说明如何扫描给定值集中的attribute1和不同值集中的attribute2。这将使用标准的AWS JavaScript SDK,尤其是高级文档客户端。

I'm not sure what your dynamodb-lib refers to but here's an example of how you can scan for attribute1 in a given set of values and attribute2 in a different set of values. This uses the standard AWS JavaScript SDK, and specifically the high-level document client.

请注意,您不能使用等号( == )测试,则必须使用包含( IN )测试。而且您不能使用 query ,但必须使用 scan

Note that you cannot use an equality (==) test here, you have to use an inclusion (IN) test. And you cannot use query, but must use scan.

const AWS = require('aws-sdk');

let dc = new AWS.DynamoDB.DocumentClient({'region': 'us-east-1'});

const data = {
  radius: [ '93901', '93902', '93905', '93906', '93907', '93912', '93933', '93942', '93944', '93950', '95377', '95378', '95385', '95387', '95391' ],
  availableServices: ['Snow removal', 'Ice Removal', 'Salting', 'Same Day Response'],
};

// These hold ExpressionAttributeValues
const zipcodes = {};
const services = {};

data.radius.forEach((zipcode, i) => {
  zipcodes[`:zipcode${i}`] = zipcode;
})

data.availableServices.forEach((service, i) => {
  services[`:services${i}`] = service;
})

// These hold FilterExpression attribute aliases
const zipcodex = Object.keys(zipcodes).toString();
const servicex = Object.keys(services).toString();

const params = {
  TableName: "jobs",
  FilterExpression: `zipCode IN (${zipcodex}) AND packageSelected IN (${servicex})`,
  ExpressionAttributeValues : {...zipcodes, ...services},
};

dc.scan(params, (err, data) => {
  if (err) {
    console.log('Error', err);
  } else {
    for (const item of data.Items) {
      console.log('item:', item);
    }
  }
});

这篇关于Dynamo数据库查询过滤器Node.js的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆