mnemo_cards/mnemo_cards_backend/lib/database/converters.dart
Dmitry 550b56276e
Some checks are pending
Backend CI / test (push) Waiting to run
Backend CI / build (push) Blocked by required conditions
Mobile App CI / test (push) Waiting to run
Mobile App CI / build-android (push) Blocked by required conditions
Mobile App CI / build-ios (push) Blocked by required conditions
Web App CI / test (push) Waiting to run
Web App CI / build (push) Blocked by required conditions
Deploy Mnemo Cards / Deploy Backend (push) Waiting to run
Deploy Mnemo Cards / Deploy Web App (push) Blocked by required conditions
Deploy Mnemo Cards / Final Verification (push) Blocked by required conditions
Deploy Telegram Bot / Deploy Telegram Bot (push) Waiting to run
postgress fix
2025-12-13 23:55:50 +03:00

120 lines
2.9 KiB
Dart

import 'package:drift/drift.dart';
import 'dart:convert';
import 'package:uuid/uuid.dart';
const _uuid = Uuid();
// Конвертеры для JSON полей - общие для всех таблиц
class JsonMapConverter extends TypeConverter<Map<String, dynamic>?, String?> {
const JsonMapConverter();
@override
Map<String, dynamic>? fromSql(String? fromDb) {
if (fromDb == null || fromDb.isEmpty || fromDb == '{}') return null;
try {
return json.decode(fromDb) as Map<String, dynamic>;
} catch (e) {
return null;
}
}
@override
String toSql(Map<String, dynamic>? value) {
if (value == null || value.isEmpty) return '{}';
return json.encode(value);
}
}
class JsonListConverter extends TypeConverter<List<dynamic>, String> {
const JsonListConverter();
@override
List<dynamic> fromSql(String? fromDb) {
if (fromDb == null || fromDb.isEmpty || fromDb == '[]') return [];
try {
return json.decode(fromDb) as List<dynamic>;
} catch (e) {
return [];
}
}
@override
String toSql(List<dynamic>? value) {
if (value == null || value.isEmpty) return '[]';
return json.encode(value);
}
}
class StringListConverter extends TypeConverter<List<String>, String> {
const StringListConverter();
@override
List<String> fromSql(String fromDb) {
if (fromDb.isEmpty || fromDb == '[]') return [];
try {
final decoded = json.decode(fromDb);
return (decoded as List).map((e) => e as String).toList();
} catch (e) {
return [];
}
}
@override
String toSql(List<String> value) {
if (value.isEmpty) return '[]';
return json.encode(value);
}
}
class DateTimeListConverter extends TypeConverter<List<DateTime>?, String> {
const DateTimeListConverter();
@override
List<DateTime>? fromSql(String? fromDb) {
if (fromDb == null || fromDb.isEmpty || fromDb == '[]') return null;
try {
final decoded = json.decode(fromDb) as List;
return decoded.map((e) {
if (e is String) {
return DateTime.parse(e);
}
return DateTime.now();
}).toList();
} catch (e) {
return null;
}
}
@override
String toSql(List<DateTime>? value) {
if (value == null || value.isEmpty) return '[]';
return json.encode(value.map((e) => e.toIso8601String()).toList());
}
}
class IntListConverter extends TypeConverter<List<int>, String> {
const IntListConverter();
@override
List<int> fromSql(String fromDb) {
if (fromDb.isEmpty || fromDb == '[]') return [];
try {
final decoded = json.decode(fromDb);
return (decoded as List).map((e) => e as int).toList();
} catch (e) {
return [];
}
}
@override
String toSql(List<int> value) {
if (value.isEmpty) return '[]';
return json.encode(value);
}
}
/// Helper function for UUID generation
String generateUuid() {
return _uuid.v4();
}