mirror of https://github.com/AMT-Cheif/drift.git
add docs for CRUD
This commit is contained in:
parent
4c4122f52c
commit
2ea3d3d2bc
|
@ -1,9 +1,64 @@
|
||||||
|
import 'package:drift/drift.dart';
|
||||||
|
|
||||||
import '../setup/database.dart';
|
import '../setup/database.dart';
|
||||||
|
|
||||||
extension ManagerExamples on AppDatabase {
|
extension ManagerExamples on AppDatabase {
|
||||||
// #docregion create
|
// #docregion manager_create
|
||||||
Future<void> createTodoItem() async {
|
Future<void> createTodoItem() async {
|
||||||
await managers.todoItems.create((o) => o(title: '', content: ''));
|
// Create a new item
|
||||||
|
await managers.todoItems
|
||||||
|
.create((o) => o(title: 'Title', content: 'Content'));
|
||||||
|
|
||||||
|
// We can also use `mode` and `onConflict` parameters, just
|
||||||
|
// like in the `[InsertStatement.insert]` method on the table
|
||||||
|
await managers.todoItems.create(
|
||||||
|
(o) => o(title: 'Title', content: 'New Content'),
|
||||||
|
mode: InsertMode.replace);
|
||||||
|
|
||||||
|
// We can also create multiple items at once
|
||||||
|
await managers.todoItems.bulkCreate(
|
||||||
|
(o) => [
|
||||||
|
o(title: 'Title 1', content: 'Content 1'),
|
||||||
|
o(title: 'Title 2', content: 'Content 2'),
|
||||||
|
],
|
||||||
|
);
|
||||||
}
|
}
|
||||||
// #enddocregion create
|
// #enddocregion manager_create
|
||||||
|
|
||||||
|
// #docregion manager_update
|
||||||
|
Future<void> updateTodoItems() async {
|
||||||
|
// Update all items
|
||||||
|
await managers.todoItems.update((o) => o(content: Value('New Content')));
|
||||||
|
|
||||||
|
// Update multiple items
|
||||||
|
await managers.todoItems
|
||||||
|
.filter((f) => f.id.isIn([1, 2, 3]))
|
||||||
|
.update((o) => o(content: Value('New Content')));
|
||||||
|
}
|
||||||
|
// #docregion manager_update
|
||||||
|
|
||||||
|
// #docregion manager_replace
|
||||||
|
Future<void> replaceTodoItems() async {
|
||||||
|
// Replace a single item
|
||||||
|
var obj = await managers.todoItems.filter((o) => o.id(1)).getSingle();
|
||||||
|
obj = obj.copyWith(content: 'New Content');
|
||||||
|
await managers.todoItems.replace(obj);
|
||||||
|
|
||||||
|
// Replace multiple items
|
||||||
|
var objs =
|
||||||
|
await managers.todoItems.filter((o) => o.id.isIn([1, 2, 3])).get();
|
||||||
|
objs = objs.map((o) => o.copyWith(content: 'New Content')).toList();
|
||||||
|
await managers.todoItems.bulkReplace(objs);
|
||||||
|
}
|
||||||
|
// #docregion manager_replace
|
||||||
|
|
||||||
|
// #docregion manager_delete
|
||||||
|
Future<void> deleteTodoItems() async {
|
||||||
|
// Delete all items
|
||||||
|
await managers.todoItems.delete();
|
||||||
|
|
||||||
|
// Delete a single item
|
||||||
|
await managers.todoItems.filter((f) => f.id(5)).delete();
|
||||||
|
}
|
||||||
|
// #docregion manager_delete
|
||||||
}
|
}
|
||||||
|
|
|
@ -23,11 +23,27 @@ instructions.
|
||||||
|
|
||||||
### Filtering across tables
|
### Filtering across tables
|
||||||
|
|
||||||
|
### Ordering
|
||||||
|
|
||||||
## Updates
|
## Updates
|
||||||
|
We can use the manager to update rows in bulk or individual rows that meet a certain condition.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_update' %}
|
||||||
|
|
||||||
|
We can also replace an entire row with a new one. Or even replace multiple rows at once.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_replace' %}
|
||||||
|
|
||||||
## Creating rows
|
## Creating rows
|
||||||
|
The manager includes a method for quickly inserting rows into a table.
|
||||||
|
We can insert a single row or multiple rows at once.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_create' %}
|
||||||
|
|
||||||
{% include "blocks/snippet" snippets = snippets name = 'create' %}
|
|
||||||
|
|
||||||
## Deleting rows
|
## Deleting rows
|
||||||
|
We may also delete rows from a table using the manager.
|
||||||
|
Any rows that meet the specified condition will be deleted.
|
||||||
|
|
||||||
|
{% include "blocks/snippet" snippets = snippets name = 'manager_delete' %}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue