SkillDevs / electric_dart

A Dart implementation for Electric (electric-sql.com).
Apache License 2.0
104 stars 9 forks source link

Supabase support #1

Closed TC3000 closed 10 months ago

TC3000 commented 1 year ago

Hello. Does this work with Supabase? https://supabase.com/

js2702 commented 1 year ago

This is something that should be asked to the Electric team as we are not sure if there could be some differences with how supabase handles the PostgreSQL database.

You can ask them over here https://discord.com/invite/B7kHGwDcbj or in their official repo https://github.com/electric-sql/electric.

maximblack commented 1 year ago

Electric requires a Postgres user with admin rights. The Supabase Cloud does not provide such.

I'm playing now with self-hosted Supabase + electric + electric_dart, and all are working as expected

big thanks to the authors of electric_dart!

davidmartos96 commented 1 year ago

@maximblack Hey, happy to hear it's working for you! Docs are a bit rough. Did you encounter any issues while setting everything up?

maximblack commented 1 year ago

@davidmartos96 Everything is going well so far. I'm relatively new to Dart/Flutter, so there's a lot for me to learn :)

I'm thinking about automating the creation of .drift files in the electricsql_cli, which involves generating CREATE TABLE... statements. This can be achieved by using a temporary SQLite instance, possibly in memory. This way, the drift build_runner can generate the DataClass and Companion from it. Additionally, we can consider removing the repository for drift/sqlite3 and instead use drift directly. This approach would allow us to maintain a single Postgres schema, from which the client can generate its code.

davidmartos96 commented 1 year ago

@maximblack Generating the drift "code" in the cli is a nice idea. We thought about it but it wasn't a priority right now, but that would definitely make it easier for newcomers, either for Drift or for Electric.

I didn't know drift supported generation based on a sqlite file, that's great!

What do you mean about removing the drift/sqlite3 repository and using drift directly?

davidmartos96 commented 1 year ago

There is one minor thing regarding drift code generation. There is some active work to support date formats into electric, using a TEXT format in sqlite. That involves TypeConverters that drift would need to use https://drift.simonbinder.eu/docs/advanced-features/type_converters/

There is a separate branch in this repo which adds support for that, providing the required TypeConverters to use with drift https://github.com/simolus3/drift/issues/2644

maximblack commented 1 year ago

@davidmartos96

What do you mean about removing the drift/sqlite3 repository and using drift directly?

in the example app provided in the repo, there is a repository with custom implementation for drift/sqlite3

so for each new table/migration, we need to support that repository also. using the drift db directly is our approach

this is not related to the electric_dart lib itself

There is a separate branch in this repo that adds support for that, providing the required TypeConverters to use with drift https://github.com/simolus3/drift/issues/2644

great, the .drift files have the support for converter according to https://drift.simonbinder.eu/docs/advanced-features/type_converters/#using-converters-in-moor

davidmartos96 commented 1 year ago

@maximblack I see what you mean now. Yes, I agree that we should consider using drift directly. The sqlite3 driver can be used with electric_dart as well, but it also requires more low level knowledge on how Electric works. Like the DateTime encode/decode format I shared above. We initially created the example with both drivers, but we are leaning towards drift-only seeing how the official electric client for Typescript is evolving.

davidmartos96 commented 11 months ago

Hello @maximblack Version 0.3.0 of the CLI and the core package add support to the extended data types in version 0.7.0 of Electric (uuid, int2, int4, date, timestamp...)

We've also refactored the electricsql_cli tool so that it automatically generates the Drift database schema, like you suggested in a previous comment. It uses Prisma introspection under the hood to extract the Postgres types and map them correctly with the drift syntax in the schema, very similar with how the official JS client works in the client code generation. If you try it out, let us know how that works for your use case.

jtkeyva commented 11 months ago

@maximblack hey would you mind sharing your experience? any luck with cloud hosted supabase?

@davidmartos96 that's great to hear. any news on supporting cloud hosted supabase?

davidmartos96 commented 11 months ago

@jtkeyva No news, still waiting for upcoming changes on the Electric backend service and the Typescript client. The official repo or the Discord server would be the best place to ask.

alterhuman commented 11 months ago

This is from the discussion on official repo:

Hi, we shelved adding supabase supoort ftm. We expect to get back into this 24Q1

We shipped the main blocker on permissions, the proxy, but we found a few more issues with permission granting that is holding off working with some hosted platforms like Supabase. We’re looking more systematically to be deployable acrosss hosted platforms, but for the next release this year we don’t expect to fully unlock Supabase support.

@davidmartos96 what postgres solution are you using in your own flutter apps? Also, a bit off topic but, what are you using for auth?

davidmartos96 commented 11 months ago

@alterhuman Postgres solution we are using Google Cloud, but for now we've only tested the postgres integration via Docker. But any Postgres solution with Logical replication should work.

For auth we are using firebase_auth. But we haven't integrated it fully with electric yet, as a database tied to user data is still a work in progress from the Electric team.

alterhuman commented 11 months ago

@davidmartos96 planning to do a flutter project with electric, what would you suggested for PostresQL? I need auth as well so was thinking Supabase self hosted on DO. Is there a better managed Postgres that also works with electric?

davidmartos96 commented 11 months ago

@alterhuman I'm not too experienced with Supabase, but I think it could be a good match.

The Electric team is working on an example of an Electric web app using Supabase + auth. You might want to follow that. https://github.com/electric-sql/electric/pull/695

I don't see any reason why that won't work with Electric Dart client once they complete the functionality on the Electric service.

Either a full managed solution (auth+database like Supabase) or a hosted PG database in any provider + auth delegated to a different service (firebase/supabase/custom backend) could work. There are plenty of hosted Postgres solutions, but I have only used the one provided by Google Cloud.

Regarding hosting options you could ask on the Discord server, there are far more people that could help you with that, and it's something that applies to the official Javascript client as well, not only Dart

alterhuman commented 11 months ago

@davidmartos96 thanks a lot for the quick reply. I had no idea they were able to (almost) solve the supabase permissions issue, this saves me so much headache of self hosting.

I just hope you continue to work on the dart client & make this awesome solution available to flutter devs. ❤️ Do you plan to implement the features from the TS client to dart client as soon as they come?

davidmartos96 commented 11 months ago

@alterhuman We really like the strategy that Electric took regarding the syncing. As of now the functionality on the official client is on par with the Dart client. That includes all the postgres types mapping to Dart (int2, int4, float8, json, enums, dates...).

Big thanks to Simon, the author of drift, who has managed to create a very capable SQLite client and ORM for this kind of integration.

After the big work was completed with the sync logic, unit and integration tests, right now, maintaining the Dart client up to date is not too time consuming.

alterhuman commented 11 months ago

That's great to know. Gonna try the Supabase solution using edge functions. Thanks again!

alterhuman commented 11 months ago

@davidmartos96 they have released documentation for Supabase cloud/platform. Will this work with dart client?

Also, what does this mean in the documentation?

Electric does not yet support permissions. Electrified tables are exposed to the public Internet.

Does that mean the table schema/columns or all the row data as well?

davidmartos96 commented 11 months ago

@alterhuman It should be compatible yes. At least with electric_dart latest master. I believe this was the only change required on the client, which has been ported already to Dart https://github.com/electric-sql/electric/commit/96e75630828dec845b8942ea455fcfff0bd54b0f The rest of the changes are in the Electric backend component.

Regarding limitations on the Supabase integration with the service itself, you can ask the Electric team over Discord.

davidmartos96 commented 10 months ago

Electric v0.8 and the Electric Dart client v0.4+ now support Supabase for authentication and for the Postgres data synchronization.

It would be great to have an example that uses Supabase authentication supabase_auth_ui + a simple database schema that synchronizes with the Supabase Postgres.

For inspiration, the official Typescript client has a Checkout example, which uses auth and edge functions https://electric-sql.com/docs/examples/checkout

Contributions are welcome!

We'll close this in favor of #5