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

Could we generate a `class Repository<T extends SupadartClass<T>>` that works for all model classes? #40

Open bookshiyi opened 3 months ago

bookshiyi commented 3 months ago

When I use this project to create classes, it is very nice. but then I also need to write a lot of operation classes of each model classes.

So, generate a class Repository<T extends SupadartClass<T>> that works for all model classes is possible?

like this(at present, due to syntax restrictions, this code does not work):

class Repository<T extends SupadartClass<T>>{
  static table = Supabase.instance.client.from(T.table_name);

  static Future<List<T>?> list() => table.select().withConverter(T.converter);
  static Future<T?> get(String id) => table.select().eq('id', id).single().withConverter(T.converterSingle);
  static Future<List<T>?> delete(String id) => table.delete().eq('id', id).select().withConverter(Products.converter);
  ...
}

Usage:

final bookList = await Repository<Books>.list();
final music = await Repository<Music>.get('xxxxxx');
...

If others are facing a similar situation, welcome to share any experience or any ideas.

mmvergara commented 3 months ago

This is really useful for common operations!

althought this is assuming that every table primary key will be id (which is mostlikely by default).

but the list() fn is very doable.

bookshiyi commented 3 months ago

references: https://pub.dev/packages/swagger_parser https://pub.dev/packages/swagger_dart_code_generator

mmvergara commented 3 months ago

references: https://pub.dev/packages/swagger_parser https://pub.dev/packages/swagger_dart_code_generator

Hi so, i don't quite understand why is this relevant? it it related about the queries? sorry im just really new to dart and swagger. i just did this project on a whim lol.

bookshiyi commented 3 months ago

Morning~ I am also a beginner for swagger, but these project has provide the generation of get(), post(), delete(), patch() functions according to swagger.paths fields.

Maybe we could reference them to do these:

  1. only generate get() for view (or generate all functions according to permissions which defined in swagger.paths ) #42
  2. generate repositories class #40
  3. fill default value from swagger like : gen_random_uuid(), now(), auth.uid() etc
mmvergara commented 3 months ago

I forgot to respond to this, I see where your going with this. assuming we got to generate those 3, could you give me an example on how each of them would look like generated? again sorry for my slow comprehension

bookshiyi commented 3 months ago

hi, @mmvergara, Sorry, I missed your email too.

I didn't think too well about how we should achieve it.

On the other hand, these two projects are not based on supabase sdk, so we may also have some modifications.

These two projects, which have existed for a relatively long time, may have great inspiration for our projects, not just the three aspects mentioned above.

mmvergara commented 3 months ago

I see, Noted, will explore this further.