Skip to main content

transactGet

Introduction

transactGet method performs transact Get request using aws-sdk TransactGetItemsCommand.

execute

transactGet takes 2 arguments:

  1. (required) an array of item identifier:
    • item identifier can be primaryIndex
    • if table is HASH and RANGE table, item identifier must be an object with primaryIndex and sortKey.
  2. options (optionnal)
const res = await User.transactGet(
[ "user-1", "user-2" ],
{ ReturnConsumedCapacity: "TOTAL" }
);

or

const res = await User.transactGet(
[
{
id: "user-1",
},
{
id: "user-2",
}
],
{ ReturnConsumedCapacity: "TOTAL" }
);

options

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

exec

exec boolean directive to execute or not the actual request.

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

const cmd: TransactGetItemsInput = await User.transactGet(
[ "user-1", "user-2" ],
{ exec: false }
);

Select

Select accepts an array of attributes and produces a ProjectionExpression.

const res = await User.transactGet(
["user-1", "user-2"],
{ Select: ["id", "isActive"] }
);

note

Select affects returned Items type.

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

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

To apply Select individually for each item, passe an object with $key and $select:

const res = await User.transactGet([
{
$key: {
id: "user-1",
},
$select: ["firstname"],
},
{
$key: {
id: "user-2",
},
$select: ["age"],
},
]);

getterInfo

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

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