Drizzle ORM with SvelteKit
Define a Drizzle schema, run migrations, and query from SvelteKit load functions.
Drizzle ORM with SvelteKit is a free Sveltejs Academy lesson on CoddyKit — 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, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.
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-kitDefine 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 generateApply 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.
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, and the Sveltejs Academy course includes 4 lessons in total. 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.
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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.
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
- Auth + Database + API: Architecture
- Drizzle ORM with SvelteKit
- Deploying to Vercel with a Database
- End-to-End Type Safety with tRPC