Drizzle supabase timeout issues

chachewHOBBY

3 months ago

I am trying to connect to supabase via drizzle in my nuxt 3 application, but any time i try to query something from the database it just times out after about 2 minutes.

I can connect to supabase fine when using:

import { createClient, type SupabaseClient } from '@supabase/supabase-js';

let _supabaseClient: SupabaseClient | null = null;
export const supabaseClient = (): SupabaseClient => {
  if (!_supabaseClient) {
    _supabaseClient = createClient(env.DATABASE_API_URL, env.DATABASE_SERVICE_ROLE);
  }
  return _supabaseClient as SupabaseClient;
};

const supabase = supabaseClient();
const _user = await supabase.auth.getUser('<jwt>'); <-------- Works

But NOT when i try to make a connection using:

import { drizzle } from 'drizzle-orm/node-postgres';
import { eq } from 'drizzle-orm';
import pg from 'pg';
import * as schema from './schema/index';
import profile from '~~/supabase/schema/profile';

const { Pool } = pg;

export const connection = new Pool({
  connectionString: env.DATABASE_URL,
  max: 1
});

export const db = drizzle({
  client: connection,
  connection: {
    ssl: true
  },
  schema
});

const user = await db.query.profile.findFirst({
  where: eq(profile.id, _user.data.user.id)
}); <-------- Times out and fails
Solved

1 Replies

3 months ago

Hello,

Because when you connect via pg you aren't providing any authentication, but you do provide authentication when using SupabaseClient

When using pg you would need to use the full database URL like - postgresql://<username>:<password>@<domain>:<port>/<database>

Hope this helps,

Brody


Status changed to Awaiting User Response railway[bot] 3 months ago


Status changed to Solved brody 3 months ago


Drizzle supabase timeout issues - Railway Help Station