mmvergara / supadart

Typesafe queries in Supabase Flutter! Generate Flutter / Dart 🎯 classes from your Supabase schema.
https://supadart.vercel.app
MIT License
43 stars 5 forks source link

Feature Request: Support for Enum Types in Model Generation #55

Closed kurikin closed 3 months ago

kurikin commented 3 months ago

Hi @mmvergara First, I'd like to express my gratitude for creating supadart. It's an excellent library that significantly enhances the Dart development experience with Supabase. I have a suggestion regarding model generation, specifically concerning the handling of Enum types.

Current Behavior

When using Enum types in the database schema, as described in the Supabase documentation (https://supabase.com/docs/guides/database/postgres/enums), the current version of supadart is unable to generate the corresponding Dart models correctly.

For example, given the following schema:

create type mood as enum (
  'happy',
  'sad',
  'excited',
  'calm'
);

create table person (
  id serial primary key,
  name text,
  current_mood mood
);

Attempting to generate a model for this schema results in an error message:

"Something went wrong, please open an issue on this : Unsupported Dart type: please open an issue,"

Feature Request

I propose adding support for Enum types in the model generation process. This would allow developers to seamlessly work with Enum fields in their Supabase schemas and corresponding Dart models.

BR

mmvergara commented 3 months ago

Hi thanks for opening an issue and for pointing it out, will work on it asap!

mmvergara commented 3 months ago

@kurikin CLI and Web app now supports enums (I believe so, as I've written some tests for them). Please update your CLI and generate new types.

Make sure to also read this new part of docs for enums crud https://github.com/mmvergara/supadart?tab=readme-ov-file#enums-crud

We'll keep this issue open for now, Looking forward to your feedback.

kurikin commented 3 months ago

@mmvergara Thank you for your incredibly quick response and implementation!

I've updated the CLI and tested the new version. The new enum support is working well overall, but I've discovered a minor issue that occurs in specific cases: When the enum name contains uppercase letters (e.g., Mood or MoodType), the generated Dart file wraps the enum name in double quotes.

Here's an example: For a Mood enum used in a Person table, the generated code looks like this:

enum "MOOD" { happy, sad, excited, calm }
class Person implements SupadartClass<Person> {
  // [supadart:has_enums]
  final int id;
  final String? name;
  final "MOOD"? current_mood;

  const Person({
    required this.id,
    this.name,
    this.current_mood,
  });
}

As you can see, "MOOD" is wrapped in double quotes, which isn't valid Dart syntax.

Could you please update the code generation to remove these quotes for enum names? Thank you again for your fantastic work on this library!

mmvergara commented 3 months ago

@kurikin I see, that's really weird cause i tried it in the same scenario and the way it is coded.

Could you possibly provide the configs printed out by the cli along with the Person Table schema?

kurikin commented 3 months ago

@mmvergara Thank you for looking into this. I'm sharing the project URL and anon key. There's no sensitive data in this project, so it's safe to share:

Project URL: https://qglwwwspmytyeqeqxtbz.supabase.co
Anon key: eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJzdXBhYmFzZSIsInJlZiI6InFnbHd3d3NwbXl0eWVxZXF4dGJ6Iiwicm9sZSI6ImFub24iLCJpYXQiOjE3MjMyODMyMTYsImV4cCI6MjAzODg1OTIxNn0.vR1lhJBFDqLg1H1_9X2I0Lu1Zu3hl2xm907M-YuyW08

Database Schema

γ‚Ήγ‚―γƒͺγƒΌγƒ³γ‚·γƒ§γƒƒγƒˆ 2024-08-11 15 59 14 γ‚Ήγ‚―γƒͺγƒΌγƒ³γ‚·γƒ§γƒƒγƒˆ 2024-08-11 15 59 05

CLI Output

> dart cli/bin/supadart.dart
πŸš€ Supadart v1.5.0
==============================
URL:        https://qglwwwspmytyeqeqxtbz.supabase.co
ANON KEY:   eyJhbGciOiJIUzI1NiIsInR5c...
Output:     lib/models/
Separated:  true
Dart:       false
Mappings:   null
==============================
Generating...
🎯 Generated: lib/models/generated_enums.dart
🎯 Generated: lib/models/models.dart
🎯 Generated: lib/models/client_extension.dart
🎯 Generated: lib/models/supadart_abstract_class.dart
🎯 Generated: lib/models/person.dart
πŸŽ‰ Done!
mmvergara commented 3 months ago

@kurikin Thanks for your patience. It should be fix now, try updating the cli

PR https://github.com/mmvergara/supadart/pull/58

kurikin commented 3 months ago

@mmvergara Thank you for the quick fix. I've tested the latest version, and I can confirm that the issue with enum handling has been resolved. I appreciate your prompt attention to this matter and the rapid improvement.

mmvergara commented 3 months ago

@kurikin Aightt, thank you again for opening this issue, try to reset your anon key if you can, even though its safe.. just in case.