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';
|
2025-12-13 20:55:50 +00:00
|
|
|
import '../converters.dart' show generateUuid;
|
2025-12-13 13:27:05 +00:00
|
|
|
|
|
|
|
|
/// Таблица Tokens - токены авторизации пользователей
|
|
|
|
|
class Tokens extends Table {
|
2025-12-13 23:35:14 +00:00
|
|
|
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
|
2025-12-20 18:26:15 +00:00
|
|
|
TextColumn get id =>
|
|
|
|
|
text().withDefault(const CustomExpression('gen_random_uuid()::text'))();
|
|
|
|
|
TextColumn get userId =>
|
|
|
|
|
text().references(Users, #id, onDelete: KeyAction.cascade)();
|
|
|
|
|
|
2025-12-13 13:27:05 +00:00
|
|
|
TextColumn get token => text().unique()();
|
|
|
|
|
TextColumn get externalUserId => text()();
|
2025-12-20 18:26:15 +00:00
|
|
|
|
|
|
|
|
Column<PgDateTime> get created =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).withDefault(now())();
|
2025-12-13 23:35:14 +00:00
|
|
|
Column<PgDateTime> get expires => customType(PgTypes.timestampWithTimezone)();
|
2025-12-20 18:26:15 +00:00
|
|
|
BoolColumn get isDeleted =>
|
|
|
|
|
boolean().customConstraint('NOT NULL DEFAULT FALSE')();
|
|
|
|
|
Column<PgDateTime> get deletedAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).nullable()();
|
|
|
|
|
|
2025-12-13 20:55:50 +00:00
|
|
|
@override
|
|
|
|
|
Set<Column> get primaryKey => {id};
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 13:27:05 +00:00
|
|
|
@override
|
|
|
|
|
List<String> get customConstraints => [
|
|
|
|
|
'CONSTRAINT valid_expiry CHECK (expires > created)',
|
|
|
|
|
];
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Таблица RefreshTokens - refresh токены для JWT
|
|
|
|
|
class RefreshTokens extends Table {
|
2025-12-13 23:35:14 +00:00
|
|
|
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
|
2025-12-20 18:26:15 +00:00
|
|
|
TextColumn get id =>
|
|
|
|
|
text().withDefault(const CustomExpression('gen_random_uuid()::text'))();
|
|
|
|
|
TextColumn get userId =>
|
|
|
|
|
text().references(Users, #id, onDelete: KeyAction.cascade)();
|
|
|
|
|
|
|
|
|
|
TextColumn get jti => text().unique()(); // JWT ID
|
|
|
|
|
|
|
|
|
|
BoolColumn get isBlacklisted => boolean().customConstraint(
|
|
|
|
|
'NOT NULL DEFAULT FALSE',
|
|
|
|
|
)(); // PostgreSQL использует нативный BOOLEAN
|
|
|
|
|
|
|
|
|
|
Column<PgDateTime> get createdAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).withDefault(now())();
|
|
|
|
|
Column<PgDateTime> get expiresAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone)();
|
|
|
|
|
BoolColumn get isDeleted =>
|
|
|
|
|
boolean().customConstraint('NOT NULL DEFAULT FALSE')();
|
|
|
|
|
Column<PgDateTime> get deletedAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).nullable()();
|
|
|
|
|
|
2025-12-13 20:55:50 +00:00
|
|
|
@override
|
|
|
|
|
Set<Column> get primaryKey => {id};
|
2025-12-13 13:27:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Таблица TelegramAuthCodes - коды для авторизации через Telegram
|
|
|
|
|
class TelegramAuthCodes extends Table {
|
2025-12-13 23:35:14 +00:00
|
|
|
// ID как String, но фактически UUID (генерируется PostgreSQL на стороне БД)
|
2025-12-20 18:26:15 +00:00
|
|
|
TextColumn get id =>
|
|
|
|
|
text().withDefault(const CustomExpression('gen_random_uuid()::text'))();
|
|
|
|
|
|
2025-12-13 13:27:05 +00:00
|
|
|
TextColumn get code => text().unique()();
|
|
|
|
|
TextColumn get telegramUserId => text()();
|
|
|
|
|
TextColumn get telegramUsername => text().nullable()();
|
|
|
|
|
TextColumn get firstName => text().nullable()();
|
|
|
|
|
TextColumn get lastName => text().nullable()();
|
2025-12-20 18:26:15 +00:00
|
|
|
|
2025-12-13 22:00:36 +00:00
|
|
|
BoolColumn get isUsed => boolean()
|
|
|
|
|
.withDefault(const Constant(false))
|
2025-12-13 23:35:14 +00:00
|
|
|
.customConstraint('')(); // PostgreSQL использует нативный BOOLEAN
|
2025-12-20 18:26:15 +00:00
|
|
|
Column<PgDateTime> get usedAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).nullable()();
|
|
|
|
|
|
|
|
|
|
Column<PgDateTime> get createdAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).withDefault(now())();
|
|
|
|
|
Column<PgDateTime> get expiresAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone)();
|
|
|
|
|
BoolColumn get isDeleted =>
|
|
|
|
|
boolean().customConstraint('NOT NULL DEFAULT FALSE')();
|
|
|
|
|
Column<PgDateTime> get deletedAt =>
|
|
|
|
|
customType(PgTypes.timestampWithTimezone).nullable()();
|
|
|
|
|
|
2025-12-13 20:55:50 +00:00
|
|
|
@override
|
|
|
|
|
Set<Column> get primaryKey => {id};
|
2025-12-13 13:27:05 +00:00
|
|
|
}
|