import { pgTable, text, timestamp, boolean, integer, jsonb, customType, primaryKey, } from "drizzle-orm/pg-core"; // --------------------------------------------------------------------------- // Custom type — bytea for encrypted secret storage // --------------------------------------------------------------------------- const bytea = customType<{ data: Buffer }>({ dataType() { return "bytea"; }, }); // --------------------------------------------------------------------------- // Domain data — all team-scoped // --------------------------------------------------------------------------- export const sources = pgTable( "sources", { id: text("id").notNull(), teamId: text("team_id").notNull(), name: text("name").notNull(), kind: text("kind").notNull(), config: jsonb("config").notNull().$default(() => ({})), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [primaryKey({ columns: [table.id, table.teamId] })], ); export const tools = pgTable( "tools", { id: text("id").notNull(), teamId: text("team_id").notNull(), sourceId: text("source_id").notNull(), pluginKey: text("plugin_key").notNull(), name: text("name").notNull(), description: text("description"), mayElicit: boolean("may_elicit").$default(() => false), inputSchema: jsonb("input_schema"), outputSchema: jsonb("output_schema"), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [primaryKey({ columns: [table.id, table.teamId] })], ); export const toolDefinitions = pgTable( "tool_definitions", { name: text("name").notNull(), teamId: text("team_id").notNull(), schema: jsonb("schema").notNull(), }, (table) => [primaryKey({ columns: [table.name, table.teamId] })], ); export const secrets = pgTable( "secrets", { id: text("id").notNull(), teamId: text("team_id").notNull(), name: text("name").notNull(), purpose: text("purpose"), encryptedValue: bytea("encrypted_value").notNull(), iv: bytea("iv").notNull(), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [primaryKey({ columns: [table.id, table.teamId] })], ); export const policies = pgTable( "policies", { id: text("id").notNull(), teamId: text("team_id").notNull(), name: text("name").notNull(), action: text("action").notNull(), matchToolPattern: text("match_tool_pattern"), matchSourceId: text("match_source_id"), priority: integer("priority").notNull().$default(() => 0), createdAt: timestamp("created_at", { withTimezone: true }).notNull().defaultNow(), }, (table) => [primaryKey({ columns: [table.id, table.teamId] })], ); // --------------------------------------------------------------------------- // Plugin KV — escape hatch for plugin-specific data // --------------------------------------------------------------------------- export const pluginKv = pgTable( "plugin_kv", { teamId: text("team_id").notNull(), namespace: text("namespace").notNull(), key: text("key").notNull(), value: text("value").notNull(), }, (table) => [primaryKey({ columns: [table.teamId, table.namespace, table.key] })], );