42 lines
1.8 KiB
Dart
42 lines
1.8 KiB
Dart
import 'package:drift/drift.dart';
|
|
import 'package:uto_manager/infrastructure/database/app_database.dart';
|
|
|
|
part 'soldier_positions_dao.g.dart';
|
|
|
|
// The Drift-generated row type for the SoldierPositions table is also named
|
|
// `SoldierPosition` (same as our domain entity). We use a typedef here so the
|
|
// DAO can refer to the DB row type unambiguously.
|
|
typedef SoldierPositionRow = SoldierPosition;
|
|
|
|
/// Data Access Object for [SoldierPositions] table operations.
|
|
@DriftAccessor(tables: [SoldierPositions])
|
|
class SoldierPositionsDao extends DatabaseAccessor<AppDatabase> with _$SoldierPositionsDaoMixin {
|
|
SoldierPositionsDao(super.db);
|
|
|
|
/// Returns all soldier position rows.
|
|
Future<List<SoldierPositionRow>> getAll() => select(soldierPositions).get();
|
|
|
|
/// Returns the position with [positionId], or null if not found.
|
|
Future<SoldierPositionRow?> getById(String positionId) =>
|
|
(select(soldierPositions)..where((t) => t.positionId.equals(positionId))).getSingleOrNull();
|
|
|
|
/// Inserts a new position row (insert-or-replace).
|
|
Future<void> upsert(SoldierPositionsCompanion entry) =>
|
|
into(soldierPositions).insertOnConflictUpdate(entry);
|
|
|
|
/// Inserts multiple rows in a single transaction.
|
|
Future<void> upsertAll(List<SoldierPositionsCompanion> entries) =>
|
|
batch((b) => b.insertAllOnConflictUpdate(soldierPositions, entries));
|
|
|
|
/// Updates an existing position row.
|
|
Future<bool> updatePosition(SoldierPositionsCompanion entry) =>
|
|
update(soldierPositions).replace(entry);
|
|
|
|
/// Deletes the position with [positionId].
|
|
Future<int> deletePosition(String positionId) =>
|
|
(delete(soldierPositions)..where((t) => t.positionId.equals(positionId))).go();
|
|
|
|
/// Returns a stream that emits all positions whenever the table changes.
|
|
Stream<List<SoldierPositionRow>> watchAll() => select(soldierPositions).watch();
|
|
}
|