query
Introduction
query method performs query request using aws-sdk QueryCommand.
Items in QueryCommandOutput is fully typed based on your Schema declaration.
execute
query takes 2 arguments:
- (required) an object which includes:
- item
primaryIndex - optionnally KeyConditionExpression
- optionnally Condition expression which will perform
FilterExpression
- item
- options (optionnal)
const res = await User.query(
{
groupId: "group-1", // Schema primaryIndex
// Schema sortKey
userId: {
$startsWith: "user-",
},
// FilterExpression
age: {
$gt: 22
}
},
{ ReturnConsumedCapacity: "TOTAL" }
);
res.Items[0].firstname;
options
options is an object which accepts QueryCommandInput options and DynamoQL specific options.
exec
exec boolean directive to execute or not the actual request.
When false query will return QueryCommandInput object.
import type { QueryCommandInput } from "@aws-sdk/client-dynamodb";
const cmd: QueryCommandInput = await User.query(
{
groupId: "group-1"
},
{ exec: false }
);
Select
Select accepts an array of attributes which produces a ProjectionExpression, or "ALL" | "COUNT".
const res = await User.query(
{
groupId: "group-1"
},
{ Select: ["id", "isActive"] }
);
note
Select affects QueryCommandOutput type.
const { Items } = await User.query(
{
groupId: "group-1",
},
{ Select: ["id", "isActive"] }
);
Items[0].firstname; // Property 'firstname' does not exist on type { id: string; isActive?:boolean } ...
const res = await User.query(
{
groupId: "group-1",
},
{ Select: "COUNT" }
);
res.Items; // Property 'Items' does not exist on type QueryCommandOutput
getterInfo
getterInfo allows you to pass any value to your Schema get function's third argument.
const res = await User.query(
{
groupId: "group-1",
},
{ getterInfo: { forFrontend: true } }
);