Expand docs on how write database / dao classes

This commit is contained in:
Simon Binder 2019-03-27 18:08:41 +01:00
parent 3ef49f6017
commit 99d245d0ac
No known key found for this signature in database
GPG Key ID: B807FDF954BA00CF
6 changed files with 36 additions and 11 deletions

View File

@ -10,12 +10,6 @@ env:
script: ./tool/mono_repo_wrapper.sh script: ./tool/mono_repo_wrapper.sh
after_success: ./tool/upload_coverage.sh after_success: ./tool/upload_coverage.sh
# Only building master means that we don't run two builds for each pull request.
branches:
only:
- master
- develop
cache: cache:
directories: directories:
- "$HOME/.pub-cache" - "$HOME/.pub-cache"

View File

@ -3,6 +3,17 @@ import 'package:moor/moor.dart';
/// Use this class as an annotation to inform moor_generator that a database /// Use this class as an annotation to inform moor_generator that a database
/// class should be generated using the specified [UseMoor.tables]. /// class should be generated using the specified [UseMoor.tables].
///
/// To write a database class, first annotate an empty class with [UseMoor] and
/// run the build runner using (flutter packages) pub run build_runner build.
/// Moor will have generated a class that has the same name as your database
/// class, but with `_$` as a prefix. You can now extend that class and provide
/// a [QueryEngine] to use moor:
/// ```dart
/// class MyDatabase extends _$MyDatabase { // _$MyDatabase was generated
/// MyDatabase() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'path.db'));
/// }
/// ```
class UseMoor { class UseMoor {
/// The tables to include in the database /// The tables to include in the database
final List<Type> tables; final List<Type> tables;
@ -10,6 +21,8 @@ class UseMoor {
/// Optionally, the list of daos to use. A dao can also make queries like a /// Optionally, the list of daos to use. A dao can also make queries like a
/// regular database class, making is suitable to extract parts of your /// regular database class, making is suitable to extract parts of your
/// database logic into smaller components. /// database logic into smaller components.
///
/// For instructions on how to write a dao, see the documentation of [UseDao]
final List<Type> daos; final List<Type> daos;
/// Use this class as an annotation to inform moor_generator that a database /// Use this class as an annotation to inform moor_generator that a database
@ -17,8 +30,24 @@ class UseMoor {
const UseMoor({@required this.tables, this.daos = const []}); const UseMoor({@required this.tables, this.daos = const []});
} }
/// Annotation to use on classes that implement [DatabaseAccessor]. It specified /// Annotation to use on classes that implement [DatabaseAccessor]. It specifies
/// which tables should be managed in this dao. /// which tables should be made available in this dao.
///
/// To write a dao, you'll first have to write a database class. See [UseMoor]
/// for instructions on how to do that. Then, create an empty class that is
/// annotated with [UseDao] and that extends [DatabaseAccessor]. For instance,
/// if you have a class called `MyDatabase`, this could look like this:
/// ```dart
/// class MyDao extends DatabaseAccessor<MyDatabase> {
/// TodosDao(MyDatabase db) : super(db);
/// }
/// ```
/// After having run the build step once more, moor will have generated a mixin
/// called `_$MyDaoMixin`. Change your class definition to
/// `class MyDao extends DatabaseAccessor<MyDatabase> with _$MyDaoMixin` and
/// you're ready to make queries inside your dao. You can obtain an instance of
/// that dao by using the getter that will be generated inside your database
/// class.
class UseDao { class UseDao {
/// The tables accessed by this DAO. /// The tables accessed by this DAO.
final List<Type> tables; final List<Type> tables;

View File

@ -8,9 +8,11 @@ import 'package:moor/src/runtime/statements/select.dart';
import 'package:moor/src/runtime/statements/update.dart'; import 'package:moor/src/runtime/statements/update.dart';
/// Class that runs queries to a subset of all available queries in a database. /// Class that runs queries to a subset of all available queries in a database.
///
/// This comes in handy to structure large amounts of database code better: The /// This comes in handy to structure large amounts of database code better: The
/// migration logic can live in the main [GeneratedDatabase] class, but code /// migration logic can live in the main [GeneratedDatabase] class, but code
/// can be extracted into [DatabaseAccessor]s outside of that database. /// can be extracted into [DatabaseAccessor]s outside of that database.
/// For details on how to write a dao, see [UseDao].
abstract class DatabaseAccessor<T extends GeneratedDatabase> abstract class DatabaseAccessor<T extends GeneratedDatabase>
extends DatabaseConnectionUser with QueryEngine { extends DatabaseConnectionUser with QueryEngine {
@protected @protected

View File

@ -6,7 +6,7 @@ import 'database.dart';
part 'todos_dao.g.dart'; part 'todos_dao.g.dart';
@UseDao(tables: [Todos]) @UseDao(tables: [Todos])
class TodosDao extends DatabaseAccessor<Database> with _TodosDaoMixin { class TodosDao extends DatabaseAccessor<Database> with _$TodosDaoMixin {
TodosDao(Database db) : super(db); TodosDao(Database db) : super(db);
Stream<List<TodoEntry>> todosInCategory(Category category) { Stream<List<TodoEntry>> todosInCategory(Category category) {

View File

@ -6,6 +6,6 @@ part of 'todos_dao.dart';
// DaoGenerator // DaoGenerator
// ************************************************************************** // **************************************************************************
mixin _TodosDaoMixin on DatabaseAccessor<Database> { mixin _$TodosDaoMixin on DatabaseAccessor<Database> {
$TodosTable get todos => db.todos; $TodosTable get todos => db.todos;
} }

View File

@ -41,7 +41,7 @@ class DaoGenerator extends GeneratorForAnnotation<UseDao> {
final daoName = enclosingClass.displayName; final daoName = enclosingClass.displayName;
buffer.write('mixin _${daoName}Mixin on ' buffer.write('mixin _\$${daoName}Mixin on '
'DatabaseAccessor<${dbImpl.displayName}> {\n'); 'DatabaseAccessor<${dbImpl.displayName}> {\n');
for (var table in tableTypes) { for (var table in tableTypes) {