mirror of https://github.com/AMT-Cheif/drift.git
Documentation for type converters in moor files
This commit is contained in:
parent
5f91667a24
commit
4685059b14
|
@ -10,6 +10,9 @@ You can achieve this by using `TypeConverters`. In this example, we'll use the t
|
||||||
[json_serializable](https://pub.dev/packages/json_annotation) package to store a custom object in a
|
[json_serializable](https://pub.dev/packages/json_annotation) package to store a custom object in a
|
||||||
text column. Moor supports any Dart type for which you provide a `TypeConverter`, we're using that
|
text column. Moor supports any Dart type for which you provide a `TypeConverter`, we're using that
|
||||||
package here to make the example simpler.
|
package here to make the example simpler.
|
||||||
|
|
||||||
|
## Using converters in Dart
|
||||||
|
|
||||||
```dart
|
```dart
|
||||||
import 'dart:convert';
|
import 'dart:convert';
|
||||||
|
|
||||||
|
@ -72,3 +75,20 @@ The generated `User` class will then have a `preferences` column of type
|
||||||
`Preferences`. Moor will automatically take care of storing and loading
|
`Preferences`. Moor will automatically take care of storing and loading
|
||||||
the object in `select`, `update` and `insert` statements. This feature
|
the object in `select`, `update` and `insert` statements. This feature
|
||||||
also works with [compiled custom queries]({{ "/queries/custom" | absolute_url }}).
|
also works with [compiled custom queries]({{ "/queries/custom" | absolute_url }}).
|
||||||
|
|
||||||
|
## Using converters in moor
|
||||||
|
|
||||||
|
Since moor 2.4, type converters can also be used inside moor files.
|
||||||
|
Assuming that the `Preferences` and `PreferenceConverter` are contained in
|
||||||
|
`preferences.dart`, that file can imported into moor for the type converter to
|
||||||
|
be available.
|
||||||
|
|
||||||
|
```sql
|
||||||
|
import 'preferences.dart';
|
||||||
|
|
||||||
|
CREATE TABLE users (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT,
|
||||||
|
preferences TEXT MAPPED BY `const PreferenceConverter()`
|
||||||
|
);
|
||||||
|
```
|
|
@ -117,13 +117,6 @@ Additionally, columns that have the type name `BOOLEAN` or `DATETIME` will have
|
||||||
written as an `INTEGER` column when the table gets created.
|
written as an `INTEGER` column when the table gets created.
|
||||||
|
|
||||||
## Imports
|
## Imports
|
||||||
{{% alert title="Limited support" %}}
|
|
||||||
> Importing a moor file from another moor file will work as expected.
|
|
||||||
Unfortunately, importing a Dart file from moor does not work in all
|
|
||||||
scenarios. Please upvote [this issue](https://github.com/dart-lang/build/issues/493)
|
|
||||||
on the build package to help solve this.
|
|
||||||
{{% /alert %}}
|
|
||||||
|
|
||||||
You can put import statements at the top of a `moor` file:
|
You can put import statements at the top of a `moor` file:
|
||||||
```sql
|
```sql
|
||||||
import 'other.moor'; -- single quotes are required for imports
|
import 'other.moor'; -- single quotes are required for imports
|
||||||
|
@ -182,6 +175,22 @@ This feature also works for
|
||||||
- whole order-by clauses: `SELECT * FROM todos ORDER BY $order`
|
- whole order-by clauses: `SELECT * FROM todos ORDER BY $order`
|
||||||
- limit clauses: `SELECT * FROM todos LIMIT $limit`
|
- limit clauses: `SELECT * FROM todos LIMIT $limit`
|
||||||
|
|
||||||
|
### Type converters
|
||||||
|
|
||||||
|
You can import and use [type converters]({{< relref "../Advanced Features/type_converters.md" >}})
|
||||||
|
written in Dart in a moor file. Importing a Dart file works with a regular `import` statement.
|
||||||
|
To apply a type converter on a column definition, you can use the `MAPPED BY` column constraints:
|
||||||
|
```sql
|
||||||
|
CREATE TABLE users (
|
||||||
|
id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT,
|
||||||
|
name TEXT,
|
||||||
|
preferences TEXT MAPPED BY `const PreferenceConverter()`
|
||||||
|
);
|
||||||
|
```
|
||||||
|
|
||||||
|
More details on type converts in moor files are available
|
||||||
|
[here]({{< relref "../Advanced Features/type_converters.md#using-converters-in-moor" >}})
|
||||||
|
|
||||||
## Supported statements
|
## Supported statements
|
||||||
At the moment, the following statements can appear in a `.moor` file.
|
At the moment, the following statements can appear in a `.moor` file.
|
||||||
|
|
||||||
|
|
|
@ -1,6 +1,8 @@
|
||||||
## unreleased
|
## unreleased
|
||||||
|
|
||||||
- Support aggregate expressions and `group by` in the Dart api
|
- Support aggregate expressions and `group by` in the Dart api
|
||||||
|
- Support type converters in moor files! The [documentation](https://moor.simonbinder.eu/docs/advanced-features/type_converters/)
|
||||||
|
has been updated to explain how to use them.
|
||||||
|
|
||||||
## 2.3.0
|
## 2.3.0
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue