supabase / supabase-flutter

Flutter integration for Supabase. This package makes it simple for developers to build secure and scalable products.
https://supabase.com/
MIT License
662 stars 155 forks source link

`date` columns should be provided as `DateTime` instead of `String`. #845

Open mateusfccp opened 4 months ago

mateusfccp commented 4 months ago

Is your feature request related to a problem? Please describe. Currently, if I have a table with a column of type date, when I query for the data, the runtime type of the data is String.

For instance, for a date value of 2024-02-25, I receive the string "2024-25-02".

Describe the solution you'd like Instead, we should receive DateTime.

For instance, for a date value of 2024-02-25, I should receive the object DateTime(2024, 02, 25).

Describe alternatives you've considered The alternative is to do nothing and let the consumer parse the date. This is how we have to do currently.

void main() async {
    final data = await supabase.from('members').select('birthday');
    for (final datum in data) {
        final birthday = DateTime.parse(datum['birthday']); // Manually parsing the received string
        // Do something with `birthday`...
    }
}
Vinzent03 commented 4 months ago

I think postgrest-js just returns them as string as well. We would have to do the same as you and manually try to parse every column. This may cause issues with columns that are not intended to be DateTime objects. It's better if the api is clean and everything is string/int/bool, because the data is coming in as JSON.

dshukertjr commented 4 months ago

We would need the type generation to happen for this. It would be awesome if we could support it though!

mateusfccp commented 4 months ago

This may cause issues with columns that are not intended to be DateTime objects.

Do you think it's common to define a column as date and don't want it to be a DateTime? If I want some data to be a string, wouldn't I rather use varchar or text as the column type?

It's better if the api is clean and everything is string/int/bool, because the data is coming in as JSON.

Is it a requirement that Data comes as JSON in Dart? I don't see any advantage in doing so...

Vinzent03 commented 4 months ago

The postgrest endpoint only returns a json for our query, we don't have any information about the type of the columns. Therefore, we cannot do any converting.

mateusfccp commented 3 months ago

The postgrest endpoint only returns a json for our query, we don't have any information about the type of the columns. Therefore, we cannot do any converting.

Didn't know about this detail.

Maybe we should have this information, as it's really important.