54 lines
2.1 KiB
Dart
54 lines
2.1 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:uto_manager/infrastructure/database/app_database.dart';
|
|
|
|
part 'unit_dao.g.dart';
|
|
|
|
/// Data Access Object for [Units] table operations.
|
|
@DriftAccessor(tables: [Units])
|
|
class UnitDao extends DatabaseAccessor<AppDatabase> with _$UnitDaoMixin {
|
|
UnitDao(super.db);
|
|
|
|
/// Returns all unit rows.
|
|
Future<List<Unit>> getAllUnits() => select(units).get();
|
|
|
|
/// Returns the unit with [unitId], or null if not found.
|
|
Future<Unit?> getUnitById(String unitId) =>
|
|
(select(units)..where((t) => t.unitId.equals(unitId))).getSingleOrNull();
|
|
|
|
/// Returns all direct children of [parentUnitId].
|
|
Future<List<Unit>> getChildren(String parentUnitId) =>
|
|
(select(units)..where((t) => t.parentUnitId.equals(parentUnitId))).get();
|
|
|
|
/// Returns all root units (no parent).
|
|
Future<List<Unit>> getRoots() => (select(units)..where((t) => t.parentUnitId.isNull())).get();
|
|
|
|
/// Returns all units at the given [echelon] string value.
|
|
Future<List<Unit>> getByEchelon(String echelon) =>
|
|
(select(units)..where((t) => t.echelon.equals(echelon))).get();
|
|
|
|
/// Inserts a new unit row. Throws on duplicate primary key.
|
|
Future<void> insertUnit(UnitsCompanion entry) =>
|
|
into(units).insert(entry, mode: InsertMode.insertOrFail);
|
|
|
|
/// Inserts multiple unit rows in a single transaction.
|
|
Future<void> insertAllUnits(List<UnitsCompanion> entries) =>
|
|
batch((b) => b.insertAll(units, entries, mode: InsertMode.insertOrFail));
|
|
|
|
/// Updates an existing unit row.
|
|
Future<bool> updateUnit(UnitsCompanion entry) => update(units).replace(entry);
|
|
|
|
/// Deletes the unit with [unitId].
|
|
Future<int> deleteUnit(String unitId) =>
|
|
(delete(units)..where((t) => t.unitId.equals(unitId))).go();
|
|
|
|
/// Deletes all units.
|
|
Future<int> deleteAllUnits() => delete(units).go();
|
|
|
|
/// Returns a stream that emits all units whenever the table changes.
|
|
Stream<List<Unit>> watchAllUnits() => select(units).watch();
|
|
|
|
/// Returns a stream that emits children of [parentUnitId] on changes.
|
|
Stream<List<Unit>> watchChildren(String parentUnitId) =>
|
|
(select(units)..where((t) => t.parentUnitId.equals(parentUnitId))).watch();
|
|
}
|