mirror of https://github.com/AMT-Cheif/drift.git
Clarify use of custom code in table dsl classes in readme
This commit is contained in:
parent
3e1cbee3c6
commit
d4b97ab6fc
|
@ -80,10 +80,10 @@ class MyDatabase {
|
|||
}
|
||||
```
|
||||
|
||||
__⚠️ Warning:__ Even though it might look like it, the content of a `Table` class does not support full Dart code. It can only
|
||||
be used to declare the table name, its primary key and columns. The code inside of a table class will never be
|
||||
executed. Instead, the generator will take a look at your table classes to figure out how their structure looks like.
|
||||
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.
|
||||
__⚠️ Note:__ The column definitions, the table name and the primary key must be known at
|
||||
compile time. For column definitions and the primary key, the function must use the `=>`
|
||||
operator and can't contain anything more than what's included in this `readme` and the
|
||||
examples. Otherwise, the generator won't be able to know what's going on.
|
||||
|
||||
### Generating the code
|
||||
Moor integrates with the dart `build` system, so you can generate all the code needed with
|
||||
|
|
|
@ -80,10 +80,10 @@ class MyDatabase {
|
|||
}
|
||||
```
|
||||
|
||||
__⚠️ Warning:__ Even though it might look like it, the content of a `Table` class does not support full Dart code. It can only
|
||||
be used to declare the table name, its primary key and columns. The code inside of a table class will never be
|
||||
executed. Instead, the generator will take a look at your table classes to figure out how their structure looks like.
|
||||
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.
|
||||
__⚠️ Note:__ The column definitions, the table name and the primary key must be known at
|
||||
compile time. For column definitions and the primary key, the function must use the `=>`
|
||||
operator and can't contain anything more than what's included in this `readme` and the
|
||||
examples. Otherwise, the generator won't be able to know what's going on.
|
||||
|
||||
### Generating the code
|
||||
Moor integrates with the dart `build` system, so you can generate all the code needed with
|
||||
|
|
|
@ -1,4 +1,5 @@
|
|||
import 'dart:async';
|
||||
import 'dart:convert';
|
||||
import 'package:moor_example/database/todos_dao.dart';
|
||||
import 'package:moor_flutter/moor_flutter.dart';
|
||||
|
||||
|
@ -13,6 +14,10 @@ class Todos extends Table {
|
|||
DateTimeColumn get targetDate => dateTime().nullable()();
|
||||
|
||||
IntColumn get category => integer().nullable()();
|
||||
|
||||
String toJson(TodoEntry entry) {
|
||||
return json.encode(entry.toJson());
|
||||
}
|
||||
}
|
||||
|
||||
@DataClassName('Category')
|
||||
|
|
|
@ -23,6 +23,14 @@ class TodoEntry {
|
|||
category: intType.mapFromDatabaseResponse(data['category']),
|
||||
);
|
||||
}
|
||||
factory TodoEntry.fromJson(Map<String, dynamic> json) {
|
||||
return TodoEntry(
|
||||
id: json['id'] as int,
|
||||
content: json['content'] as String,
|
||||
targetDate: json['targetDate'] as DateTime,
|
||||
category: json['category'] as int,
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -69,21 +77,26 @@ class TodoEntry {
|
|||
class $TodosTable extends Todos implements TableInfo<Todos, TodoEntry> {
|
||||
final GeneratedDatabase _db;
|
||||
$TodosTable(this._db);
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id =>
|
||||
GeneratedIntColumn('id', false, hasAutoIncrement: true);
|
||||
_id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
|
||||
GeneratedTextColumn _content;
|
||||
@override
|
||||
GeneratedTextColumn get content => GeneratedTextColumn(
|
||||
GeneratedTextColumn get content => _content ??= GeneratedTextColumn(
|
||||
'content',
|
||||
false,
|
||||
);
|
||||
GeneratedDateTimeColumn _targetDate;
|
||||
@override
|
||||
GeneratedDateTimeColumn get targetDate => GeneratedDateTimeColumn(
|
||||
GeneratedDateTimeColumn get targetDate =>
|
||||
_targetDate ??= GeneratedDateTimeColumn(
|
||||
'target_date',
|
||||
true,
|
||||
);
|
||||
GeneratedIntColumn _category;
|
||||
@override
|
||||
GeneratedIntColumn get category => GeneratedIntColumn(
|
||||
GeneratedIntColumn get category => _category ??= GeneratedIntColumn(
|
||||
'category',
|
||||
true,
|
||||
);
|
||||
|
@ -137,6 +150,12 @@ class Category {
|
|||
description: stringType.mapFromDatabaseResponse(data['`desc`']),
|
||||
);
|
||||
}
|
||||
factory Category.fromJson(Map<String, dynamic> json) {
|
||||
return Category(
|
||||
id: json['id'] as int,
|
||||
description: json['description'] as String,
|
||||
);
|
||||
}
|
||||
Map<String, dynamic> toJson() {
|
||||
return {
|
||||
'id': id,
|
||||
|
@ -169,11 +188,13 @@ class $CategoriesTable extends Categories
|
|||
implements TableInfo<Categories, Category> {
|
||||
final GeneratedDatabase _db;
|
||||
$CategoriesTable(this._db);
|
||||
GeneratedIntColumn _id;
|
||||
@override
|
||||
GeneratedIntColumn get id =>
|
||||
GeneratedIntColumn('id', false, hasAutoIncrement: true);
|
||||
_id ??= GeneratedIntColumn('id', false, hasAutoIncrement: true);
|
||||
GeneratedTextColumn _description;
|
||||
@override
|
||||
GeneratedTextColumn get description => GeneratedTextColumn(
|
||||
GeneratedTextColumn get description => _description ??= GeneratedTextColumn(
|
||||
'`desc`',
|
||||
false,
|
||||
);
|
||||
|
@ -209,8 +230,10 @@ class $CategoriesTable extends Categories
|
|||
|
||||
abstract class _$Database extends GeneratedDatabase {
|
||||
_$Database(QueryExecutor e) : super(const SqlTypeSystem.withDefaults(), e);
|
||||
$TodosTable get todos => $TodosTable(this);
|
||||
$CategoriesTable get categories => $CategoriesTable(this);
|
||||
$TodosTable _todos;
|
||||
$TodosTable get todos => _todos ??= $TodosTable(this);
|
||||
$CategoriesTable _categories;
|
||||
$CategoriesTable get categories => _categories ??= $CategoriesTable(this);
|
||||
TodosDao _todosDao;
|
||||
TodosDao get todosDao => _todosDao ??= TodosDao(this as Database);
|
||||
@override
|
||||
|
|
Loading…
Reference in New Issue