Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: minimise the copy/think/paste needed to get the minimal impleme… #2951

Merged
merged 2 commits into from
Apr 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 9 additions & 14 deletions docs/lib/snippets/setup/database.dart
Original file line number Diff line number Diff line change
@@ -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';

Expand All @@ -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.
Expand All @@ -70,7 +65,7 @@ LazyDatabase _openConnection() {
return NativeDatabase.createInBackground(file);
});
}
// #enddocregion open
// #enddocregion after_generation

class WidgetsFlutterBinding {
static void ensureInitialized() {}
Expand Down
11 changes: 6 additions & 5 deletions docs/pages/docs/setup.md
Original file line number Diff line number Diff line change
Expand Up @@ -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' %}

Expand All @@ -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
Expand Down
Loading