-
Notifications
You must be signed in to change notification settings - Fork 10k
[Hyperdrive] add drivers and orms docs #22125
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 12 commits
Commits
Show all changes
24 commits
Select commit
Hold shift + click to select a range
d7eb2e1
[Hyperdrive] drafting custom cert support for Hyperdrive
thomasgauvin d578ae1
corrections
thomasgauvin 7a2aeb5
wip
thomasgauvin 1bb1afc
add changelog
thomasgauvin 2eb045c
adjust per comments
thomasgauvin 6131158
add note on wrangler version
thomasgauvin a4409e7
add troubleshooting guides for ssl certs
thomasgauvin a5bcb69
change changelog entry date
thomasgauvin 28638fa
Update src/content/docs/hyperdrive/configuration/tls-ssl-certificates…
thomasgauvin 8442a7b
Update src/content/docs/hyperdrive/configuration/tls-ssl-certificates…
thomasgauvin a637925
PCX review
Oxyjun a244fa6
add drivers and orms to hyperdrive docs
thomasgauvin d490626
Apply suggestions from code review
Oxyjun 9297e73
Apply suggestions from code review
Oxyjun 28676b2
Specifying product when rendering partials
Oxyjun 721b41e
Importing Render components
Oxyjun 0410c7b
Fixing invalid links
Oxyjun 03cb1cc
adjust per discord feedback
thomasgauvin 249d6d6
more nits
thomasgauvin 02df4be
Pushing changes to Postgres Drizzle ORM tutorial
Oxyjun 0517e7b
Editing MySQL version of the tutorial
Oxyjun 955db1e
Adding note about when to run the optional
Oxyjun ed882f4
Removing outdated redirects to avoid breaking
Oxyjun f1444e2
Adding missing redirects.
Oxyjun File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
14 changes: 14 additions & 0 deletions
14
...nt/docs/hyperdrive/examples/connect-to-mysql/mysql-database-providers/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,14 @@ | ||
| --- | ||
| type: overview | ||
| pcx_content_type: navigation | ||
| title: Database Providers | ||
| hideChildren: false | ||
| sidebar: | ||
| order: 5 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components"; | ||
|
|
||
| <DirectoryListing /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
122 changes: 122 additions & 0 deletions
122
...yperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/drizzle-orm.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,122 @@ | ||
| --- | ||
| pcx_content_type: example | ||
| title: Drizzle ORM | ||
| sidebar: | ||
| order: 3 | ||
| meta: | ||
| title: Using Drizzle ORM with Hyperdrive for MySQL | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; | ||
|
|
||
| [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. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| - A Cloudflare account with Workers access | ||
| - A MySQL database | ||
| - A [Hyperdrive configuration to your MySQL database](/hyperdrive/get-started/#3-connect-hyperdrive-to-a-database) | ||
|
|
||
| ## Installation | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Install the Drizzle ORM and its dependencies such as the [mysql2](https://github.com/sidorares/node-mysql2) driver: | ||
|
|
||
| ```sh | ||
| # mysql2 v3.13.0 or later is required | ||
| npm i drizzle-orm mysql2 dotenv | ||
| npm i -D drizzle-kit tsx @types/node | ||
| ``` | ||
|
|
||
| Add the required Node.js compatibility flags and Hyperdrive binding to your `wrangler.jsonc` file: | ||
|
|
||
| <Render file="hyperdrive-node-compatibility-requirement" /> | ||
|
|
||
| ## Configure Drizzle | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ### Define a schema | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| With Drizzle ORM, we define the schema in TypeScript rather than writing raw SQL. Here's how to define a users table: | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| // src/schema.ts | ||
| import { mysqlTable, int, varchar, timestamp } from "drizzle-orm/mysql-core"; | ||
|
|
||
| export const users = mysqlTable("users", { | ||
| id: int("id").primaryKey().autoincrement(), | ||
| name: varchar("name", { length: 255 }).notNull(), | ||
| email: varchar("email", { length: 255 }).notNull().unique(), | ||
| createdAt: timestamp("created_at").defaultNow(), | ||
| }); | ||
| ``` | ||
|
|
||
| ### Connect Drizzle ORM to the database with Hyperdrive | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| Use your the credentials of your Hyperdrive configuration for your database when using the | ||
| Drizzle ORM within your Worker project as such: | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| // src/db/schema.ts | ||
|
|
||
| import { drizzle } from "drizzle-orm/mysql2"; | ||
| import { createConnection } from "mysql2"; | ||
| import { users } from "./db/schema"; | ||
|
|
||
| export default { | ||
| async fetch(request, env, ctx): Promise<Response> { | ||
| // Create a connection using the mysql2 driver with the Hyperdrive credentials (only accessible from your Worker). | ||
| const connection = await createConnection({ | ||
| host: env.HYPERDRIVE.host, | ||
| user: env.HYPERDRIVE.user, | ||
| password: env.HYPERDRIVE.password, | ||
| database: env.HYPERDRIVE.database, | ||
| port: env.HYPERDRIVE.port, | ||
|
|
||
| // Required to enable mysql2 compatibility for Workers | ||
| disableEval: true, | ||
| }); | ||
|
|
||
| // Create the Drizzle client with the mysql2 driver connection | ||
| const db = drizzle(connection); | ||
|
|
||
| // Sample query to get all users | ||
| const allUsers = await db.select().from(users); | ||
|
|
||
| return Response.json(allUsers); | ||
| }, | ||
| } satisfies ExportedHandler<Env>; | ||
| ``` | ||
|
|
||
| ### Configure Drizzle-Kit for migrations (optional) | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| 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. | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```toml | ||
| # Replace with your direct database connection string | ||
| DATABASE_URL='mysql://user:[email protected]/database-name' | ||
| ``` | ||
|
|
||
| 2. Create a `drizzle.config.ts` file in the root of your project to configure Drizzle Kit | ||
| and add the following content: | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| ```ts | ||
| import 'dotenv/config'; | ||
| import { defineConfig } from 'drizzle-kit'; | ||
| export default defineConfig({ | ||
| out: './drizzle', | ||
| schema: './src/db/schema.ts', | ||
| dialect: 'mysql', | ||
| dbCredentials: { | ||
| url: process.env.DATABASE_URL!, | ||
| }, | ||
| }); | ||
| ``` | ||
|
|
||
| 3. Generate the migration file for your database according to your schema files and apply the migrations to your database. | ||
| `bash | ||
| npx drizzle-kit generate | ||
| npx drizzle-kit migrate | ||
| ` | ||
Oxyjun marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
12 changes: 12 additions & 0 deletions
12
...docs/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/index.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,12 @@ | ||
| --- | ||
| pcx_content_type: navigation | ||
| title: Libraries and Drivers | ||
| sidebar: | ||
| order: 8 | ||
| group: | ||
| hideIndex: true | ||
| --- | ||
|
|
||
| import { DirectoryListing } from "~/components"; | ||
|
|
||
| <DirectoryListing /> |
15 changes: 15 additions & 0 deletions
15
...docs/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,15 @@ | ||
| --- | ||
| pcx_content_type: example | ||
| title: mysql | ||
| sidebar: | ||
| order: 2 | ||
| meta: | ||
| title: Using mysql driver with Hyperdrive | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; | ||
|
|
||
| The [mysql](https://github.com/mysqljs/mysql) package is a MySQL driver for Node.js. | ||
| This example demonstrates how to use it with Cloudflare Workers and Hyperdrive. | ||
|
|
||
| <Render file="use-mysql-to-make-query" product="hyperdrive" /> |
13 changes: 13 additions & 0 deletions
13
...ocs/hyperdrive/examples/connect-to-mysql/mysql-drivers-and-libraries/mysql2.mdx
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,13 @@ | ||
| --- | ||
| pcx_content_type: example | ||
| title: mysql2 | ||
| sidebar: | ||
| order: 1 | ||
| --- | ||
|
|
||
| import { Render } from "~/components"; | ||
|
|
||
| The [mysql2](https://github.com/sidorares/node-mysql2) package is a modern MySQL driver for Node.js with better performance and built-in Promise support. | ||
| This example demonstrates how to use it with Cloudflare Workers and Hyperdrive. | ||
|
|
||
| <Render file="use-mysql2-to-make-query" product="hyperdrive" /> |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.