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:
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.tsWhy Manual Indexes?
| Benefit | Explanation |
|---|---|
| Cold start optimization | Indexes aren't recreated on every app startup. |
| Serverless efficiency | Reduces initialization time in serverless environments. |
| Predictable performance | Indexes are created once, not repeatedly. |
| Better DX | No 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.