Update docs to reflect the new extension-based API

This commit is contained in:
Simon Binder 2019-11-09 21:16:24 +01:00
parent ba6411e6f0
commit 85fa0d4764
No known key found for this signature in database
GPG Key ID: 7891917E4147B8C0
9 changed files with 101 additions and 19 deletions

View File

@ -13,14 +13,19 @@ linkTitle: "Moor"
<a class="btn btn-lg btn-secondary mr-3 mb-4" href="https://pub.dev/packages/moor_flutter">
Get fom pub <i class="fas fa-code ml-2 "></i>
</a>
<p class="lead mt-5">
With a fluent query api, a powerful sql analyzer, auto-updating streams and much moor,
moor makes persistence fun. Scroll down to learn about moor's key features, or visit the
<a href="{{< relref "/docs/Getting started" >}}">getting started guide</a> for a step-by-step guide on using moor.
</p>
</div>
{{< /blocks/cover >}}
{{% blocks/lead color="dark" %}}
Moor is an easy to use, reactive persistence library for Flutter apps. Define your
database tables in pure Dart and enjoy a fluent query API, auto-updating streams
and more!
Moor is an easy to use, reactive persistence library for Flutter apps. Define tables in Dart or
SQL and enjoy a fluent query API, auto-updating streams and more!
{{% /blocks/lead %}}
{{< blocks/section color="primary" >}}
@ -42,9 +47,32 @@ code.
{{% blocks/feature icon="fas fa-star" title="And much more!" %}}
Moor provides auto-updating `Streams` for all your queries, makes dealing with transactions and migrations easy
Moor provides auto-updating streams for all your queries, makes dealing with transactions and migrations easy
and lets your write modular database code with DAOs. We even have a [sql IDE](too) builtin to the project
When using moor, working with databases in Dart is fun!
{{% /blocks/feature %}}
{{< /blocks/section >}}
{{< blocks/section color="light" type="section" >}}
<h2>Key moor features</h2>
Here are some of the many ways moor helps you write awesome database code:
<ul>
<li><b>Auto-updating streams</b>: With moor, any query - no matter how complex - can be turned into a stream that emits new data as the underlying data changes.</li>
<li><b>Polyglot</b>: Moor let's you write queries in a fluent Dart api or directly in SQL - you can even embed Dart expressions in SQL.</li>
<li><b>Safety</b>: Moor can verify your table declarations and queries at compile time, providing helpful and descriptive hints when it finds problems.</li>
<li><b>Boilerplate-free</b>: Stop writing mapping code yourself - moor can take of that. Moor generates Dart code around your data so you can focus
on building great apps.</li>
<li><b>Flexible</b>: Want to write queries in SQL? Moor verifies them at compile time and generates Dart apis for them. Prefer to write them in Dart?
Moor will generate efficient SQL for Dart queries.</li>
<li><b>Easy to learn</b>: Instead of having to learn yet another ORM, moor lets you write queries in SQL and generates typesafe wrappers. Queries and tables
can also be written in Dart that looks similar to SQL without loosing type-safety.</li>
<li><b>Fast _and_ powerful</b>: With the new `moor_ffi` backend, moor can outperform key-value stores without putting any compromises and the integrity
and flexibility that relational databases provide. Moor is the only major persistence library with builtin support for multiple isolates.</li>
<li><b>Well tested and production ready</b>: Each component of moor is verified by a wide range of unit and integration tests. Moor powers many Flutter apps
in production.</li>
<li><b>Cross-Platform</b>: Moor works on iOS, Android, Linux, macOS, Windows and on the web. It doesn't even require Flutter.</li>
</ul>
{{< /blocks/section >}}

View File

@ -32,13 +32,28 @@ Future<List<Animal>> findAnimalsByLegs(int legCount) {
```
## Boolean algebra
You can nest boolean expressions by using the top-level `and`, `or` and `not` functions
You can nest boolean expressions by using the `&`, `!` operators and the `not` method
exposed by moor:
```dart
// find all animals that aren't mammals and have 4 legs
select(animals)..where((a) => and(not(a.isMammal), a.amountOfLegs.equals(4)))
select(animals)..where((a) => a.isMammal.not() & a.amountOfLegs.equals(4))
```
## Arithmetic
For `int` and `double` expressions, you can use the `+`, `-`, `*` and `/` operators. To
run calculations between a sql expression and a Dart value, wrap it in a `Variable`:
```dart
Future<List<Product>> canBeBought(int amount, int price) {
return (select(products)..where((p) {
final totalPrice = p.price * Variable(amount);
return totalPrice.isSmallerOrEqualValue(price);
})).get();
}
```
String expressions define a `+` operator as well. Just like you would expect, it performs
concatenation in sql.
## Nullability
To check whether an expression returns null, you can use the top-level `isNull` function,
which takes any expression and returns a boolean expression. The expression returned will
@ -46,20 +61,21 @@ resolve to `true` if the inner expression resolves to null and `false` otherwise
As you would expect, `isNotNull` works the other way around.
## Date and Time
For columns and expressions that return a `DateTime`, you can use the top-level
`year`, `month`, `day`, `hour`, `minute` and `second` functions to extract individual
For columns and expressions that return a `DateTime`, you can use the
`year`, `month`, `day`, `hour`, `minute` and `second` getters to extract individual
fields from that date:
```dart
select(users)..where((u) => year(u.birthDate).isLessThan(1950))
select(users)..where((u) => u.birthDate.year.isLessThan(1950))
```
To obtain the current date or the current time as an expression, use the `currentDate`
and `currentDateAndTime` constants provided by moor.
## `IN` and `NOT IN`
You can check whether an expression is in a list of values by using the `isIn` function:
You can check whether an expression is in a list of values by using the `isIn` andd `isNotIn`
method:
```dart
select(animals)..where((a) => isIn(a.amountOfLegs, [3, 7, 4, 2]))
select(animals)..where((a) => a.amountOfLegs.isIn([3, 7, 4, 2]);
```
Again, the `isNotIn` function works the other way around.

View File

@ -42,7 +42,7 @@ stream using `watch()`.
You can apply filters to a query by calling `where()`. The where method takes a function that
should map the given table to an `Expression` of boolean. A common way to create such expression
is by using `equals` on expressions. Integer columns can also be compared with `isBiggerThan`
and `isSmallerThan`. You can compose expressions using `and(a, b), or(a, b)` and `not(a)`.
and `isSmallerThan`. You can compose expressions using `a & b, a | b` and `a.not()`.
### Limit
You can limit the amount of results returned by calling `limit` on queries. The method accepts
the amount of rows to return and an optional offset.

View File

@ -43,11 +43,9 @@ Make sure that your project depends on moor 2.0 or later. Then
set to true. Contrary to its name, that flag turns on the plugin system, so you
don't need to worry about angular.
2. Tell Dart Code to analyze moor files as well. Add this to your `settings.json`:
```json
"dart.additionalAnalyzerFileExtensions": [
"moor"
]
```
```json
"dart.additionalAnalyzerFileExtensions": ["moor"]
```
3. Finally, close and reopen your IDE so that the analysis server is restarted. The analysis server will
then load the moor plugin and start providing analysis results for `.moor` files. Loading the plugin
can take some time (around a minute for the first time).

View File

@ -1,7 +1,13 @@
## unreleased
- New extension methods to simplify the Dart api!
- Use `&`, `or` and `.not()` to combine boolean expressions
- Use `&`, `or` and `.not()` to combine boolean expressions.
```dart
// OLD
select(animals)..where((a) => and(not(a.isMammal), a.amountOfLegs.equals(4)))
// NEW:
select(animals)..where((a) => a.isMammal.not() & a.amountOfLegs.equals(4))
```
- Arithmetic: New `+`, `-`, `*` and `/` operators for int and double sql expressions
- New `+` operator for string concatenation
- Fix crash when `customStatement` is the first operation used on a database ([#199](https://github.com/simolus3/moor/issues/199))
@ -10,6 +16,7 @@
- Experimental support to run moor on a background isolate
- Reduce use of parentheses in SQL code generated at runtime
- Query streams now emit errors that happened while running the query
- Upgraded the sql parser which now supports `WITH` clauses in moor files
## 2.0.1

View File

@ -29,10 +29,24 @@ abstract class Expression<D, T extends SqlType<D>> implements Component {
/// Casts this expression to an expression with [D] and [T] parameter without
/// changing what's written with [writeInto]. In particular, using [dartCast]
/// will __NOT__ generate a `CAST` expression in sql.
///
/// This method is used internally by moor.
Expression<D2, T2> dartCast<D2, T2 extends SqlType<D2>>() {
return _CastExpression<D, D2, T, T2>(this);
}
/// An expression that is true if `this` resolves to any of the values in
/// [values].
Expression<bool, BoolType> isIn(Iterable<D> values) {
return _InExpression(this, values, false);
}
/// An expression that is true if `this` does not resolve to any of the values
/// in [values].
Expression<bool, BoolType> isNotIn(Iterable<D> values) {
return _InExpression(this, values, true);
}
/// Writes this expression into the [GenerationContext], assuming that there's
/// an outer expression with [precedence]. If the [Expression.precedence] of
/// `this` expression is lower, it will be wrapped in

View File

@ -2,14 +2,20 @@ part of '../query_builder.dart';
/// An expression that is true if the given [expression] resolves to any of the
/// values in [values].
@Deprecated('Use Expression.isIn instead')
Expression<bool, BoolType> isIn<X extends SqlType<T>, T>(
Expression<T, X> expression, Iterable<T> values,
{bool not = false}) {
return _InExpression(expression, values, not);
if (not == false) {
return expression.isNotIn(values);
} else {
return expression.isIn(values);
}
}
/// An expression that is true if the given [expression] does not resolve to any
/// of the values in [values].
@Deprecated('Use Expression.isNotIn instead')
Expression<bool, BoolType> isNotIn<X extends SqlType<T>, T>(
Expression<T, X> expression, Iterable<T> values) =>
isIn(expression, values, not: true);

View File

@ -1,5 +1,8 @@
part of '../query_builder.dart';
// we're not using extensions for this because I'm not sure if / how this could
// look together with NNBD in the future
/// Expression that is true if the inner expression resolves to a null value.
Expression<bool, BoolType> isNull(Expression inner) => _NullCheck(inner, true);

View File

@ -3,10 +3,12 @@ import 'package:moor/moor.dart';
import 'package:moor/moor.dart' as moor;
import '../data/tables/todos.dart';
import '../data/utils/expect_generated.dart';
void main() {
test('in expressions are generated', () {
final innerExpression = GeneratedTextColumn('name', null, true);
// ignore: deprecated_member_use_from_same_package
final isInExpression = moor.isIn(innerExpression, ['Max', 'Tobias']);
final context = GenerationContext.fromDb(TodoDb(null));
@ -15,4 +17,12 @@ void main() {
expect(context.sql, 'name IN (?, ?)');
expect(context.boundVariables, ['Max', 'Tobias']);
});
test('not in expressions are generated', () {
final innerExpression = GeneratedTextColumn('name', null, true);
// ignore: deprecated_member_use_from_same_package
final isNotIn = moor.isNotIn(innerExpression, ['Foo', 'Bar']);
isNotIn.expectGenerates('name NOT IN (?, ?)');
});
}