mirror of https://github.com/AMT-Cheif/drift.git
docs: minimise the copy/think/paste needed to get the minimal impleme… (#2951)
* docs: minimise the copy/think/paste needed to get the minimal implementation working * fix: revert tag name & add closing --------- Co-authored-by: Ewan Nisbet <ewan-git@thatdogmachine.com>
This commit is contained in:
parent
c6f0fa27aa
commit
ca0dee4f83
|
@ -1,21 +1,19 @@
|
|||
// #docregion after_generation
|
||||
// #docregion before_generation
|
||||
import 'package:drift/drift.dart';
|
||||
|
||||
// #enddocregion before_generation
|
||||
// #enddocregion after_generation
|
||||
|
||||
// #docregion open
|
||||
// These imports are necessary to open the sqlite3 database
|
||||
// #docregion after_generation
|
||||
// These additional imports are necessary to open the sqlite3 database
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
import 'package:sqlite3/sqlite3.dart';
|
||||
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
|
||||
|
||||
// ... the TodoItems table definition stays the same
|
||||
// #enddocregion open
|
||||
|
||||
// #docregion before_generation
|
||||
part 'database.g.dart';
|
||||
|
||||
|
@ -27,25 +25,22 @@ class TodoItems extends Table {
|
|||
IntColumn get category => integer().nullable()();
|
||||
}
|
||||
// #enddocregion table
|
||||
// #docregion open
|
||||
|
||||
@DriftDatabase(tables: [TodoItems])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
// #enddocregion open
|
||||
// #enddocregion before_generation
|
||||
// #enddocregion after_generation
|
||||
// After generating code, this class needs to define a `schemaVersion` getter
|
||||
// and a constructor telling drift where the database should be stored.
|
||||
// These are described in the getting started guide: https://drift.simonbinder.eu/getting-started/#open
|
||||
// #enddocregion before_generation
|
||||
// #docregion open
|
||||
// #docregion after_generation
|
||||
AppDatabase() : super(_openConnection());
|
||||
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
// #docregion before_generation
|
||||
}
|
||||
// #enddocregion before_generation, open
|
||||
|
||||
// #docregion open
|
||||
// #enddocregion before_generation
|
||||
|
||||
LazyDatabase _openConnection() {
|
||||
// the LazyDatabase util lets us find the right location for the file async.
|
||||
|
@ -70,7 +65,7 @@ LazyDatabase _openConnection() {
|
|||
return NativeDatabase.createInBackground(file);
|
||||
});
|
||||
}
|
||||
// #enddocregion open
|
||||
// #enddocregion after_generation
|
||||
|
||||
class WidgetsFlutterBinding {
|
||||
static void ensureInitialized() {}
|
||||
|
|
|
@ -81,7 +81,7 @@ to store todo items for a todo list app.
|
|||
Everything there is to know about defining tables in Dart is described on the [Dart tables]({{'Dart API/tables.md' | pageUrl}}) page.
|
||||
If you prefer using SQL to define your tables, drift supports that too! You can read all about the [SQL API]({{ 'SQL API/index.md' | pageUrl }}) here.
|
||||
|
||||
For now, the contents of `database.dart` are:
|
||||
For now, populate the contents of `database.dart` with:
|
||||
|
||||
{% include "blocks/snippet" snippets = snippets name = 'before_generation' %}
|
||||
|
||||
|
@ -97,10 +97,11 @@ After running either command, the `database.g.dart` file containing the generate
|
|||
class will have been generated.
|
||||
You will now see errors related to missing overrides and a missing constructor. The constructor
|
||||
is responsible for telling drift how to open the database. The `schemaVersion` getter is relevant
|
||||
for migrations after changing the database, we can leave it at `1` for now. The database class
|
||||
now looks like this:
|
||||
<a name="open">
|
||||
{% include "blocks/snippet" snippets = snippets name = 'open' %}
|
||||
for migrations after changing the database, we can leave it at `1` for now. Update `database.dart`
|
||||
so it now looks like this:
|
||||
|
||||
<a name="open"></a>
|
||||
{% include "blocks/snippet" snippets = snippets name = 'after_generation' %}
|
||||
|
||||
The Android-specific workarounds are necessary because sqlite3 attempts to use `/tmp` to store
|
||||
private data on unix-like systems, which is forbidden on Android. We also use this opportunity
|
||||
|
|
Loading…
Reference in New Issue