mnemo_cards/mnemo_cards_backend/lib/database/tables/relations.dart

64 lines
2.6 KiB
Dart
Raw Normal View History

2025-12-13 13:27:05 +00:00
import 'package:drift/drift.dart';
2025-12-13 23:35:14 +00:00
import 'package:drift_postgres/drift_postgres.dart';
2025-12-13 13:27:05 +00:00
import 'users.dart';
import 'packs.dart' show CardPacks, GameCards, VoiceModels;
/// Junction table для связи Users ↔ CardPacks (многие ко многим)
/// Хранит информацию о том, какие паки куплены пользователем
class UserPacks extends Table {
2025-12-13 23:35:14 +00:00
TextColumn get userId => text()
.references(Users, #id, onDelete: KeyAction.cascade)();
TextColumn get packId => text()
.references(CardPacks, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
// Когда пользователь получил доступ к паку
2025-12-13 23:35:14 +00:00
Column<PgDateTime> get grantedAt => customType(PgTypes.timestampWithTimezone)
.withDefault(now())();
2025-12-13 13:27:05 +00:00
// Как пользователь получил пак (purchase, promo, free, admin)
TextColumn get grantType => text().withDefault(const Constant('purchase'))();
@override
Set<Column> get primaryKey => {userId, packId};
}
/// Таблица PreviewCards - связь CardPacks с preview карточками
/// Хранит какие карточки показывать в превью пака
class PreviewCards extends Table {
2025-12-13 23:35:14 +00:00
TextColumn get packId => text()
.references(CardPacks, #id, onDelete: KeyAction.cascade)();
TextColumn get cardId => text()
.references(GameCards, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
IntColumn get order => integer().withDefault(const Constant(0))();
@override
Set<Column> get primaryKey => {packId, cardId};
}
/// Таблица CardPackCards - связь CardPacks с GameCards (many-to-many)
/// Хранит какие карточки принадлежат какому паку
class CardPackCards extends Table {
2025-12-13 23:35:14 +00:00
TextColumn get packId => text()
.references(CardPacks, #id, onDelete: KeyAction.cascade)();
TextColumn get cardId => text()
.references(GameCards, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
IntColumn get order => integer().withDefault(const Constant(0))();
@override
Set<Column> get primaryKey => {packId, cardId};
}
/// Таблица CardVoices - связь GameCards с VoiceModels (many-to-many)
/// Хранит какие голосовые файлы принадлежат какой карточке
class CardVoices extends Table {
2025-12-13 23:35:14 +00:00
TextColumn get cardId => text()
.references(GameCards, #id, onDelete: KeyAction.cascade)();
TextColumn get voiceId => text()
.references(VoiceModels, #id, onDelete: KeyAction.cascade)();
2025-12-13 13:27:05 +00:00
@override
Set<Column> get primaryKey => {cardId, voiceId};
}