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

Overview

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

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.

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:
      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

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)

Vercel

Create api/index.js

exports.default = require("prisma-data-proxy-alt/dist/server").default;

Set postinstall in the scripts of package.json.

  "scripts": {
    "postinstall": "prisma generate"
  },

Create vercel.json

{
  "version": 2,
  "routes": [
    {
      "src": "/.*",
      "dest": "/api/index.js"
    }
  ]
}

Open the Vercel web console and link the repository to the project. Then set the DATABASE_URL and DATA_PROXY_API_KEY as environment variables and deploy.

  • _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

Comments
  • Error opening a TLS connection (unable to get local issuer certificate)

    Error opening a TLS connection (unable to get local issuer certificate)

    Hi,

    I followed the documentation and created a docker-compose file with env variables set for the database (Planetscale Tokyo region), prisma schema path, data proxy API key and port. When running docker compose up, I get the following error:

    ...
    prisma-data-proxy-alt-https-portal-1  | [cont-init.d] done.
    prisma-data-proxy-alt-https-portal-1  | [services.d] starting services
    prisma-data-proxy-alt-https-portal-1  | [services.d] done.
    prisma-data-proxy-alt-data-proxy-1    | [3/4] Linking dependencies...
    prisma-data-proxy-alt-data-proxy-1    | [4/4] Building fresh packages...
    prisma-data-proxy-alt-data-proxy-1    | success Saved lockfile.
    prisma-data-proxy-alt-data-proxy-1    | Done in 19.17s.
    prisma-data-proxy-alt-data-proxy-1    | yarn run v1.22.19
    prisma-data-proxy-alt-data-proxy-1    | $ /app/node_modules/.bin/pdp
    prisma-data-proxy-alt-data-proxy-1    | prisma:info Starting a mysql pool with 9 connections.
    prisma-data-proxy-alt-data-proxy-1    | 🔮 Alternative Prisma Data Proxy listening on port 3000
    prisma-data-proxy-alt-data-proxy-1    | prisma:info Encountered error during initialization:
    prisma-data-proxy-alt-data-proxy-1    | prisma:error 
    prisma-data-proxy-alt-data-proxy-1    | Error opening a TLS connection: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:../ssl/statem/statem_clnt.c:1914: (unable to get local issuer certificate)
    prisma-data-proxy-alt-data-proxy-1    | /app/node_modules/@prisma/client/runtime/index.js:24071
    prisma-data-proxy-alt-data-proxy-1    |                 const err = new PrismaClientInitializationError(json.message, this.clientVersion, json.error_code);
    prisma-data-proxy-alt-data-proxy-1    |                             ^
    prisma-data-proxy-alt-data-proxy-1    | 
    prisma-data-proxy-alt-data-proxy-1    | PrismaClientInitializationError: Error opening a TLS connection: error:1416F086:SSL routines:tls_process_server_certificate:certificate verify failed:../ssl/statem/statem_clnt.c:1914: (unable to get local issuer certificate)
    prisma-data-proxy-alt-data-proxy-1    |     at LineStream.<anonymous> (/app/node_modules/@prisma/client/runtime/index.js:24071:29)
    prisma-data-proxy-alt-data-proxy-1    |     at LineStream.emit (node:events:513:28)
    prisma-data-proxy-alt-data-proxy-1    |     at addChunk (node:internal/streams/readable:324:12)
    prisma-data-proxy-alt-data-proxy-1    |     at readableAddChunk (node:internal/streams/readable:297:9)
    prisma-data-proxy-alt-data-proxy-1    |     at Readable.push (node:internal/streams/readable:234:10)
    prisma-data-proxy-alt-data-proxy-1    |     at LineStream._pushBuffer (/app/node_modules/@prisma/client/runtime/index.js:20137:17)
    prisma-data-proxy-alt-data-proxy-1    |     at LineStream._transform (/app/node_modules/@prisma/client/runtime/index.js:20131:8)
    prisma-data-proxy-alt-data-proxy-1    |     at Transform._write (node:internal/streams/transform:175:8)
    prisma-data-proxy-alt-data-proxy-1    |     at writeOrBuffer (node:internal/streams/writable:392:12)
    prisma-data-proxy-alt-data-proxy-1    |     at _write (node:internal/streams/writable:333:10) {
    prisma-data-proxy-alt-data-proxy-1    |   clientVersion: '4.2.1',
    prisma-data-proxy-alt-data-proxy-1    |   errorCode: 'P1011'
    prisma-data-proxy-alt-data-proxy-1    | }
    prisma-data-proxy-alt-data-proxy-1    | 
    prisma-data-proxy-alt-data-proxy-1    | Node.js v18.7.0
    prisma-data-proxy-alt-data-proxy-1    | error Command failed with exit code 1.
    prisma-data-proxy-alt-data-proxy-1    | info Visit https://yarnpkg.com/en/docs/cli/run for documentation about this command.
    prisma-data-proxy-alt-data-proxy-1 exited with code 1
    

    Do you happen to know the reason for that error? I don't really understand what is meant with (unable to get local issuer certificate). Should that be on my local machine, in Docker or is that something on the Planetscale side? How do I create that?

    I am on Mac OS Monterey 12.5

    I would appreciate your help a lot!

    Regards, Oka

    opened by MyNameIsOka 12
  • Does this data proxy support `include`?

    Does this data proxy support `include`?

    Does this data proxy support include?

    I tried this query:

        const leaderboardRows = await prisma.leaderboard_row.findMany({
            include: {
                profile: true,
            },
            where: {
                leaderboard_id: leaderboardId,
            },
            skip: start - 1,
            take: count,
            orderBy: {
                ['rank']: 'asc',
            },
        });
    

    and got:

      Invalid `prisma.leaderboard_row.findMany()` invocation:
      Cannot return null for non-nullable field leaderboard_row.profile.
    

    Data proxy logs:

    prisma:query SELECT 1
    prisma:query SELECT "public"."leaderboard_row"."leaderboard_id", "public"."leaderboard_row"."profile_id", "public"."leaderboard_row"."name", "public"."leaderboard_row"."rank", "public"."leaderboard_row"."rating", "public"."leaderboard_row"."last_match_time", "public"."leaderboard_row"."drops", "public"."leaderboard_row"."losses", "public"."leaderboard_row"."streak", "public"."leaderboard_row"."wins", "public"."leaderboard_row"."updated_at", "public"."leaderboard_row"."rank_country" FROM "public"."leaderboard_row" WHERE "public"."leaderboard_row"."leaderboard_id" = $1 ORDER BY "public"."leaderboard_row"."rank" ASC LIMIT $2 OFFSET $3
    

    When I removed the include the query succeeded:

        const leaderboardRows = await prisma.leaderboard_row.findMany({
            // include: {
            //     profile: true,
            // },
            where: {
                leaderboard_id: leaderboardId,
            },
            skip: start - 1,
            take: count,
            orderBy: {
                ['rank']: 'asc',
            },
        });
    
    opened by denniske 2
  • Not working in Vercel

    Not working in Vercel

    Hi the build is successful but it's not working tho I just copied the examples in the repo

    can someone help me how to deploy it properly in vercel thank you!

    image

    image

    image

    opened by glendell03 1
  • Recieved corrupt message when running locally

    Recieved corrupt message when running locally

    Hi, thank you for your work on this project, it's been a real help using Prisma with Deno.

    However, when trying to use the proxy I get an error:

    Cannot fetch data from service:
    error sending request for url (https://localhost:3851/4.7.1/b7b4fb7095922bd7b7c1126f4ffb522c4a94a7c7baa9310b74ef3789231d8756/graphql): error trying to connect: received corrupt message
        at wn.handleRequestError (file:///.../backend/generated/client/runtime/edge-esm.js:80:18717)
        at wn.handleAndLogRequestError (file:///.../backend/generated/client/runtime/edge-esm.js:80:18249)
        at wn.request (file:///.../generated/client/runtime/edge-esm.js:80:18087)
        at async t._request (file:///.../backend/generated/client/runtime/edge-esm.js:97:2022)
        at async file:///.../backend/src/prisma.ts:27:15
    Watcher Process finished. Restarting on file change...
    

    First thing I noticed was that it was trying to use https://. Is this perhaps the issue?

    I put a console.log in the beforeMiddleware.js to log every request. Requests sent manually via curl to the proxy-alt server log to the console, but when using the Prisma client nothing gets logged, possibly indicating it never actually reached the server. I have a feeling it's expecting an https response and getting an http one, or something along those lines.

    I'd like to run the proxy on localhost for integration tests :)

    opened by jeremyjacob 1
  • Prisma Client Docker Build errors

    Prisma Client Docker Build errors

    Hello,

    I have tried using the docker-compose.yml, and Dockerfile example, and none have worked even though the prisma generate command is ran.

    Docker container output log

    Error: @prisma/client did not initialize yet. Please run "prisma generate" and try to import it again.
    

    In my package.json I have the following versions of Prisma.

    "@prisma/client": "^4.7.1",
    "prisma": "^4.7.1",
    

    Could the aforementioned Prisma version be causing the issue?

    Is Prisma version v4.0.0 the only version that is currently compatible?

    opened by NorkzYT 1
  • Can you provide more details about the benchmark setup?

    Can you provide more details about the benchmark setup?

    Hey 👋 ,

    I am from Prisma and we learned about your awesome work today! Thanks for doing this for the open source community. 🙏 We have also seen your interesting benchmarks. Can you provide more details about the setup? We would like to reproduce those results and need the schema file and the test data.

    This would help us a lot in tracking this down and improving on our end.

    opened by mavilein 3
  • Different Graphql Server?

    Different Graphql Server?

    Is there any specific reason for using apollo-server? It adds a bunch of overhead that is not needed?

    https://github.com/benawad/node-graphql-benchmarks

    opened by JonasRothmann 3
Releases(v2.0.2)
Owner
AijiUejima
I'm working for an IT company in Nagoya, Japan on web development
AijiUejima
Open-source continuous regression testing that you can self-host.

Touca Touca helps you see the side-effects of your changes, as you write code. Touca SDKs let you describe the behavior and performance of your softwa

Touca 135 Dec 30, 2022
DropSpace is an easy way to self-host a file drop.

DropSpace Simple file sharing made in Laravel About DropSpace DropSpace is an easy way to self-host a file drop. Simply upload your file, and share th

Kazó Levente 25 Dec 9, 2022
switch Alternate Style Sheets

alternate-stylesheets.js switch Alternate Style Sheets Installation npm $ npm install alternate-stylesheets yarn $ yarn add alternate-stylesheets Usag

sasaplus1 1 Dec 11, 2021
A NestJS module that allows you use Prisma, set up multiple Prisma services, and use multi-tenancy in each Prisma service.

NestJS Prisma Module Installation To use this package, first install it: npm i @sabinthedev/nestjs-prisma Basic Usage In order to use this package, yo

Sabin Adams 39 Dec 2, 2022
proxy 🦄 yxorp is your Web Proxy as a Service (SAAS) Multi-tenant, Multi-Threaded, with Cache & Article Spinner

proxy ?? yxorp is your Web Proxy as a Service (SAAS) Multi-tenant, Multi-Threaded, with Cache & Article Spinner. Batteries are included, Content Spinning and Caching Engine, all housed within a stunning web GUI. A unique high-performance, plug-and-play, multi-threaded website mirror and article spinner

4D/ҵ.com Dashboards 13 Dec 30, 2022
Proxy but misspelled -- closed proxy for the internet

pyrox Proxy that runs on Cloudflare Workers. Setup Install wrangler2. npm install wrangler. Generate a public Ed25519 key, exported under SPKI mode wi

bots.gg 10 Sep 9, 2022
Create a self-maintaining index via proxy

proxy-indexer Proxy-indexer allows you to easily index collections of mutable objects based on their own mutable properties. While this is relatively

Anthony Manning-Franklin 5 Nov 18, 2022
Fullstack Turborepo starter. Typescript, Nestjs, Nextjs, Tailwind, Prisma, Github Actions, Docker, And Reverse proxy configured

Turborepo (NestJS + Prisma + NextJS + Tailwind + Typescript + Jest) Starter This is fullstack turborepo starter. It comes with the following features.

Ejaz Ahmed 132 Jan 9, 2023
Deploy your uAdmin project and host it with a single click (and a few more steps 👀) !

Golang and uAdmin CI/CD using ?? Railway! Steps to Deploy Make sure to create a Github account and link it with ?? Railway Click Give your new Reposit

Gaurav Gosain 6 Nov 13, 2022
Create and Host a dark Web Site (Tor Hidden Service)

Tutorial: Hosting a site on Dark Web (Tor Hidden Service) Introduction Before starting, lets clear some questions that might come up in your mind: Wha

Arnav Kumar 16 Oct 18, 2022
Prisma +2 generator to emit Yup schemas from your Prisma schema

Prisma Yup Generator Automatically generate Yup schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 31 Dec 24, 2022
Prisma 2+ generator to emit Joi schemas from your Prisma schema

Prisma Joi Generator Automatically generate Joi schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 26 Dec 24, 2022
Prisma +2 generator to emit a tRPC shield from your Prisma schema

Prisma tRPC Shield Generator Automatically generate a tRPC Shield from your Prisma Schema. Updates every time npx prisma generate runs. Table of Conte

Omar Dulaimi 27 Dec 24, 2022
Prisma 2+ generator to emit Zod schemas from your Prisma schema

Prisma Zod Generator Automatically generate Zod schemas from your Prisma Schema, and use them to validate your API endpoints or any other use you have

Omar Dulaimi 212 Dec 27, 2022
Host your own replica server instantly on Glitch.

Earthstar Replica Server for Glitch Want your own replica server? Click the button above. Create a file at .data/known_shares.json Add the shares you'

Earthstar Project 5 Apr 26, 2022
A WASI implementation that uses VS Code's extension host as the implementing API

Project This repo has been populated by an initial template to help get you started. Please make sure to update the content to build a great experienc

Microsoft 93 Dec 24, 2022
🪶 An light weight image host built using typescript.

Feather; an next-gen image uploader built to be used with sharex. Built using typescript, expressjs. To get started ( IN ORDER ) ~ Hosting ~ npm i ~ n

Saige 5 Jun 14, 2022
Deploys a AWS Bastion Host preconfigured for Tailscale access.

cdk-tailscale-bastion This packages creates an AWS Bastion configured for Tailscale. This covers steps 1,2 & 4 of the Tailscale RDS guide. You may fin

JT 10 Dec 3, 2022
Ansible template that shows how to create dynamic staging servers with ..com

Vagrant + Ansible + Docker Swarm + Trafeik + NGINX template Template repo that creates virtual machines with Docker, Docker Swarm, Traefik, NGINX. It

Mateusz Bagiński 4 Oct 15, 2022