简体   繁体   English

当键不是 Node.js 和 typescript 的分区或排序键时,如何从 dynamo db 过滤数据?

[英]How to filter the data from dynamo db when the key is not a partition or Sort key with Node.js and typescript?

My table looks like [alias, inheritedLdap, LdapGroup ] here alias is the string and the LdapGroup is the List form eg: [{S:aws}].我的表看起来像 [alias, inheritedLdap, LdapGroup ] 这里别名是字符串,LdapGroup 是列表形式,例如:[{S:aws}]。 So basically my use case is to get the list of aliases whose ldapGroup is aws.所以基本上我的用例是获取 ldapGroup 为 aws 的别名列表。 Here the alias is the partition key, we don't have the sort key.这里的别名是分区键,我们没有排序键。 So I need to write a method which takes the ldapGroup as the parameter and filter the list of the alias when the ldapGroup is aws.所以我需要写一个方法,以ldapGroup为参数,并在ldapGroup为aws时过滤别名列表。 But ldapGroup doesn't contain scalar values.但是 ldapGroup 不包含标量值。 I tried to implement the code but its failing when I try to compile,我试图实现代码,但是当我尝试编译时它失败了,

public async getMemberList(): Promise<any> {
      const input: any = {
      TableName: UserInfoDao.TABLE_NAME, // use this from the constants
      ProjectionExpression: "alias",
      FilterExpression: "#l = :ldapGroups",
      ExpressionAttributeNames: {
         "#l": "ldapGroups"
       },
      ExpressionAttributeValues: {
        ":ldapGroups": "PPOA"
       }
    };
    try {
       const ddbClient = DynamDBClient.getInstance();
      return await ddbClient.scan(input);
    } catch (error) {
     const message = `ERROR: Failed to retrieve alias for given ldapGroups:
     ERROR: ${JSON.stringify(error)}`;
    error.message = message;
    throw error;
  } 
}

But when I use the ScanCommandOutput and ScanCommadInput in my code instead of any, its shows the error that the但是当我在我的代码中使用 ScanCommandOutput 和 ScanCommadInput 而不是任何一个时,它显示错误

Type 'Record<string, AttributeValue>[] | undefined' is not assignable to type 'ScanCommandInput'.   Type 'undefined' is not assignable to type 'ScanCommandInput'
Property '$metadata' is missing in type 'Request<ScanOutput, AWSError>' but required in type 'ScanCommandOutput'. 

Can someone help me with this one.有人可以帮我解决这个问题吗?

I am expecting whether my approach is correct or not我期待我的做法是否正确

This works for me, I made some edits you your example:这对我有用,我对你的例子做了一些编辑:

import { DynamoDBClient } from "@aws-sdk/client-dynamodb";
import { ScanCommand, ScanCommandInput } from "@aws-sdk/lib-dynamodb";

const client = new DynamoDBClient({
    region: 'eu-west-1',
});

class MyClass {

    public getMemberList(): Promise<any> {

        const input: ScanCommandInput = {
            TableName: 'Test1',
            // ProjectionExpression: "alias",
            FilterExpression: "contains(#l, :ldapGroups)",
            ExpressionAttributeNames: {
                "#l": "ldapGroups"
            },
            ExpressionAttributeValues: {
                ":ldapGroups": "aws"
            }
        };

        try {
            return client.send(new ScanCommand(input))
        } catch (error) {
            const message = `ERROR: Failed to retrieve alias for given ldapGroups: ERROR: ${JSON.stringify(error)}`;
            error.message = message;
            throw error;
        }
    }

}

const c = new MyClass();
c.getMemberList().then(res => console.log(res)).catch(err => console.log(err));


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

相关问题 如何使用 boto3 dynamo DB 查询 GSI 和分区键? - How to query on GSI and partition Key with boto3 dynamo DB? 如何在 AWS-lambda 中使用 nodeJS 对 dynamo DB 中的数据进行排序 - How to sort data from dynamo DB using nodeJS in AWS-lambda 如何在谷歌翻译Node.js代码中设置API KEY - How set API KEY in Google Translate Node.js code 在 nodejs 中非分区键和排序键列上使用 GSI 和过滤器表达式进行 Dynamodb 查询 - Dynamodb query with GSI and filter expression on non partition key and sort key columns in nodejs 如何仅使用分区键从 aws Dynamodb 获取数据? - How to get data from aws Dynamodb with using partition key only? 如何从云 function 对 firestore 数据库进行 CRUD 操作? (节点.js) - How to do CRUD operations on a firestore DB from a cloud function? (Node.js) 使用过滤器表达式从 dynamo 数据库查询返回固定数量的项目 - Return fixed number of items from dynamo db query with filter expression 在 DynamoDB 中使用 json 作为排序键/分区键值的好习惯吗? - Is using json as sort key/partition key value good practice in DynamoDB? 数据仓库中的 Redshift Dist Key 和 Sort Key - Redshift Dist Key and Sort Key in a Data Warehouse DynamoDB 中相同分区键的数据分布 - Same partition key's data distribution in DynamoDB
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM