import { sql } from "drizzle-orm"; import { boolean, foreignKey, index, jsonb, numeric, pgTable, text, unique, } from "drizzle-orm/pg-core"; import { features } from "../../featureModels/featureTable.js"; import { organizations } from "../../orgModels/orgTable.js"; import type { DbOverageAllowed, DbSpendLimit, DbUsageAlert, } from "../billingControls/customerBillingControls.js"; import { customers } from "../cusTable.js"; export const entities = pgTable( "entities", { id: text(), org_id: text("org_id"), created_at: numeric({ mode: "number" }).notNull(), internal_id: text("internal_id").primaryKey().notNull(), internal_customer_id: text("internal_customer_id").notNull(), env: text(), name: text(), deleted: boolean().default(false).notNull(), internal_feature_id: text("internal_feature_id"), spend_limits: jsonb().$type(), usage_alerts: jsonb().$type(), overage_allowed: jsonb().$type(), // Optional... feature_id: text("feature_id"), }, (table) => [ foreignKey({ columns: [table.internal_customer_id], foreignColumns: [customers.internal_id], name: "entities_internal_customer_id_fkey", }).onDelete("cascade"), foreignKey({ columns: [table.internal_feature_id], foreignColumns: [features.internal_id], name: "entities_internal_feature_id_fkey", }).onDelete("cascade"), foreignKey({ columns: [table.org_id], foreignColumns: [organizations.id], name: "entities_org_id_fkey", }).onDelete("cascade"), unique("entity_id_constraint").on( table.org_id, table.env, table.internal_customer_id, table.id, ), index("idx_entities_internal_customer_id").using( "hash", table.internal_customer_id, ), index("idx_entities_customer_internal_desc").on( table.internal_customer_id, sql`${table.internal_id} DESC`, ), ], );