Skip to content

Commit 0517e7b

Browse files
committed
Editing MySQL version of the tutorial
1 parent 02df4be commit 0517e7b

File tree

2 files changed

+91
-47
lines changed
  • src/content/docs/hyperdrive/examples

2 files changed

+91
-47
lines changed

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

Lines changed: 87 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ meta:
77
title: Using Drizzle ORM with Hyperdrive for MySQL
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 MySQL via Cloudflare Hyperdrive in a Workers application.
1313

@@ -33,33 +33,45 @@ 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/schema.ts
42-
import { mysqlTable, int, varchar, timestamp } from "drizzle-orm/mysql-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 = mysqlTable("users", {
45-
id: int("id").primaryKey().autoincrement(),
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/schema.ts
47+
import { mysqlTable, int, varchar, timestamp } from "drizzle-orm/mysql-core";
48+
49+
export const users = mysqlTable("users", {
50+
id: int("id").primaryKey().autoincrement(),
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 the credentials of your Hyperdrive configuration for your database when using the Drizzle ORM.
5361

54-
Use your the credentials of 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
57-
// src/db/schema.ts
64+
```ts title="src/index.ts"
65+
// src/index.ts
5866

5967
import { drizzle } from "drizzle-orm/mysql2";
6068
import { createConnection } from "mysql2";
6169
import { users } from "./db/schema";
6270

71+
export interface Env {
72+
HYPERDRIVE: Hyperdrive;
73+
}
74+
6375
export default {
6476
async fetch(request, env, ctx): Promise<Response> {
6577
// Create a connection using the mysql2 driver with the Hyperdrive credentials (only accessible from your Worker).
@@ -85,35 +97,67 @@ export default {
8597
} satisfies ExportedHandler<Env>;
8698
```
8799

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).
93101

94-
```toml
95-
# Replace with your direct database connection string
96-
DATABASE_URL='mysql://user:[email protected]/database-name'
97-
```
102+
### 2.3. Configure Drizzle-Kit for migrations (optional)
98103

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.
100105

101-
```ts
102-
import 'dotenv/config';
103-
import { defineConfig } from 'drizzle-kit';
104-
export default defineConfig({
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
112+
DATABASE_URL='mysql://user:[email protected]/database-name'
113+
```
114+
115+
2. Create a `drizzle.config.ts` file in the root folder of your project to configure Drizzle Kit and add the following content:
116+
117+
```ts title="drizzle.config.ts"
118+
import 'dotenv/config';
119+
import { defineConfig } from 'drizzle-kit';
120+
export default defineConfig({
121+
out: './drizzle',
122+
schema: './src/db/schema.ts',
123+
dialect: 'mysql',
124+
dbCredentials: {
125+
url: process.env.DATABASE_URL!,
126+
},
127+
});
128+
```
113129

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

116-
```bash
117-
npx drizzle-kit generate
118-
npx drizzle-kit migrate
119-
```
132+
```bash
133+
npx drizzle-kit generate
134+
```
135+
```bash output
136+
No config path provided, using default 'drizzle.config.ts'
137+
Reading config file '/Users/junlee/tutorials/jun-hyperdrive-drizzle-mysql/drizzle.config.ts'
138+
Reading schema files:
139+
/Users/junlee/tutorials/jun-hyperdrive-drizzle-mysql/src/db/schema.ts
140+
141+
1 tables
142+
users 4 columns 0 indexes 0 fks
143+
144+
[✓] Your SQL migration file ➜ drizzle/0000_daffy_rhodey.sql 🚀
145+
```
146+
```bash
147+
npx drizzle-kit migrate
148+
```
149+
```bash output
150+
No config path provided, using default 'drizzle.config.ts'
151+
Reading config file '/Users/junlee/tutorials/jun-hyperdrive-drizzle-mysql/drizzle.config.ts'
152+
```
153+
</Steps>
154+
155+
## 3. Deploy your Worker
156+
157+
Deploy your Worker.
158+
159+
```bash
160+
npx wrangler deploy
161+
```
162+
163+
<Render file="create-hyperdrive-config-next-steps" product="hyperdrive" />

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -107,15 +107,15 @@ If your database is empty, continue to step [2.3. Configure Drizzle-Kit for migr
107107
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.
108108

109109
<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.
111111

112-
```toml title="src/.env"
113-
# src/.env
112+
```toml title=".env"
113+
# .env
114114
# Replace with your direct database connection string
115115
DATABASE_URL='postgres://user:[email protected]/database-name'
116116
```
117117

118-
2. Create a `drizzle.config.ts` file in the root of your project to configure Drizzle Kit and add the following content:
118+
2. Create a `drizzle.config.ts` file in the root folder of your project to configure Drizzle Kit and add the following content:
119119

120120
```ts title="drizzle.config.ts"
121121
// drizzle.config.ts

0 commit comments

Comments
 (0)