supabase / postgrest-dart

Dart client for PostgREST
https://supabase.com
MIT License
136 stars 38 forks source link

Add `withConverter` and pass in a generic type #47

Closed carlomigueldy closed 2 years ago

carlomigueldy commented 3 years ago

Feature request

Is your feature request related to a problem? Please describe.

Yeah sort of, this would refer to be as "boilerplate" code. Since we typically do that manually every time we fetch data.

// where response if the returned results from Supabase client
User.fromJson(response.data)

Describe the solution you'd like

Firestore has implemented this feature. So I'll just link up that from pub.dev changelog.

https://pub.dev/packages/cloud_firestore/changelog#200

Additional context

image

point-source commented 3 years ago

Should also be possible to implement this in a way that mirrors the supabase javascript API. We could pass the type in on the .from method and check to see if it inherits from a supabase abstract data type which requires fromJson/toJson methods. It's definitely not as flexible as the firebase implementation so that may still be better. Just suggesting it depending on how important it is that this library keeps in line with the official supabase documentation.

k0shk0sh commented 3 years ago

you can create an extension for this:

ex:

typedef FromSupabase<T> = T Function(
  dynamic data,
  PostgrestError error,
);

extension Converter<T> on Future<PostgrestResponse> {
  Future<T> withConverter<T>({
    FromSupabase<T> fromResponse,
  }) async {
    final data = await this;
    return fromResponse.call(data.data, data.error);
  }
}

not tested and was written entirely in my imagination.