diff --git a/lib/data/objectbox/objectbox_connections_repo.dart b/lib/data/objectbox/objectbox_connections_repo.dart index d7bae31..734918c 100644 --- a/lib/data/objectbox/objectbox_connections_repo.dart +++ b/lib/data/objectbox/objectbox_connections_repo.dart @@ -5,10 +5,12 @@ import '../../models/connection.dart'; import '../../models/exec_error.dart'; import '../../objectbox.g.dart'; import '../interfaces/connections_repo_intf.dart'; +import '../memory/memory_connections_repo.dart'; import 'objectbox_cache.dart'; class ObjectBoxConnectionsRepo implements IConnectionsRepo { late final Box box; + final memCache = MemoryConnectionsRepo(); ObjectBoxConnectionsRepo() { box = getIt().store.box(); @@ -16,18 +18,25 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo { @override bool addAllConnections(Iterable newConnections) { + memCache.addAllConnections(newConnections); final result = box.putMany(newConnections.toList()); return result.length == newConnections.length; } @override bool addConnection(Connection connection) { + memCache.addConnection(connection); box.putAsync(connection); return true; } @override Result getById(String id) { + final result = memCache.getById(id); + if (result.isSuccess) { + return result; + } + return _getConnection( Connection_.id.equals(id), ).mapError( @@ -39,6 +48,10 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo { @override Result getByName(String name) { + final result = memCache.getByName(name); + if (result.isSuccess) { + return result; + } return _getConnection( Connection_.name.equals(name), ).mapError( @@ -74,16 +87,18 @@ class ObjectBoxConnectionsRepo implements IConnectionsRepo { @override bool updateConnection(Connection connection) { + memCache.updateConnection(connection); box.put(connection); return true; } Result _getConnection(Condition query) { - final result = box.query(query).build().findFirst(); - if (result == null) { + final connection = box.query(query).build().findFirst(); + if (connection == null) { return buildErrorResult(type: ErrorType.notFound); } - return Result.ok(result); + memCache.addConnection(connection); + return Result.ok(connection); } }