Line data Source code
1 : import 'dart:async'; 2 : 3 : import 'package:event_bloc/event_bloc.dart'; 4 : import 'package:event_db/src/model.dart'; 5 : 6 : /// Represents a class that implements some specific implementation of an 7 : /// interface with storage. Classes that implement this must retain data 8 : /// that is saved using the various functions such as [saveModel] and be 9 : /// retrieved using [findModel] in a separate session. 10 : abstract class DatabaseRepository extends Repository { 11 : /// This functions finds all models of the given type that have their 12 : /// [GenericModel.id] start with the prefix given by the [supplier]s 13 : /// [GenericModel.prefixTypeForId] 14 : FutureOr<Iterable<T>> findAllModelsOfType<T extends GenericModel>( 15 : String database, 16 : T Function() supplier, 17 : ); 18 : 19 : /// Finds the [T] model in [database] with id of [key]. 20 : FutureOr<T?> findModel<T extends GenericModel>(String database, String key); 21 : 22 : /// Deletes the [T] [model] in [database]. [model] doesn not need to be 23 : /// exactly the same, just have the same id. 24 : FutureOr<bool> deleteModel<T extends GenericModel>(String database, T model); 25 : 26 : /// Saves the [T] [model] in [database]. [model]s that do not have an existing 27 : /// id will have one automatically assigned to them. This function doesn't 28 : /// care if there is an already existing model or not. Existing models will 29 : /// just be overridden 30 : FutureOr<T> saveModel<T extends GenericModel>(String database, T model); 31 : 32 : /// Finds all the models in the given [database] that have the given [keys] 33 : /// 34 : /// The default implementation simply calls [findModel] multiple times. 35 : /// A concrete implementation with a more efficient way of doing this should 36 : /// override this function. 37 2 : FutureOr<Iterable<T>> findModels<T extends GenericModel>( 38 : String database, 39 : Iterable<String> keys, 40 : ) async => 41 8 : (await Future.wait(keys.map((e) async => findModel<T>(database, e)))) 42 4 : .where((element) => element != null) 43 4 : .map((e) => e!) 44 2 : .toList(); 45 : 46 : /// Finds all models in [database] that have values that match the passed 47 : /// [model]'s values 48 : /// 49 : /// Only values that are mapped to keys in [fields] will be considered. 50 : /// 51 : /// The default implementation is to call [findAllModelsOfType]s and then 52 : /// perform a comparison individiually. 53 : /// 54 : /// A concrete implementation with a more efficient way of doing this should 55 : /// override this function. 56 1 : FutureOr<Iterable<T>> searchByModelAndFields<T extends GenericModel>( 57 : String database, 58 : T Function() supplier, 59 : T model, 60 : List<String> fields, 61 : ) async { 62 1 : final values = await findAllModelsOfType(database, supplier); 63 1 : return values.where( 64 2 : (element) => model.hasSameFields( 65 : model: element, 66 : onlyFields: fields, 67 : ), 68 : ); 69 : } 70 : } 71 : 72 : /// Wraps a [DatabaseRepository] with all of the databaseFunctions having a 73 : /// given [databaseName] automatically passed as the database argument. 74 : class SpecificDatabase { 75 : /// [database] has all of the underlying database implementation for all of 76 : /// the database functions of this object. 77 : /// 78 : /// [databaseName] is the value passed as the database in all of the database 79 : /// functions of this object. 80 2 : SpecificDatabase(this.database, this.databaseName); 81 : 82 : /// The underlying database implementation for all of the functions of this 83 : /// object. 84 : final DatabaseRepository database; 85 : 86 : /// The value passed as the database in all of the database functions of this 87 : /// object. 88 : final String databaseName; 89 : 90 : /// This functions finds all models of the given type that have their 91 : /// [GenericModel.id] start with the prefix given by the [supplier]s 92 : /// [GenericModel.prefixTypeForId] 93 2 : FutureOr<Iterable<T>> findAllModelsOfType<T extends GenericModel>( 94 : T Function() supplier, 95 : ) => 96 6 : database.findAllModelsOfType(databaseName, supplier); 97 : 98 : /// Finds the [T] model in [databaseName] with id of [key]. 99 1 : FutureOr<T?> findModel<T extends GenericModel>(String key) => 100 3 : database.findModel(databaseName, key); 101 : 102 : /// Deletes the [T] [model] in [databaseName]. [model] doesn not need to be 103 : /// exactly the same, just have the same id. 104 1 : FutureOr<bool> deleteModel<T extends GenericModel>(T model) => 105 3 : database.deleteModel(databaseName, model); 106 : 107 : /// Saves the [T] [model] in [databaseName]. [model]s that do not have an 108 : /// existing id will have one automatically assigned to them. This function 109 : /// doesn't care if there is an already existing model or not. Existing 110 : /// models will just be overridden 111 2 : FutureOr<T> saveModel<T extends GenericModel>(T model) => 112 6 : database.saveModel(databaseName, model); 113 : 114 : /// Finds all the models in [databaseName] that have the given [keys] 115 1 : FutureOr<Iterable<T>> findModels<T extends GenericModel>( 116 : Iterable<String> keys, 117 : ) => 118 3 : database.findModels<T>(databaseName, keys); 119 : }