You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx
+63-23Lines changed: 63 additions & 23 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -7,7 +7,7 @@ meta:
7
7
title: Using Drizzle ORM with Hyperdrive for PostgreSQL
8
8
---
9
9
10
-
import { Render } from"~/components";
10
+
import { Render, Steps } from"~/components";
11
11
12
12
[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.
13
13
@@ -33,27 +33,35 @@ Add the required Node.js compatibility flags and Hyperdrive binding to your `wra
33
33
34
34
## 2. Configure Drizzle
35
35
36
-
### 2.1 Define a schema
36
+
### 2.1. Define a schema
37
37
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.
### 2.2. Connect Drizzle ORM to the database with Hyperdrive
51
59
52
-
### 2.2 Connect Drizzle ORM to the database with Hyperdrive
60
+
Use your Hyperdrive configuration for your database when using the Drizzle ORM.
53
61
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.
55
63
56
-
```ts
64
+
```ts title="src/index.ts"
57
65
// src/index.ts
58
66
import { drizzle } from"drizzle-orm/postgres-js";
59
67
importpostgresfrom"postgres";
@@ -88,26 +96,29 @@ export default {
88
96
```
89
97
90
98
:::note
91
-
92
99
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)
93
100
when using Drizzle ORM. Both are supported and compatible.
94
-
95
101
:::
96
102
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).
98
104
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)
100
106
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.
102
108
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
104
114
# Replace with your direct database connection string
0 commit comments