batchPut
Introduction
batchPut
method performs batch put request using aws-sdk BatchWriteItemCommand.
In addition to BatchWriteItemCommandOutput batchPut
response will include Items
which is fully typed based on your Schema declaration.
Items
is usefull as your Schema may include default
, set
or other transformer options.
execute
batchPut
takes 2 arguments:
- list of Item (required)
- options (optionnal)
any invalid value in Item will lead to dev time and runtime error
const res = await User.batchPut(
[
{
id: "user-1",
age: 27,
firstname: "John",
sex: "male",
data: "something",
},
{
id: "user-2",
age: 22,
firstname: "Sara",
sex: "female",
data: "something",
friends: ["user-1"],
isActive: false,
},
{
id: "user-3",
age: 461,
firstname: "E.T.,",
sex: "not human", // Type '"not human"' is not assignable to type '"male" | "female"'.
data: "something",
},
],
{ ReturnConsumedCapacity: "TOTAL" }
);
options
options
is an object which accepts BatchWriteItemCommandInput
options and DynamoQL specific options.
exec
exec
boolean directive to execute or not the actual request.
When false
batchPut will return BatchWriteItemCommandInput
object.
import type { BatchWriteItemCommandInput } from "@aws-sdk/client-dynamodb";
const cmd: BatchWriteItemCommandInput = await User.batchPut(
[
{
id: "user-1",
age: 27,
firstname: "John",
sex: "male",
data: "something",
},
{
id: "user-2",
age: 22,
firstname: "Sara",
sex: "female",
data: "something",
friends: ["user-1"],
isActive: false,
},
],
{ exec: false }
);
setterInfo
setterInfo
allows you to pass any value to your Schema set function's third argument.
const res = await User.batchPut(
[
{
id: "user-1",
age: 27,
firstname: "John",
sex: "male",
data: "something",
},
{
id: "user-2",
age: 22,
firstname: "Sara",
sex: "female",
data: "something",
friends: ["user-1"],
isActive: false,
},
],
{ setterInfo: { generateHash: true } }
);