billing.sql.ts 2.0 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. import { bigint, boolean, int, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
  2. import { timestamps, 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. paymentMethodLast4: varchar("payment_method_last4", { length: 4 }),
  12. balance: bigint("balance", { mode: "number" }).notNull(),
  13. monthlyLimit: int("monthly_limit"),
  14. monthlyUsage: bigint("monthly_usage", { mode: "number" }),
  15. timeMonthlyUsageUpdated: utc("time_monthly_usage_updated"),
  16. reload: boolean("reload"),
  17. reloadError: varchar("reload_error", { length: 255 }),
  18. timeReloadError: utc("time_reload_error"),
  19. timeReloadLockedTill: utc("time_reload_locked_till"),
  20. },
  21. (table) => [...workspaceIndexes(table), uniqueIndex("global_customer_id").on(table.customerID)],
  22. )
  23. export const PaymentTable = mysqlTable(
  24. "payment",
  25. {
  26. ...workspaceColumns,
  27. ...timestamps,
  28. customerID: varchar("customer_id", { length: 255 }),
  29. invoiceID: varchar("invoice_id", { length: 255 }),
  30. paymentID: varchar("payment_id", { length: 255 }),
  31. amount: bigint("amount", { mode: "number" }).notNull(),
  32. timeRefunded: utc("time_refunded"),
  33. },
  34. (table) => [...workspaceIndexes(table)],
  35. )
  36. export const UsageTable = mysqlTable(
  37. "usage",
  38. {
  39. ...workspaceColumns,
  40. ...timestamps,
  41. model: varchar("model", { length: 255 }).notNull(),
  42. provider: varchar("provider", { length: 255 }).notNull(),
  43. inputTokens: int("input_tokens").notNull(),
  44. outputTokens: int("output_tokens").notNull(),
  45. reasoningTokens: int("reasoning_tokens"),
  46. cacheReadTokens: int("cache_read_tokens"),
  47. cacheWrite5mTokens: int("cache_write_5m_tokens"),
  48. cacheWrite1hTokens: int("cache_write_1h_tokens"),
  49. cost: bigint("cost", { mode: "number" }).notNull(),
  50. },
  51. (table) => [...workspaceIndexes(table)],
  52. )