Skip to content

Commit 02df4be

Browse files
committed
Pushing changes to Postgres Drizzle ORM tutorial
1 parent 249d6d6 commit 02df4be

File tree

1 file changed

+63
-23
lines changed
  • src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries

1 file changed

+63
-23
lines changed

src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx

Lines changed: 63 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ meta:
77
title: Using Drizzle ORM with Hyperdrive for PostgreSQL
88
---
99

10-
import { Render } from "~/components";
10+
import { Render, Steps } from "~/components";
1111

1212
[Drizzle ORM](https://orm.drizzle.team/) is a lightweight TypeScript ORM with a focus on type safety. This example demonstrates how to use Drizzle ORM with PostgreSQL via Cloudflare Hyperdrive in a Workers application.
1313

@@ -33,27 +33,35 @@ Add the required Node.js compatibility flags and Hyperdrive binding to your `wra
3333

3434
## 2. Configure Drizzle
3535

36-
### 2.1 Define a schema
36+
### 2.1. Define a schema
3737

38-
With Drizzle ORM, we define the schema in TypeScript rather than writing raw SQL. Here is how to define a `users` table:
38+
With Drizzle ORM, we define the schema in TypeScript rather than writing raw SQL.
3939

40-
```ts
41-
// src/db/schema.ts
42-
import { pgTable, serial, varchar, timestamp } from "drizzle-orm/pg-core";
40+
<Steps>
41+
1. Create a folder `/db/` in `/src/`.
42+
2. Create a `schema.ts` file.
43+
3. In `schema.ts`, define a `users` table as shown below.
4344

44-
export const users = pgTable("users", {
45-
id: serial("id").primaryKey(),
46-
name: varchar("name", { length: 255 }).notNull(),
47-
email: varchar("email", { length: 255 }).notNull().unique(),
48-
createdAt: timestamp("created_at").defaultNow(),
49-
});
50-
```
45+
```ts title="src/db/schema.ts"
46+
// src/db/schema.ts
47+
import { pgTable, serial, varchar, timestamp } from "drizzle-orm/pg-core";
48+
49+
export const users = pgTable("users", {
50+
id: serial("id").primaryKey(),
51+
name: varchar("name", { length: 255 }).notNull(),
52+
email: varchar("email", { length: 255 }).notNull().unique(),
53+
createdAt: timestamp("created_at").defaultNow(),
54+
});
55+
```
56+
</Steps>
57+
58+
### 2.2. Connect Drizzle ORM to the database with Hyperdrive
5159

52-
### 2.2 Connect Drizzle ORM to the database with Hyperdrive
60+
Use your Hyperdrive configuration for your database when using the Drizzle ORM.
5361

54-
Use your Hyperdrive configuration for your database when using the Drizzle ORM within your Worker project as shown:
62+
Populate your `index.ts` file as shown below.
5563

56-
```ts
64+
```ts title="src/index.ts"
5765
// src/index.ts
5866
import { drizzle } from "drizzle-orm/postgres-js";
5967
import postgres from "postgres";
@@ -88,26 +96,29 @@ export default {
8896
```
8997

9098
:::note
91-
9299
You may use [node-postgres](https://orm.drizzle.team/docs/get-started-postgresql#node-postgres) or [Postgres.js](https://orm.drizzle.team/docs/get-started-postgresql#postgresjs)
93100
when using Drizzle ORM. Both are supported and compatible.
94-
95101
:::
96102

97-
### 2.3 Configure Drizzle-Kit for migrations (optional)
103+
If your database is empty, continue to step [2.3. Configure Drizzle-Kit for migration](/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm/#23-configure-drizzle-kit-for-migrations-optional).
98104

99-
You can generate and run SQL migrations on your database based on your schema using Drizzle Kit CLI. Refer to [Drizzle ORM docs](https://orm.drizzle.team/docs/get-started/postgresql) for additional guidance.
105+
### 2.3. Configure Drizzle-Kit for migrations (optional)
100106

101-
1. Create a `.env` file and add your database connection string. The Drizzle Kit CLI will use this connection string to create and apply the migrations.
107+
You can generate and run SQL migrations on your database based on your schema using Drizzle Kit CLI. Refer to [Drizzle ORM docs](https://orm.drizzle.team/docs/get-started/postgresql-new) for additional guidance.
102108

103-
```toml
109+
<Steps>
110+
1. Create a `.env` file in your `/src/` folder, and add your database connection string. The Drizzle Kit CLI will use this connection string to create and apply the migrations.
111+
112+
```toml title="src/.env"
113+
# src/.env
104114
# Replace with your direct database connection string
105115
DATABASE_URL='postgres://user:[email protected]/database-name'
106116
```
107117

108118
2. Create a `drizzle.config.ts` file in the root of your project to configure Drizzle Kit and add the following content:
109119

110-
```ts
120+
```ts title="drizzle.config.ts"
121+
// drizzle.config.ts
111122
import "dotenv/config";
112123
import { defineConfig } from "drizzle-kit";
113124
export default defineConfig({
@@ -122,7 +133,36 @@ You can generate and run SQL migrations on your database based on your schema us
122133

123134
3. Generate the migration file for your database according to your schema files and apply the migrations to your database.
124135

136+
Run the following two commands:
137+
125138
```bash
126139
npx drizzle-kit generate
140+
```
141+
```bash output
142+
No config path provided, using default 'drizzle.config.ts'
143+
Reading config file '/Users/junlee/tutorials/jun-hyperdrive-drizzle/drizzle.config.ts'
144+
1 tables
145+
users 4 columns 0 indexes 0 fks
146+
147+
[✓] Your SQL migration file ➜ drizzle/0000_mysterious_queen_noir.sql 🚀
148+
```
149+
150+
```bash
127151
npx drizzle-kit migrate
128152
```
153+
```bash output
154+
No config path provided, using default 'drizzle.config.ts'
155+
Reading config file '/Users/junlee/tutorials/jun-hyperdrive-drizzle/drizzle.config.ts'
156+
Using 'postgres' driver for database querying
157+
```
158+
</Steps>
159+
160+
## 3. Deploy your Worker
161+
162+
Deploy your Worker.
163+
164+
```bash
165+
npx wrangler deploy
166+
```
167+
168+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />

0 commit comments

Comments
 (0)