Skip to content

Khuwn-Soulutions/supabase_codegen

Repository files navigation

Supabase Codegen

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:

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

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

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

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

About

A codegen library that grabs and generates classes from Supabase tables and enums in Dart.

Topics

Resources

Stars

Watchers

Forks

Contributors 4

  •  
  •  
  •  
  •