-
I'm trying to write my own This is somewhat what I want to be able to accomplish: // database.dart
class AppDatabase extends _$AppDatabase {
Future<void> connectOrCreate<T extends Table, D extends DataClass>(Insertable<D> id, Insertable<D> item) {
// implementation...
} // main.dart
void createData(String id, DbModel item) {
database.model.connectOrCreate(id, item);
} Since right now I have to include a table inside the method like this: // database
connectOrCreate<T extends Table, D extends DataClass>({
required TableInfo<T, D> table, required Insertable<D> id, required Isertable<D> item,
}) {
// Implementation...
}
// ---
// main.dart
void createData(String id, DbModel item) {
database.model.connectOrCreate(model, id, item);
// or
database.connectOrCreate(model, id, item);
}
|
Beta Was this translation helpful? Give feedback.
Replies: 1 comment 4 replies
-
Using
If extension ConnectOrCreate<T extends Table, Row> on TableInfo<T, Row> {
Future<void> connectOrCreate(Insertable<Row> id, Insertable<Row> item) {
// As an example for a simplified implementation.
return this.attachedDatabase.into(this).insert(item);
}
} Then you could call this with |
Beta Was this translation helpful? Give feedback.
You can use schema introspection to work around these issues. That's necessarily a bit unsafe, but there's no way to represent "extensions on tables having a column named id" in Darts type system, so that's the best we can do.