Modified from prisma-edge-functions. Thanks for their excellent work.
Edge functions are serverless functions at The Edge. They are written in JavaScript or TypeScript and run on a global network. It allows running server-side code geographically close to end users and enjoy lower latency.
This sample application shows how you can work with TiDB Cloud and edge functions. And here is a live demo build on vercel.
- Go to TiDB Cloud and create a serverless tier cluster.
- Navigate to your serverless tier cluster's dashboard.
- Click Connect to set a password, you can also build the prisma connect string with this pattern
mysql://USER:PASSWORD@HOST:PORT/edge_function?sslaccept=strict.
- Connect to your serverless tier cluster and create a database named edge_function. This application will build on this database.
- Create a setup user with only the privileges needed to migrate and seed
edge_function. Keep this account separate from the read-only application user described below.
Initialize a new database before deploying locally, on Vercel, or on Netlify. Web builds only generate Prisma Client and compile the app.
-
Fork and clone this repository, then install dependencies before supplying database credentials:
git clone https://github.com/${user_name}/nextjs-prisma-edge-example.git cd nextjs-prisma-edge-example npm ci --ignore-scripts
-
On a trusted machine or in a separate migration job, run setup with the setup user's direct TiDB connection string:
export MIGRATE_DATABASE_URL='mysql://SETUP_USER:PASSWORD@HOST:PORT/edge_function?sslaccept=strict' npm run prisma:setup unset MIGRATE_DATABASE_URL
This generates the direct database client, applies migrations, and seeds the quotes. Run it only once: seeding appends data. For later schema changes, supply the setup credential and run only
npm run prisma:migrate. Retain migration output with the commit and operator, without logging credentials. -
Create a separate user for the application's proxy connection. After creating it with its own password, grant only the read access the app needs:
GRANT SELECT ON edge_function.Quote TO 'RUNTIME_USER'@'%';
Replace
RUNTIME_USERwith the actual TiDB username, including its cluster prefix where applicable. Check that it has no write or schema privileges through other grants or roles.
Edge environments only support HTTP-based connections. Therefore, we can not connect to TiDB Cloud which use long-lived TCP connections directly.
The Prisma Data Proxy is a proxy server for your database that allows you to interact with your database over HTTP, manages a connection pool, and ensures database connections are reused. It allows database access from Edge runtimes such as Vercel Edge Functions, Netlify Edge Functions and Cloudflare Workers.
-
sign in to the Prisma Data Platform and click New Project.
-
Fill in the Connection string with the read-only runtime user's credentials, using this pattern:
mysql://RUNTIME_USER:PASSWORD@HOST:PORT/edge_function?sslaccept=strict. Do not use the setup user's connection string. -
Configure TiDB Cloud firewall rules to allow only the proxy's outbound addresses and the trusted database setup environment. Enable static outbound IPs for the proxy where supported, and verify connectivity before removing broader rules.
-
Select a Data Proxy region that is geographically close to your TiDB Cloud cluster location. Then click Create project.
-
Click Skip and continue to Data Platform to go to the Data Platform.
-
Click Data Proxy in Data Platform, then click Create a new connection string to create a new connection string that starts with
prisma://. Copy and save it for later use. -
Click Schema Viewer in Data Platform, you can fill in the repository and click Link Prisma schema after you fork a repository. It is optional, if you don't link to your repository, prisma data proxy will sync the schema automatically by introspection mechanism.
-
Complete Initialize the database and Set up Prisma Data Proxy.
-
Optional: Link Prisma schema to the new repository you create. Follow Set up Prisma Data Proxy step 7.
-
Set
DATABASE_URLto the read-only Prisma Data Proxy connection string.export DATABASE_URL='prisma://aws-us-east-1.prisma-data.com/?api_key=YOUR_PROXY_KEY'
-
Generate Prisma Client that will connect through the Prisma Data Proxy using HTTP.
npm run prisma:proxy
-
Start the app.
npm run dev
🎉 Mission Completes.
The app is now running, navigate to http://localhost:3000/ in your browser to explore it.
The Deploy button will take you through Vercel's project creation flow. Vercel will help to clone this job to your own GitHub repository and automatically deploy it.
First complete Initialize the database and Set up Prisma Data Proxy.
- Click the Deploy button.
- Click GitHub button and authenticate GitHub account.
- Select your Git Scope and fill in Repository Name for your own GitHub repository.
- Click Create to create the git repository.
- Optional: Link Prisma schema to the new repository you create. Follow Set up Prisma Data Proxy step 7.
- Set
DATABASE_URLto the read-only Prisma Data Proxy connection string. Do not addMIGRATE_DATABASE_URLto the hosting project. - Click Deploy.
🎉 Mission Completes.
Now wait for the deployment to complete, then you can view your site on the default domain generated by Vercel.
The Deploy to Netlify button will take you to Netlify's deployment page. Then Netlify will help to clone this job to your own GitHub repository and automatically deploy it.
First complete Initialize the database and Set up Prisma Data Proxy.
- Click the Deploy to Netlify button.
- Click Connect to GitHub and authenticate GitHub account.
- Fill in Repository name for your own GitHub repository.
- Set
DATABASE_URLto the read-only Prisma Data Proxy connection string. Do not addMIGRATE_DATABASE_URLto the hosting project. - Click Save & Deploy.
- [Optional]: Link Prisma schema to the new repository you create. Follow Set up Prisma Data Proxy step 7.
🎉 Mission Completes.
Now wait for the deployment to complete, then you can view your site on the default domain generated by Netlify.
Before the next deployment, remove MIGRATE_DATABASE_URL and other setup credentials from every Vercel or Netlify environment. Repository changes do not delete saved hosting variables. Rotate previously hosted setup credentials and configure the proxy with the read-only application user.
Review dependency updates manually. Require PR approval and the CI build checks through branch protection. Limit deployment and migration access to maintainers, and use a separate database and proxy connection for previews.



