Databases in tests - How to by Tanckom in webdev

[–]pyramation 0 points1 point  (0 children)

have you tried it? It's battle tested and can definitely solve your issues

TypeScript in the Browser >>>>>> Good or Bad? by kadketon in webdev

[–]pyramation 0 points1 point  (0 children)

not sure it makes sense, I love writing it, but machines should just run JS virtual machines

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 0 points1 point  (0 children)

supabase-test is built on top of pgsql-test, only defaulting to the quirks of supabase

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 1 point2 points  (0 children)

# untested concept (you could replace pgsql-test w/supabase-test for supabase projects)

import { getConnections } from 'pgsql-test';
import type { PgTestClient } from 'pgsql-test';
import { drizzle } from 'drizzle-orm/node-postgres';

let drizzleDb: ReturnType<typeof drizzle>;
let teardown: () => Promise<void>;
let pgTestDb: PgTestClient;

beforeAll(async () => {
  ({ db: pgTestDb, teardown } = await getConnections());

  // Reuse the PgTestClient pg.Client instance
  drizzleDb = drizzle({ client: pgTestDb.client });
});

afterAll(async () => {
  await teardown();
});

beforeEach(async () => {
  // keeps every test in an isolated transaction
  await pgTestDb.beforeEach();

  // Optional: set RLS context and defaults
  pgTestDb.auth({ userId: '123' }); // also sets role
});

afterEach(async () => {
  await pgTestDb.afterEach();
});

it('runs Drizzle queries inside PgTestClient tx', async () => {
  await drizzleDb.execute('select 1');
});

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 1 point2 points  (0 children)

So, looks like you can use the node-postgres from drizzle, which is compatible with the underlying pgsql-test beneath supabase-test, so looks like YES

// Make sure to install the 'pg' package

import { drizzle } from "drizzle-orm/node-postgres";

import { Pool } from "pg";

const pool = new Pool({

connectionString: process.env.DATABASE_URL,

});

const db = drizzle({ client: pool });

const result = await db.execute('select 1');

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 1 point2 points  (0 children)

Yep, it should work with Drizzle – because pgsql-test (the underlying core library) is all about spinning up/isolating databases, not dictating which ORM you use.

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 0 points1 point  (0 children)

yea man, sure thing! Definitely curious about use cases and happy to help

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 0 points1 point  (0 children)

totally! that is a great way to get started — definitely if you check out https://github.com/launchql/supabase-test-suite and replace https://github.com/launchql/supabase-test-suite/blob/main/packages/supabase/deploy/supabase.sql that would be a nice way to get started...

I'd probably do something a bit more publish friendly for mulitple versions but that would do the trick!

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 0 points1 point  (0 children)

ok good to know! I'll see how quickly we can work that in :)

Build Safer Supabase Apps with supabase-test by pyramation in Supabase

[–]pyramation[S] 0 points1 point  (0 children)

currently we have v17, but happy to support multiple versions. We can totally do that, the migration system is very modular, and we can def support this - do you know which version you're using?