PutItem
To get defined Schema PutItem Entity we can use Schema's PutItem
type.
Considering our Schema is:
- TypeScript
- JSDoc
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);
// @ts-check
const { Schema } = require("dynamoql");
const userSchema = new Schema(
/** @type {const} */ ({
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 }],
})
);
- TypeScript
- JSDoc
type UserPutItemEntity = (typeof userSchema)["PutItem"];
/**
* @typedef {userSchema["PutItem"]} UserPutItemEntity
*/
For our example Schema, PutItem
type produces following definition:
- TypeScript
- JSDoc
interface UserPutItemEntity {
id: string;
age: number;
firstname: string;
isActive?: boolean;
sex: "female" | "male";
friends?: string[];
data: number | string | Set<string>
}
/**
* @typedef {object} UserPutItemEntity
* @property {string} id
* @property {number} age
* @property {string} firstname
* @property {boolean} [isActive]
* @property {"female" | "male"} sex
* @property {Array<string>} [friends]
* @property {number |string | Set<string>} data
*/
Then use it as any type:
- TypeScript
- JSDoc
const newUser: UserPutItemEntity = {
id: "user-1",
age: 24,
data: "something",
firstname: "John",
sex: "male"
};
/**
* @type UserPutItemEntity
*/
const newUser = {
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.