-
Notifications
You must be signed in to change notification settings - Fork 490
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Epic: local dev setup for serverless proxy #4989
Comments
Indeed, this would be awsome! 🚀 Something very simple for a start would be great (for instance, Docker images are great, but if you target development, this should be flexible enough as to permit all dev setups). --edit: found additional info in this issue... In particular, the fact that websockets are only used if you use Client or Pool, not the neon() function |
I already commend on the closed thread (neondatabase/serverless#33 (comment)) but I think it makes sense to share it here too. I've taken the steps from neondatabase/serverless#33 (comment) and created a Docker compose project to simplify the setup process for a local neon proxy + PostgreSQL DB when using the You can find the repo here: https://github.com/TimoWilhelm/local-neon-http-proxy. |
@kelvich I would consider this a high impact. We almost didn't consider neon because our developers prefer to have a fully offline localhost development option. Installing docker and running docker is a huge burden for those of us who have simple infrastructure (node+postgres only in our case). For a lot of folks considering neon, having neon appear like a drop-in replacement for postgres would remove a lot of hesitation. It's almost there except for the localhost experience :) Perhaps this could be designed to work outside of the box? For example, the library could auto-detect whether a connection string is a neon string or a regular postgres string and use a built-in proxy to reduce the number of moving parts. Maybe wsproxy could be ported to javascript to reduce the number of moving parts and steps to installation. |
A bit more context: neondatabase/serverless#33 |
I tried to get pretty far on this with a local postgres database running in docker with a NextJS edge function, using a bunch of different |
For those arriving here, I did get this working... Here is my setup (big thanks for @kincaidoneil who cracked the code with disabling import {
Pool as NeonPool,
neonConfig,
} from "@neondatabase/serverless";
import {
PrismaNeon,
} from "@prisma/adapter-neon";
import { PrismaClient } from "@prisma/client";
import ws from "ws";
neonConfig.webSocketConstructor = typeof WebSocket !== "undefined" ? WebSocket : ws;
function buildNeonClient(databaseUrl: string, isLocalDevelopment: boolean): PrismaClient {
// https://gist.github.com/kincaidoneil/bc2516111f0ec8850cd6020b8191b27b
if (isLocalDevelopment) {
neonConfig.pipelineConnect = false;
neonConfig.useSecureWebSocket = false;
neonConfig.wsProxy = () => "localhost:5433";
}
const neon = new NeonPool({ connectionString: databaseUrl });
const adapter = new PrismaNeon(neon);
const prisma = new PrismaClient({ adapter });
return prisma;
} Then, to make this work, run a websocket -> TCP proxy like this: # Your database URL would point to this proxy, assuming postgres is at localhost:5432
# example: DATABASE_URL=postgresql://postgres:password@localhost:5433/postgres
wstcp --log-level debug --bind-addr 127.0.0.1:5433 127.0.0.1:5432 |
I've noticed this works well when accessing the db from the front end (for example taking some action on the front end that calls an API route) but for some reason when I use Postman to hit the api endpoints directly it doesn't proxy the request correctly--the db call fails in the middleware.ts file. This is what the error looks like:
proxy logs:
I don't have a solution yet but I'll update this if I figure something out |
followed all your steps and still get "Error: PrismaClient is unable to run in this browser environment, or has been bundled for the browser (running in |
Motivation
With more adoption of serverless driver we got more requests for local and CI setups for proxy.
DoD
We have local setup that is:
Implementation ideas
So far we had two ideas:
neon dev
commandSecond approach is intriguing but it is way more work (e.g. we should distribute binaries in npm, like esbuild does) so it seems better to proceed with docker image approach for now.
For the local experience to be end-to-end we need the following things:
So we can target for the following flow:
To make things easier to use I suggest to embed postgres into the image. At the same time it would be nice to support env var that can supply external postgres URL for cases when postgres is already configured.
The text was updated successfully, but these errors were encountered: