Sveltejs Academy · Lesson

Drizzle ORM with SvelteKit

Define a Drizzle schema, run migrations, and query from SvelteKit load functions.

Lesson 2 of 413 steps

Drizzle ORM with SvelteKit is a free Sveltejs Academy lesson on CoddyKit. This is lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the Sveltejs Academy learning path, and your progress syncs across the web and the CoddyKit app. The Sveltejs Academy course includes 4 lessons in total.

Drizzle

Drizzle is a lightweight TypeScript ORM with great type inference and SQL-like syntax.

Install

Install Drizzle and your DB driver.

npm install drizzle-orm postgres
npm install -D drizzle-kit

Define Schema

Schema lives in $lib/server/db/schema.ts.

import { pgTable, serial, text } from "drizzle-orm/pg-core";
export const users = pgTable("users", {
  id: serial("id").primaryKey(),
  email: text("email").notNull()
});

Connect

Create a DB client in $lib/server/db/index.ts.

import postgres from "postgres";
import { drizzle } from "drizzle-orm/postgres-js";
const sql = postgres(DATABASE_URL);
export const db = drizzle(sql, { schema });

Query

Use the typed query builder.

const user = await db.select().from(users).where(eq(users.id, 1));

Migrations

Generate migrations from schema changes.

npx drizzle-kit generate

Apply Migrations

Push to your database with drizzle-kit migrate.

Relations

Define relations between tables for joinable queries.

Type Inference

Drizzle infers row types from your schema, ending manual type drift.

Compatibility

Drizzle supports Postgres, MySQL, SQLite, and edge runtimes via specific drivers.

Use in Load

Import the db instance in +page.server.js and query inside load.

Quick Check

Where do you define a Drizzle schema?

Recap

Drizzle gives type-safe queries from a TypeScript schema. Use it in server-only files alongside SvelteKit endpoints.

Free to start

Learn HTML with an AI tutor — free

Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.

Courses
41
Lessons
163

Frequently Asked Questions

Is the “Drizzle ORM with SvelteKit” lesson free?

Yes — the full text of “Drizzle ORM with SvelteKit” is free to read here on the web. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the Sveltejs Academy course, upgrade to CoddyKit PRO. The Sveltejs Academy course includes 4 lessons in total.

What will I learn in “Drizzle ORM with SvelteKit”?

Define a Drizzle schema, run migrations, and query from SvelteKit load functions. You practise Sveltejs Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.

Do I need any experience to start Sveltejs Academy?

No prior experience is required. Sveltejs Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.

How long does the “Drizzle ORM with SvelteKit” lesson take?

Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.

Can I write and run code in this Sveltejs Academy lesson?

Yes. Every Sveltejs Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.

All lessons in this course

  1. Auth + Database + API: Architecture
  2. Drizzle ORM with SvelteKit
  3. Deploying to Vercel with a Database
  4. End-to-End Type Safety with tRPC
← Back to Sveltejs Academy