billing.sql.ts 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859
  1. import { bigint, boolean, index, int, 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. },
  24. (table) => [...workspaceIndexes(table), uniqueIndex("global_customer_id").on(table.customerID)],
  25. )
  26. export const PaymentTable = mysqlTable(
  27. "payment",
  28. {
  29. ...workspaceColumns,
  30. ...timestamps,
  31. customerID: varchar("customer_id", { length: 255 }),
  32. invoiceID: varchar("invoice_id", { length: 255 }),
  33. paymentID: varchar("payment_id", { length: 255 }),
  34. amount: bigint("amount", { mode: "number" }).notNull(),
  35. timeRefunded: utc("time_refunded"),
  36. },
  37. (table) => [...workspaceIndexes(table)],
  38. )
  39. export const UsageTable = mysqlTable(
  40. "usage",
  41. {
  42. ...workspaceColumns,
  43. ...timestamps,
  44. model: varchar("model", { length: 255 }).notNull(),
  45. provider: varchar("provider", { length: 255 }).notNull(),
  46. inputTokens: int("input_tokens").notNull(),
  47. outputTokens: int("output_tokens").notNull(),
  48. reasoningTokens: int("reasoning_tokens"),
  49. cacheReadTokens: int("cache_read_tokens"),
  50. cacheWrite5mTokens: int("cache_write_5m_tokens"),
  51. cacheWrite1hTokens: int("cache_write_1h_tokens"),
  52. cost: bigint("cost", { mode: "number" }).notNull(),
  53. keyID: ulid("key_id"),
  54. },
  55. (table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
  56. )