drift/docs/lib/snippets/setup/database.dart

97 lines
3.2 KiB
Dart
Raw Normal View History

// #docregion after_generation
2023-09-13 03:01:23 -07:00
// #docregion before_generation
import 'package:drift/drift.dart';
// #enddocregion before_generation
// #enddocregion after_generation
2023-09-13 03:01:23 -07:00
// #docregion after_generation
// These additional imports are necessary to open the sqlite3 database
2023-09-13 03:01:23 -07:00
import 'dart:io';
import 'package:drift/native.dart';
import 'package:path_provider/path_provider.dart';
import 'package:path/path.dart' as p;
2023-10-24 12:14:57 -07:00
import 'package:sqlite3/sqlite3.dart';
import 'package:sqlite3_flutter_libs/sqlite3_flutter_libs.dart';
2023-09-13 03:01:23 -07:00
// #docregion before_generation
part 'database.g.dart';
2023-09-16 09:38:22 -07:00
// #docregion table
2023-09-13 03:01:23 -07:00
class TodoItems extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get title => text().withLength(min: 6, max: 32)();
TextColumn get content => text().named('body')();
2024-04-17 12:43:36 -07:00
IntColumn get category =>
integer().nullable().references(TodoCategory, #id)();
DateTimeColumn get createdAt => dateTime().nullable()();
2023-09-13 03:01:23 -07:00
}
2024-04-17 12:43:36 -07:00
class TodoCategory extends Table {
IntColumn get id => integer().autoIncrement()();
TextColumn get description => text()();
}
2023-09-16 09:38:22 -07:00
// #enddocregion table
2023-09-13 03:01:23 -07:00
2024-04-17 12:43:36 -07:00
@DriftDatabase(tables: [TodoItems, TodoCategory])
2023-09-13 03:01:23 -07:00
class AppDatabase extends _$AppDatabase {
// #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
// #docregion after_generation
2023-09-13 03:01:23 -07:00
AppDatabase() : super(_openConnection());
@override
int get schemaVersion => 1;
// #docregion before_generation
}
// #enddocregion before_generation
2023-09-13 03:01:23 -07:00
LazyDatabase _openConnection() {
// the LazyDatabase util lets us find the right location for the file async.
return LazyDatabase(() async {
// put the database file, called db.sqlite here, into the documents folder
// for your app.
final dbFolder = await getApplicationDocumentsDirectory();
final file = File(p.join(dbFolder.path, 'db.sqlite'));
2023-10-24 12:14:57 -07:00
// Also work around limitations on old Android versions
if (Platform.isAndroid) {
await applyWorkaroundToOpenSqlite3OnOldAndroidVersions();
}
// Make sqlite3 pick a more suitable location for temporary files - the
// one from the system may be inaccessible due to sandboxing.
final cachebase = (await getTemporaryDirectory()).path;
// We can't access /tmp on Android, which sqlite3 would try by default.
// Explicitly tell it about the correct temporary directory.
sqlite3.tempDirectory = cachebase;
2023-09-13 03:01:23 -07:00
return NativeDatabase.createInBackground(file);
});
}
// #enddocregion after_generation
2023-09-13 03:01:23 -07:00
class WidgetsFlutterBinding {
static void ensureInitialized() {}
}
// #docregion use
void main() async {
WidgetsFlutterBinding.ensureInitialized();
final database = AppDatabase();
await database.into(database.todoItems).insert(TodoItemsCompanion.insert(
title: 'todo: finish drift setup',
content: 'We can now write queries and define our own tables.',
));
List<TodoItem> allItems = await database.select(database.todoItems).get();
print('items in database: $allItems');
}
// #enddocregion use