mirror of https://github.com/AMT-Cheif/drift.git
65 lines
2.2 KiB
Dart
65 lines
2.2 KiB
Dart
// ignore_for_file: directives_ordering
|
|
|
|
// #docregion open
|
|
// These imports are only needed to open the database
|
|
import 'dart:io';
|
|
|
|
import 'package:drift/native.dart';
|
|
import 'package:path_provider/path_provider.dart';
|
|
import 'package:path/path.dart' as p;
|
|
|
|
// #enddocregion open
|
|
|
|
// #docregion overview
|
|
import 'package:drift/drift.dart';
|
|
|
|
// assuming that your file is called filename.dart. This will give an error at
|
|
// first, but it's needed for drift to know about the generated code
|
|
part 'filename.g.dart';
|
|
|
|
// this will generate a table called "todos" for us. The rows of that table will
|
|
// be represented by a class called "Todo".
|
|
class Todos extends Table {
|
|
IntColumn get id => integer().autoIncrement()();
|
|
TextColumn get title => text().withLength(min: 6, max: 32)();
|
|
TextColumn get content => text().named('body')();
|
|
IntColumn get category => integer().nullable()();
|
|
}
|
|
|
|
// This will make drift generate a class called "Category" to represent a row in
|
|
// this table. By default, "Categorie" would have been used because it only
|
|
//strips away the trailing "s" in the table name.
|
|
@DataClassName('Category')
|
|
class Categories extends Table {
|
|
IntColumn get id => integer().autoIncrement()();
|
|
TextColumn get description => text()();
|
|
}
|
|
|
|
// this annotation tells drift to prepare a database class that uses both of the
|
|
// tables we just defined. We'll see how to use that database class in a moment.
|
|
// #docregion open
|
|
@DriftDatabase(tables: [Todos, Categories])
|
|
class MyDatabase extends _$MyDatabase {
|
|
// #enddocregion overview
|
|
// we tell the database where to store the data with this constructor
|
|
MyDatabase() : super(_openConnection());
|
|
|
|
// you should bump this number whenever you change or add a table definition.
|
|
// Migrations are covered later in the documentation.
|
|
@override
|
|
int get schemaVersion => 1;
|
|
// #docregion overview
|
|
}
|
|
// #enddocregion overview
|
|
|
|
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'));
|
|
return NativeDatabase(file);
|
|
});
|
|
}
|