Files
project-fileshare/apps/backend/prisma/schema.prisma

330 lines
9.5 KiB
Plaintext
Raw Normal View History

2026-10-29 13:21:30 +06:00
datasource db {
provider = "mysql"
url = env("DATABASE_URL")
}
generator client {
provider = "prisma-client-js"
}
enum UserRole {
SUPER_ADMIN
ADMIN
USER
}
enum UserStatus {
ACTIVE
SUSPENDED
PENDING
}
enum FileCategory {
VIDEO
IMAGE
DOCUMENT
ARCHIVE
AUDIO
OTHER
}
enum FileStatus {
PENDING
UPLOADING
PROCESSING
READY
FAILED
}
enum PermissionResourceType {
FILE
FOLDER
USER_FILES
}
enum ShareResourceType {
FILE
FOLDER
}
enum UploadSessionStatus {
INITIALIZED
UPLOADING
COMPLETED
ABORTED
EXPIRED
}
enum ProcessingJobStatus {
QUEUED
PROCESSING
COMPLETED
FAILED
}
model User {
id String @id @default(uuid())
email String @unique
username String @unique
fullName String
companyName String @default("Ecohub")
allowedCompanies String? @db.Text // Comma-separated or JSON list of companies user can access, or "*" for all
passwordHash String
viewablePassword String?
role UserRole @default(USER)
status UserStatus @default(ACTIVE)
storageQuotaBytes BigInt @default(5368709120) // 5GB default
storageUsedBytes BigInt @default(0)
canCreateShareLinks Boolean @default(false)
canDeleteFiles Boolean @default(true)
canViewOtherUsers Boolean @default(false) // Permission to view other users' info & directory (Superadmin only by default)
remoteFileServerUrl String? // User-specific Remote File Server Link / Upload Endpoint (Optional)
canUseRemoteServer Boolean @default(true) // Permission to upload/mirror to remote file server
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
lastLoginAt DateTime?
// Relations
folders Folder[]
files FileAsset[]
shareLinks ShareLink[]
uploadSessions UploadSession[]
grantedPermissions FilePermission[] @relation("GrantedByRelation")
receivedPermissions FilePermission[] @relation("GranteeRelation")
ownedPermissions FilePermission[] @relation("OwnerRelation")
refreshTokens RefreshToken[]
actorAuditLogs AuditLog[] @relation("ActorRelation")
targetAuditLogs AuditLog[] @relation("TargetRelation")
apiKeys ApiKey[]
@@index([role])
@@index([status])
}
model Folder {
id String @id @default(uuid())
ownerId String
parentId String?
name String
logicalPath String
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
parent Folder? @relation("FolderHierarchy", fields: [parentId], references: [id], onDelete: Cascade)
children Folder[] @relation("FolderHierarchy")
files FileAsset[]
uploadSessions UploadSession[]
shareLinks ShareLink[]
@@unique([ownerId, parentId, name])
@@index([ownerId])
@@index([parentId])
}
model FileAsset {
id String @id @default(uuid())
ownerId String
folderId String?
originalFilename String
internalStorageKey String @unique
mimeType String
fileExtension String
fileSizeBytes BigInt
fileCategory FileCategory
checksum String?
status FileStatus @default(PENDING)
durationSeconds Float?
width Int?
height Int?
videoCodec String?
audioCodec String?
thumbnailStorageKey String?
hlsMasterPlaylistKey String?
isSoftDeleted Boolean @default(false)
deletedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
folder Folder? @relation(fields: [folderId], references: [id], onDelete: SetNull)
videoProcessingJobs VideoProcessingJob[]
shareLinks ShareLink[]
@@index([ownerId])
@@index([folderId])
@@index([fileCategory])
@@index([status])
@@index([isSoftDeleted])
}
model FilePermission {
id String @id @default(uuid())
resourceType PermissionResourceType
resourceId String? // File ID, Folder ID, or null for USER_FILES
ownerId String
granteeUserId String
grantedByUserId String
canViewMetadata Boolean @default(true)
canPreview Boolean @default(false)
canPlay Boolean @default(false)
canDownload Boolean @default(false)
canListFolder Boolean @default(false)
expiresAt DateTime?
revokedAt DateTime?
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
owner User @relation("OwnerRelation", fields: [ownerId], references: [id], onDelete: Cascade)
grantee User @relation("GranteeRelation", fields: [granteeUserId], references: [id], onDelete: Cascade)
grantedBy User @relation("GrantedByRelation", fields: [grantedByUserId], references: [id], onDelete: Cascade)
@@index([granteeUserId])
@@index([ownerId])
@@index([resourceType, resourceId])
@@index([revokedAt, expiresAt])
}
model UploadSession {
id String @id @default(uuid())
ownerId String
folderId String?
originalFilename String
mimeType String
fileCategory FileCategory
totalSizeBytes BigInt
uploadedSizeBytes BigInt @default(0)
tempStorageKey String @unique
status UploadSessionStatus @default(INITIALIZED)
expiresAt DateTime
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
folder Folder? @relation(fields: [folderId], references: [id], onDelete: SetNull)
@@index([ownerId])
@@index([status])
@@index([expiresAt])
}
model VideoProcessingJob {
id String @id @default(uuid())
fileAssetId String
status ProcessingJobStatus @default(QUEUED)
progress Int @default(0)
attempts Int @default(0)
errorReason String? @db.Text
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
completedAt DateTime?
// Relations
fileAsset FileAsset @relation(fields: [fileAssetId], references: [id], onDelete: Cascade)
@@index([fileAssetId])
@@index([status])
}
model RefreshToken {
id String @id @default(uuid())
tokenHash String @unique
userId String
userAgent String?
ipAddress String?
expiresAt DateTime
revokedAt DateTime?
createdAt DateTime @default(now())
// Relations
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([userId])
@@index([expiresAt])
}
model AuditLog {
id String @id @default(uuid())
actorUserId String?
action String
resourceType String
resourceId String?
targetUserId String?
ipAddress String?
userAgent String?
detailsJson String? @db.Text
createdAt DateTime @default(now())
// Relations
actor User? @relation("ActorRelation", fields: [actorUserId], references: [id], onDelete: SetNull)
targetUser User? @relation("TargetRelation", fields: [targetUserId], references: [id], onDelete: SetNull)
@@index([actorUserId])
@@index([action])
@@index([resourceType, resourceId])
@@index([createdAt])
}
model SystemSetting {
id String @id @default(uuid())
key String @unique
value String @db.Text
description String?
updatedAt DateTime @updatedAt
}
model ShareLink {
id String @id @default(uuid())
token String @unique
title String?
ownerId String
resourceType ShareResourceType
fileId String?
folderId String?
allowDownload Boolean @default(true)
passwordHash String?
expiresAt DateTime?
viewCount Int @default(0)
downloadCount Int @default(0)
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
// Relations
owner User @relation(fields: [ownerId], references: [id], onDelete: Cascade)
file FileAsset? @relation(fields: [fileId], references: [id], onDelete: Cascade)
folder Folder? @relation(fields: [folderId], references: [id], onDelete: Cascade)
@@index([token])
@@index([ownerId])
@@index([fileId])
@@index([folderId])
}
model ApiKey {
id String @id @default(uuid())
name String
key String @unique
keyPrefix String
userId String
permissions String @default("FULL_ACCESS")
description String?
lastUsedAt DateTime?
expiresAt DateTime?
isActive Boolean @default(true)
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
@@index([key])
@@index([userId])
@@index([isActive])
}