Replies: 1 comment
-
Thank you for starting this discussion!
Fundamentally, the main database class needs to know about all the tables involved to generate the default migrations creating all tables. Knowing things like references between tables is also relevant for updating query streams in some cases. // feature1.dart
import 'package:drift/drift.dart';
import 'feature1.drift.dart';
class Users extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get name => text()();
}
// Note that this doesn't have to reference the exact database class:
@DriftAccessor(tables: [Users])
class UsersDao extends DatabaseAccessor<GeneratedDatabase> with $UsersDaoMixin {
UsersDao(super.attachedDatabase);
Future<List<User>> get all => users.all().get();
} import 'package:drift/drift.dart';
import 'database.drift.dart';
@DriftDatabase(include: {'feature1.dart'})
class AppDatabase extends $AppDatabase { You can then continue to use It's worth noting that modular code generation has originally been designed with drift files in mind. It also works much better with them by automatically creating DAOs where necessary and also adding implicit getters. At the moment, drift also prints an ugly warning when using |
Beta Was this translation helpful? Give feedback.
-
Hello everyone,
I'm working on a large Flutter project that is heavily modularized. Each feature uses its own database tables and DAOs. However, my central database class in Drift needs to define all the tables and DAOs in the annotation, leading to a large number of imports and making modularity difficult. Here is an example of how it currently looks:
Problem:
The imports from the many features make the file unwieldy and go against the goal of modularity. How can we structure the database in a way that remains modular and easy to manage?
How have you solved this problem in your projects? Are there best practices or design patterns that promote modularity in Drift databases without overloading the central database class?
Best regards!
Beta Was this translation helpful? Give feedback.
All reactions