Database Indexes

Create MongoDB indexes via scripts to keep serverless cold starts fast.

Thunder is designed for serverless-first deployment. To optimize cold starts, database indexes are not created automatically at startup. Instead, you define them in scripts and run them manually during development or deployment.

Creating Indexes

Place index scripts in the scripts/ folder:

scripts/createIndexes.ts
import { mongodb } from "@/database.ts";
import { userModel } from "@/schemas/user.ts";

async function createIndexes() {
  await userModel.createIndex({ email: 1 }, { unique: true });
  await userModel.createIndex({ createdAt: -1 });

  console.log("✓ Indexes created successfully");
}

await createIndexes();

Run it with Deno:

deno run -A scripts/createIndexes.ts

Why Manual Indexes?

BenefitExplanation
Cold start optimizationIndexes aren't recreated on every app startup.
Serverless efficiencyReduces initialization time in serverless environments.
Predictable performanceIndexes are created once, not repeatedly.
Better DXNo hidden database operations happening behind your back.

The scripts/ folder is the right home for any one-time setup task - not just indexes, but also data migrations and seed scripts. Run them on demand with deno run -A scripts/<name>.ts.


On this page