diff --git a/.travis.yml b/.travis.yml index 3a0dc803..1b9ed4eb 100644 --- a/.travis.yml +++ b/.travis.yml @@ -10,12 +10,6 @@ env: script: ./tool/mono_repo_wrapper.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: directories: - "$HOME/.pub-cache" diff --git a/moor/lib/src/dsl/database.dart b/moor/lib/src/dsl/database.dart index 1fdc1402..ba19ffba 100644 --- a/moor/lib/src/dsl/database.dart +++ b/moor/lib/src/dsl/database.dart @@ -3,6 +3,17 @@ import 'package:moor/moor.dart'; /// Use this class as an annotation to inform moor_generator that a database /// 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 { /// The tables to include in the database final List tables; @@ -10,6 +21,8 @@ class UseMoor { /// 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 /// database logic into smaller components. + /// + /// For instructions on how to write a dao, see the documentation of [UseDao] final List daos; /// 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 []}); } -/// Annotation to use on classes that implement [DatabaseAccessor]. It specified -/// which tables should be managed in this dao. +/// Annotation to use on classes that implement [DatabaseAccessor]. It specifies +/// 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 { +/// 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 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 { /// The tables accessed by this DAO. final List tables; diff --git a/moor/lib/src/runtime/database.dart b/moor/lib/src/runtime/database.dart index 34829ead..8eaa9f8d 100644 --- a/moor/lib/src/runtime/database.dart +++ b/moor/lib/src/runtime/database.dart @@ -8,9 +8,11 @@ import 'package:moor/src/runtime/statements/select.dart'; import 'package:moor/src/runtime/statements/update.dart'; /// 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 /// migration logic can live in the main [GeneratedDatabase] class, but code /// can be extracted into [DatabaseAccessor]s outside of that database. +/// For details on how to write a dao, see [UseDao]. abstract class DatabaseAccessor extends DatabaseConnectionUser with QueryEngine { @protected diff --git a/moor_flutter/example/lib/database/todos_dao.dart b/moor_flutter/example/lib/database/todos_dao.dart index 493bcdf8..ed0705fc 100644 --- a/moor_flutter/example/lib/database/todos_dao.dart +++ b/moor_flutter/example/lib/database/todos_dao.dart @@ -6,7 +6,7 @@ import 'database.dart'; part 'todos_dao.g.dart'; @UseDao(tables: [Todos]) -class TodosDao extends DatabaseAccessor with _TodosDaoMixin { +class TodosDao extends DatabaseAccessor with _$TodosDaoMixin { TodosDao(Database db) : super(db); Stream> todosInCategory(Category category) { diff --git a/moor_flutter/example/lib/database/todos_dao.g.dart b/moor_flutter/example/lib/database/todos_dao.g.dart index 950d1e60..86c20fdd 100644 --- a/moor_flutter/example/lib/database/todos_dao.g.dart +++ b/moor_flutter/example/lib/database/todos_dao.g.dart @@ -6,6 +6,6 @@ part of 'todos_dao.dart'; // DaoGenerator // ************************************************************************** -mixin _TodosDaoMixin on DatabaseAccessor { +mixin _$TodosDaoMixin on DatabaseAccessor { $TodosTable get todos => db.todos; } diff --git a/moor_generator/lib/src/dao_generator.dart b/moor_generator/lib/src/dao_generator.dart index 475739e4..66c5f31b 100644 --- a/moor_generator/lib/src/dao_generator.dart +++ b/moor_generator/lib/src/dao_generator.dart @@ -41,7 +41,7 @@ class DaoGenerator extends GeneratorForAnnotation { final daoName = enclosingClass.displayName; - buffer.write('mixin _${daoName}Mixin on ' + buffer.write('mixin _\$${daoName}Mixin on ' 'DatabaseAccessor<${dbImpl.displayName}> {\n'); for (var table in tableTypes) {