Skip to main content

PutItem

To get defined Schema PutItem Entity we can use Schema's PutItem type.

Considering our Schema is:

import { Schema } from "dynamoql";

const userSchema = new Schema({
id: {
type: String,
primaryIndex: true,
},
age: Number,
firstname: {
type: String,
required: true,
capitalize: true
},
isActive: {
type: Boolean,
},
sex: {
type: String,
required: true,
enum: ["female", "male"],
},
friends: {
type: Array,
items: String,
},
data: [Number, String, { type: Set, items: String }]
} as const);
type UserPutItemEntity = (typeof userSchema)["PutItem"];

For our example Schema, PutItem type produces following definition:

interface UserPutItemEntity {
id: string;
age: number;
firstname: string;
isActive?: boolean;
sex: "female" | "male";
friends?: string[];
data: number | string | Set<string>
}

Then use it as any type:

const newUser: UserPutItemEntity = {
id: "user-1",
age: 24,
data: "something",
firstname: "John",
sex: "male"
};
danger

Do not use PutItem as JS value. It is undefined and will lead to runtime error.