drizzle-team / drizzle-orm

Headless TypeScript ORM with a head. Runs on Node, Bun and Deno. Lives on the Edge and yes, it's a JavaScript ORM too 😅
https://orm.drizzle.team
Apache License 2.0
24.35k stars 634 forks source link

[BUG]: Vercel Edge + Supabase issues #1424

Open samducker opened 1 year ago

samducker commented 1 year ago

What version of drizzle-orm are you using?

0.28.6

What version of drizzle-kit are you using?

No response

Describe the Bug

I am trying to use drizzle over vercel edge and following the guides with cloudflare workers + supabase shown on the drizzle documentation (as vercel edge is just a cloudflare wrapper)

Doc: https://orm.drizzle.team/docs/quick-postgresql/supabase

This is my code

import { NextFetchEvent, NextRequest } from 'next/server';
import { Pool } from 'pg';
import { drizzle } from 'drizzle-orm/node-postgres';
import * as schema from '@/db/schema';
import { users } from '@/db/schema';
import { eq } from 'drizzle-orm';
import { createSuccessResponse } from '../../common/errors-and-responses';

export const runtime = 'edge';
export const dynamic = 'auto';
export const dynamicParams = true;
export const revalidate = false;
export const fetchCache = 'auto';
export const preferredRegion = 'iad1';
export const maxDuration = 5;

export async function GET(request: NextRequest, context: NextFetchEvent) {
  const pool = new Pool({ connectionString: process.env.DATABASE_POOL_URL });
  const db = drizzle(pool, { schema });
  const user = db.query.users.findFirst({
    where: eq(users.email, 'myemail@gmail.com')
  });
  context.waitUntil(pool.end());
  return createSuccessResponse({ user });
}

This is my error

Import trace for requested module:
./node_modules/pg/lib/native/client.js
./node_modules/pg/lib/native/index.js
./node_modules/pg/lib/index.js
./src/app/api/(application)/testing/route.ts
./node_modules/next/dist/build/webpack/loaders/next-app-loader.js?name=app%2Fapi%2F(application)%2Ftesting%2Froute&page=%2Fapi%2F(application)%2Ftesting%2Froute&appPaths=&pagePath=private-next-app-dir%2Fapi%2F(application)%2Ftesting%2Froute.ts&appDir=%2FUsers%2Fsam%2FDocuments%2FGitHub%2Flucky-duck%2Fmybuyside%2Fsrc%2Fapp&pageExtensions=tsx&pageExtensions=ts&pageExtensions=jsx&pageExtensions=js&rootDir=%2FUsers%2Fsam%2FDocuments%2FGitHub%2Flucky-duck%2Fmybuyside&isDev=true&tsconfigPath=tsconfig.json&basePath=&assetPrefix=&nextConfigOutput=&preferredRegion=iad1&middlewareConfig=e30%3D!./src/app/api/(application)/testing/route.ts?__next_edge_ssr_entry__
 ⚠ ./node_modules/pg/lib/native/client.js
Module not found: Can't resolve 'pg-native' in '/Users/sam/Documents/GitHub/lucky-duck/mybuyside/node_modules/pg/lib/native'

normal (non edge) works fine I am using supabase connection pooler also

Expected behavior

No response

Environment & setup

No response

rphlmr commented 1 year ago

Isn't node-pg relying on node? Edge/cloudflare is a non node runtime.

You could switch to postgres.js https://github.com/porsager/postgres

samducker commented 1 year ago

I was just following the instructions from the official doc related to cloudflare worker which is also edge.

I will try this!

On Sat, Oct 28 2023 at 8:54 am, Raphaël Moreau < @.*** > wrote:

Isn't node-pg relying on node? Edge/cloudflare is a non node runtime.

You could switch to postgres.js https://github.com/porsager/postgres

— Reply to this email directly, view it on GitHub ( https://github.com/drizzle-team/drizzle-orm/issues/1424#issuecomment-1783739142 ) , or unsubscribe ( https://github.com/notifications/unsubscribe-auth/AF7VYSYGD5CORCA57HGLKJDYBS233AVCNFSM6AAAAAA6REST7OVHI2DSMVQWIX3LMV43OSLTON2WKQ3PNVWWK3TUHMYTOOBTG4ZTSMJUGI ). You are receiving this because you authored the thread. Message ID: <drizzle-team/drizzle-orm/issues/1424/1783739142 @ github. com>

biohackerellie commented 1 year ago

Wanted to add to this, that I did try using postgres-js driver with supabase and I'm still experiencing the same issues as @samducker. Both :

import * as schema from './schema';
import { drizzle } from 'drizzle-orm/postgres-js';
import { Pool } from 'pg';

const connectionSring = process.env.DATABASE_URL as string;
const pool = new Pool({ connectionString: connectionSring });
export const db = drizzle(pool, { schema });

and

import * as schema from './schema';
import { drizzle } from 'drizzle-orm/postgres-js';
import postgres from 'postgres';

const connectionSring = process.env.DATABASE_URL as string;
const client = postgres(connectionSring);
export const db = drizzle(client, { schema });

have the same issues with edge runtime and supabase.

Would likely need a whole new driver in order for this functionality to work. Personally considering switching to neon because I don't want to go back to prisma accelerate just for edge functions 😅