mirror of https://github.com/AMT-Cheif/drift.git
docs complete!
This commit is contained in:
parent
b67e4234e7
commit
3c39d8456b
|
@ -78,42 +78,34 @@ extension ManagerExamples on AppDatabase {
|
||||||
// #docregion manager_filter
|
// #docregion manager_filter
|
||||||
Future<void> filterTodoItems() async {
|
Future<void> filterTodoItems() async {
|
||||||
// All items with a title of "Title"
|
// All items with a title of "Title"
|
||||||
await managers.todoItems.filter((f) => f.title("Title")).get();
|
managers.todoItems.filter((f) => f.title("Title"));
|
||||||
|
|
||||||
// All items with a title of "Title" and content of "Content"
|
// All items with a title of "Title" and content of "Content"
|
||||||
await managers.todoItems
|
managers.todoItems.filter((f) => f.title("Title") & f.content("Content"));
|
||||||
.filter((f) => f.title("Title") & f.content("Content"))
|
|
||||||
.get();
|
|
||||||
|
|
||||||
// All items with a title of "Title" or content that is not null
|
// All items with a title of "Title" or content that is not null
|
||||||
await managers.todoItems
|
managers.todoItems.filter((f) => f.title("Title") | f.content.not.isNull());
|
||||||
.filter((f) => f.title("Title") | f.content.not.isNull())
|
|
||||||
.get();
|
|
||||||
}
|
}
|
||||||
// #enddocregion manager_filter
|
// #enddocregion manager_filter
|
||||||
|
|
||||||
// #docregion manager_type_specific_filter
|
// #docregion manager_type_specific_filter
|
||||||
Future filterWithType() async {
|
Future filterWithType() async {
|
||||||
// Filter all items created since 7 days ago
|
// Filter all items created since 7 days ago
|
||||||
await managers.todoItems
|
managers.todoItems.filter(
|
||||||
.filter((f) =>
|
(f) => f.createdAt.isAfter(DateTime.now().subtract(Duration(days: 7))));
|
||||||
f.createdAt.isAfter(DateTime.now().subtract(Duration(days: 7))))
|
|
||||||
.get();
|
|
||||||
|
|
||||||
// Filter all items with a title that starts with "Title"
|
// Filter all items with a title that starts with "Title"
|
||||||
await managers.todoItems.filter((f) => f.title.startsWith('Title')).get();
|
managers.todoItems.filter((f) => f.title.startsWith('Title'));
|
||||||
}
|
}
|
||||||
// #enddocregion manager_type_specific_filter
|
// #enddocregion manager_type_specific_filter
|
||||||
|
|
||||||
// #docregion manager_ordering
|
// #docregion manager_ordering
|
||||||
Future orderWithType() async {
|
Future orderWithType() async {
|
||||||
// Order all items by their creation date in ascending order
|
// Order all items by their creation date in ascending order
|
||||||
await managers.todoItems.orderBy((o) => o.createdAt.asc()).get();
|
managers.todoItems.orderBy((o) => o.createdAt.asc());
|
||||||
|
|
||||||
// Order all items by their title in ascending order and then by their content in ascending order
|
// Order all items by their title in ascending order and then by their content in ascending order
|
||||||
await managers.todoItems
|
managers.todoItems.orderBy((o) => o.title.asc() & o.content.asc());
|
||||||
.orderBy((o) => o.title.asc() & o.content.asc())
|
|
||||||
.get();
|
|
||||||
}
|
}
|
||||||
// #enddocregion manager_ordering
|
// #enddocregion manager_ordering
|
||||||
|
|
||||||
|
@ -136,6 +128,35 @@ extension ManagerExamples on AppDatabase {
|
||||||
await managers.todoItems.filter((f) => f.title("Title")).exists();
|
await managers.todoItems.filter((f) => f.title("Title")).exists();
|
||||||
}
|
}
|
||||||
// #enddocregion manager_exists
|
// #enddocregion manager_exists
|
||||||
|
|
||||||
|
// #docregion manager_filter_forward_references
|
||||||
|
Future relationalFilter() async {
|
||||||
|
// Get all items with a category description of "School"
|
||||||
|
managers.todoItems
|
||||||
|
.filter((f) => f.category((f) => f.description("School")));
|
||||||
|
|
||||||
|
// These can be combined with other filters
|
||||||
|
// For example, get all items with a title of "Title" or a category description of "School"
|
||||||
|
await managers.todoItems
|
||||||
|
.filter(
|
||||||
|
(f) => f.title("Title") | f.category((f) => f.description("School")),
|
||||||
|
)
|
||||||
|
.exists();
|
||||||
|
}
|
||||||
|
// #enddocregion manager_filter_forward_references
|
||||||
|
|
||||||
|
// #docregion manager_filter_forward_references
|
||||||
|
Future reverseRelationalFilter() async {
|
||||||
|
// Get the category that has a todo item with an id of 1
|
||||||
|
managers.todoCategory.filter((f) => f.todoItemsRefs((f) => f.id(1)));
|
||||||
|
|
||||||
|
// These can be combined with other filters
|
||||||
|
// For example, get all categories with a description of "School" or a todo item with an id of 1
|
||||||
|
managers.todoCategory.filter(
|
||||||
|
(f) => f.description("School") | f.todoItemsRefs((f) => f.id(1)),
|
||||||
|
);
|
||||||
|
}
|
||||||
|
// #enddocregion manager_filter_forward_references
|
||||||
}
|
}
|
||||||
|
|
||||||
// #docregion manager_filter_extensions
|
// #docregion manager_filter_extensions
|
||||||
|
@ -153,14 +174,10 @@ extension After2000Filter on ColumnFilters<DateTime> {
|
||||||
|
|
||||||
Future filterWithExtension(AppDatabase db) async {
|
Future filterWithExtension(AppDatabase db) async {
|
||||||
// Use the custom filters on any column that is of type DateTime
|
// Use the custom filters on any column that is of type DateTime
|
||||||
await db.managers.todoItems
|
db.managers.todoItems.filter((f) => f.createdAt.after2000orBefore1900());
|
||||||
.filter((f) => f.createdAt.after2000orBefore1900())
|
|
||||||
.get();
|
|
||||||
|
|
||||||
// Use the custom filter on the `unixepoch` column
|
// Use the custom filter on the `unixepoch` column
|
||||||
await db.managers.todoItems
|
db.managers.todoItems.filter((f) => f.createdAt.filterOnUnixEpoch(0));
|
||||||
.filter((f) => f.createdAt.filterOnUnixEpoch(0))
|
|
||||||
.get();
|
|
||||||
}
|
}
|
||||||
// #enddocregion manager_filter_extensions
|
// #enddocregion manager_filter_extensions
|
||||||
|
|
||||||
|
@ -172,7 +189,7 @@ extension After2000Ordering on ColumnOrderings<DateTime> {
|
||||||
|
|
||||||
Future orderingWithExtension(AppDatabase db) async {
|
Future orderingWithExtension(AppDatabase db) async {
|
||||||
// Use the custom orderings on any column that is of type DateTime
|
// Use the custom orderings on any column that is of type DateTime
|
||||||
await db.managers.todoItems.orderBy((f) => f.createdAt.byUnixEpoch()).get();
|
db.managers.todoItems.orderBy((f) => f.createdAt.byUnixEpoch());
|
||||||
}
|
}
|
||||||
// #enddocregion manager_ordering_extensions
|
// #enddocregion manager_ordering_extensions
|
||||||
|
|
||||||
|
@ -185,7 +202,7 @@ extension NoContentOrBefore2000FilterX on $$TodoItemsTableFilterComposer {
|
||||||
|
|
||||||
Future customFilter(AppDatabase db) async {
|
Future customFilter(AppDatabase db) async {
|
||||||
// Use the custom filter on the `TodoItems` table
|
// Use the custom filter on the `TodoItems` table
|
||||||
await db.managers.todoItems.filter((f) => f.noContentOrBefore2000()).get();
|
db.managers.todoItems.filter((f) => f.noContentOrBefore2000());
|
||||||
}
|
}
|
||||||
// #enddocregion manager_custom_filter
|
// #enddocregion manager_custom_filter
|
||||||
|
|
||||||
|
@ -197,6 +214,20 @@ extension ContentThenCreationDataX on $$TodoItemsTableOrderingComposer {
|
||||||
|
|
||||||
Future customOrdering(AppDatabase db) async {
|
Future customOrdering(AppDatabase db) async {
|
||||||
// Use the custom ordering on the `TodoItems` table
|
// Use the custom ordering on the `TodoItems` table
|
||||||
await db.managers.todoItems.orderBy((f) => f.contentThenCreatedAt()).get();
|
db.managers.todoItems.orderBy((f) => f.contentThenCreatedAt());
|
||||||
}
|
}
|
||||||
// #enddocregion manager_custom_ordering
|
// #enddocregion manager_custom_ordering
|
||||||
|
|
||||||
|
// #docregion reference_name_example
|
||||||
|
class User extends Table {
|
||||||
|
IntColumn get id => integer().autoIncrement()();
|
||||||
|
TextColumn get name => text()();
|
||||||
|
@ReferenceName("users")
|
||||||
|
IntColumn get group => integer().nullable().references(Group, #id)();
|
||||||
|
}
|
||||||
|
|
||||||
|
class Group extends Table {
|
||||||
|
IntColumn get id => integer().autoIncrement()();
|
||||||
|
TextColumn get name => text()();
|
||||||
|
}
|
||||||
|
// #enddocregion reference_name_example
|
|
@ -36,6 +36,18 @@ Type specific filters for `int`, `double`, `Int64`, `DateTime` and `String` are
|
||||||
|
|
||||||
|
|
||||||
### Filtering across tables
|
### Filtering across tables
|
||||||
|
You can filter across references to other tables by using the generated reference filters. You can nest these as deep as you'd like and the manager will take care of adding the aliased joins behind the scenes.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_filter_forward_references' %}
|
||||||
|
|
||||||
|
You can also filter across back references. This is useful when you have a one-to-many relationship and want to filter the parent table based on the child table.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_filter_back_references' %}
|
||||||
|
|
||||||
|
The code generator will name this filterset using the name of the table that is being referenced. If there are multiple references to the same table, the code generator will print an error and wont generate the filterset.
|
||||||
|
Use the `@ReferenceName(...)` on the foreign key to specify a custom name for the filterset.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'reference_name_example' %}
|
||||||
|
|
||||||
### Ordering
|
### Ordering
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue