簡體   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?

我的表看起來像 [alias, inheritedLdap, LdapGroup ] 這里別名是字符串,LdapGroup 是列表形式,例如:[{S:aws}]。 所以基本上我的用例是獲取 ldapGroup 為 aws 的別名列表。 這里的別名是分區鍵,我們沒有排序鍵。 所以我需要寫一個方法,以ldapGroup為參數,並在ldapGroup為aws時過濾別名列表。 但是 ldapGroup 不包含標量值。 我試圖實現代碼,但是當我嘗試編譯時它失敗了,

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;
  } 
}

但是當我在我的代碼中使用 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'. 

有人可以幫我解決這個問題嗎?

我期待我的做法是否正確

這對我有用,我對你的例子做了一些編輯:

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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM