88 lines
2.9 KiB
Dart
88 lines
2.9 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:drift_flutter/drift_flutter.dart';
|
|
import 'tables.dart';
|
|
|
|
part 'db.g.dart';
|
|
|
|
@DriftDatabase(
|
|
tables: [
|
|
Accounts,
|
|
CategoryGroups,
|
|
Categories,
|
|
Transactions,
|
|
Budgets,
|
|
],
|
|
)
|
|
class AppDatabase extends _$AppDatabase {
|
|
AppDatabase([QueryExecutor? e]) : super(e ?? _openConnection());
|
|
|
|
static QueryExecutor _openConnection() {
|
|
return driftDatabase(name: 'ynab_lite');
|
|
}
|
|
|
|
@override
|
|
int get schemaVersion => 1;
|
|
|
|
// ── Seed ──────────────────────────────────────────────────────────────────
|
|
Future<void> seedIfEmpty() async {
|
|
final count = await select(accounts).get().then((r) => r.length);
|
|
if (count > 0) return;
|
|
|
|
// default category groups
|
|
final groups = [
|
|
CategoryGroupsCompanion.insert(name: 'Logement & Charges Fixes', sortOrder: const Value(1)),
|
|
CategoryGroupsCompanion.insert(name: 'Alimentation', sortOrder: const Value(2)),
|
|
CategoryGroupsCompanion.insert(name: 'Transport', sortOrder: const Value(3)),
|
|
CategoryGroupsCompanion.insert(name: 'Loisirs & Divertissements', sortOrder: const Value(4)),
|
|
CategoryGroupsCompanion.insert(name: 'Épargne & Objectifs', sortOrder: const Value(5)),
|
|
];
|
|
for (final g in groups) {
|
|
await into(categoryGroups).insert(g);
|
|
}
|
|
|
|
// categories
|
|
final cats = [
|
|
(group: 1, name: 'Loyer', icon: 'home'),
|
|
(group: 1, name: 'Électricité', icon: 'bolt'),
|
|
(group: 1, name: 'Eau', icon: 'water_drop'),
|
|
(group: 1, name: 'Assurance habitation', icon: 'shield'),
|
|
(group: 2, name: 'Courses', icon: 'shopping_cart'),
|
|
(group: 2, name: 'Restaurants', icon: 'restaurant'),
|
|
(group: 3, name: 'Essence', icon: 'local_gas_station'),
|
|
(group: 3, name: 'Transports en commun', icon: 'directions_bus'),
|
|
(group: 4, name: 'Streaming', icon: 'tv'),
|
|
(group: 4, name: 'Sorties', icon: 'celebration'),
|
|
(group: 5, name: 'Épargne d\'urgence', icon: 'savings'),
|
|
(group: 5, name: 'Vacances', icon: 'flight'),
|
|
];
|
|
for (int i = 0; i < cats.length; i++) {
|
|
await into(categories).insert(CategoriesCompanion.insert(
|
|
name: cats[i].name,
|
|
groupId: cats[i].group,
|
|
icon: Value(cats[i].icon),
|
|
sortOrder: Value(i + 1),
|
|
));
|
|
}
|
|
|
|
// default account
|
|
await into(accounts).insert(AccountsCompanion.insert(
|
|
name: 'Compte courant',
|
|
type: 'checking',
|
|
balance: const Value(2500.0),
|
|
sortOrder: const Value(1),
|
|
));
|
|
await into(accounts).insert(AccountsCompanion.insert(
|
|
name: 'Livret A',
|
|
type: 'savings',
|
|
balance: const Value(5000.0),
|
|
sortOrder: const Value(2),
|
|
));
|
|
await into(accounts).insert(AccountsCompanion.insert(
|
|
name: 'Espèces',
|
|
type: 'cash',
|
|
balance: const Value(120.0),
|
|
sortOrder: const Value(3),
|
|
));
|
|
}
|
|
}
|