Skip to main content

batchGet

Introduction

batchGet method performs batch get request using aws-sdk BatchGetItemCommand.

In addition to BatchGetItemCommandOutput batchGet response will include Items which is fully typed based on your Schema declaration.

execute

batchGet takes 2 arguments:

  1. list of Items Key's (required)
  2. options (optionnal)
const res = await User.batchGet(
["user-1", "user-2", "user-3", "user-4"],
{ ReturnConsumedCapacity: "TOTAL" }
);

res.Items?.[0].firstname;

If your table is a composite table (HASH and RANGE) you must pass primaryIndex and sortKey as object.

const res = await User.batchGet([
{
groupId: "group-1",
userId: "user-1",
},
{
groupId: "group-1",
userId: "user-2",
},
{
groupId: "group-2",
userId: "user-3",
},
{
groupId: "group-2",
userId: "user-4",
},
]);

options

options is an object which accepts BatchGetItemCommandInput options and DynamoQL specific options.

exec

exec boolean directive to execute or not the actual request.
When false batchGet will return BatchGetItemCommandInput object.

import type { BatchGetItemCommandInput } from "@aws-sdk/client-dynamodb";

const cmd: BatchGetItemCommandInput = await User.batchGet(
["user-1", "user-2", "user-3", "user-4"],
{ exec: false }
);

Select

Select accepts an array of attributes and produces a ProjectionExpression.

const cmd = await User.batchGet(
["user-1", "user-2", "user-3", "user-4"],
{ Select: ["id", "isActive"] }
);
note

Select affects returned Items type.

const { Items } = await User.batchGet(
["user-1", "user-2", "user-3", "user-4"],
{ Select: ["id", "isActive"] }
);

Items?.[0].firstname; // Property 'firstname' does not exist on type { id: string; isActive?:boolean } ...

getterInfo

getterInfo allows you to pass any value to your Schema get function's third argument.

const res = await User.batchGet(
["user-1", "user-2", "user-3", "user-4"],
{ getterInfo: { forFrontend: true } }
);