Repository examples

Preload async data in repository with RiverPod and dotenv

class Repository<T> {
  final FirebaseFirestore db;
  final String path;
  Repository({required this.db, required this.path}) {
    if (kReleaseMode) {
      load = dotenv.load(fileName: ".env.production").then((value) => env = Environment.fromENV(dotenv));
    } else {
      load = dotenv.load(fileName: ".env.development").then((value) => env = Environment.fromENV(dotenv));
    }
  }

  late Future load;
  late Environment env;

  Future<void> put(String id, T item) async {
    log('Repository > put');

    await load;

    try {
      final fullPath = path.replaceAll('accountId}', env.accountId).replaceAll('id}', id);

      var options = SetOptions(merge: false);
      await db.doc(fullPath).set((item as dynamic).toJson(), options);
    } catch (e) {
      throw CustomServiceException(e.toString(), service: 'Repository', method: 'put');
    }
    return;
  }

  Future<void> update(String id, Map<String, dynamic> item) async {
    return;
  }

  Future<T?> getById(String id) async {
    return null;
  }

  Future<T?> getAllByOwnerUID(String uid) async {
    return null;
  }

  Future<void> removeById(String id) async {
    return;
  }

  Future<void> removeByUID(String uid) async {
    return;
  }
}