dreampad/lib/app/db/db_service.dart
2023-11-28 10:45:09 +08:00

41 lines
916 B
Dart

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();
}
}