[英]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.