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

How to handle many tables #480

Open
chrislaurie-io opened this issue Apr 8, 2020 · 4 comments
Open

How to handle many tables #480

chrislaurie-io opened this issue Apr 8, 2020 · 4 comments
Labels
question Further information is requested

Comments

@chrislaurie-io
Copy link

I am looking to use flutter to rewrite a business apllication. It has about 80 tables. From what I understand I must have the AppDatabase as a singleton. Doing this via a provider works for me. My question is how to structure my dart files etc so that I can isolate each table in its own file instead of ALL the classes in one file above the _$AppDatabase class and the ALL the worker methods for all the tables below.

Is there a way to avoid the possible spaghetti nightmare of a singe database.dart file?

@chrislaurie-io
Copy link
Author

So looking through the documentation it seems the obvious solution is to use .moor files. This also allows a collection of table definitions to live in the same moor file, or even by themselves. This look like a very cool and neat solution.

I assume that there is still only 1 schema. I need to check out how to use migrations with moor files.

@simolus3 simolus3 added the question Further information is requested label Apr 8, 2020
@simolus3
Copy link
Owner

simolus3 commented Apr 8, 2020

Using moor files would probably be the best approach, but you can also separate your Dart table classes across multiple files. It's true that most or all of the the generated code would be in one file, but usually that doesn't matter too much with generated code.

You can have all the generated core in a single file without handwritten code turning into an absolute mess. For instance, you can use DAO classes that operate on a subset of tables or queries.

I assume that there is still only 1 schema.

You can definitely use multiple independent databases with moor, if that's what you need.

@adummy832
Copy link

adummy832 commented Jun 24, 2022

Hi @simolus3,

Just wanted to ask if how can I implement this using drift? Do I change ".moor" files into ".drift"?
Currently my app structure, Looks like this in which contains all table definitions & business logic in a huge file.

lib/
|- database/
|-    app_database.dart
|-    app_database.g.dart

I want to somehow implement a production grade structure that looks like this

lib/
|- database/
|-  tables/
|-    table_1.dart /// contains definitions, daos, custom class, etc..
|-    table_2.dart
|-    app_tables.dart /// barrel file
|-  utils/
|-    utilities.dart
|-  app_database.dart
|-  app_database.g.dart

Been browsing the drift docs for this matter but i can't find any ideas to begin with,
Any insights of yours will be highly appreciated. Thanks :)

@simolus3
Copy link
Owner

FWIW you can already implement the structure like this and just import your table files into app_database.dart (just use regular Dart imports and reference the classes in @DriftDatabase).

If you want to split complex queries defined in Dart across multiple files, you could use DAOs. If the idea is for drift to generate more than one file to make it smaller, the issue to track is #490 - I'd love to have feedback on the ideas shared there if you're interested in that.

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

No branches or pull requests

3 participants