Skip to content
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

feat: add edge deployment examples #5738

Open
wants to merge 3 commits into
base: latest
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

This example demonstrates how to deploy a [Cloudflare Worker](https://workers.cloudflare.com/) with a [D1 database](https://developers.cloudflare.com/d1/). It's based on Cloudflare's [`hello-world`](https://github.com/cloudflare/workers-sdk/tree/4fdd8987772d914cf50725e9fa8cb91a82a6870d/packages/create-cloudflare/templates/hello-world) template for Workers.


## Getting started

### 1. Download example and install dependencies
Expand Down Expand Up @@ -115,11 +114,8 @@ You can now access the Worker on `https://prisma-d1-example.__USERNAME__.workers
[{"id":1,"email":"[email protected]","name":"Jane Doe (Remote)"}]
```


## Notes about migrations

This project uses an already existing [SQL schema](./prisma/schema.sql) file which you execute via `wrangler d1 execute` to keep the demo simple.

If you want to evolve your schema and keep track of the migration history, you can take a look at the recommended [migration workflows](https://www.prisma.io/docs/orm/overview/databases/cloudflare-d1#migration-workflows) for D1 in the Prisma docs.


Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
# Deploy a Cloudflare Worker with Neon Database

This example demonstrates how to deploy a [Cloudflare Worker](https://workers.cloudflare.com/) with [Neon Database](https://neon.tech/).

## Getting started

### 1. Download example and install dependencies

Download this example:

```
tbd
```

Install npm dependencies:

```
cd with-neon
npm install
```

### 2. Set the database connection locally

When running locally, Cloudflare Workers read their environment variables from a file called `.dev.vars`. Create this file:

```
touch .dev.vars
```

Next, add your database connection URL as an environment variable in that file:

```
DATABASE_URL="postgresql://user:password@host/neondb?sslmode=require"
```

It might look like this:

```
DATABASE_URL="postgresql://janedoe:mypassword42@ep-nameless-pond-a23b1mdz.eu-central-1.aws.neon.tech/neondb?sslmode=require"
```

> **Note**: If you don't have a database connection URL yet, you can create a new database in the Neon dashboard.

### 3. Migrate and seed the database

Run the following command to create the `User` table in your database:

```
npm run env -- npx prisma migrate dev --name init
```

> **Note**: The `.dev.vars` file used by Cloudflare Workers is incompatible with `.env` files that are used by Prisma ORM. That's why the `prisma` command is prefixed with the custom [env](./package.json#L9) script to load the environment variables and make them available to the Prisma CLI. Learn more in the [docs](https://www.prisma.io/orm/prisma-client/deployment/edge/deploy-to-cloudflare#setting-your-database-connection-url-as-an-environment-variable).

### 4. Run the worker locally

You can now run the Worker locally using the following command:

```
npm run dev
```

You can now access the Worker on [`http://localhost:8787`](http://localhost:8787).

### 5. Set the `DATABASE_URL` environment variable and deploy the Worker

To deploy the Worker, you first need to the `DATABASE_URL` environment variable via the `wrangler` CLI:

```
npx wrangler secret put DATABASE_URL
```

The command is interactive and will ask you to enter the value for the `DATABASE_URL` env var as the next step in the terminal.

> **Note**: This command requires you to be authenticated, and will ask you to log in to your Cloudflare account in case you are not.

Then you can go ahead then deploy the worker:

```
npx wrangler deploy
```
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deploy a Cloudflare Worker with PlanetScale
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deploy a Cloudflare Worker with PostgreSQL
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deploy a Vercel Edge Function with Neon Database
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deploy a Vercel Edge Function with PlanetScale
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
# Deploy a Vercel Edge Function with Vercel Postgres
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@

This example demonstrates how to deploy a [Cloudflare Worker](https://workers.cloudflare.com/) with a [D1 database](https://developers.cloudflare.com/d1/). It's based on Cloudflare's [`hello-world`](https://github.com/cloudflare/workers-sdk/tree/4fdd8987772d914cf50725e9fa8cb91a82a6870d/packages/create-cloudflare/templates/hello-world) template for Workers.


## Getting started

### 1. Download example and install dependencies
Expand Down Expand Up @@ -115,11 +114,8 @@ You can now access the Worker on `https://prisma-d1-example.__USERNAME__.workers
[{"id":1,"email":"[email protected]","name":"Jane Doe (Remote)"}]
```


## Notes about migrations

This project uses an already existing [SQL schema](./prisma/schema.sql) file which you execute via `wrangler d1 execute` to keep the demo simple.

If you want to evolve your schema and keep track of the migration history, you can take a look at the recommended [migration workflows](https://www.prisma.io/docs/orm/overview/databases/cloudflare-d1#migration-workflows) for D1 in the Prisma docs.


Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@
},
"devDependencies": {
"@cloudflare/vitest-pool-workers": "0.2.3",
"@cloudflare/workers-types": "4.20240423.0",
"@cloudflare/workers-types": "^4.20240320.1",
"prisma": "5.13.0",
"typescript": "5.4.5",
"vitest": "1.5.2",
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
# http://editorconfig.org
root = true

[*]
indent_style = tab
tab_width = 2
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.yml]
indent_style = space
172 changes: 172 additions & 0 deletions deployment-platforms/edge/cloudflare-workers/with-neon/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,172 @@
# Logs

logs
_.log
npm-debug.log_
yarn-debug.log*
yarn-error.log*
lerna-debug.log*
.pnpm-debug.log*

# Diagnostic reports (https://nodejs.org/api/report.html)

report.[0-9]_.[0-9]_.[0-9]_.[0-9]_.json

# Runtime data

pids
_.pid
_.seed
\*.pid.lock

# Directory for instrumented libs generated by jscoverage/JSCover

lib-cov

# Coverage directory used by tools like istanbul

coverage
\*.lcov

# nyc test coverage

.nyc_output

# Grunt intermediate storage (https://gruntjs.com/creating-plugins#storing-task-files)

.grunt

# Bower dependency directory (https://bower.io/)

bower_components

# node-waf configuration

.lock-wscript

# Compiled binary addons (https://nodejs.org/api/addons.html)

build/Release

# Dependency directories

node_modules/
jspm_packages/

# Snowpack dependency directory (https://snowpack.dev/)

web_modules/

# TypeScript cache

\*.tsbuildinfo

# Optional npm cache directory

.npm

# Optional eslint cache

.eslintcache

# Optional stylelint cache

.stylelintcache

# Microbundle cache

.rpt2_cache/
.rts2_cache_cjs/
.rts2_cache_es/
.rts2_cache_umd/

# Optional REPL history

.node_repl_history

# Output of 'npm pack'

\*.tgz

# Yarn Integrity file

.yarn-integrity

# dotenv environment variable files

.env
.env.development.local
.env.test.local
.env.production.local
.env.local

# parcel-bundler cache (https://parceljs.org/)

.cache
.parcel-cache

# Next.js build output

.next
out

# Nuxt.js build / generate output

.nuxt
dist

# Gatsby files

.cache/

# Comment in the public line in if your project uses Gatsby and not Next.js

# https://nextjs.org/blog/next-9-1#public-directory-support

# public

# vuepress build output

.vuepress/dist

# vuepress v2.x temp and cache directory

.temp
.cache

# Docusaurus cache and generated files

.docusaurus

# Serverless directories

.serverless/

# FuseBox cache

.fusebox/

# DynamoDB Local files

.dynamodb/

# TernJS port file

.tern-port

# Stores VSCode versions used for testing VSCode extensions

.vscode-test

# yarn v2

.yarn/cache
.yarn/unplugged
.yarn/build-state.yml
.yarn/install-state.gz
.pnp.\*

# wrangler project

.dev.vars
.wrangler/
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"printWidth": 140,
"singleQuote": true,
"semi": true,
"useTabs": true
}
Loading
Loading