generator client { provider = "prisma-client-js" previewFeatures = ["metrics"] } datasource db { provider = "postgresql" url = env("DATABASE_URL") } model User { id String @id @default(uuid()) @db.Uuid username String @unique @db.VarChar(32) displayName String @map("display_name") @db.VarChar(100) passwordHash String @map("password_hash") createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) memberships ChatMember[] messages Message[] sessions Session[] attachments Attachment[] @@map("users") } model Session { tokenHash String @id @map("token_hash") @db.Char(64) userId String @map("user_id") @db.Uuid expiresAt DateTime @map("expires_at") @db.Timestamptz(3) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@index([userId]) @@map("sessions") } model Chat { id String @id @default(uuid()) @db.Uuid title String @db.VarChar(100) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) members ChatMember[] messages Message[] attachments Attachment[] @@map("chats") } model ChatMember { chatId String @map("chat_id") @db.Uuid userId String @map("user_id") @db.Uuid chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Restrict) @@id([chatId, userId]) @@index([userId]) @@map("chat_members") } model Message { id String @id @default(uuid()) @db.Uuid chatId String @map("chat_id") @db.Uuid senderId String @map("sender_id") @db.Uuid text String @db.VarChar(4000) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) sender User @relation(fields: [senderId], references: [id], onDelete: Restrict) idempotencyKeys IdempotencyKey[] attachments MessageAttachment[] @@index([chatId, createdAt, id]) @@map("messages") } model Attachment { id String @id @default(uuid()) @db.Uuid chatId String @map("chat_id") @db.Uuid ownerId String @map("owner_id") @db.Uuid filename String @db.VarChar(255) contentType String @map("content_type") @db.VarChar(32) sizeBytes Int @map("size_bytes") sha256 String @db.Char(64) objectKey String @unique @map("object_key") @db.VarChar(128) stagingKey String? @unique @map("staging_key") @db.VarChar(128) status String @db.VarChar(16) errorCode String? @map("error_code") @db.VarChar(64) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3) chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict) messages MessageAttachment[] variants AttachmentVariant[] jobs AttachmentJob[] @@index([chatId]) @@index([ownerId]) @@map("attachments") } model AttachmentVariant { attachmentId String @map("attachment_id") @db.Uuid pipelineVersion Int @map("pipeline_version") name String @db.VarChar(16) contentType String @map("content_type") @db.VarChar(32) objectKey String @unique @map("object_key") @db.VarChar(128) sizeBytes Int @map("size_bytes") width Int height Int sha256 String @db.Char(64) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade) @@id([attachmentId, pipelineVersion, name]) @@map("attachment_variants") } model AttachmentJob { id String @id @db.Uuid attachmentId String @map("attachment_id") @db.Uuid pipelineVersion Int @map("pipeline_version") status String @db.VarChar(16) attempt Int maxAttempts Int @map("max_attempts") nextAttemptAt DateTime @map("next_attempt_at") @db.Timestamptz(3) leaseOwner String? @map("lease_owner") @db.Uuid leaseUntil DateTime? @map("lease_until") @db.Timestamptz(3) errorCode String? @map("error_code") @db.VarChar(64) lastError String? @map("last_error") @db.VarChar(200) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) updatedAt DateTime @default(now()) @updatedAt @map("updated_at") @db.Timestamptz(3) finishedAt DateTime? @map("finished_at") @db.Timestamptz(3) attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: Cascade) events JobEvent[] outbox OutboxEntry[] @@unique([attachmentId, pipelineVersion]) @@index([status, leaseUntil]) @@map("attachment_jobs") } model JobEvent { id BigInt @id @default(autoincrement()) jobId String @map("job_id") @db.Uuid attempt Int event String @db.VarChar(32) detail String? @db.VarChar(200) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade) @@index([jobId]) @@map("job_events") } model OutboxEntry { id BigInt @id @default(autoincrement()) jobId String @map("job_id") @db.Uuid payload Json availableAt DateTime @default(now()) @map("available_at") @db.Timestamptz(3) publishedAt DateTime? @map("published_at") @db.Timestamptz(3) publishAttempts Int @default(0) @map("publish_attempts") createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) job AttachmentJob @relation(fields: [jobId], references: [id], onDelete: Cascade) @@index([publishedAt, availableAt]) @@index([jobId]) @@map("outbox") } model MessageAttachment { messageId String @map("message_id") @db.Uuid position Int @db.SmallInt attachmentId String @map("attachment_id") @db.Uuid message Message @relation(fields: [messageId], references: [id], onDelete: Cascade) attachment Attachment @relation(fields: [attachmentId], references: [id], onDelete: NoAction) @@id([messageId, position]) @@unique([messageId, attachmentId]) @@index([attachmentId]) @@map("message_attachments") } model IdempotencyKey { userId String @map("user_id") @db.Uuid chatId String @map("chat_id") @db.Uuid key String @db.VarChar(128) requestHash String @map("request_hash") @db.Char(64) messageId String @map("message_id") @db.Uuid createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) message Message @relation(fields: [messageId], references: [id], onDelete: Cascade) @@id([userId, chatId, key]) @@map("idempotency_keys") }