mirrored_repositories/test/chapter-07/todo/lib/repositories
import 'package:isar/isar.dart';
import 'package:path_provider/path_provider.dart';
import 'package:todo/repositories/isar/task_model.dart';

class IsarDatasource {
  Isar? _isar;

  Future<Isar> get instance async {
    if (_isar != null) {
      return _isar!;
    }

    final dir = await getApplicationDocumentsDirectory();
    _isar = await Isar.open(
      [TaskModelSchema],
      directory: dir.path,
    );

    return _isar!;
  }

  Future<List<TaskModel>> getTasks() async {
    final isar = await instance;
    return isar.taskModels.where().findAll();
  }

  Future<void> deleteAllTasks() async {
    final isar = await instance;
    await isar.writeTxn(() {
      return isar.taskModels.where().deleteAll();
    });
  }

  Future<void> putAllTasks(List<TaskModel> models) async {
    final isar = await instance;
    await isar.writeTxn(() {
      return isar.taskModels.putAll(models);
    });
  }
}
