dreampad/lib/app/db/db_service.dart

41 lines
916 B
Dart
Raw Permalink Normal View History

2023-11-28 10:44:58 +08:00
import 'package:flustars_flutter3/flustars_flutter3.dart';
import 'package:get/get.dart';
import 'package:sqflite/sqflite.dart';
import 'package:dreampad/app/shared/shared.dart';
class DbService extends GetxService {
late Database db;
Future<DbService> init() async {
db = await _getDatabase();
return this;
}
Future onConfigure(Database db) async {
await db.execute('PRAGMA foreign_keys = ON');
}
Future<Database> _getDatabase() async {
final String path = DirectoryUtil.getStoragePath(
category: Constant.dbDir,
fileName: 'dreampad.db',
)!;
return await openDatabase(
path,
onConfigure: onConfigure,
onCreate: (db, version) async {
var batch = db.batch();
createTable(batch);
await batch.commit();
},
version: 1,
);
}
void createTable(Batch batch) {}
Future close() async {
db.close();
}
}