Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

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

Closed
ductranit opened this issue Oct 27, 2021 · 3 comments
Closed

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

ductranit opened this issue Oct 27, 2021 · 3 comments

Comments

@ductranit
Copy link

ductranit commented Oct 27, 2021

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
Copy link
Collaborator

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

  • sembast (io) uses the file system
  • sembast_web uses indexed db database
  • sembase_sqflite uses SQLite

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
Copy link
Author

ductranit commented Oct 27, 2021

@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:

  • Is there an option to clear old data before importing?
  • I guess it doesn't have stream copy json? I have to load the whole json file to import?
  • I didn't test it yet, but do you think the import speed is good? As my database can have over 1 million records

@alextekartik
Copy link
Collaborator

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants