According to the official website, Supabase is an open source Firebase alternative for building secure and performant Postgres backends with minimal configuration.

Checkout official Supabase + Drizzle docs.

Install dependencies

npm
yarn
pnpm
bun
npm i drizzle-orm postgres
npm i -D drizzle-kit

Create your models

schema.ts
import { pgTable, serial, text, varchar } from "drizzle-orm/pg-core";

export const users = pgTable('users', {
  id: serial('id').primaryKey(),
  fullName: text('full_name'),
  phone: varchar('phone', { length: 256 }),
});

Make your first query

index.ts
import { drizzle } from 'drizzle-orm/postgres-js'
import postgres from 'postgres'
import { users } from './schema'

const connectionString = process.env.DATABASE_URL
const client = postgres(connectionString)
const db = drizzle(client);

const allUsers = await db.select().from(users);

Connect to your database using the Connection Pooler for serverless environments, and the Direct Connection for long-running servers.

Usage with Cloudflare Workers

Now that Cloudflare Workers supports TCP connections, you can use node-postgres to connect to Supabase’s connection pooler.

worker.ts
import { Pool } from "pg";
import { drizzle } from "drizzle-orm/node-postgres";
 
export default {
  async fetch(req, env, ctx) {
    const pool = new Pool({ connectionString: env.DATABASE_URL });
    const db = drizzle(pool)
    const result = await db.select().from(...);
    ctx.waitUntil(pool.end());
    return new Response(now);
  }
}