billing.sql.ts 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  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 BlackPlans = ["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: (typeof BlackPlans)[number]
  28. useBalance?: boolean
  29. coupon?: string
  30. }>(),
  31. subscriptionID: varchar("subscription_id", { length: 28 }),
  32. subscriptionPlan: mysqlEnum("subscription_plan", BlackPlans),
  33. timeSubscriptionBooked: utc("time_subscription_booked"),
  34. timeSubscriptionSelected: utc("time_subscription_selected"),
  35. liteSubscriptionID: varchar("lite_subscription_id", { length: 28 }),
  36. lite: json("lite").$type<{
  37. useBalance?: boolean
  38. }>(),
  39. },
  40. (table) => [
  41. ...workspaceIndexes(table),
  42. uniqueIndex("global_customer_id").on(table.customerID),
  43. uniqueIndex("global_subscription_id").on(table.subscriptionID),
  44. ],
  45. )
  46. export const SubscriptionTable = mysqlTable(
  47. "subscription",
  48. {
  49. ...workspaceColumns,
  50. ...timestamps,
  51. userID: ulid("user_id").notNull(),
  52. rollingUsage: bigint("rolling_usage", { mode: "number" }),
  53. fixedUsage: bigint("fixed_usage", { mode: "number" }),
  54. timeRollingUpdated: utc("time_rolling_updated"),
  55. timeFixedUpdated: utc("time_fixed_updated"),
  56. },
  57. (table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
  58. )
  59. export const LiteTable = mysqlTable(
  60. "lite",
  61. {
  62. ...workspaceColumns,
  63. ...timestamps,
  64. userID: ulid("user_id").notNull(),
  65. rollingUsage: bigint("rolling_usage", { mode: "number" }),
  66. weeklyUsage: bigint("weekly_usage", { mode: "number" }),
  67. monthlyUsage: bigint("monthly_usage", { mode: "number" }),
  68. timeRollingUpdated: utc("time_rolling_updated"),
  69. timeWeeklyUpdated: utc("time_weekly_updated"),
  70. timeMonthlyUpdated: utc("time_monthly_updated"),
  71. },
  72. (table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
  73. )
  74. export const PaymentTable = mysqlTable(
  75. "payment",
  76. {
  77. ...workspaceColumns,
  78. ...timestamps,
  79. customerID: varchar("customer_id", { length: 255 }),
  80. invoiceID: varchar("invoice_id", { length: 255 }),
  81. paymentID: varchar("payment_id", { length: 255 }),
  82. amount: bigint("amount", { mode: "number" }).notNull(),
  83. timeRefunded: utc("time_refunded"),
  84. enrichment: json("enrichment").$type<
  85. | {
  86. type: "subscription" | "lite"
  87. couponID?: string
  88. }
  89. | {
  90. type: "credit"
  91. }
  92. >(),
  93. },
  94. (table) => [...workspaceIndexes(table)],
  95. )
  96. export const UsageTable = mysqlTable(
  97. "usage",
  98. {
  99. ...workspaceColumns,
  100. ...timestamps,
  101. model: varchar("model", { length: 255 }).notNull(),
  102. provider: varchar("provider", { length: 255 }).notNull(),
  103. inputTokens: int("input_tokens").notNull(),
  104. outputTokens: int("output_tokens").notNull(),
  105. reasoningTokens: int("reasoning_tokens"),
  106. cacheReadTokens: int("cache_read_tokens"),
  107. cacheWrite5mTokens: int("cache_write_5m_tokens"),
  108. cacheWrite1hTokens: int("cache_write_1h_tokens"),
  109. cost: bigint("cost", { mode: "number" }).notNull(),
  110. keyID: ulid("key_id"),
  111. sessionID: varchar("session_id", { length: 30 }),
  112. enrichment: json("enrichment").$type<{
  113. plan: "sub" | "byok" | "lite"
  114. }>(),
  115. },
  116. (table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
  117. )