|
| 1 | +/** |
| 2 | + * @file src/db/schema.ts |
| 3 | + * @description Drizzle ORM schema definitions for all D1 tables |
| 4 | + * @owner AI-Builder |
| 5 | + */ |
| 6 | + |
| 7 | +import { sqliteTable, text, integer, real, index, uniqueIndex } from 'drizzle-orm/sqlite-core' |
| 8 | + |
| 9 | +/** |
| 10 | + * Request logs table - stores HTTP request logs for monitoring |
| 11 | + */ |
| 12 | +export const requestLogs = sqliteTable('request_logs', { |
| 13 | + id: integer('id').primaryKey({ autoIncrement: true }), |
| 14 | + timestamp: text('timestamp').notNull(), |
| 15 | + level: text('level').notNull(), |
| 16 | + message: text('message').notNull(), |
| 17 | + method: text('method').notNull(), |
| 18 | + path: text('path').notNull(), |
| 19 | + status: integer('status').notNull(), |
| 20 | + latencyMs: integer('latency_ms').notNull(), |
| 21 | + payloadSizeBytes: integer('payload_size_bytes').notNull(), |
| 22 | + correlationId: text('correlation_id').notNull(), |
| 23 | + metadata: text('metadata'), |
| 24 | +}, (table) => ({ |
| 25 | + timestampIdx: index('idx_request_logs_timestamp').on(table.timestamp), |
| 26 | + correlationIdIdx: index('idx_request_logs_correlation_id').on(table.correlationId), |
| 27 | +})) |
| 28 | + |
| 29 | +/** |
| 30 | + * Sessions table - stores user session information for agent workflows |
| 31 | + */ |
| 32 | +export const sessions = sqliteTable('sessions', { |
| 33 | + id: integer('id').primaryKey({ autoIncrement: true }), |
| 34 | + sessionId: text('session_id').unique(), |
| 35 | + prompt: text('prompt'), |
| 36 | + createdAt: text('created_at').default('CURRENT_TIMESTAMP'), |
| 37 | +}) |
| 38 | + |
| 39 | +/** |
| 40 | + * Searches table - stores search term information for repository discovery |
| 41 | + */ |
| 42 | +export const searches = sqliteTable('searches', { |
| 43 | + id: integer('id').primaryKey({ autoIncrement: true }), |
| 44 | + sessionId: text('session_id').references(() => sessions.sessionId), |
| 45 | + searchTerm: text('search_term'), |
| 46 | + status: text('status').default('pending'), |
| 47 | + createdAt: text('created_at').default('CURRENT_TIMESTAMP'), |
| 48 | +}) |
| 49 | + |
| 50 | +/** |
| 51 | + * Repository analysis table - stores AI analysis results for discovered repositories |
| 52 | + */ |
| 53 | +export const repoAnalysis = sqliteTable('repo_analysis', { |
| 54 | + id: integer('id').primaryKey({ autoIncrement: true }), |
| 55 | + sessionId: text('session_id').references(() => sessions.sessionId), |
| 56 | + searchId: integer('search_id').references(() => searches.id), |
| 57 | + repoFullName: text('repo_full_name'), |
| 58 | + repoUrl: text('repo_url'), |
| 59 | + description: text('description'), |
| 60 | + relevancyScore: real('relevancy_score'), |
| 61 | + analyzedAt: text('analyzed_at').default('CURRENT_TIMESTAMP'), |
| 62 | +}, (table) => ({ |
| 63 | + // Unique constraint on session_id and repo_full_name combination |
| 64 | + sessionRepoUnique: uniqueIndex('repo_analysis_session_id_repo_full_name_unique').on( |
| 65 | + table.sessionId, |
| 66 | + table.repoFullName |
| 67 | + ), |
| 68 | +})) |
| 69 | + |
| 70 | +/** |
| 71 | + * GitHub management config table - stores GitHub workflow retrofit operations |
| 72 | + */ |
| 73 | +export const ghManagementConfig = sqliteTable('gh_management_config', { |
| 74 | + id: integer('id').primaryKey({ autoIncrement: true }), |
| 75 | + timestamp: text('timestamp').notNull().default('CURRENT_TIMESTAMP'), |
| 76 | + repoName: text('repo_name').notNull(), |
| 77 | + action: text('action').notNull(), |
| 78 | + status: text('status').notNull(), |
| 79 | + statusDetails: text('status_details'), |
| 80 | + createdAt: text('created_at').default('CURRENT_TIMESTAMP'), |
| 81 | +}, (table) => ({ |
| 82 | + timestampIdx: index('idx_gh_management_config_timestamp').on(table.timestamp), |
| 83 | + repoNameIdx: index('idx_gh_management_config_repo_name').on(table.repoName), |
| 84 | + actionIdx: index('idx_gh_management_config_action').on(table.action), |
| 85 | + statusIdx: index('idx_gh_management_config_status').on(table.status), |
| 86 | +})) |
0 commit comments