-
-
Notifications
You must be signed in to change notification settings - Fork 40
⭐ feat(core): Add usage billing #268
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Large diffs are not rendered by default.
Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
@@ -0,0 +1,15 @@ | ||||||||||||||||||||||||||||||||||||||||
import { bigint, pgEnum, pgTable } from "drizzle-orm/pg-core"; | ||||||||||||||||||||||||||||||||||||||||
import { teamID, timestamps } from "../drizzle/types"; | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const CreditsType = ["gpu", "bandwidth", "storage"] as const; | ||||||||||||||||||||||||||||||||||||||||
export const creditsEnum = pgEnum('credits_type', CreditsType); | ||||||||||||||||||||||||||||||||||||||||
|
||||||||||||||||||||||||||||||||||||||||
export const usage = pgTable( | ||||||||||||||||||||||||||||||||||||||||
"usage", | ||||||||||||||||||||||||||||||||||||||||
{ | ||||||||||||||||||||||||||||||||||||||||
...teamID, | ||||||||||||||||||||||||||||||||||||||||
...timestamps, | ||||||||||||||||||||||||||||||||||||||||
type: creditsEnum("type").notNull(), | ||||||||||||||||||||||||||||||||||||||||
creditsUsed: bigint("credits_used", { mode: "number" }).notNull(), | ||||||||||||||||||||||||||||||||||||||||
} | ||||||||||||||||||||||||||||||||||||||||
) | ||||||||||||||||||||||||||||||||||||||||
Comment on lines
+7
to
+15
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 🛠️ Refactor suggestion Consider adding references to related entities The usage table doesn't include references to the user, game, or session that generated the usage. Consider adding foreign key columns to establish these relationships. export const usage = pgTable(
"usage",
{
...teamID,
...timestamps,
type: creditsEnum("type").notNull(),
creditsUsed: bigint("credits_used", { mode: "number" }).notNull(),
+ // Reference to the user who generated this usage
+ userID: text("user_id").notNull().references(() => user.id),
+ // References to related entities (uncomment when implemented)
+ // gameID: text("game_id").references(() => game.id),
+ // sessionID: text("session_id").references(() => session.id),
}
) Add a primary key to the usage table The export const usage = pgTable(
"usage",
{
+ id: text("id").primaryKey(),
...teamID,
...timestamps,
type: creditsEnum("type").notNull(),
creditsUsed: bigint("credits_used", { mode: "number" }).notNull(),
}
) 📝 Committable suggestion
Suggested change
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
import { z } from "zod"; | ||
import { Common } from "../common"; | ||
import { Examples } from "../examples"; | ||
import { CreditsType } from "./billing.sql"; | ||
|
||
export namespace Billing { | ||
export const Info = z.object({ | ||
id: z.string().openapi({ | ||
description: Common.IdDescription, | ||
example: Examples.Usage.id, | ||
}), | ||
creditsUsed: z.number().openapi({ | ||
description: "The credits used", | ||
example: Examples.Usage.creditsUsed | ||
}), | ||
type: z.enum(CreditsType).openapi({ | ||
description: "The type of credits this was billed on" | ||
}), | ||
// game: | ||
// session: | ||
}) | ||
.openapi({ | ||
ref: "Billing", | ||
description: "Represents a usage billing", | ||
example: Examples.Usage, | ||
}); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,15 +1,16 @@ | ||
import { teamIndexes } from "../team/team.sql"; | ||
import { timestamps, utc, teamID } from "../drizzle/types"; | ||
import { index, pgTable, text, uniqueIndex, varchar } from "drizzle-orm/pg-core"; | ||
import { index, pgEnum, pgTable, text, uniqueIndex, varchar } from "drizzle-orm/pg-core"; | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 💡 Verification agent 🧩 Analysis chainGood improvement using PostgreSQL native enum type. Converting from a text column with validation to a native PostgreSQL enum type improves type safety and database integrity. However, this change requires a database migration strategy to handle existing data. Make sure you have a migration plan in place for this schema change. Existing records with Also applies to: 6-6, 13-13 🏁 Script executed: #!/bin/bash
# Description: Check if migration files exist for the member table role column change
echo "Looking for migration files related to member table:"
fd -t f "member.*migration" -E "*.md"
echo "Checking if there are any drizzle migration files:"
fd -t f "migration" -E "*.md" | grep -i "member\|role" || echo "No specific member role migration files found" Length of output: 421 Add migration for member.role enum change We didn’t find any existing migration files handling the conversion of the
Once you’ve added and tested this migration, the enum change in |
||
|
||
export const role = ["admin", "member", "owner"] as const; | ||
const pgRole = pgEnum("role", role) | ||
|
||
export const memberTable = pgTable( | ||
"member", | ||
{ | ||
...teamID, | ||
...timestamps, | ||
role: text("role", { enum: role }).notNull(), | ||
role: pgRole().notNull(), | ||
timeSeen: utc("time_seen"), | ||
email: varchar("email", { length: 255 }).notNull(), | ||
}, | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
💡 Verification agent
🧩 Analysis chain
Verify dependency versions.
Adding the ingestion package and updating the SDK is aligned with adding billing functionality. However, it's worth verifying these are the most appropriate versions for your needs.
🏁 Script executed:
Length of output: 526
Update @polar-sh/sdk to the latest patch release
Based on the npm registry and GitHub releases:
Please bump the SDK dependency to pick up the newest patch:
📝 Committable suggestion