generator client { provider = "prisma-client" output = "../src/generated/prisma" moduleFormat = "cjs" } datasource db { provider = "postgresql" } 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) chatsEpoch BigInt @default(0) @map("chats_epoch") memberships ChatMember[] messages Message[] sessions Session[] idempotencyRecords MessageIdempotency[] attachments Attachment[] directUploads DirectUpload[] @@map("users") } model Session { id String @id @default(uuid()) @db.Uuid userId String @map("user_id") @db.Uuid tokenHash String @unique @map("token_hash") @db.Char(64) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) expiresAt DateTime @map("expires_at") @db.Timestamptz(3) revokedAt DateTime? @map("revoked_at") @db.Timestamptz(3) user User @relation(fields: [userId], references: [id], onDelete: Cascade) @@index([userId]) @@index([expiresAt]) @@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) messageEpochs ChatMessageEpoch[] members ChatMember[] messages Message[] idempotencyRecords MessageIdempotency[] attachments Attachment[] directUploads DirectUpload[] @@index([createdAt, id]) @@map("chats") } model ChatMember { chatId String @map("chat_id") @db.Uuid userId String @map("user_id") @db.Uuid position Int chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) user User @relation(fields: [userId], references: [id], onDelete: Restrict) @@id([chatId, userId]) @@unique([chatId, position]) @@index([userId, chatId]) @@map("chat_members") } model ChatMessageEpoch { chatId String @map("chat_id") @db.Uuid bucket Int @db.SmallInt epoch BigInt @default(0) chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) @@id([chatId, bucket]) @@map("chat_message_epochs") } 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) attachmentIds String[] @default([]) @map("attachment_ids") @db.Uuid 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) idempotencyRecords MessageIdempotency[] @@index([chatId, createdAt, id]) @@index([senderId]) @@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 objectKey String @unique @map("object_key") filename String @db.VarChar(255) contentType String @map("content_type") @db.VarChar(32) sizeBytes Int @map("size_bytes") sha256 String @db.Char(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: Restrict) owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict) @@index([chatId]) @@index([ownerId]) @@map("attachments") } enum DirectUploadStatus { pending completed rejected expired @@map("direct_upload_status") } model DirectUpload { 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) status DirectUploadStatus @default(pending) expiresAt DateTime @map("expires_at") @db.Timestamptz(3) createdAt DateTime @default(now()) @map("created_at") @db.Timestamptz(3) chat Chat @relation(fields: [chatId], references: [id], onDelete: Restrict) owner User @relation(fields: [ownerId], references: [id], onDelete: Restrict) @@index([status, expiresAt]) @@index([chatId]) @@index([ownerId]) @@map("direct_uploads") } model MessageIdempotency { userId String @map("user_id") @db.Uuid chatId String @map("chat_id") @db.Uuid key String @db.VarChar(128) messageId String? @map("message_id") @db.Uuid expiresAt DateTime @map("expires_at") @db.Timestamptz(3) user User @relation(fields: [userId], references: [id], onDelete: Cascade) chat Chat @relation(fields: [chatId], references: [id], onDelete: Cascade) message Message? @relation(fields: [messageId], references: [id], onDelete: Cascade) @@id([userId, chatId, key]) @@index([chatId]) @@index([messageId]) @@index([expiresAt]) @@map("message_idempotency") }