我对 DynamoDB 非常陌生,需要帮助将以下查询添加到我的代码中。这是我所拥有的,但它给了我一个异常错误。
ValidationException: One or more parameter values were invalid: ComparisonOperator CONTAINS is not valid for M AttributeValue type
我的代码:
export async function searchGraba({ term }: { term: string }) {
const pks = await db.send(
new ScanCommand({
TableName: "Event",
IndexName: "id-index",
ScanFilter:
{ id:
{ ComparisonOperator: "CONTAINS",
AttributeValueList: [{ S: term }],
},
},
}));
console.log(pks.Items);
return pks.Items;
}
我尝试了不同的方法,它没有返回任何项目,但没有给我错误。
export async function searchGraba({ term }: { term: string }) {
const pks = await db.send(
new ScanCommand({
TableName: "Event",
IndexName: "id-index",
FilterExpression: "contains(#id, :id)",
ExpressionAttributeNames: { "#id": "id" },
ExpressionAttributeValues: { ":id": { S: term } },
})
);
console.log(pks.Items);
return pks.Items;
}
我的数据库客户端配置正确,我运行了多个查询命令(没有过滤器)和扫描命令。这是来自 aws cli 的描述表命令。
aws dynamodb describe-table \
--table-name Event
{
"Table": {
"AttributeDefinitions": [
{
"AttributeName": "Phone",
"AttributeType": "S"
},
{
"AttributeName": "id",
"AttributeType": "S"
}
],
"TableName": "Event",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"TableStatus": "ACTIVE",
"CreationDateTime": "2024-03-11T02:20:20.600000+00:00",
"ProvisionedThroughput": {
"LastDecreaseDateTime": "2024-03-11T02:32:19.941000+00:00",
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"TableSizeBytes": 100,
"ItemCount": 3,
"TableArn": "arn:aws:dynamodb:ca-central-1:300485252224:table/Event",
"TableId": "f1472d40-37c0-4405-a7c5-b5bac836a2ff",
"GlobalSecondaryIndexes": [
{
"IndexName": "PhoneNumberIndex",
"KeySchema": [
{
"AttributeName": "Phone",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"IndexSizeBytes": 100,
"ItemCount": 3,
"IndexArn": "arn:aws:dynamodb:ca-central-1:300485252224:table/Event/index/PhoneNumberIndex"
},
{
"IndexName": "id-index",
"KeySchema": [
{
"AttributeName": "id",
"KeyType": "HASH"
}
],
"Projection": {
"ProjectionType": "ALL"
},
"IndexStatus": "ACTIVE",
"ProvisionedThroughput": {
"NumberOfDecreasesToday": 0,
"ReadCapacityUnits": 1,
"WriteCapacityUnits": 1
},
"IndexSizeBytes": 100,
"ItemCount": 3,
"IndexArn": "arn:aws:dynamodb:ca-central-1:300485252224:table/Event/index/id-index"
}
],
"TableClassSummary": {
"TableClass": "STANDARD"
},
"DeletionProtectionEnabled": false
}
}
这是我的数据库客户端配置。
import { DynamoDB } from "@aws-sdk/client-dynamodb";
import { DynamoDBDocument } from "@aws-sdk/lib-dynamodb";
const awsCredetnials = {
accessKeyId: process.env.AWS_ACCOUNT_ACCESS_KEY,
secretAccessKey: process.env.AWS_ACCOUNT_SECRET_KEY,
};
const dynamoConfig = {
region: process.env.AWS_ACCOUNT_REGION,
credentials: awsCredetnials,
} as {
credentials: {
accessKeyId: string;
secretAccessKey: string;
};
region: string;
};
const db = DynamoDBDocument.from(new DynamoDB(dynamoConfig), {
marshallOptions: {
convertEmptyValues: true,
removeUndefinedValues: true,
convertClassInstanceToMap: false,
},
});
export { db };
您有一个以
id
作为分区键的索引,您不应该使用过滤器进行扫描,这是低效的。你应该使用Query
:
export async function searchGraba({ term }: { term: string }) {
const pks = await db.send(
new Query({
TableName: "Event",
IndexName: "id-index",
KeyConditionExpression: `id = ${term}`,
}));
console.log(pks.Items);
return pks.Items;
}
鉴于您正在尝试执行包含操作,您无法过滤键,因此如果您想知道 Id 是否包含值,则需要从不将其作为分区键的表或索引中执行此操作:
export async function searchGraba({ term }: { term: string }) {
const pks = await db.send(
new ScanCommand({
TableName: "Event",
IndexName: "id-index",
FilterExpression: `contains(id, ${term})`
}));
console.log(pks.Items);
return pks.Items;
}