Pārlūkot izejas kodu

数据库修改,增加 sembast 数据库

James 7 mēneši atpakaļ
vecāks
revīzija
327de887e1

+ 2 - 1
lib/appfx.dart

@@ -5,7 +5,8 @@ export 'util/fileUtils.dart';
 export 'util/logger.dart';
 export 'util/riverpodUtils.dart';
 
-export 'store/dbMgrBase.dart';
+export 'store/objbxDbMgrBase.dart';
+export 'store/semDbMgrBase.dart';
 export 'store/sharedStore.dart';
 
 export 'theme/appTheme.dart';

+ 0 - 25
lib/store/appStoreBase.dart

@@ -1,25 +0,0 @@
-
-// import 'package:path/path.dart' as p;
-// import 'package:path_provider/path_provider.dart';
-import 'package:objectbox/objectbox.dart';
-
-
-class AppStoreBase {
-  /// The Store of this app.
-  late final Store store;
-
-  // AppStoreBase._create(this.store) {
-  //   // Add any additional setup code, e.g. build queries.
-  //
-  //   // new box instance
-  // }
-  //
-  // /// Create an instance of ObjectBox to use throughout the app.
-  // static Future<AppStoreBase> create() async {
-  //   final docsDir = await getApplicationDocumentsDirectory();
-  //   // Future<Store> openStore() {...} is defined in the generated objectbox.g.dart
-  //   final store = await openStore(directory: p.join(docsDir.path, "obx"));
-  //   return AppStoreBase._create(store);
-  // }
-
-}

+ 2 - 2
lib/store/dbMgrBase.dart → lib/store/objbxDbMgrBase.dart

@@ -2,14 +2,14 @@
 import 'package:objectbox/objectbox.dart';
 
 
-abstract class dbMgrBase<T> {
+abstract class ObjbxDbMgrBase<T> {
 
   /// The Store of this app.
   late final Store store;
 
   late final Box<T> objectBox;
 
-  dbMgrBase(this.store) {
+  ObjbxDbMgrBase(this.store) {
     objectBox = Box(store);
   }
 

+ 56 - 0
lib/store/semDbMgrBase.dart

@@ -0,0 +1,56 @@
+
+import 'package:sembast/sembast_io.dart';
+import 'dart:async';
+
+
+abstract class SemDbMgrBase<T> {
+
+  final Database database;
+  abstract final String repositoryName;
+
+  SemDbMgrBase(this.database);
+
+  // 获取存储
+  StoreRef<String, Map<String, dynamic>> get storeRef {
+    return StoreRef<String, Map<String, dynamic>>(repositoryName);
+  }
+
+  // 转换对象为Map
+  Map<String, dynamic> toMap(T item);
+
+  // 从Map创建对象
+  T fromMap(Map<String, dynamic> map);
+
+  // 添加或更新记录
+  Future<String> addOrUpdate(T item, {String? id}) async {
+    final itemMap = toMap(item);
+    final recordId = id ?? DateTime.now().millisecondsSinceEpoch.toString();
+
+    await storeRef.record(recordId).put(database, itemMap);
+    return recordId;
+  }
+
+  // 获取所有记录
+  Future<List<T>> getAll() async {
+    final records = await storeRef.find(database);
+    return records.map((record) => fromMap(record.value)).toList();
+  }
+
+  // 根据ID获取记录
+  Future<T?> getById(String id) async {
+    final record = await storeRef.record(id).get(database);
+    return record != null ? fromMap(record) : null;
+  }
+
+  // 删除记录
+  Future<void> delete(String id) async {
+    await storeRef.record(id).delete(database);
+  }
+
+  // 清空存储
+  Future<void> clear() async {
+    await storeRef.delete(database);
+  }
+}
+
+

+ 16 - 0
pubspec.lock

@@ -680,6 +680,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "0.28.0"
+  sembast:
+    dependency: "direct main"
+    description:
+      name: sembast
+      sha256: d3f0d0ba501a5f1fd7d6c8532ee01385977c8a069c334635dae390d059ae3d6d
+      url: "https://pub.dev"
+    source: hosted
+    version: "3.8.5"
   shared_preferences:
     dependency: "direct main"
     description:
@@ -821,6 +829,14 @@ packages:
       url: "https://pub.dev"
     source: hosted
     version: "1.4.1"
+  synchronized:
+    dependency: transitive
+    description:
+      name: synchronized
+      sha256: "0669c70faae6270521ee4f05bffd2919892d42d1276e6c495be80174b6bc0ef6"
+      url: "https://pub.dev"
+    source: hosted
+    version: "3.3.1"
   term_glyph:
     dependency: transitive
     description:

+ 1 - 0
pubspec.yaml

@@ -16,6 +16,7 @@ dependencies:
   riverpod_annotation: ^2.3.3
   objectbox: ^4.1.0
   objectbox_flutter_libs: any
+  sembast: ^3.8.5
   logger: ^2.0.2+1
   bot_toast: ^4.1.3
   path: ^1.8.3