insert
The insert builder is used to add new items to your DynamoDB tables. It automatically handles key generation based on your defined Key Strategies.
import { db } from "./db";import { users } from "./schema";
await db .insert(users) .values({ id: "123", name: "John Doe", email: "john@example.com", }) .execute();Methods
Section titled “Methods”insert(entity)
Section titled “insert(entity)”Initializes the insert builder for a specific entity.
- Arguments:
entity: A Mizzle entity definition.
- Returns:
InsertBuilder
values(data)
Section titled “values(data)”Specifies the data to be inserted. The data object must conform to the entity’s schema. Mizzle will automatically populate the physical Partition Key and Sort Key based on your entity’s strategies.
- Arguments:
data: An object containing the fields defined in your entity.
- Returns:
InsertBase
returning()
Section titled “returning()”Instructs Mizzle to return the inserted item (including the resolved PK and SK).
- Returns:
this
execute()
Section titled “execute()”Executes the PutItem operation against DynamoDB.
- Returns:
Promise<T | undefined>(Returns the item ifreturning()was called).
Key Resolution
Section titled “Key Resolution”When you call execute(), Mizzle performs the following steps:
- Defaults: Applies any
defaultordefaultFnvalues defined in your columns. - Strategies: Executes the
prefixKey,staticKey, orcompositeKeystrategies defined for the entity and any GSIs. - Physical Mapping: Maps the logical fields to the physical table attributes.
- PutItem: Sends the fully formed item to DynamoDB.