Skip to content

Secondary Indexes

Mizzle supports Global Secondary Indexes (GSI) and Local Secondary Indexes (LSI) to enable flexible query patterns on your DynamoDB tables.

GSIs allow you to query data across the entire table using a different Partition Key and optionally a Sort Key.

Define GSIs in your dynamoTable configuration.

import { dynamoTable, string, gsi } from "@aurios/mizzle";
export const table = dynamoTable("my-table", {
pk: string("pk"),
sk: string("sk"),
indexes: {
emailIndex: gsi("email", "status")
}
});
  • Arguments:
    • pkColumn: The name of the attribute to use as the Partition Key for the index.
    • skColumn: (Optional) The name of the attribute to use as the Sort Key for the index.

LSIs allow you to query data within a single Partition Key using a different Sort Key.

export const table = dynamoTable("my-table", {
pk: string("pk"),
sk: string("sk"),
indexes: {
createdAtIndex: lsi("createdAt")
}
});
  • Arguments:
    • skColumn: The name of the attribute to use as the Sort Key for the index.

Mizzle’s select builder automatically detects when your where clause matches an index.

// This will automatically use 'emailIndex' GSI
const user = await db.select()
.from(users)
.where(eq(users.email, "test@example.com"))
.execute();

You don’t need to manually specify the index name; Mizzle chooses the most efficient one based on the keys provided.