Aidra Driver 1.3.5+68
Aidra Driver - Your path to green energy
Loading...
Searching...
No Matches
isar_local_database.dart
Go to the documentation of this file.
1import 'package:isar/isar.dart';
2import 'package:path_provider/path_provider.dart';
3
6 static Isar? _isar;
7
9
12 return _instance!;
13 }
14
15 Future<void> initialize(List<CollectionSchema<dynamic>> schemas) async {
16 if (_isar != null) return;
17
18 final dir = await getApplicationDocumentsDirectory();
19 _isar = await Isar.open(
20 schemas,
21 directory: dir.path,
22 );
23 }
24
25 Isar get isar {
26 if (_isar == null) {
27 throw Exception('Isar not initialized. Call initialize() first.');
28 }
29 return _isar!;
30 }
31
32 IsarCollection<T> collection<T>() {
33 return isar.collection<T>();
34 }
35
36 Future<Id> put<T>(T object) async {
37 late Id id;
38 await isar.writeTxn(() async {
39 id = await collection<T>().put(object);
40 });
41 return id;
42 }
43
44 Future<List<Id>> putAll<T>(List<T> objects) async {
45 late List<Id> ids;
46 await isar.writeTxn(() async {
47 ids = await collection<T>().putAll(objects);
48 });
49 return ids;
50 }
51
52 Future<T?> get<T>(Id id) async {
53 return await collection<T>().get(id);
54 }
55
56 Future<List<T>> getAll<T>() async {
57 return await collection<T>().where().findAll();
58 }
59
60 Future<bool> delete<T>(Id id) async {
61 late bool success;
62 await isar.writeTxn(() async {
63 success = await collection<T>().delete(id);
64 });
65 return success;
66 }
67
68 Future<int> deleteAll<T>(List<Id> ids) async {
69 late int count;
70 await isar.writeTxn(() async {
71 count = await collection<T>().deleteAll(ids);
72 });
73 return count;
74 }
75
76 Future<int> count<T>() async {
77 return await collection<T>().count();
78 }
79
80 Future<void> clear<T>() async {
81 await isar.writeTxn(() async {
82 await collection<T>().clear();
83 });
84 }
85
86 Stream<void> watch<T>({bool fireImmediately = false}) {
87 return collection<T>().watchLazy(fireImmediately: fireImmediately);
88 }
89
90 Future<void> close() async {
91 await _isar?.close();
92 _isar = null;
93 _instance = null;
94 }
95}
Future< int > count() async
static IsarLocalDatabase _instance
Future< bool > delete(Id id) async
Future< List< Id > > putAll(List< T > objects) async
Future< List< T > > getAll() async
IsarCollection< T > collection()
IsarLocalDatabase _()
static IsarLocalDatabase get instance
Stream< void > watch({bool fireImmediately=false})
Future< int > deleteAll(List< Id > ids) async
Future< Id > put(T object) async
Future< void > close() async
Future< void > clear() async
Future< void > initialize(List< CollectionSchema< dynamic > > schemas) async
Future< T?> get(Id id) async