Skip to content

Compile flow to SQL

Now that we’ve defined our flow, we need to register it in the database. pgflow provides a CLI tool that compiles your TypeScript flow definition into SQL migrations that can be applied to your Supabase database.

The pgflow compilation process:

  1. Analyzes your TypeScript flow definition
  2. Extracts information about steps, dependencies, and options
  3. Generates SQL commands that register the flow and its structure in your database
  4. Creates a migration file that can be applied to your database

This is an essential step because pgflow’s runtime executes flows based on their database representation, not the TypeScript code directly.

Run the pgflow compile command, pointing to your flow definition file:

npx pgflow@latest compile supabase/functions/_flows/greet_user.ts

This will:

  • Parse your TypeScript flow definition
  • Extract the flow structure, step dependencies, and configuration
  • Generate SQL commands to register the flow in the database
  • Create a timestamped migration file in your Supabase migrations folder

You should see output like this:

✓ Successfully compiled flow to SQL
✓ Migration file created: supabase/migrations/20250505120000_create_greet_user_flow.sql

Let’s look at what got generated. Open the migration file in your editor:

cat supabase/migrations/*_create_greet_user_flow.sql

The migration file contains SQL commands that:

  1. Create the flow definition
  2. Add each step with its configuration
  3. Define dependencies between steps

The generated SQL looks like this:

SELECT pgflow.create_flow('greet_user');
SELECT pgflow.add_step('greet_user', 'full_name');
SELECT pgflow.add_step('greet_user', 'greeting', ARRAY['full_name']);

This SQL representation is what the pgflow runtime system uses to execute your workflow.

Now that we have the SQL migration, we need to apply it to our database:

npx supabase migrations up

This will execute the SQL migration and register your flow definition in the database.

If successful, you should see output like:

Applying migration 20250505120000_create_greet_user_flow.sql...done