Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/openstatusHQ/openstatus. ๐ซ Status page with uptime monitoring & API monitoring as code ๐ซ openstatus.dev
bun drizzle-orm monitoring monitoring-as-code nextjs observability on-call open-source shadcn-ui status-page statuspage synthetic-monitoring tinybird turso uptime uptime-checker uptime-monitor
Something went wrong. Try again.
2.2 kB ยท 71 lines
TypeScript
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172import { eq } from "@openstatus/db";import { selectWorkspaceSchema, workspace as workspaceTable,} from "@openstatus/db/src/schema";import { getLimits } from "@openstatus/db/src/schema/plan/utils";
import type { DB } from "./context";import { LimitExceededError, NotFoundError } from "./errors";
/** * Re-exported plan-defaults lookup. For per-workspace overrides, read * `ctx.workspace.limits` directly (already parsed by selectWorkspaceSchema). */export const getPlanLimits = getLimits;
// Count-style limits โ the subset of Plan Limits that represent numeric row// counts enforceable via COUNT(*) + delta. Heterogeneous fields (booleans,// arrays, "Unlimited"|number) are intentionally excluded.export type LimitKey = | "monitors" | "status-pages" | "page-components" | "notification-channels" | "synthetic-checks";
/** * Count currently-used resources of `limit` kind for the given workspace. * Cases are filled in per-domain migration PR. Unknown keys throw โ the default * branch is a drift signal: a new LimitKey was added without a counter. */async function countCurrent( _tx: DB, _workspaceId: number, limit: LimitKey,): Promise<number> { switch (limit) { // Per-domain cases land in PR 1+ as each domain migrates. default: throw new Error( `assertWithinLimit: counter for "${limit}" not implemented. Add a case in countCurrent in packages/services/src/limits.ts.`, ); }}
export async function assertWithinLimit(args: { tx: DB; workspaceId: number; limit: LimitKey; delta?: number;}): Promise<void> { const { tx, workspaceId, limit, delta = 1 } = args;
const row = await tx .select() .from(workspaceTable) .where(eq(workspaceTable.id, workspaceId)) .get(); if (!row) throw new NotFoundError("workspace", workspaceId);
const fresh = selectWorkspaceSchema.parse(row); const max = fresh.limits[limit]; if (typeof max !== "number") { throw new Error( `assertWithinLimit: limit "${limit}" resolved to non-numeric value`, ); }
const count = await countCurrent(tx, workspaceId, limit); if (count + delta > max) throw new LimitExceededError(limit, max);}