• Stars
    star
    199
  • Rank 196,105 (Top 4 %)
  • Language
    TypeScript
  • License
    MIT License
  • Created over 2 years ago
  • Updated over 1 year ago

Reviews

There are no reviews yet. Be the first to send feedback to the community and the maintainers!

Repository Details

This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io)

npm version codecov CI

Alternative Prisma Data Proxy

This is a library to alternate and self-host the Prisma Data Proxy (cloud.prisma.io).

In order to deploy your project to edge runtimes (such as Cloudflare Workers or Vercel Edge Functions) and use Prisma, you will need to use the Prisma Data Proxy.
However, at present, instances can only be built in limited areas, and there are also delays caused by cold standby. This is a very stressful problem.

Therefore, we have created a server library to replace Prisma Data Proxy. With it, you are free from stressful limitations. You can deploy it on any platform in any region you like and use any data source you like, such as Supabase or Planetscale.

No changes are required to your prisma client code, just set the DATABASE_URL to the URL you self-hosted with this library.
This is not an official library, but it works the same as Prisma Data Proxy.

Overview

overview

Performance

Using the Alternative Prisma Data Proxy, a significant reduction in clause latency can be expected regardless of the region of the instance. See here for details.

Setup

If you are using @prsima/client v3, install prisma-data-proxy-alt@^1.
The latest library (v2) suports only @prima/client v4.

yarn add prisma-data-proxy-alt

Include prisma schema in your project. The same schema as the client.

cp your_client_project_path/prisma/schema.prisma ./prisma/schema.prisma

Install prisma and @prisma/client.

yarn add -D prisma
yarn add @prisma/client

Launch proxy server

Give environment variables by creating .env, etc.

PRISMA_SCHEMA_PATH=/absolute/path/for/your/schema.prisma
DATABASE_URL={database URL scheme e.g. postgresql://postgres:pass@db:5432/postgres?schema=public}
DATA_PROXY_API_KEY={random string for authentication}
PORT={server port e.g. 3000}
yarn pdp

This will bring up the proxy server, but it must be SSL-enabled to connect from @prisma/client.
So here are the steps to establish a local connection with SSL using docker-compose and https-portal with a self certificate.

Create entrypoint.sh.

#!/bin/sh

yarn install
exec "$@"

Create docker-compose.yml.

version: '3'

services:
  data-proxy:
    image: node:18-bullseye-slim
    working_dir: /app
    ports:
      - "3000:3000"
    entrypoint: /app/entrypoint.sh
    command: yarn pdp
    environment:
      PRISMA_SCHEMA_PATH: /app/for/your/schema.prisma
      DATABASE_URL: your DATABASE_URL
      DATA_PROXY_API_KEY: your DATA_PROXY_API_KEY
      PORT: "3000"
    volumes:
      - ./:/app:cached
      - node_modules:/app/node_modules
  https-portal:
    image: steveltn/https-portal:1
    ports:
      - "443:443"
    environment:
      STAGE: local
      DOMAINS: 'localhost -> http://data-proxy:3000'
    volumes:
      - ./ssl-certs:/var/lib/https-portal

volumes:
  node_modules:
docker-compose up

Now you can connect with data proxy with DATABASE_URL=prisma://localhost?api_key={DATA_PROXY_API_KEY}.

Deploy

GCP Cloud Run

Create Dockerfile

FROM node:16.15-bullseye-slim as base

RUN apt-get update && apt-get install -y tini ca-certificates \
  && apt-get clean \
  && rm -rf /var/lib/apt/lists/*

WORKDIR /app

FROM base as builder

COPY package.json .
COPY yarn.lock .
COPY prisma/schema.prisma ./prisma/schema.prisma

RUN yarn install

RUN yarn prisma generate

FROM base

COPY --from=builder /app/node_modules ./node_modules
COPY --from=builder /app/package.json ./package.json

ENV PRISMA_SCHEMA_PATH=/app/node_modules/.prisma/client/schema.prisma

USER node

ENTRYPOINT ["/usr/bin/tini", "--"]
CMD ["yarn", "pdp"]

Create cloudbuild.yml

steps:
  - name: 'gcr.io/kaniko-project/executor:latest'
    args:
      - --destination=gcr.io/$PROJECT_ID/prisma-data-proxy-alt:$SHORT_SHA
      - --destination=gcr.io/$PROJECT_ID/prisma-data-proxy-alt:latest
      - --cache=true
  - name: 'gcr.io/google.com/cloudsdktool/cloud-sdk'
    entrypoint: gcloud
    args:
      - run
      - deploy
      - prisma-data-proxy-alt
      - --image
      - gcr.io/$PROJECT_ID/prisma-data-proxy-alt:latest
      - --region
      - $_REGION
      - --allow-unauthenticated
      - --set-env-vars
      - DATABASE_URL=$_DATABASE_URL
      - --set-env-vars
      - DATA_PROXY_API_KEY=$_DATA_PROXY_API_KEY
substitutions:
  _REGION: asia-northeast1
  _DATABASE_URL: your_database_url
  _DATA_PROXY_API_KEY: your_api_key

Create a new trigger from the GCP Cloud Build web console and link it to your repository.

Set _REGION, _DATABASE_URL, and _DATA_PROXY_API_KEY in the substitution values.

  • _REGION: The region of deploy target for Cloud Run
  • _DATABASE_URL: Connection URL to your data source (mysql, postgres, etc...)
  • _DATA_PROXY_API_KEY: Arbitrary string to be used when connecting data proxy. e.g. prisma://your.deployed.domain?api_key={DATA_PROXY_API_KEY}
    (do not divulge it to outside parties)

For Client (on your application)

On the client side, generate the Prisma client in data proxy mode --data-proxy. official document

yarn prisma generate --data-proxy

Set the DATABSE_URL from the domain of the server you deployed and the api key (DATA_PROXY_API_KEY) you set for it.

DATABSE_URL=prisma://${YOUR_DEPLOYED_PROJECT_DOMAIN}?api_key=${DATA_PROXY_API_KEY}

Now you can connect to the (alternative) Data Proxy from your application. ๐ŸŽ‰

Contribution

Please read CONTRIBUTING.md for details on our code of conduct, and the process for submitting pull requests to us.

LICENCE

This project is licensed under the MIT License - see the LICENSE file for details

More Repositories

1

zod-i18n

Useful for translating zod error messages.
TypeScript
591
star
2

remix-esbuild-override

This is a library that makes it possible to change the configuration values of the Remix compiler (esbuild).
TypeScript
90
star
3

vitest-environment-vprisma

Vitest environment for testing with prisma.
TypeScript
70
star
4

next-with-split

This is a plugin for split testing (A/B testing) in Next.js.
TypeScript
68
star
5

next-qs-props

This library makes it possible to handle query strings in Next.js getStaticProps.
TypeScript
51
star
6

next-fortress

This is a Next.js plugin that redirects or rewrite for accesses that are not authenticated.
TypeScript
45
star
7

kiribi

๐ŸŽ‡ A simple job management library consisting of the Cloudflare stack.
TypeScript
43
star
8

turbo-with-prisma

TypeScript
26
star
9

next-image-loader

Plugin to transparently override the loader in next/image.
TypeScript
20
star
10

remix-service-bindings

TypeScript
17
star
11

use-postal-jp

้ƒตไพฟ็•ชๅทใ‚’ไฝๆ‰€ใซๅค‰ๆ›ใ™ใ‚‹Reactใ‚ซใ‚นใ‚ฟใƒ ใƒ•ใƒƒใ‚ฏใงใ™ใ€‚ไฝๆ‰€ใƒ‡ใƒผใ‚ฟใ‚’ๅ†…้ƒจใซๆŒใŸใšใ€APIใงไฝๆ‰€ๅค‰ๆ›ใ™ใ‚‹ใŸใ‚่ปฝ้‡ใชใƒ‘ใƒƒใ‚ฑใƒผใ‚ธใซใชใฃใฆใ„ใพใ™ใ€‚
TypeScript
17
star
12

prisma-fts-middleware

This library performs Prisma full-text search with external tools such as ElasticSearch, OpenSearch, and Algolia.
TypeScript
15
star
13

kv-cacheable

This library helps implement caching using Cloudflare Workers KV.
TypeScript
10
star
14

remix-emotion-on-cloudflare

TypeScript
9
star
15

sb-prisma

This is a project to make the supabase REST API available from PrismaClient.
TypeScript
9
star
16

blurhash-cf-worker

HTML
4
star
17

remix-chakra-ui-on-cloudflare

TypeScript
3
star
18

npm-runtime-search-api

TypeScript
3
star
19

authrize-preview-sample

TypeScript
3
star
20

vercel-og-on-cloudflare

TypeScript
3
star
21

remix-cf-service-bindings

JavaScript
2
star
22

cloudflare-meetup-nagoya-1

TypeScript
2
star
23

neon-sample

TypeScript
1
star
24

workers-cloud-gallery

TypeScript
1
star
25

cloudflare-fonts-example

TypeScript
1
star
26

botui-nx

TypeScript
1
star
27

saki-photo

TypeScript
1
star
28

cf-workers-event-tracker

TypeScript
1
star
29

botui

TypeScript
1
star
30

worker-puppetter

TypeScript
1
star
31

cfw-bq

This is a BigQuery client library for Cloudflare Workers. It allows you to query BigQuery from within a Cloudflare Worker.
TypeScript
1
star