|
1 | | -import type { |
2 | | - TestableDb, |
3 | | - TestableDbWhereResult, |
4 | | -} from '@codebuff/common/types/contracts/database' |
5 | 1 | import type { Logger } from '@codebuff/common/types/contracts/logger' |
6 | 2 |
|
| 3 | +// ============================================================================ |
| 4 | +// Testable Database Interface |
| 5 | +// ============================================================================ |
| 6 | + |
| 7 | +/* eslint-disable @typescript-eslint/no-explicit-any */ |
| 8 | +/** |
| 9 | + * Minimal database interface for dependency injection in API routes. |
| 10 | + * Both the real CodebuffPgDatabase and test mocks can satisfy this interface. |
| 11 | + * |
| 12 | + * This allows tests to provide mock implementations without type casting. |
| 13 | + * Uses `any` for table/column parameters to be compatible with Drizzle ORM's |
| 14 | + * specific table types while remaining flexible for mocks. |
| 15 | + */ |
| 16 | +export interface TestableDb { |
| 17 | + insert: (table: any) => { |
| 18 | + values: (data: any) => PromiseLike<any> |
| 19 | + } |
| 20 | + update: (table: any) => { |
| 21 | + set: (data: any) => { |
| 22 | + where: (condition: any) => PromiseLike<any> |
| 23 | + } |
| 24 | + } |
| 25 | + select: (columns?: any) => { |
| 26 | + from: (table: any) => { |
| 27 | + where: (condition: any) => TestableDbWhereResult |
| 28 | + } |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +/** |
| 33 | + * Result type for where() that supports multiple query patterns: |
| 34 | + * - .limit(n) for simple queries |
| 35 | + * - .orderBy(...).limit(n) for sorted queries |
| 36 | + * - .then() for promise-like resolution |
| 37 | + */ |
| 38 | +export interface TestableDbWhereResult { |
| 39 | + then: <TResult = any[]>( |
| 40 | + onfulfilled?: ((value: any[]) => TResult | PromiseLike<TResult>) | null | undefined, |
| 41 | + ) => PromiseLike<TResult> |
| 42 | + limit: (n: number) => PromiseLike<any[]> |
| 43 | + orderBy: (...columns: any[]) => { |
| 44 | + limit: (n: number) => PromiseLike<any[]> |
| 45 | + } |
| 46 | +} |
| 47 | +/* eslint-enable @typescript-eslint/no-explicit-any */ |
| 48 | + |
7 | 49 | // Compatibility layer: use bun:test mock when available, otherwise identity function |
8 | 50 | // This allows the utilities to work in both Bun and Jest environments |
9 | 51 | /* eslint-disable @typescript-eslint/no-require-imports, @typescript-eslint/no-explicit-any */ |
|
0 commit comments