diff --git a/.travis.yml b/.travis.yml index 94e1f366..d61cd759 100644 --- a/.travis.yml +++ b/.travis.yml @@ -4,8 +4,8 @@ dart: - stable env: - - PKG="sally" - - PKG="sally_generator" + - PKG="moor" + - PKG="moor_generator" script: ./tool/mono_repo_wrapper.sh @@ -17,5 +17,5 @@ branches: cache: directories: - "$HOME/.pub-cache" - - sally/.dart_tool/build - - sally_generator/.dart_tool/build + - moor/.dart_tool/build + - moor_generator/.dart_tool/build diff --git a/README.md b/README.md index 1f5b231d..2e2d8fff 100644 --- a/README.md +++ b/README.md @@ -1,12 +1,12 @@ -# Sally -[![Build Status](https://travis-ci.com/simolus3/sally.svg?token=u4VnFEE5xnWVvkE6QsqL&branch=master)](https://travis-ci.com/simolus3/sally) +# Moor +[![Build Status](https://travis-ci.com/simolus3/moor.svg?token=u4VnFEE5xnWVvkE6QsqL&branch=master)](https://travis-ci.com/simolus3/moor) -Sally is an easy to use and safe way to persist data for Flutter apps. It features +moor is an easy to use and safe way to persist data for Flutter apps. It features a fluent Dart DSL to describe tables and will generate matching database code that can be used to easily read and store your app's data. It also features a reactive API that will deliver auto-updating streams for your queries. -- [Sally](#sally) +- [moor](#moor) * [Getting started](#getting-started) + [Adding the dependency](#adding-the-dependency) + [Declaring tables](#declaring-tables) @@ -27,34 +27,34 @@ API that will deliver auto-updating streams for your queries. ## Getting started ### Adding the dependency -First, let's add sally to your project's `pubspec.yaml`. The library is not yet +First, let's add moor to your project's `pubspec.yaml`. The library is not yet out on pub, so you'll need to use the git repository for now: ```yaml dependencies: - sally: + moor: git: - url: https://github.com/simolus3/sally.git - path: sally/ + url: https://github.com/simolus3/moor.git + path: moor/ dev_dependencies: - sally_generator: + moor_generator: git: - url: https://github.com/simolus3/sally.git - path: sally_generator/ + url: https://github.com/simolus3/moor.git + path: moor_generator/ build_runner: ^1.2.0 ``` -We're going to use the `sally_flutter` library to specify tables and access the database. The -`sally_generator` library will take care of generating the necessary code so the +We're going to use the `moor_flutter` library to specify tables and access the database. The +`moor_generator` library will take care of generating the necessary code so the library knows how your table structure looks like. ### Declaring tables You can use the DSL included with this library to specify your libraries with simple dart code: ```dart -import 'package:sally_flutter/sally_flutter.dart'; +import 'package:moor_flutter/moor_flutter.dart'; // assuming that your file is called filename.dart. This will give an error at first, -// but it's needed for sally to know about the generated code +// but it's needed for moor to know about the generated code part 'filename.g.dart'; // this will generate a table called "todos" for us. The rows of that table will @@ -66,7 +66,7 @@ class Todos extends Table { IntColumn get category => integer().nullable()(); } -// This will make sally generate a class called "Category" to represent a row in this table. +// This will make moor generate a class called "Category" to represent a row in this table. // By default, "Categorie" would have been used because it only strips away the trailing "s" // in the table name. @DataClassName("Category") @@ -76,9 +76,9 @@ class Categories extends Table { TextColumn get description => text()(); } -// this annotation tells sally to prepare a database class that uses both of the +// this annotation tells moor to prepare a database class that uses both of the // tables we just defined. We'll see how to use that database class in a moment. -@UseSally(tables: [Todos, Categories]) +@Usemoor(tables: [Todos, Categories]) class MyDatabase { } @@ -90,14 +90,14 @@ executed. Instead, the generator will take a look at your table classes to figur This won't work if the body of your tables is not constant. This should not be problem, but please be aware of this as you can't put logic inside these classes. ### Generating the code -Sally integrates with the dart `build` system, so you can generate all the code needed with +moor integrates with the dart `build` system, so you can generate all the code needed with `flutter packages pub run build_runner build`. If you want to continously rebuild the code whever you change your code, run `flutter packages pub run build_runner watch` instead. -After running either command once, sally generator will have created a class for your +After running either command once, moor generator will have created a class for your database and data classes for your entities. To use it, change the `MyDatabase` class as follows: ```dart -@UseSally(tables: [Todos, Categories]) +@Usemoor(tables: [Todos, Categories]) class MyDatabase extends _$MyDatabase { // we tell the database where to store the data with this constructor MyDatabase() : super(FlutterQueryExecutor.inDatabaseFolder(path: 'db.sqlite')); @@ -128,7 +128,7 @@ class MyDatabase extends _$MyDatabase { ### Select statements You can create `select` statements by starting them with `select(tableName)`, where the table name -is a field generated for you by sally. Each table used in a database will have a matching field +is a field generated for you by moor. Each table used in a database will have a matching field to run queries against. A query can be run once with `get()` or be turned into an auto-updating stream using `watch()`. #### Where @@ -232,7 +232,7 @@ Stream> categoriesWithCount() { ``` ## Migrations -Sally provides a migration API that can be used to gradually apply schema changes after bumping +moor provides a migration API that can be used to gradually apply schema changes after bumping the `schemaVersion` getter inside the `Database` class. To use it, override the `migration` getter. Here's an example: Let's say you wanted to add a due date to your todo entries: ```dart @@ -273,7 +273,7 @@ available from your main database class. Consider the following code: ```dart part 'todos_dao.g.dart'; -// the _TodosDaoMixin will be created by sally. It contains all the necessary +// the _TodosDaoMixin will be created by moor. It contains all the necessary // fields for the tables. The type annotation is the database class // that should use this dao. @UseDao(tables: [Todos]) @@ -292,7 +292,7 @@ class TodosDao extends DatabaseAccessor with _TodosDaoMixin { } } ``` -If we now change the annotation on the `MyDatabase` class to `@UseSally(tables: [Todos, Categories], daos: [TodosDao])` +If we now change the annotation on the `MyDatabase` class to `@Usemoor(tables: [Todos, Categories], daos: [TodosDao])` and re-run the code generation, a getter `todosDao` can be used to access the instance of that dao. ## TODO-List and current limitations diff --git a/moor/example/example.g.dart b/moor/example/example.g.dart index 06cbf434..8acf7d66 100644 --- a/moor/example/example.g.dart +++ b/moor/example/example.g.dart @@ -3,7 +3,7 @@ part of 'example.dart'; // ************************************************************************** -// moorGenerator +// MoorGenerator // ************************************************************************** class Category { diff --git a/moor/test/data/tables/todos.g.dart b/moor/test/data/tables/todos.g.dart index 9d9ac930..37bf7f4d 100644 --- a/moor/test/data/tables/todos.g.dart +++ b/moor/test/data/tables/todos.g.dart @@ -3,7 +3,7 @@ part of 'todos.dart'; // ************************************************************************** -// moorGenerator +// MoorGenerator // ************************************************************************** class TodoEntry { diff --git a/moor_flutter/README.md b/moor_flutter/README.md index 4e44a85c..4a17da98 100644 --- a/moor_flutter/README.md +++ b/moor_flutter/README.md @@ -1,7 +1,7 @@ -# moor +# Moor [![Build Status](https://travis-ci.com/simolus3/moor.svg?token=u4VnFEE5xnWVvkE6QsqL&branch=master)](https://travis-ci.com/simolus3/moor) -moor is an easy to use and safe way to persist data for Flutter apps. It features +Moor is an easy to use and safe way to persist data for Flutter apps. It features a fluent Dart DSL to describe tables and will generate matching database code that can be used to easily read and store your app's data. It also features a reactive API that will deliver auto-updating streams for your queries. diff --git a/moor_flutter/example/android/app/build.gradle b/moor_flutter/example/android/app/build.gradle index 6b03fc27..7e423ae6 100644 --- a/moor_flutter/example/android/app/build.gradle +++ b/moor_flutter/example/android/app/build.gradle @@ -26,7 +26,7 @@ apply plugin: 'kotlin-android' apply from: "$flutterRoot/packages/flutter_tools/gradle/flutter.gradle" android { - compileSdkVersion 27 + compileSdkVersion 28 sourceSets { main.java.srcDirs += 'src/main/kotlin' @@ -38,9 +38,9 @@ android { defaultConfig { // TODO: Specify your own unique Application ID (https://developer.android.com/studio/build/application-id.html). - applicationId "eu.simonbinder.moorexample.moorexample" + applicationId "eu.simonbinder.moor_example.example" minSdkVersion 16 - targetSdkVersion 27 + targetSdkVersion 28 versionCode flutterVersionCode.toInteger() versionName flutterVersionName testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner" diff --git a/moor_flutter/example/android/app/src/debug/AndroidManifest.xml b/moor_flutter/example/android/app/src/debug/AndroidManifest.xml new file mode 100644 index 00000000..38643255 --- /dev/null +++ b/moor_flutter/example/android/app/src/debug/AndroidManifest.xml @@ -0,0 +1,7 @@ + + + + diff --git a/moor_flutter/example/android/app/src/main/AndroidManifest.xml b/moor_flutter/example/android/app/src/main/AndroidManifest.xml index 8863383c..a093ec9b 100644 --- a/moor_flutter/example/android/app/src/main/AndroidManifest.xml +++ b/moor_flutter/example/android/app/src/main/AndroidManifest.xml @@ -1,11 +1,5 @@ - - - + package="eu.simonbinder.moor_example.example"> + + diff --git a/moor_flutter/lib/sally_flutter.dart b/moor_flutter/lib/moor_flutter.dart similarity index 100% rename from moor_flutter/lib/sally_flutter.dart rename to moor_flutter/lib/moor_flutter.dart diff --git a/moor_generator/lib/generator.dart b/moor_generator/lib/generator.dart index 2e0add63..14778707 100644 --- a/moor_generator/lib/generator.dart +++ b/moor_generator/lib/generator.dart @@ -4,4 +4,4 @@ import 'package:source_gen/source_gen.dart'; import 'package:moor_generator/src/moor_generator.dart'; Builder moorBuilder(BuilderOptions _) => - SharedPartBuilder([moorGenerator(), DaoGenerator()], 'moor'); + SharedPartBuilder([MoorGenerator(), DaoGenerator()], 'moor'); diff --git a/moor_generator/lib/src/errors.dart b/moor_generator/lib/src/errors.dart index 3dcc0e95..81b47928 100644 --- a/moor_generator/lib/src/errors.dart +++ b/moor_generator/lib/src/errors.dart @@ -1,17 +1,17 @@ import 'package:analyzer/dart/element/element.dart'; -class moorError { +class MoorError { final bool critical; final String message; final Element affectedElement; - moorError({this.critical = false, this.message, this.affectedElement}); + MoorError({this.critical = false, this.message, this.affectedElement}); } class ErrorStore { - final List errors = []; + final List errors = []; - void add(moorError error) => errors.add(error); + void add(MoorError error) => errors.add(error); bool get hasCriticalError => errors.any((e) => e.critical); } diff --git a/moor_generator/lib/src/sally_generator.dart b/moor_generator/lib/src/moor_generator.dart similarity index 97% rename from moor_generator/lib/src/sally_generator.dart rename to moor_generator/lib/src/moor_generator.dart index c6dfd89d..3c6a8a21 100644 --- a/moor_generator/lib/src/sally_generator.dart +++ b/moor_generator/lib/src/moor_generator.dart @@ -12,7 +12,7 @@ import 'package:moor_generator/src/parser/table_parser.dart'; import 'package:moor_generator/src/writer/database_writer.dart'; import 'package:source_gen/source_gen.dart'; -class moorGenerator extends GeneratorForAnnotation { +class MoorGenerator extends GeneratorForAnnotation { //final Map _astForLibs = {}; final ErrorStore errors = ErrorStore(); @@ -51,7 +51,7 @@ class moorGenerator extends GeneratorForAnnotation { for (var table in tableTypes) { if (!tableTypeChecker.isAssignableFrom(table.element)) { - errors.add(moorError( + errors.add(MoorError( critical: true, message: 'The type $table is not a moor table', affectedElement: element)); diff --git a/moor_generator/lib/src/parser/column_parser.dart b/moor_generator/lib/src/parser/column_parser.dart index da676181..d6ab741c 100644 --- a/moor_generator/lib/src/parser/column_parser.dart +++ b/moor_generator/lib/src/parser/column_parser.dart @@ -24,7 +24,7 @@ const String errorMessage = 'This getter does not create a valid column that ' 'columns are formed. If you have any questions, feel free to raise an issue.'; class ColumnParser extends ParserBase { - ColumnParser(moorGenerator generator) : super(generator); + ColumnParser(MoorGenerator generator) : super(generator); SpecifiedColumn parse(MethodDeclaration getter) { /* @@ -39,7 +39,7 @@ class ColumnParser extends ParserBase { final expr = returnExpressionOfMethod(getter); if (!(expr is FunctionExpressionInvocation)) { - generator.errors.add(moorError( + generator.errors.add(MoorError( affectedElement: getter.declaredElement, message: errorMessage, critical: true, @@ -69,7 +69,7 @@ class ColumnParser extends ParserBase { switch (methodName) { case functionNamed: if (foundExplicitName != null) { - generator.errors.add(moorError( + generator.errors.add(MoorError( critical: false, affectedElement: getter.declaredElement, message: @@ -79,7 +79,7 @@ class ColumnParser extends ParserBase { foundExplicitName = readStringLiteral(remainingExpr.argumentList.arguments.first, () { - generator.errors.add(moorError( + generator.errors.add(MoorError( critical: false, affectedElement: getter.declaredElement, message: diff --git a/moor_generator/lib/src/parser/parser.dart b/moor_generator/lib/src/parser/parser.dart index f2400428..dd963858 100644 --- a/moor_generator/lib/src/parser/parser.dart +++ b/moor_generator/lib/src/parser/parser.dart @@ -10,7 +10,7 @@ class Parser { } class ParserBase { - final moorGenerator generator; + final MoorGenerator generator; ParserBase(this.generator); @@ -18,7 +18,7 @@ class ParserBase { final body = method.body; if (!(body is ExpressionFunctionBody)) { - generator.errors.add(moorError( + generator.errors.add(MoorError( affectedElement: method.declaredElement, critical: true, message: diff --git a/moor_generator/lib/src/parser/table_parser.dart b/moor_generator/lib/src/parser/table_parser.dart index e21ed77a..3ba73474 100644 --- a/moor_generator/lib/src/parser/table_parser.dart +++ b/moor_generator/lib/src/parser/table_parser.dart @@ -11,7 +11,7 @@ import 'package:moor_generator/src/moor_generator.dart'; // ignore: implementati import 'package:recase/recase.dart'; class TableParser extends ParserBase { - TableParser(moorGenerator generator) : super(generator); + TableParser(MoorGenerator generator) : super(generator); SpecifiedTable parse(ClassElement element) { final sqlName = _parseTableName(element); @@ -58,7 +58,7 @@ class TableParser extends ParserBase { tableNameDeclaration.node as MethodDeclaration); final tableName = readStringLiteral(returnExpr, () { - generator.errors.add(moorError( + generator.errors.add(MoorError( critical: true, message: 'This getter must return a string literal, and do nothing more', @@ -79,7 +79,7 @@ class TableParser extends ParserBase { as MethodDeclaration; final body = ast.body; if (body is! ExpressionFunctionBody) { - generator.errors.add(moorError( + generator.errors.add(MoorError( affectedElement: primaryKeyGetter, message: 'This must return a set literal using the => syntax!')); return null; @@ -105,7 +105,7 @@ class TableParser extends ParserBase { parsedPrimaryKey.add(column); } } else { - generator.errors.add(moorError( + generator.errors.add(MoorError( affectedElement: primaryKeyGetter, message: 'This must return a set literal!')); return null; diff --git a/moor_generator/test/parser/parser_test.dart b/moor_generator/test/parser/parser_test.dart index c9c2c833..9bd3e862 100644 --- a/moor_generator/test/parser/parser_test.dart +++ b/moor_generator/test/parser/parser_test.dart @@ -8,7 +8,7 @@ import 'package:build_test/build_test.dart'; void main() async { LibraryElement testLib; - moorGenerator generator; + MoorGenerator generator; setUpAll(() async { testLib = await resolveSource(r''' @@ -48,7 +48,7 @@ void main() async { }); setUp(() { - generator = moorGenerator(); + generator = MoorGenerator(); generator ..columnParser = ColumnParser(generator) ..tableParser = TableParser(generator);