73 lines
1.7 KiB
Dart

import 'package:flutter_riverpod/flutter_riverpod.dart';
import 'package:isar/isar.dart';
/// Isar 数据库提供者
final isarProvider = Provider<Isar>((ref) {
throw UnimplementedError('Isar instance must be provided');
});
/// 原理图仓库提供者
final schematicRepositoryProvider = Provider<SchematicRepository>((ref) {
final isar = ref.watch(isarProvider);
return SchematicRepository(isar);
});
/// 元件仓库提供者
final componentRepositoryProvider = Provider<ComponentRepository>((ref) {
final isar = ref.watch(isarProvider);
return ComponentRepository(isar);
});
/// 原理图仓库
class SchematicRepository {
final Isar _isar;
SchematicRepository(this._isar);
Future<List<Schematic>> getAll() async {
return await _isar.schematics.where().findAll();
}
Future<Schematic?> getById(int id) async {
return await _isar.schematics.get(id);
}
Future<void> save(Schematic schematic) async {
await _isar.writeTxn(() async {
await _isar.schematics.put(schematic);
});
}
Future<void> delete(int id) async {
await _isar.writeTxn(() async {
await _isar.schematics.delete(id);
});
}
}
/// 元件仓库
class ComponentRepository {
final Isar _isar;
ComponentRepository(this._isar);
Future<List<Component>> getBySchematic(int schematicId) async {
return await _isar.components
.filter()
.schematicIdEqualTo(schematicId)
.findAll();
}
Future<void> save(Component component) async {
await _isar.writeTxn(() async {
await _isar.components.put(component);
});
}
Future<void> delete(int id) async {
await _isar.writeTxn(() async {
await _isar.components.delete(id);
});
}
}