drift icon indicating copy to clipboard operation
drift copied to clipboard

[Suggestion] Store initial data in asset files instead of the generated database code

Open North101 opened this issue 2 years ago • 2 comments

I'm not sure whether or not this is worth doing but if there is a lot of data to be inserted when creating the db, it seems overkill to store it as generated code. Does it make sense to generate these as assets which are then read from during db creation?

North101 avatar Jun 17 '22 13:06 North101

Does it make sense to generate these as assets which are then read from during db creation?

Interesting idea! drift_dev could optionally emit a prepared sqlite3 database into a configurable location, but I'm not sure how well that works in practice? How much initial data do you have? If its more than several thousand rows, you probably don't have all those as @create statements in .drift files? So I'm wondering how this initial data would be specified then.

FWIW it should be fairly straightforward to generate such asset with some a script too. Essentially you just need to create an instance of your database class outside of a Flutter app and make it store the sqlite3 database file as an asset. Then you could include the logic to add all the initial data in that script, which may be easier than including it in drift files.

simolus3 avatar Jun 17 '22 20:06 simolus3

So currently I use JSON assets that I load and import because I want to be able to check if there is a newer version online on load (I could just not have the JSON files but I want to reduce the load as it's not updated often). So I have no current need for it.

I was thinking of having it as a .sql file but having drift produce an sqlite db with all of the initial data is a much better idea. I could probably write a script to do this (though having a command for it would be helpful for others)

North101 avatar Jun 17 '22 22:06 North101