tekartik / sembast.dart

Simple io database
BSD 2-Clause "Simplified" License
780 stars 64 forks source link

Is there any way to overwrite local database on sembast web? #290

Closed ductranit closed 1 day ago

ductranit commented 2 years ago

I have the setup database like this:

if (kIsWeb) {
        var db = await databaseFactoryWeb.openDatabase('myDatabase.db'');
 } else {
        var root = final documentPath = await getApplicationSupportDirectory();
        final dbPath = '${root.path}/myDatabase.db';
        var db = await databaseFactoryIo.openDatabase(dbPath);
 }

On mobile I know exactly the database file path, then I can overwrite it with my custom snapshot file (I know there is a way to import/export data, but it isn't suitable for my requirements), like this:

// overwrite database by snapshot before init
final dbFile = File(dbPath);
if (await dbFile.exists()) {
    await dbFile.delete();
}
await snapshotFile.copy(dbPath);

But on web, I don't know how to do the same because there is no the file path. I wonder where does the database locate on web?

alextekartik commented 2 years ago

sembast(io), sembast_web, sembast_sqflite uses different backend:

There is no such thing as a file on the web and the format on each platform is obviously different.

I wonder where does the database locate on web?

A database on the web is an IndexedDB database.

I know there is a way to import/export data, but it isn't suitable for my requirements

I'm afraid that is the only way though, sorry. It is a one line action to export/import a database and in general application should not rely of the backend implementation.

then I can overwrite it with my custom snapshot file

I guess by snapshot file you mean a file on the file system managed by databaseFactoryIo (sembast).

Out of curiosity, how did you manage to get your custom snapshot file accessible on the web? If you managed, you could do the same by json encoding the exported map.

ductranit commented 2 years ago

@alextekartik Thanks for your quick response.

Out of curiosity, how did you manage to get your custom snapshot file accessible on the web? If you managed, you could do the same by json encoding the exported map.

I sync data on desktop, then I copy the file as snapshot and put it into flutter assets, then I replace snapshot for all platforms. I choose that way because my database is quite large so it isn't good to load the whole json file.

As you said, import/export is the only choice, so I have some questions:

alextekartik commented 2 years ago

Is there an option to clear old data before importing?

import creates the database, so the database is clear before

I guess it doesn't have stream copy json? I have to load the whole json file to import?

You need the whole json in memory

I didn't test it yet, but do you think the import speed is good? As my database can have over 1 million records

sembast won't work for 1 million records. I suggest switching to another database for such requirements (SQLite).

--alex