feat: add Drift dependencies and basic database setup
This commit is contained in:
@@ -0,0 +1,49 @@
|
||||
import 'dart:io';
|
||||
|
||||
import 'package:drift/drift.dart';
|
||||
import 'package:drift/native.dart';
|
||||
import 'package:path_provider/path_provider.dart';
|
||||
import 'package:path/path.dart' as p;
|
||||
|
||||
// Эта строка указывает Drift сгенерировать файл database.g.dart
|
||||
part 'database.g.dart';
|
||||
|
||||
// Определение таблицы Transactions
|
||||
// Имя таблицы в SQL будет 'transactions' (snake_case от имени класса)
|
||||
class Transactions extends Table {
|
||||
IntColumn get id => integer().autoIncrement()(); // Primary key
|
||||
TextColumn get categoryName => text().named('category_name')(); // Имя категории
|
||||
RealColumn get amount => real()(); // Сумма транзакции
|
||||
DateTimeColumn get date => dateTime()(); // Дата транзакции
|
||||
TextColumn get merchant => text()(); // Название продавца/магазина
|
||||
}
|
||||
|
||||
// Класс базы данных
|
||||
// Аннотация @DriftDatabase указывает Drift сгенерировать код для этой базы данных
|
||||
@DriftDatabase(tables: [Transactions])
|
||||
class AppDatabase extends _$AppDatabase {
|
||||
AppDatabase() : super(_openConnection());
|
||||
|
||||
// Версия схемы. Увеличивайте при изменении структуры таблиц.
|
||||
@override
|
||||
int get schemaVersion => 1;
|
||||
|
||||
// Методы для взаимодействия с таблицей Transactions будут добавлены здесь
|
||||
// Например:
|
||||
// Future<List<Transaction>> getAllTransactions() => select(transactions).get();
|
||||
// Stream<List<Transaction>> watchAllTransactions() => select(transactions).watch();
|
||||
// Future<int> addTransaction(TransactionsCompanion entry) => into(transactions).insert(entry);
|
||||
}
|
||||
|
||||
// Функция для открытия соединения с базой данных
|
||||
LazyDatabase _openConnection() {
|
||||
// Вычисление пути к файлу базы данных
|
||||
return LazyDatabase(() async {
|
||||
final dbFolder = await getApplicationDocumentsDirectory();
|
||||
final file = File(p.join(dbFolder.path, 'db.sqlite'));
|
||||
|
||||
// При необходимости можно включить логирование SQL-запросов
|
||||
// return NativeDatabase(file, logStatements: true);
|
||||
return NativeDatabase(file);
|
||||
});
|
||||
}
|
||||
@@ -32,6 +32,10 @@ dependencies:
|
||||
sdk: flutter
|
||||
fl_chart: ^0.71.0
|
||||
intl: ^0.19.0 # For date formatting
|
||||
drift: ^2.18.0 # Added Drift
|
||||
sqlite3_flutter_libs: ^0.5.22 # Recommended for Drift on Flutter
|
||||
path_provider: ^2.1.3 # To find database file location
|
||||
path: ^1.9.0 # To construct database file path
|
||||
|
||||
# The following adds the Cupertino Icons font to your application.
|
||||
# Use with the CupertinoIcons class for iOS style icons.
|
||||
@@ -40,6 +44,8 @@ dependencies:
|
||||
dev_dependencies:
|
||||
flutter_test:
|
||||
sdk: flutter
|
||||
drift_dev: ^2.18.0 # Added Drift code generator
|
||||
build_runner: ^2.4.11 # Added build_runner
|
||||
|
||||
# The "flutter_lints" package below contains a set of recommended lints to
|
||||
# encourage good coding practices. The lint set provided by the package is
|
||||
|
||||
Reference in New Issue
Block a user