tekartik / sembast.dart

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

How does it work: the use of storeRef for sembast_sqflite? #309

Closed androidmaven closed 8 months ago

androidmaven commented 2 years ago

Firstly, thanks for the lib: super useful and easy to use.

To my question: When we use the sembast_sqfilte, how does the db saves data as it regards StoreRef.store()? For example, we store data with different storeKeys. StoreRef.store("animals") and StoreRef.store("planets")

Is the storeKey included as an ID of the data? for example [id, name, storeKey] so that data is being queried by a combination of keys in SQL?

records = SQL.query(Select * from 'allRecords' WHERE storeKey = '1234')

Or a table is being created as the storeKey where all data are being stored inside such table, so that data is being queried from that table?

records = SQL.query(Select * from 'storeKey' WHERE .....) -

Or is it using a different Logic? I'd appreciate your help to help me understand better.

Thanks.

alextekartik commented 2 years ago

Good question! I guess it should deserve a better documentation...sembast is an in-memory database, i.e all content is loaded into memory during open. It has several drawbacks such as not working well with big databases and slow to open if you have a lot of records. Default io implementation simply append written data to json like files, but sometimes after many updates and deletion the file is built again to remove obsolete data. sqflite based implementation simply add a record in a SQLite database and when you reopen the database the data is loaded again from SQLite matching what you add before in memory.

You can view the schema using any SQLite browser app.

In fact all the records are stored in a single table (entry), no matter what store it is in. Each record contains the store, the key and the data of the record and when you query the database, everything happens in memory without SQL being involved at all. There is a few additional meta information, such as auto increment info and signature/codec information if any.