Skip to main content

Database with Drizzle ORM

Introduction

This starter kit uses Drizzle ORM, a lightweight, type-safe SQL ORM that provides excellent developer experience without compromising on performance.

Project Structure

Our database code is organized in src/server/db/:

  • schema.ts - Table definitions and relationships
  • index.ts - Database connection and configuration
  • queries.ts - Centralized query functions

Example Query

Here's a simple query to demonstrate Drizzle's type-safe approach:

import { db } from ".";
import { eq } from "drizzle-orm";
import { users } from "./schema";

export const getUserById = async (id: string) => {
const user = await db.query.users.findFirst({
where: eq(users.id, id),
columns: {
id: true,
email: true,
name: true,
createdAt: true,
},
});
return user;
};

Key features demonstrated:

  • Type-safe queries with TypeScript
  • Intuitive query building
  • Automatic type inference
  • Column selection for performance
Why Drizzle?

We chose Drizzle over other ORMs because:

  • Zero runtime overhead
  • Type-safe SQL queries
  • Simple migration system
  • Great developer experience
  • Superior performance

[Continue with "Setting up PostgreSQL" section...]