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-mysql/mysql-drivers-and-libraries/drizzle-orm.mdx
+87-43Lines changed: 87 additions & 43 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 MySQL
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 MySQL via Cloudflare Hyperdrive in a Workers application.
13
13
@@ -33,33 +33,45 @@ 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.
// Create a connection using the mysql2 driver with the Hyperdrive credentials (only accessible from your Worker).
@@ -85,35 +97,67 @@ export default {
85
97
} satisfiesExportedHandler<Env>;
86
98
```
87
99
88
-
### 2.3 Configure Drizzle-Kit for migrations (optional)
89
-
90
-
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/mysql-new) for additional guidance.
91
-
92
-
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.
100
+
If your database is empty, continue to step [2.3. Configure Drizzle-Kit for migration](/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm/#23-configure-drizzle-kit-for-migrations-optional).
93
101
94
-
```toml
95
-
# Replace with your direct database connection string
### 2.3. Configure Drizzle-Kit for migrations (optional)
98
103
99
-
2. Create a `drizzle.config.ts` file in the root of your project to configure Drizzle Kit and add the following content:
104
+
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/mysql-new) for additional guidance.
100
105
101
-
```ts
102
-
import'dotenv/config';
103
-
import { defineConfig } from'drizzle-kit';
104
-
exportdefaultdefineConfig({
105
-
out: './drizzle',
106
-
schema: './src/db/schema.ts',
107
-
dialect: 'mysql',
108
-
dbCredentials: {
109
-
url: process.env.DATABASE_URL!,
110
-
},
111
-
});
112
-
```
106
+
<Steps>
107
+
1. Create a `.env` file in the root folder of your project, and add your database connection string. The Drizzle Kit CLI will use this connection string to create and apply the migrations.
108
+
109
+
```toml title=".env"
110
+
# .env
111
+
# Replace with your direct database connection string
Copy file name to clipboardExpand all lines: src/content/docs/hyperdrive/examples/connect-to-postgres/postgres-drivers-and-libraries/drizzle-orm.mdx
+4-4Lines changed: 4 additions & 4 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -107,15 +107,15 @@ If your database is empty, continue to step [2.3. Configure Drizzle-Kit for migr
107
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.
108
108
109
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.
110
+
1. Create a `.env` file the root folder of your project, and add your database connection string. The Drizzle Kit CLI will use this connection string to create and apply the migrations.
111
111
112
-
```toml title="src/.env"
113
-
#src/.env
112
+
```toml title=".env"
113
+
# .env
114
114
# Replace with your direct database connection string
0 commit comments