Use Data Proxy
To use the Data Proxy for a JavaScript/TypeScript application that uses Prisma, you must first create a project in Prisma Data Platform. When you create the project, you do the following:
- select a Data Proxy region for the project (make sure that it is close to your database)
- create a Data Proxy connection string
You can then configure the connection string in your development or deployment environment and generate Prisma Client for the Data Proxy.
Enable the Data Proxy for a project
When you create a project in the platform, the Data Proxy is automatically enabled for your project.
Prerequisites
- Create a project to add your application in the platform. During the process, select a geographic location for the Data Proxy that is closest to the location of your database.
- If your database is behind a firewall, enable static IP addresses for your project and add the IP addresses for the selected Data Proxy region to the allowlist of your database.
- US East 1 (West Virginia)
- 54.204.197.119
- 3.222.148.224
- 54.204.47.202
- 3.227.136.248
- EU Central 1 (Frankfurt, Germany)
- 35.157.74.165
- 18.194.25.248
- 18.184.112.103
- 18.157.219.25
- US East 1 (West Virginia)
Steps
Open your project in the Prisma Data Platform. You can do so from the Projects page.
Get a Data Proxy connection string for the selected environment.
With the project open and an environment selected, click the Data Proxy tab.
Click Create a connection string.
NoteYou can generate as many Data Proxy connection strings as you need. You can later revoke any of the connection strings that you no longer need.Enter a name for the new Data Proxy connection string and click Create.
Copy the
prisma://
connection string.ImportantSave theprisma://
connection string securely. After you navigate to another page, you cannot retrieve the connection string again from the Data Proxy. You can only generate a new connection string, if necessary.
Generate Prisma Client for the Data Proxy
After you create your project in the platform and enable the Data Proxy, you can generate Prisma Client for the Data Proxy and use it with the prisma://
connection string you obtained.
You can generate and use Prisma Client for the Data Proxy in the following environments:
- In your local development environment, to test the process.
- In your deployment platform. You must replace the direct database connection string with the Data Proxy connection string and make sure that Prisma Client is auto-generated with the
--data-proxy
flag as described below.
Prerequisites
- Enable Data Proxy for your project.
- Copy the Data Proxy connection string. Do so for the project environment for which you want to generate Prisma Client.
Steps
In your local development environment, replace the direct database connection string with the Data Proxy
prisma://
connection string..env- DATABASE_URL="postgresql://****:****@****"+ DATABASE_URL="prisma://****:****@****"Note
For security reasons, we strongly recommend that you always use environment variables for your database connection strings and Data Proxy strings. Do not hard-code them into yourschema.prisma
file.
For more information, see Using.env
files.Install
prisma
and@prisma/client
.
Make sure to use version 3.15.2 or later.$npm install prisma@latest --save-dev$npm install @prisma/client@latest --saveGenerate Prisma Client with the
--data-proxy
option.$npx prisma generate --data-proxyThe generated Prisma Client is a lightweight version because it excludes the local query engine files. The Data Proxy handles the query engine logic.
Instantiate Prisma Client in your application code.
To use the Data Proxy with Node.js, instantiate
@prisma/client
. Learn more.TypeScriptJavaScriptimport { PrismaClient } from '@prisma/client'const prisma = new PrismaClient()// use `prisma` in your application to read and write data in your DBTo use the Data Proxy in Edge runtimes, such as Cloudflare Workers or Vercel Edge Functions, instantiate
@prisma/client/edge
.NoteYou can use@prisma/client/edge
only with the Data Proxy. In such cases, make sure to use the--data-proxy
flag or thePRISMA_GENERATE_DATAPROXY=true
environment variable when you generate Prisma Client.TypeScriptJavaScriptimport { PrismaClient } from '@prisma/client/edge'const prisma = new PrismaClient()// use `prisma` in your application to read and write data in your DB
Result
Your application now uses the Data Proxy.
The generated Prisma Client has a reduced bundle size because the entire query engine logic is now hosted with the Data Proxy.
Manage the number of database connections
You can adjust the maximum number of database connections of each Data Proxy connection pool.
To do so, in the Prisma Data Platform add the connection_limit
parameter to the database connection string of the environment in your project.
postgresql://user:password@host:5432/mydb?connection_limit=60