Skip to content

Commit

Permalink
docs: minimise the copy/think/paste needed to get the minimal impleme… (
Browse files Browse the repository at this point in the history
#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>
  • Loading branch information
ewann and Ewan Nisbet committed Apr 14, 2024
1 parent c6f0fa2 commit ca0dee4
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 19 deletions.
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

0 comments on commit ca0dee4

Please sign in to comment.