我正在尝试建立一个带有条纹的通用网站,可以接受一次性付款和订阅,还可以处理退款和发票。
该应用程序还将有一个仪表板来查看和管理这些数据。
我正在努力建立这些模型之间的关系,但我目前已经达到了这一点,也不确定这是否是一个长期的好方法,例如这是否会在模型之间带来太多不必要的关系。
我已经发出了这个问题不需要的字段,以使关系尽可能清晰。
model User {
id String @id @default(auto()) @map("_id") @db.ObjectId
orders Order[]
subscriptions Subscription[]
invoices Invoice[]
payments Payment[]
}
model Order {
id String @id @default(auto()) @map("_id") @db.ObjectId
payments Payment[]
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
}
model Subscription {
id String @id @default(auto()) @map("_id") @db.ObjectId
payments Payment[]
invoices Invoice[]
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
}
model Charge {
id String @id @default(auto()) @map("_id") @db.ObjectId
paymentId String @db.ObjectId
payment Payment @relation(fields: [paymentId], references: [id])
}
model Refund {
id String @id @default(auto()) @map("_id") @db.ObjectId
paymentId String @db.ObjectId
payment Payment @relation(fields: [paymentId], references: [id])
}
model Payment {
id String @id @default(auto()) @map("_id") @db.ObjectId
charges Charge[]
refunds Refund[]
orderId String? @unique @db.ObjectId
order Order? @relation(fields: [orderId], references: [id])
subscriptionId String? @unique @db.ObjectId
subscription Subscription? @relation(fields: [subscriptionId], references: [id])
invoiceId String? @unique
invoice Invoice? @relation(fields: [invoiceId], references: [id])
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
}
model Invoice {
id String @id @map("_id")
payments Payment[]
subscriptionId String? @unique @db.ObjectId
subscription Subscription? @relation(fields: [subscriptionId], references: [id])
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
}
当您可以制作两个表格并在付款中添加一列时,制作四个表格似乎是多余的。
调整付款表以添加交易类型。
此交易类型将取代表格(订单、费用、退款)。订阅可以用账户表代替。我假设一个 Stripe 用户可以添加多个帐户。您将需要一个链接到您的用户表的帐户表,并且所有交易都将通过付款表链接。具有独立的每日交易表,用于存储失败和成功的交易。 您可能需要添加另一列来指示“付款”中的交易成功。我更喜欢将付款表重命名为交易表。
如果您想通过链接多个用户之间的相关帐户来进一步建模数据库,则帐户表应该有一个单独的帐户组表。
model Payment {
id String @id @default(auto()) @map("_id") @db.ObjectId
charges Charge[]
refunds Refund[]
orderId String? @unique @db.ObjectId
order Order? @relation(fields: [orderId], references: [id])
subscriptionId String? @unique @db.ObjectId
subscription Subscription? @relation(fields: [subscriptionId], references: [id])
invoiceId String? @unique
invoice Invoice? @relation(fields: [invoiceId], references: [id])
userId String @db.ObjectId
user User @relation(fields: [userId], references: [id])
transactionType String @.............(Ex:(R:Refund),(D,Deposit),(P,Payment))