uto_manager/lib/infrastructure/database/daos/template_dao.dart

43 lines
1.7 KiB
Dart

import 'package:drift/drift.dart';
import 'package:uto_manager/infrastructure/database/app_database.dart';
part 'template_dao.g.dart';
/// Data Access Object for [Templates] table operations.
@DriftAccessor(tables: [Templates])
class TemplateDao extends DatabaseAccessor<AppDatabase> with _$TemplateDaoMixin {
TemplateDao(super.db);
/// Returns all template rows.
Future<List<Template>> getAllTemplates() => select(templates).get();
/// Returns the template with [templateId], or null if not found.
Future<Template?> getTemplateById(String templateId) =>
(select(templates)..where((t) => t.templateId.equals(templateId))).getSingleOrNull();
/// Returns all templates of the given [bctType].
Future<List<Template>> getByBctType(String bctType) =>
(select(templates)..where((t) => t.bctType.equals(bctType))).get();
/// Inserts a new template row.
Future<void> insertTemplate(TemplatesCompanion entry) =>
into(templates).insert(entry, mode: InsertMode.insertOrFail);
/// Inserts or replaces a template row (upsert).
Future<void> upsertTemplate(TemplatesCompanion entry) =>
into(templates).insertOnConflictUpdate(entry);
/// Updates an existing template row.
Future<bool> updateTemplate(TemplatesCompanion entry) => update(templates).replace(entry);
/// Deletes the template with [templateId].
Future<int> deleteTemplate(String templateId) =>
(delete(templates)..where((t) => t.templateId.equals(templateId))).go();
/// Deletes all templates.
Future<int> deleteAllTemplates() => delete(templates).go();
/// Returns a stream that emits all templates whenever the table changes.
Stream<List<Template>> watchAllTemplates() => select(templates).watch();
}