iTranslated by AI

The content below is an AI-generated translation. This is an experimental feature, and may contain errors. View original article
🐷

Self-hosting Prisma Accelerate to Access Local Databases

に公開

Necessity of Prisma Accelerate on Edge Runtime

When using Prisma in a restricted environment like Edge Runtime, the Prisma Engine, which is responsible for issuing queries to the database, cannot run directly. This is because the Prisma Engine is written in Rust. While it may be supported in the future, it currently does not work. Therefore, when using Prisma on Edge Runtime, it is necessary to use it with the Prisma Engine decoupled.

This is where Prisma Accelerate, an official service from Prisma, comes in. By using this service, queries called from the Prisma Client are converted into JsonProtocol, bypass the local Prisma Engine, and are sent to Prisma Accelerate. There, the Prisma Engine operates to access each database and execute the queries. This makes it possible to use Prisma on Edge Runtime.

Why Do You Need to Self-Host Prisma Accelerate?

Prisma Accelerate can access pre-registered databases. The problem arises when developing locally. In a development environment, the database is often located on localhost. Prisma Accelerate cannot access a local database directly. While not impossible depending on the configuration, it is cumbersome.

As a result, a problem occurs because the way you write Prisma code for Edge Runtime differs from the way you write it for a direct database connection. Furthermore, in the case of Next.js, whether or not you write export const runtime = 'edge' makes the implementation different. Since Next.js's Webpack configuration for this 'edge' part is simplified, variables cannot be used. Therefore, the runtime cannot be easily switched using environment variables, creating discrepancies between development and production code.

To unify the development and production code, the method Prisma uses to access the database must be the same. For that purpose, it is necessary to self-host Prisma Accelerate.

Self-Hosting Prisma Accelerate

So, let's try self-hosting Prisma Accelerate right away.

The package being used is this one:
https://www.npmjs.com/package/prisma-accelerate-local

The following command is an example assuming access to PostgreSQL.

npx prisma-accelerate-local postgresql://postgres:password@localhost:5432/postgres -p 8000

This starts a Prisma Accelerate mock. On the Prisma Client side, set the following environment variables:

DATABASE_URL="prisma://localhost:8000/?api_key=xxx"
NODE_TLS_REJECT_UNAUTHORIZED="0"
# To remove the NODE_TLS_REJECT_UNAUTHORIZED warning
NODE_NO_WARNINGS="1"

NODE_TLS_REJECT_UNAUTHORIZED is necessary for accessing via https. NODE_NO_WARNINGS is required to suppress the warning from NODE_TLS_REJECT_UNAUTHORIZED. These won't be necessary if you configure the path to a verified certificate in the startup options.

Now Prisma Client can access the local database even on Edge Runtime. Since it is designed to behave basically the same as Prisma Accelerate, the prisma.schema is automatically transferred. Additionally, it is set up to download and run the compatible version of the Prisma Engine.

Summary

There was absolutely no information available regarding the processes Prisma Accelerate performs, so I managed to figure it out by placing a proxy in between to inspect the communication and by examining Prisma's source code for the rest. I really wish the official team would provide tools like this that are essential for development.

GitHubで編集を提案

Discussion