mirror of
https://github.com/sqlchat/sqlchat.git
synced 2025-07-23 23:11:37 +08:00

* feat: use connection pooling * chore: minor subscription change * feat: separate subscription from payment
163 lines
5.0 KiB
Plaintext
163 lines
5.0 KiB
Plaintext
// This is your Prisma schema file,
|
|
// learn more about it in the docs: https://pris.ly/d/prisma-schema
|
|
|
|
// Ideally, we want to separate usage collection from the metadata, however Prisma does not support
|
|
// multiple databases properly. https://github.com/prisma/prisma/issues/2443 solution does not
|
|
// generate the proper client.
|
|
|
|
generator client {
|
|
provider = "prisma-client-js"
|
|
}
|
|
|
|
datasource db {
|
|
provider = "postgresql"
|
|
// Connection pooling URL
|
|
url = env("DATABASE_URL")
|
|
// Connection pooling is not supported by Prisma migrate yet. So need to use directUrl
|
|
directUrl = env("DATABASE_DIRECT_URL")
|
|
}
|
|
|
|
model Chat {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
model Json @default("{}")
|
|
ctx Json @default("{}")
|
|
messages Message[]
|
|
|
|
@@index([createdAt], map: "chat_created_at_idx")
|
|
@@map("chat")
|
|
}
|
|
|
|
model Message {
|
|
id String @id @default(cuid())
|
|
chat Chat @relation(fields: [chatId], references: [id])
|
|
chatId String @map("chat_id")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
endUser String @default("") @map("end_user")
|
|
role String @default("")
|
|
content String @default("")
|
|
upvote Boolean @default(false)
|
|
downvote Boolean @default(false)
|
|
|
|
@@index([chatId], map: "message_chat_id_idx")
|
|
@@index([createdAt], map: "message_created_at_idx")
|
|
@@index([endUser], map: "message_end_user_idx")
|
|
@@index([role], map: "message_role_idx")
|
|
@@map("message")
|
|
}
|
|
|
|
model Usage {
|
|
id String @id @default(cuid())
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
endUser String @default("") @map("end_user")
|
|
count Int @default(0)
|
|
|
|
@@index([createdAt], map: "usage_created_at_idx")
|
|
@@index([endUser], map: "usage_end_user_idx")
|
|
@@map("usage")
|
|
}
|
|
|
|
model Subscription {
|
|
id String @id @default(cuid())
|
|
user User @relation(fields: [userId], references: [id])
|
|
userId String @default("") @map("user_id")
|
|
// Denormalize to avoid join with the user table
|
|
email String @default("") @map("email")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
canceledAt DateTime? @map("canceled_at")
|
|
startAt DateTime @default(now()) @map("start_at")
|
|
expireAt DateTime @default(now()) @map("expire_at")
|
|
plan SubscriptionPlan
|
|
|
|
@@index([userId], map: "subscription_user_id_idx")
|
|
@@index([email], map: "subscription_email_idx")
|
|
@@map("subscription")
|
|
}
|
|
|
|
enum SubscriptionPlan {
|
|
PRO
|
|
}
|
|
|
|
model Payment {
|
|
id String @id @default(cuid())
|
|
user User @relation(fields: [userId], references: [id])
|
|
userId String @default("") @map("user_id")
|
|
// Denormalize to avoid join with the user table
|
|
email String @default("") @map("email")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
paymentId String @default("") @map("payment_id")
|
|
customerId String @default("") @map("customer_id")
|
|
description String @default("")
|
|
amount Int @default(0)
|
|
currency String @default("")
|
|
receipt String @default("")
|
|
|
|
@@unique([paymentId])
|
|
@@index([userId], map: "payment_user_id_idx")
|
|
@@index([email], map: "payment_email_idx")
|
|
@@map("payment")
|
|
}
|
|
|
|
// NextAuth Prisma Schema Begin
|
|
// Below are the auth related prisma schema to support NextAuth.js. In partiular, the email flow
|
|
// requires this. https://authjs.dev/reference/adapter/prisma
|
|
model Account {
|
|
id String @id @default(cuid())
|
|
userId String @map("user_id")
|
|
type String
|
|
provider String
|
|
providerAccountId String @map("provider_account_id")
|
|
refresh_token String? @db.Text
|
|
access_token String? @db.Text
|
|
expires_at Int?
|
|
token_type String?
|
|
scope String?
|
|
id_token String? @db.Text
|
|
session_state String?
|
|
|
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
|
|
@@unique([provider, providerAccountId])
|
|
@@map("accounts")
|
|
}
|
|
|
|
model Session {
|
|
id String @id @default(cuid())
|
|
sessionToken String @unique @map("session_token")
|
|
userId String @map("user_id")
|
|
expires DateTime
|
|
user User @relation(fields: [userId], references: [id], onDelete: Cascade)
|
|
|
|
@@map("sessions")
|
|
}
|
|
|
|
model User {
|
|
id String @id @default(cuid())
|
|
name String?
|
|
email String? @unique
|
|
emailVerified DateTime? @map("email_verified")
|
|
image String?
|
|
accounts Account[]
|
|
sessions Session[]
|
|
|
|
subscriptions Subscription[]
|
|
payments Payment[]
|
|
// The stripe customer id corresponds to this user
|
|
stripeId String? @unique @map("stripe_id")
|
|
createdAt DateTime @default(now()) @map("created_at")
|
|
|
|
@@index([createdAt], map: "users_created_at_idx")
|
|
@@map("users")
|
|
}
|
|
|
|
model VerificationToken {
|
|
identifier String
|
|
token String @unique
|
|
expires DateTime
|
|
|
|
@@unique([identifier, token])
|
|
@@map("verificationtokens")
|
|
}
|
|
|
|
// NextAuth Prisma Schema End
|