27 lines
826 B
Dart
27 lines
826 B
Dart
import 'package:drift/drift.dart';
|
|||
|
|
import '../app_database.dart';
|
||
|
|
import '../tables.dart';
|
||
|
|
|
||
|
|
part 'insight_dao.g.dart';
|
||
|
|
|
||
|
|
@DriftAccessor(tables: [LlmInsights])
|
||
|
|
class InsightDao extends DatabaseAccessor<AppDatabase> with _$InsightDaoMixin {
|
||
|
|
InsightDao(super.db);
|
||
|
|
|
||
|
|
Future<LlmInsight?> getCached(String promptHash) => (select(llmInsights)
|
||
|
|
..where((t) => t.promptHash.equals(promptHash))
|
||
|
|
..limit(1))
|
||
|
|
.getSingleOrNull();
|
||
|
|
|
||
|
|
Future<void> upsertInsight(LlmInsightsCompanion insight) =>
|
||
|
|
into(llmInsights).insertOnConflictUpdate(insight);
|
||
|
|
|
||
|
|
// Purge cached insights older than 7 days
|
||
|
|
Future<void> purgeStale() {
|
||
|
|
final cutoff = DateTime.now().subtract(const Duration(days: 7));
|
||
|
|
return (delete(llmInsights)
|
||
|
|
..where((t) => t.generatedAt.isSmallerThanValue(cutoff)))
|
||
|
|
.go();
|
||
|
|
}
|
||
|
|
}
|