subtracks/lib/database/converters.dart
austinried 0e6acbed0f bring in database
switch to just using source models (no extra db fields)
start re-implementing sync service
2025-11-07 11:45:13 +09:00

52 lines
1.2 KiB
Dart

import 'dart:convert';
import 'package:drift/drift.dart';
import 'package:fast_immutable_collections/fast_immutable_collections.dart';
class DurationSecondsConverter extends TypeConverter<Duration, int> {
const DurationSecondsConverter();
@override
Duration fromSql(int fromDb) => Duration(seconds: fromDb);
@override
int toSql(Duration value) => value.inSeconds;
}
class UriConverter extends TypeConverter<Uri, String> {
const UriConverter();
@override
Uri fromSql(String fromDb) => Uri.parse(fromDb);
@override
String toSql(Uri value) => value.toString();
}
// class ListQueryConverter extends TypeConverter<ListQuery, String> {
// const ListQueryConverter();
// @override
// ListQuery fromSql(String fromDb) => ListQuery.fromJson(jsonDecode(fromDb));
// @override
// String toSql(ListQuery value) => jsonEncode(value.toJson());
// }
class IListIntConverter extends TypeConverter<IList<int>, String> {
const IListIntConverter();
@override
IList<int> fromSql(String fromDb) {
return IList<int>.fromJson(
jsonDecode(fromDb),
(item) => int.parse(item as String),
);
}
@override
String toSql(IList<int> value) {
return jsonEncode(value.toJson((e) => jsonEncode(e)));
}
}