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.
What is compilation?
Section titled “What is compilation?”pgflow compiles your TypeScript flow definition into SQL migrations that register the flow in your database.
- Analyze TypeScript flow definition
- Extract steps, dependencies, and options
- Generate SQL registration commands
- Create migration file
The runtime executes flows based on their database representation, not the TypeScript code directly.
-
Compile the flow to SQL
Section titled “Compile the flow to SQL”Run the pgflow compile command, pointing to your flow definition file:
npx pgflow@latest compile supabase/functions/_flows/greet_user.tsThis 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 -
Examine the generated SQL
Section titled “Examine the generated SQL”Let’s look at what got generated. Open the migration file in your editor:
cat supabase/migrations/*_create_greet_user_flow.sqlThe migration file contains SQL commands that:
- Create the flow definition
- Add each step with its configuration
- 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.
-
Apply the migration
Section titled “Apply the migration”Now that we have the SQL migration, we need to apply it to our database:
npx supabase migrations upThis will execute the SQL migration and register your flow definition in the database.
If successful, you should see output like this:
Applying migration 20250505120000_create_greet_user_flow.sql...done