src/content/docs/tutorials/drizzle-with-frameworks/drizzle-with-encore.mdx
import Steps from "@mdx/Steps.astro"; import Npm from "@mdx/Npm.astro"; import CodeTabs from "@mdx/CodeTabs.astro"; import CodeTab from "@mdx/CodeTab.astro"; import Section from "@mdx/Section.astro"; import Callout from "@mdx/Callout.astro"; import Prerequisites from "@mdx/Prerequisites.astro";
This tutorial demonstrates how to use Drizzle ORM with Encore, an open source backend framework with built-in infrastructure automation and observability.
<Prerequisites> - You should have the Encore CLI installed. You can install it with: ```bash # macOS brew install encoredev/tap/encorecurl -L https://encore.dev/install.sh | bash
iwr https://encore.dev/install.ps1 | iex
- You should have installed Drizzle ORM and [Drizzle kit](/docs/kit-overview). You can do this by running the following command:
<Npm>
drizzle-orm
-D drizzle-kit
</Npm>
</Prerequisites>
## Setup Encore and Drizzle ORM
<Steps>
#### Create a new Encore project
You can create a new Encore project with Drizzle already configured:
```bash
encore app create my-app --example=ts/drizzle
cd my-app
Or if you have an existing Encore project, install Drizzle:
<Npm> drizzle-orm -D drizzle-kit </Npm>Define your database in a database.ts file. Encore automatically provisions a PostgreSQL database locally using Docker and in the cloud when you deploy:
import { SQLDatabase } from "encore.dev/storage/sqldb";
import { drizzle } from "drizzle-orm/node-postgres";
import * as schema from "./schema";
const db = new SQLDatabase("mydb", {
migrations: {
path: "migrations",
source: "drizzle",
},
});
export const orm = drizzle(db.connectionString, { schema });
Setting source: "drizzle" tells Encore to use Drizzle's migration format.
Create a schema.ts file to define your tables:
import * as p from "drizzle-orm/pg-core";
export const users = p.pgTable("users", {
id: p.serial().primaryKey(),
name: p.text().notNull(),
email: p.text().unique().notNull(),
createdAt: p.timestamp().defaultNow().notNull(),
});
Create a drizzle.config.ts file:
import { defineConfig } from "drizzle-kit";
export default defineConfig({
out: "migrations",
schema: "schema.ts",
dialect: "postgresql",
});
Run Drizzle Kit to generate migrations from your schema:
drizzle-kit generate
This creates migration files in the migrations folder.
Use Drizzle in your Encore endpoints:
import { api } from "encore.dev/api";
import { orm } from "./database";
import { users } from "./schema";
import { eq } from "drizzle-orm";
interface User {
id: number;
name: string;
email: string;
}
export const list = api(
{ expose: true, method: "GET", path: "/users" },
async (): Promise<{ users: User[] }> => {
const result = await orm.select().from(users);
return { users: result };
}
);
export const create = api(
{ expose: true, method: "POST", path: "/users" },
async (req: { name: string; email: string }): Promise<User> => {
const [user] = await orm
.insert(users)
.values({ name: req.name, email: req.email })
.returning();
return user;
}
);
Start your Encore app:
encore run
Encore automatically applies migrations when starting. Open localhost:9400 to see the local dashboard with API docs, database explorer, and tracing. </Steps>
<Callout type="info"> Migrations are automatically applied when you run your Encore application. You don't need to run `drizzle-kit migrate` manually. </Callout>