N
Neon15mo ago
deep-jade

How to use the serverless driver for regular postgres (in node.js)?

I have a SvelteKit app, that uses the Vercel or Node adapter depending on an environment variable. For the Vercel build we use Neon, but for the Node build, we want to use local postgres. Apparently I cannot do conditional paths in Sveltekit like the following cause the import itself causes the build to fail.
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http';
import { drizzle as drizzlePostgres } from 'drizzle-orm/postgres-js';

import postgres from 'postgres';
import { neon } from '@neondatabase/serverless';

import { sessionTable, userTable } from '../../schema';

import { DrizzlePostgreSQLAdapter } from '@lucia-auth/adapter-drizzle';

import { env } from '$env/dynamic/private';

const { PRIVATE_DRIZZLE_DATABASE_URL, PRIVATE_DRIZZLE_DATABASE_SERVERLESS } = env;

const sql =
PRIVATE_DRIZZLE_DATABASE_SERVERLESS == 'false'
? postgres(PRIVATE_DRIZZLE_DATABASE_URL)
: neon(PRIVATE_DRIZZLE_DATABASE_URL);

export const db =
PRIVATE_DRIZZLE_DATABASE_SERVERLESS == 'false' ? drizzlePostgres(sql) : drizzleNeon(sql);

export const adapter = new DrizzlePostgreSQLAdapter(db, sessionTable, userTable);
import { drizzle as drizzleNeon } from 'drizzle-orm/neon-http';
import { drizzle as drizzlePostgres } from 'drizzle-orm/postgres-js';

import postgres from 'postgres';
import { neon } from '@neondatabase/serverless';

import { sessionTable, userTable } from '../../schema';

import { DrizzlePostgreSQLAdapter } from '@lucia-auth/adapter-drizzle';

import { env } from '$env/dynamic/private';

const { PRIVATE_DRIZZLE_DATABASE_URL, PRIVATE_DRIZZLE_DATABASE_SERVERLESS } = env;

const sql =
PRIVATE_DRIZZLE_DATABASE_SERVERLESS == 'false'
? postgres(PRIVATE_DRIZZLE_DATABASE_URL)
: neon(PRIVATE_DRIZZLE_DATABASE_URL);

export const db =
PRIVATE_DRIZZLE_DATABASE_SERVERLESS == 'false' ? drizzlePostgres(sql) : drizzleNeon(sql);

export const adapter = new DrizzlePostgreSQLAdapter(db, sessionTable, userTable);
So I am wondering if there is any way to use the Serverless driver for a Serverful postgres?
4 Replies
afraid-scarlet
afraid-scarlet15mo ago
Do you mind sharing the error you're getting? Which part is failing?
deep-jade
deep-jadeOP15mo ago
I have found the main issue that Neon needs to solve for this. https://github.com/neondatabase/neon/issues/4989
Do you mind sharing the error you're getting? Which part is failing?
On Vercel I kept getting errors related to how "fs" "os" etc. are not available (because they are imported by postgres-js and the Vercel Edge Function runtime don't support them). We tried multiple things like dynamic imports but it kept failing.
GitHub
Epic: local dev setup for serverless proxy · Issue #4989 · neondata...
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: fast to spin up and has small footprint works without int...
afraid-scarlet
afraid-scarlet15mo ago
ah. This is an issue that we would like to address. I'm curious, have you considered using a Neon branch in development?
unwilling-turquoise
unwilling-turquoise14mo ago
Hey, I've got exactly the same problem. Our use-case is to run against the Neon backend in the cloud, but to also provide a on-prem version that uses a local stateful postgres database for customers that are hesitant to put their data in the cloud. So I think using branches (or other Neon features) will not help much, because the problem is the case where we're not using Neon in the backend. We tried to make it work using the approach from https://github.com/TimoWilhelm/local-neon-http-proxy , but that still requires a TLS connection to the database as far as I can tell, which is a bit painful to set up within the same docker compose stack
GitHub
GitHub - TimoWilhelm/local-neon-http-proxy: Connect to a local Post...
Connect to a local PostgreSQL database using the neon http proxy - TimoWilhelm/local-neon-http-proxy

Did you find this page helpful?