/** * Audit log. Source of truth: `db/migrations/0001_init.sql`. */ import { pgTable, uuid, text, jsonb, timestamp, index, } from "drizzle-orm/pg-core"; import { tenants } from "./tenants"; import { users } from "./tenants"; export const auditLog = pgTable( "audit_log", { id: uuid("id").primaryKey().defaultRandom(), tenantId: uuid("tenant_id").references(() => tenants.id, { onDelete: "cascade", }), userId: uuid("user_id").references(() => users.id, { onDelete: "set null", }), action: text("action").notNull(), targetType: text("target_type"), targetId: uuid("target_id"), payload: jsonb("payload"), createdAt: timestamp("created_at", { withTimezone: true }) .notNull() .defaultNow(), }, (t) => ({ tenantIdx: index("audit_log_tenant_idx").on(t.tenantId, t.createdAt), }), ); export type AuditLog = typeof auditLog.$inferSelect; export type NewAuditLog = typeof auditLog.$inferInsert;