Skip to content

Conversation

bennajah
Copy link

@bennajah bennajah commented Aug 8, 2025

This PR introduces a foundational implementation for managing PostgreSQL functions and triggers using Drizzle ORM in a Supabase environment. It provides a type-safe, declarative API to define database functions and their associated triggers.

Details:

  1. Added PgFunction class and factory with support for:
    PostgreSQL function properties such as return type, language, behavior, security, search path, and SQL body.

  2. Added PgTrigger class and factory supporting:
    Trigger configuration on tables, including multiple events, trigger type, orientation (row/statement), and linked function execution.

Example:

import { sql } from "drizzle-orm/sql";
import { authUsers, pgFunction, pgTrigger } from "drizzle-orm/supabase";

export const handleNewUser = pgFunction("handle_new_user", {
  returns: "trigger",
  language: "plpgsql",
  security: "definer",
  behavior: "volatile",
  searchPath: "",
  body: sql`$$
    begin
      insert into public.users (id, email)
      values (new.id, new.email);
      return new;
    end;
    $$`,
});

export const onAuthUserCreated = pgTrigger("on_auth_user_created", {
  table: authUsers,
  events: ["insert"],
  triggerType: "after",
  orientation: "row",
  function: handleNewUser,
});

@marcospgp
Copy link

this is so cool!

@arthberman
Copy link

we need this!

1 similar comment
@rooagi-lw
Copy link

we need this!

@avalanche-tm
Copy link

This is amazing!

@avalanche-tm
Copy link

@bennajah can't wait for this PR to be approved and I tried to patch my local drizzle-orm with your PR commit. Unfortunately it didn't generate functions and trigger in the migration file. Do you have any way of patching this which works?

@bennajah
Copy link
Author

@avalanche-tm
This just initial implementation which not fully implemented to generate the migration

@avalanche-tm
Copy link

@avalanche-tm This just initial implementation which not fully implemented to generate the migration

Ahh I see. That makes me a sad panda :(

@tkachis
Copy link

tkachis commented Aug 29, 2025

@avalanche-tm

Hey!
I created a wrapper for drizzle-kit to simplify working with new entities.
It’s just a helper script - generated with GPT, slightly tweaked manually, and tested but not intended for production use.

https://playcode.io/2523779

Usage:

  1. Place the file in your project’s root directory.
  2. Update the paths inside the script to match your setup.
  3. Run it with: tsx drizzle-wrapper.ts generate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

6 participants