Moor: DAO inheritance

Created on 19 Dec 2019  路  2Comments  路  Source: simolus3/moor

Hi.

I'm trying to create some BaseDao, in order to reuse some basic database operations:

class BaseDao<T extends DataClass> extends DatabaseAccessor<MyDatabase> {
  final Table _table;

  BaseDao(MyDatabase db, this._table) : super(db);

  Future insertOne(Insertable<T> value) => into(_table).insert(value);

  Future<List<T>> selectAll() => select(_table).get();

  Future<bool> updateOne(Insertable<T> value) => update(_table).replace(value);

  Future deleteOne(Insertable<T> value) => delete(_table).delete(value);
}

abstract class AbstractProductDao extends BaseDao<ProductTableData> {
  AbstractProductDao(MyDatabase db) : super(db, db.productTable);
}

part 'product_dao.g.dart';

@UseDao(tables: [ProductTable])
class ProductDao extends AbstractProductDao
    with _$ProductDaoMixin {
  ProductDao(MyDatabase db) : super(db);
}
[SEVERE] moor_generator:moor_generator on lib/db/daos/product_dao.dart:
Error running MoorGenerator
NoSuchMethodError: The getter 'includes' was called on null.
Receiver: null
Tried calling: includes
[SEVERE] moor_generator:moor_generator on lib/db/daos/product_dao.dart:
Error running DaoGenerator
NoSuchMethodError: The getter 'includes' was called on null.
Receiver: null
Tried calling: includes
[INFO] Running build completed, took 13.6s

[INFO] Caching finalized dependency graph...
[INFO] Caching finalized dependency graph completed, took 176ms

[SEVERE] Failed after 13.8s
pub finished with exit code 1

Unfortunately this doesn't work. Any idea how to achieve such a inheritance hierachy?

generator

Most helpful comment

I like the idea of supporting abstract daos, I'll check what needs to be done in the generator to support this.

Btw, you can probably get better type safety like this:
```dart
class BaseDao extends DatabaseAccessor {
final TableInfo _table;
// ...
abstract class AbstractProductDao extends BaseDao {
````
But first it needs to be supported in the generator, of course.

All 2 comments

I like the idea of supporting abstract daos, I'll check what needs to be done in the generator to support this.

Btw, you can probably get better type safety like this:
```dart
class BaseDao extends DatabaseAccessor {
final TableInfo _table;
// ...
abstract class AbstractProductDao extends BaseDao {
````
But first it needs to be supported in the generator, of course.

This will be supported in the next moor update.

Was this page helpful?
0 / 5 - 0 ratings

Related issues

kira1752 picture kira1752  路  3Comments

Holofox picture Holofox  路  4Comments

jerryzhoujw picture jerryzhoujw  路  4Comments

johrpan picture johrpan  路  4Comments

apoleo88 picture apoleo88  路  3Comments