-
Notifications
You must be signed in to change notification settings - Fork 125
feat(cloudflare): make Hyperdrive origin optional for local development #1146
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
Open
agcty
wants to merge
8
commits into
alchemy-run:main
Choose a base branch
from
agcty:feature/hyperdrive-no-origin-db-locally
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 7 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
f5088d0
test(cloudflare): comprehensive dead letter queue tests (#1093)
JacobMGEvans e97a76c
Merge branch 'main' of github.com:sam-goodwin/alchemy
agcty 92caa3a
Merge branch 'main' of github.com:sam-goodwin/alchemy
agcty 4984dc1
Merge branch 'main' of github.com:sam-goodwin/alchemy
agcty cd3c9f9
feat(cloudflare): make Hyperdrive origin optional for local development
agcty 812e2ae
add dev only test
Mkassabov 28f033f
add hyperdrive dev-only example
Mkassabov 821faa8
review fixes
Mkassabov 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
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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,33 @@ | ||
| # Prisma Postgres Example | ||
|
|
||
| This example provisions a Prisma Postgres project, database, and connection string using Alchemy. | ||
|
|
||
| ## Prerequisites | ||
|
|
||
| 1. Create a Prisma Postgres workspace service token. | ||
| 2. Export the token before running the example: | ||
|
|
||
| ```bash | ||
| export PRISMA_SERVICE_TOKEN="sk_..." | ||
| ``` | ||
|
|
||
| 3. Choose an Alchemy state password and export it (used to encrypt secrets locally): | ||
|
|
||
| ```bash | ||
| export ALCHEMY_PASSWORD="dev-password" | ||
| ``` | ||
|
|
||
| ## Usage | ||
|
|
||
| ```bash | ||
| bun i | ||
| ALCHEMY_PASSWORD=${ALCHEMY_PASSWORD:-dev-password} bun run alchemy.run.ts | ||
| ``` | ||
|
|
||
| The script prints the generated database connection string to stdout. | ||
|
|
||
| To tear down the resources: | ||
|
|
||
| ```bash | ||
| bun run destroy | ||
| ``` | ||
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,23 @@ | ||
| import alchemy from "alchemy"; | ||
| import { Hyperdrive, Worker } from "alchemy/cloudflare"; | ||
| import { Connection, Database, Project } from "alchemy/prisma-postgres"; | ||
|
|
||
| const app = await alchemy("alchemy-dev-only-hyperdrive"); | ||
|
|
||
| const project = await Project("project"); | ||
|
|
||
| const database = await Database("database", { | ||
| project, | ||
| region: "us-east-1", | ||
| }); | ||
|
|
||
| const connection = await Connection("connection", { database }); | ||
|
|
||
| const db = await Hyperdrive("dev-only-hyperdrive", { | ||
| origin: app.local ? undefined : connection.connectionString.unencrypted, | ||
| dev: { | ||
| origin: connection.connectionString.unencrypted, | ||
| }, | ||
| }); | ||
|
|
||
| await app.finalize(); |
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,18 @@ | ||
| { | ||
| "name": "cloudflare-dev-only-hyperdrive", | ||
| "version": "0.0.0", | ||
| "description": "Alchemy Cloudflare Dev Only Hyperdrive Example", | ||
| "type": "module", | ||
| "scripts": { | ||
| "build": "tsc -b", | ||
| "deploy": "alchemy deploy --env-file ../../.env", | ||
| "destroy": "alchemy destroy --env-file ../../.env" | ||
| }, | ||
| "devDependencies": { | ||
| "alchemy": "workspace:*", | ||
| "typescript": "catalog:" | ||
| }, | ||
| "dependencies": { | ||
| "pg": "^8.16.3" | ||
| } | ||
| } |
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,28 @@ | ||
| import { Client } from "pg"; | ||
| import type { worker } from "../alchemy.run.ts"; | ||
|
|
||
| export default { | ||
| async fetch(_request: Request, env: typeof worker.Env): Promise<Response> { | ||
| const client = new Client({ | ||
| connectionString: env.HYPERDRIVE.connectionString, | ||
| }); | ||
|
|
||
| try { | ||
| // Connect to the database | ||
| await client.connect(); | ||
| console.log("Connected to PostgreSQL database"); | ||
|
|
||
| // Perform a simple query | ||
| const result = await client.query("SELECT * FROM pg_tables"); | ||
|
|
||
| return Response.json({ | ||
| success: true, | ||
| result: result.rows, | ||
| }); | ||
| } catch (error: any) { | ||
| console.error("Database error:", error.message); | ||
|
|
||
| return new Response("Internal error occurred", { status: 500 }); | ||
| } | ||
Mkassabov marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| }, | ||
| }; | ||
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,8 @@ | ||
| { | ||
| "extends": "../../tsconfig.base.json", | ||
| "compilerOptions": { | ||
| "outDir": "dist", | ||
| "rootDir": "." | ||
| }, | ||
| "include": ["./alchemy.run.ts"] | ||
| } |
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.