cloudaxe-opencode/packages/console/core/src/schema/billing.sql.ts

94 lines
3.4 KiB
TypeScript
Raw Normal View History

2026-01-13 22:51:20 +00:00
import { bigint, boolean, index, int, json, mysqlEnum, mysqlTable, uniqueIndex, varchar } from "drizzle-orm/mysql-core"
2025-10-09 02:33:20 +00:00
import { timestamps, ulid, utc, workspaceColumns } from "../drizzle/types"
2025-08-08 17:22:54 +00:00
import { workspaceIndexes } from "./workspace.sql"
2025-09-02 07:14:56 +00:00
export const BillingTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"billing",
{
...workspaceColumns,
...timestamps,
customerID: varchar("customer_id", { length: 255 }),
paymentMethodID: varchar("payment_method_id", { length: 255 }),
2025-10-16 18:59:44 +00:00
paymentMethodType: varchar("payment_method_type", { length: 32 }),
2025-08-08 17:22:54 +00:00
paymentMethodLast4: varchar("payment_method_last4", { length: 4 }),
balance: bigint("balance", { mode: "number" }).notNull(),
2025-09-15 18:48:00 +00:00
monthlyLimit: int("monthly_limit"),
monthlyUsage: bigint("monthly_usage", { mode: "number" }),
timeMonthlyUsageUpdated: utc("time_monthly_usage_updated"),
2025-08-08 17:22:54 +00:00
reload: boolean("reload"),
2025-10-31 18:42:18 +00:00
reloadTrigger: int("reload_trigger"),
reloadAmount: int("reload_amount"),
2025-09-15 18:48:00 +00:00
reloadError: varchar("reload_error", { length: 255 }),
timeReloadError: utc("time_reload_error"),
timeReloadLockedTill: utc("time_reload_locked_till"),
2026-01-07 00:40:36 +00:00
subscriptionID: varchar("subscription_id", { length: 28 }),
2026-01-09 04:44:11 +00:00
subscriptionCouponID: varchar("subscription_coupon_id", { length: 28 }),
2026-01-13 22:51:20 +00:00
subscriptionPlan: mysqlEnum("subscription_plan", ["20", "100", "200"] as const),
timeSubscriptionBooked: utc("time_subscription_booked"),
2025-08-08 17:22:54 +00:00
},
2026-01-07 06:55:36 +00:00
(table) => [
...workspaceIndexes(table),
uniqueIndex("global_customer_id").on(table.customerID),
uniqueIndex("global_subscription_id").on(table.subscriptionID),
],
2025-08-08 17:22:54 +00:00
)
2026-01-09 00:24:20 +00:00
export const SubscriptionTable = mysqlTable(
"subscription",
{
...workspaceColumns,
...timestamps,
userID: ulid("user_id").notNull(),
rollingUsage: bigint("rolling_usage", { mode: "number" }),
fixedUsage: bigint("fixed_usage", { mode: "number" }),
timeRollingUpdated: utc("time_rolling_updated"),
timeFixedUpdated: utc("time_fixed_updated"),
},
(table) => [...workspaceIndexes(table), uniqueIndex("workspace_user_id").on(table.workspaceID, table.userID)],
)
2025-09-02 07:14:56 +00:00
export const PaymentTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"payment",
{
...workspaceColumns,
...timestamps,
customerID: varchar("customer_id", { length: 255 }),
2025-09-24 23:09:23 +00:00
invoiceID: varchar("invoice_id", { length: 255 }),
2025-08-08 17:22:54 +00:00
paymentID: varchar("payment_id", { length: 255 }),
amount: bigint("amount", { mode: "number" }).notNull(),
2025-09-23 21:58:26 +00:00
timeRefunded: utc("time_refunded"),
2026-01-09 04:44:11 +00:00
enrichment: json("enrichment").$type<
| {
type: "subscription"
couponID?: string
}
| {
type: "credit"
}
>(),
2025-08-08 17:22:54 +00:00
},
(table) => [...workspaceIndexes(table)],
)
2025-09-02 07:14:56 +00:00
export const UsageTable = mysqlTable(
2025-08-08 17:22:54 +00:00
"usage",
{
...workspaceColumns,
...timestamps,
model: varchar("model", { length: 255 }).notNull(),
2025-09-12 15:57:12 +00:00
provider: varchar("provider", { length: 255 }).notNull(),
2025-09-02 07:14:56 +00:00
inputTokens: int("input_tokens").notNull(),
outputTokens: int("output_tokens").notNull(),
reasoningTokens: int("reasoning_tokens"),
cacheReadTokens: int("cache_read_tokens"),
2025-09-12 15:57:12 +00:00
cacheWrite5mTokens: int("cache_write_5m_tokens"),
cacheWrite1hTokens: int("cache_write_1h_tokens"),
2025-08-08 17:22:54 +00:00
cost: bigint("cost", { mode: "number" }).notNull(),
2025-10-09 02:33:20 +00:00
keyID: ulid("key_id"),
2026-01-07 04:34:10 +00:00
enrichment: json("enrichment").$type<{
2026-01-07 00:40:36 +00:00
plan: "sub"
}>(),
2025-08-08 17:22:54 +00:00
},
2026-01-05 03:46:21 +00:00
(table) => [...workspaceIndexes(table), index("usage_time_created").on(table.workspaceID, table.timeCreated)],
2025-08-08 17:22:54 +00:00
)