billing.sql.ts 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. import { bigint, boolean, index, int, json, mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
  2. import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"
  3. import { workspaceIndexes } from "./workspace.sql"
  4. export const SubscriptionPlan = ["20", "100", "200"] as const
  5. export const BillingTable = mysqlTable(
  6. "billing",
  7. {
  8. ...workspaceColumns,
  9. ...timestamps,
  10. customerID: varchar("customer_id", { length: 255 }),
  11. paymentMethodID: varchar("payment_method_id", { length: 255 }),
  12. paymentMethodType: varchar("payment_method_type", { length: 32 }),
  13. paymentMethodLast4: varchar("payment_method_last4", { length: 4 }),
  14. balance: bigint("balance", { mode: "number" }).notNull(),
  15. monthlyLimit: int("monthly_limit"),
  16. monthlyUsage: bigint("monthly_usage", { mode: "number" }),
  17. timeMonthlyUsageUpdated: utc("time_monthly_usage_updated"),
  18. reload: boolean("reload"),
  19. reloadTrigger: int("reload_trigger"),
  20. reloadAmount: int("reload_amount"),
  21. reloadError: varchar("reload_error", { length: 255 }),
  22. timeReloadError: utc("time_reload_error"),
  23. timeReloadLockedTill: utc("time_reload_locked_till"),
  24. subscription: json("subscription").$type<{
  25. status: "subscribed"
  26. seats: number
  27. plan: "20" | "100" | "200"
  28. useBalance?: boolean
  29. coupon?: string
  30. }>(),
  31. subscriptionID: varchar("subscription_id", { length: 28 }),
  32. subscriptionPlan: mysqlEnum("subscription_plan", SubscriptionPlan),
  33. timeSubscriptionBooked: utc("time_subscription_booked"),
  34. timeSubscriptionSelected: utc("time_subscription_selected"),
  35. },
  36. (table) => [
  37. ...workspaceIndexes(table),
  38. uniqueIndex("global_customer_id").on(table.customerID),
  39. uniqueIndex("global_subscription_id").on(table.subscriptionID),
  40. ],
  41. )
  42. export const SubscriptionTable = mysqlTable(
  43. "subscription",
  44. {
  45. ...workspaceColumns,
  46. ...timestamps,
  47. userID: ulid("user_id").notNull(),
  48. rollingUsage: bigint("rolling_usage", { mode: "number" }),
  49. fixedUsage: bigint("fixed_usage", { mode: "number" }),
  50. timeRollingUpdated: utc("time_rolling_updated"),
  51. timeFixedUpdated: utc("time_fixed_updated"),
  52. },
  53. (table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
  54. )
  55. export const PaymentTable = mysqlTable(
  56. "payment",
  57. {
  58. ...workspaceColumns,
  59. ...timestamps,
  60. customerID: varchar("customer_id", { length: 255 }),
  61. invoiceID: varchar("invoice_id", { length: 255 }),
  62. paymentID: varchar("payment_id", { length: 255 }),
  63. amount: bigint("amount", { mode: "number" }).notNull(),
  64. timeRefunded: utc("time_refunded"),
  65. enrichment: json("enrichment").$type<
  66. | {
  67. type: "subscription"
  68. couponID?: string
  69. }
  70. | {
  71. type: "credit"
  72. }
  73. >(),
  74. },
  75. (table) => [...workspaceIndexes(table)],
  76. )
  77. export const UsageTable = mysqlTable(
  78. "usage",
  79. {
  80. ...workspaceColumns,
  81. ...timestamps,
  82. model: varchar("model", { length: 255 }).notNull(),
  83. provider: varchar("provider", { length: 255 }).notNull(),
  84. inputTokens: int("input_tokens").notNull(),
  85. outputTokens: int("output_tokens").notNull(),
  86. reasoningTokens: int("reasoning_tokens"),
  87. cacheReadTokens: int("cache_read_tokens"),
  88. cacheWrite5mTokens: int("cache_write_5m_tokens"),
  89. cacheWrite1hTokens: int("cache_write_1h_tokens"),
  90. cost: bigint("cost", { mode: "number" }).notNull(),
  91. keyID: ulid("key_id"),
  92. enrichment: json("enrichment").$type<{
  93. plan: "sub"
  94. }>(),
  95. },
  96. (table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
  97. )