dbMgrBase.dart 1.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849
  1. import 'package:objectbox/objectbox.dart';
  2. abstract class dbMgrBase<T> {
  3. /// The Store of this app.
  4. late final Store store;
  5. late final Box<T> objectBox;
  6. dbMgrBase(this.store) {
  7. objectBox = Box(store);
  8. }
  9. //
  10. bool isEmpty() => objectBox.isEmpty();
  11. //
  12. List<T> getAll() => objectBox.getAll();
  13. T get(int id) => objectBox.get(id)!;
  14. T? getFirst() => objectBox.query().build().findFirst();
  15. Future<List<T>> getAllAsync() => objectBox.getAllAsync();
  16. Stream<List<T>> getAllStream(QueryProperty<T, dynamic> orderProperty, {int orderFlags = 0}) {
  17. // Query for all tasks, sorted by their date.
  18. // https://docs.objectbox.io/queries
  19. final queryBuilder = objectBox.query().order(orderProperty, flags: orderFlags);
  20. // Build and watch the query,
  21. // set triggerImmediately to emit the query immediately on listen.
  22. return queryBuilder
  23. .watch(triggerImmediately: true)
  24. // Map it to a list of tasks to be used by a StreamBuilder.
  25. .map((query) => query.find());
  26. }
  27. int put(T obj) => objectBox.put(obj);
  28. Future<int>putAsync(T obj) => objectBox.putAsync(obj);
  29. Future<List<int>>putManyAsync(List<T> objects,
  30. {PutMode mode = PutMode.put}) => objectBox.putManyAsync(objects, mode: mode);
  31. void remove(int id) => objectBox.removeAsync(id);
  32. }