simolus3 / drift

Drift is an easy to use, reactive, typesafe persistence library for Dart & Flutter.
https://drift.simonbinder.eu/
MIT License
2.58k stars 366 forks source link

Backup and Restore for Web #2777

Open BinaryShrub opened 9 months ago

BinaryShrub commented 9 months ago

Looking to offer a backup/restore option (that I plan to integrate with Google Drive API) for automated periodic backups. I read https://drift.simonbinder.eu/docs/examples/existing_databases/ but it seems to be targeted for NativeDatabase

How can I achieve backup and restore when targeting web and all the possible fallbacks?

simolus3 commented 9 months ago

At the moment this is fairly complicated because there is no uniform file system API on the web, so drift implements a number of approaches. But drift's file system implementation is not part of the public API.

I think you can do imports by using WasmDatabase.probe and then calling deleteDatabase on an existing database and using open with an initializeDatabase function resolving to the blobs to import.

AlexandreAndrade00 commented 1 month ago

I need to open a Web database with the bytes of an SQLite file. I'm trying to use the initializeDatabase property, in the Native opener is working, and the Wasm opener is not. It is possible to open a web database with SQLite file bytes?

simolus3 commented 1 month ago

Yes, this should work - we have integration tests covering this too (they're loading an asset from an URL like this):

https://github.com/simolus3/drift/blob/24905dc6c23a375999c1771a1ab089f5918b4f66/extras/integration_tests/web_wasm/web/main.dart#L93-L97

This method is passed to the initializeDatabase property which should work. Could it be that the database already exists in the browser, it's just not properly initialized?