billing.sql.ts 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. coupon?: string
  27. seats: number
  28. plan: "20" | "100" | "200"
  29. }>(),
  30. subscriptionID: varchar("subscription_id", { length: 28 }),
  31. subscriptionPlan: mysqlEnum("subscription_plan", SubscriptionPlan),
  32. timeSubscriptionBooked: utc("time_subscription_booked"),
  33. },
  34. (table) => [
  35. ...workspaceIndexes(table),
  36. uniqueIndex("global_customer_id").on(table.customerID),
  37. uniqueIndex("global_subscription_id").on(table.subscriptionID),
  38. ],
  39. )
  40. export const SubscriptionTable = mysqlTable(
  41. "subscription",
  42. {
  43. ...workspaceColumns,
  44. ...timestamps,
  45. userID: ulid("user_id").notNull(),
  46. rollingUsage: bigint("rolling_usage", { mode: "number" }),
  47. fixedUsage: bigint("fixed_usage", { mode: "number" }),
  48. timeRollingUpdated: utc("time_rolling_updated"),
  49. timeFixedUpdated: utc("time_fixed_updated"),
  50. },
  51. (table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
  52. )
  53. export const PaymentTable = mysqlTable(
  54. "payment",
  55. {
  56. ...workspaceColumns,
  57. ...timestamps,
  58. customerID: varchar("customer_id", { length: 255 }),
  59. invoiceID: varchar("invoice_id", { length: 255 }),
  60. paymentID: varchar("payment_id", { length: 255 }),
  61. amount: bigint("amount", { mode: "number" }).notNull(),
  62. timeRefunded: utc("time_refunded"),
  63. enrichment: json("enrichment").$type<
  64. | {
  65. type: "subscription"
  66. couponID?: string
  67. }
  68. | {
  69. type: "credit"
  70. }
  71. >(),
  72. },
  73. (table) => [...workspaceIndexes(table)],
  74. )
  75. export const UsageTable = mysqlTable(
  76. "usage",
  77. {
  78. ...workspaceColumns,
  79. ...timestamps,
  80. model: varchar("model", { length: 255 }).notNull(),
  81. provider: varchar("provider", { length: 255 }).notNull(),
  82. inputTokens: int("input_tokens").notNull(),
  83. outputTokens: int("output_tokens").notNull(),
  84. reasoningTokens: int("reasoning_tokens"),
  85. cacheReadTokens: int("cache_read_tokens"),
  86. cacheWrite5mTokens: int("cache_write_5m_tokens"),
  87. cacheWrite1hTokens: int("cache_write_1h_tokens"),
  88. cost: bigint("cost", { mode: "number" }).notNull(),
  89. keyID: ulid("key_id"),
  90. enrichment: json("enrichment").$type<{
  91. plan: "sub"
  92. }>(),
  93. },
  94. (table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
  95. )