Supabase Codegen

November 23, 2025 ยท View on GitHub

style: very good analysis License: MIT Codacy Badge Powered by Mason melos

A comprehensive suite of packages for generating type-safe Dart models from Supabase databases, supporting both pure Dart and Flutter applications.

๐Ÿ“ฆ Packages

This monorepo contains four main packages:

supabase_codegen

pub package

The core package for generating type-safe Dart models from Supabase tables. Perfect for:

  • Pure Dart applications
  • Server-side Dart projects
  • Custom Dart environments

supabase_codegen_flutter

pub package

Flutter-optimized package that extends the core functionality with Flutter-specific features:

  • Automatic environment file loading from config.env
  • Convenient getters for Supabase services (auth, realtime, storage, functions)
  • Better integration with supabase_flutter
  • Flutter-specific client management

supabase_codegen_serverpod

pub package

Serverpod-optimized package that generates Serverpod database models from Supabase tables:

  • Generates Serverpod model files for tables and enums
  • Maps Supabase types to Serverpod types
  • Excludes internal Serverpod tables automatically

supabase_codegen_templates

Contains the Mason bricks used by the other packages to generate code. This package is internal and not meant to be used directly.

โœจ Features

  • Type-Safe Models: Automatically generates strongly-typed Dart classes from your Supabase tables
  • Full IDE Support: Complete IntelliSense and autocomplete for all generated models
  • Complex Relationships: Supports nested structures and table relationships
  • Null Safety: All generated models are fully null-safe
  • Custom Types: Support for enums and custom column types
  • Flexible Configuration: YAML-based configuration with command-line overrides
  • Flutter Integration: Specialized package for Flutter development
  • Serverpod Support: specialized package for Serverpod development
  • Testing Support: Built-in mock clients for comprehensive testing

๐Ÿš€ Quick Start

For Flutter Projects

  1. Install the Flutter package:

    flutter pub add supabase_codegen_flutter
    
  2. Set up your environment: Create config.env in your project root:

    SUPABASE_URL=https://your-project.supabase.co
    SUPABASE_ANON_KEY=your-anon-key
    
  3. Initialize configuration:

    dart run supabase_codegen_flutter:init
    

    This creates the .supabase_codegen.yaml configuration file.

  4. Add to pubspec.yaml:

    flutter:
      assets:
        - config.env
    
  5. Generate types:

    dart run supabase_codegen_flutter:generate_types
    
  6. Use in your app:

    import 'package:supabase_codegen_flutter/supabase_codegen_flutter.dart';
    
    void main() async {
      await loadClientFromEnv();
      runApp(const MyApp());
    }
    

For Pure Dart Projects

  1. Install the core package:

    dart pub add supabase_codegen
    
  2. Set up your environment: Create .env in your project root:

    SUPABASE_URL=https://your-project.supabase.co
    SUPABASE_ANON_KEY=your-anon-key
    
  3. Initialize configuration:

    dart run supabase_codegen:init
    

    This creates the .supabase_codegen.yaml configuration file.

  4. Generate types:

    dart run supabase_codegen:generate_types
    
  5. Use in your app:

    import 'package:supabase_codegen/supabase_codegen.dart';
    
    void main() async {
      loadClientFromEnv();
      // Your app code here
    }
    

For Serverpod Projects

Run this in your Serverpod server project (typically my_serverpod_server)

  1. Install the Serverpod Codegen package:

    dart pub add supabase_codegen_serverpod
    
  2. Set up your environment: Create .env in your project root:

    SUPABASE_URL=https://your-project.supabase.co
    SUPABASE_ANON_KEY=your-anon-key
    
  3. Initialize configuration:

    dart run supabase_codegen_serverpod:init
    

    This creates the .supabase_codegen.yaml configuration file.

  4. Generate models:

    dart run supabase_codegen_serverpod:generate_types
    
  5. Use in your Serverpod project: The generated .spy.yaml files will be in lib/src/models (or your configured output). Run serverpod generate to create the Dart classes.

๐Ÿ› ๏ธ Development

This project uses Melos for monorepo management.

Prerequisites

Setup

  1. Clone the repository:

    git clone https://github.com/Khuwn-Soulutions/supabase_codegen.git
    cd supabase_codegen
    
  2. Install dependencies:

    dart pub get
    melos bootstrap
    
  3. Run tests:

    melos run test
    
  4. Run tests with coverage:

    melos run test:coverage
    

Project Structure

supabase_codegen/
โ”œโ”€โ”€ packages/
โ”‚   โ”œโ”€โ”€ supabase_codegen/           # Core Dart package
โ”‚   โ”‚   โ”œโ”€โ”€ bin/                    # CLI tools
โ”‚   โ”‚   โ”œโ”€โ”€ lib/                    # Source code
โ”‚   โ”‚   โ”œโ”€โ”€ test/                   # Unit tests
โ”‚   โ”‚   โ””โ”€โ”€ example/                # Usage examples
โ”‚   โ””โ”€โ”€ supabase_codegen_flutter/   # Flutter package
โ”‚       โ”œโ”€โ”€ bin/                    # CLI tools
โ”‚       โ”œโ”€โ”€ lib/                    # Source code
โ”‚       โ”œโ”€โ”€ test/                   # Unit tests
โ”‚       โ””โ”€โ”€ example/                # Flutter example app
โ”‚   โ””โ”€โ”€ supabase_codegen_serverpod/ # Serverpod package
โ”‚       โ”œโ”€โ”€ bin/                    # CLI tools
โ”‚       โ”œโ”€โ”€ lib/                    # Source code
โ”‚       โ””โ”€โ”€ test/                   # Unit tests
โ”œโ”€โ”€ .github/
โ”‚   โ””โ”€โ”€ workflows/                  # CI/CD pipelines
โ””โ”€โ”€ analysis_options.yaml           # Code analysis configuration

Available Scripts

  • melos run test - Run all tests
  • melos run test:coverage - Run tests with coverage
  • melos run coverage_badge - Update coverage badges
  • melos run test:coverage_badge - Run tests and update badges

๐Ÿ“‹ Prerequisites for Development

  • Supabase project with tables
  • Dart/Flutter development environment
  • Supabase CLI (for local development)

๐Ÿงช Testing

The project includes comprehensive test suites for both packages:

Unit Tests

# Run all tests
melos run test

# Run with coverage
melos run test:coverage

Integration Tests

Each package includes example projects that demonstrate real-world usage and serve as integration tests.

๐Ÿค Contributing

We welcome contributions! Please see our Contributing Guide for details.

Development Workflow

  1. Fork the repository
  2. Create a feature branch: git checkout -b feature/your-feature
  3. Make your changes
  4. Run tests: melos run test
  5. Submit a pull request

Code Quality

This project uses:

๐Ÿ“„ License

This project is licensed under the MIT License - see the LICENSE file for details.

๐Ÿ™ Acknowledgments

๐Ÿ“ž Support


Made with โค๏ธ by Khuwn Soulutions