billing.sql.ts 3.2 KB

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