|
5 | 5 | ListToolsRequestSchema,
|
6 | 6 | ToolSchema,
|
7 | 7 | } from '@modelcontextprotocol/sdk/types.js';
|
8 |
| -import { Database } from 'sqlite3'; |
| 8 | +import sqlite3 from 'sqlite3'; |
9 | 9 | import { z } from 'zod';
|
10 | 10 | import { zodToJsonSchema } from 'zod-to-json-schema';
|
11 | 11 | import path from 'path';
|
@@ -44,33 +44,60 @@ const AppendInsightArgsSchema = z.object({
|
44 | 44 | .describe('Business insight discovered from data analysis'),
|
45 | 45 | });
|
46 | 46 |
|
| 47 | +interface RunResult { |
| 48 | + affectedRows: number; |
| 49 | +} |
| 50 | + |
| 51 | +/** |
| 52 | + * Wrapper for sqlite3.Database that bridges CommonJS and ESM modules. |
| 53 | + * This abstraction is necessary because: |
| 54 | + * 1. sqlite3 is a CommonJS module while we're using ESM (type: "module") |
| 55 | + * 2. The module interop requires careful handling of the Database import |
| 56 | + * 3. We need to promisify the callback-based API to work better with async/await |
| 57 | + */ |
| 58 | +class DatabaseWrapper { |
| 59 | + private readonly db: sqlite3.Database; |
| 60 | + |
| 61 | + constructor(filename: string) { |
| 62 | + this.db = new sqlite3.Database(filename); |
| 63 | + } |
| 64 | + |
| 65 | + query(sql: string, params: any[] = []): Promise<any[]> { |
| 66 | + return new Promise((resolve, reject) => { |
| 67 | + this.db.all(sql, params, (err: Error | null, rows: any[]) => { |
| 68 | + if (err) reject(err); |
| 69 | + else resolve(rows); |
| 70 | + }); |
| 71 | + }); |
| 72 | + } |
| 73 | + |
| 74 | + execute(sql: string, params: any[] = []): Promise<RunResult[]> { |
| 75 | + return new Promise((resolve, reject) => { |
| 76 | + this.db.run( |
| 77 | + sql, |
| 78 | + params, |
| 79 | + function (this: sqlite3.RunResult, err: Error | null) { |
| 80 | + if (err) reject(err); |
| 81 | + else resolve([{ affectedRows: this.changes }]); |
| 82 | + }, |
| 83 | + ); |
| 84 | + }); |
| 85 | + } |
| 86 | +} |
| 87 | + |
47 | 88 | class SqliteDatabase {
|
48 |
| - private readonly db: Database; |
| 89 | + private readonly db: DatabaseWrapper; |
49 | 90 | private readonly insights: string[] = [];
|
50 | 91 |
|
51 | 92 | constructor(dbPath: string) {
|
52 |
| - this.db = new Database(dbPath); |
| 93 | + this.db = new DatabaseWrapper(dbPath); |
53 | 94 | }
|
54 | 95 |
|
55 | 96 | private async query<T>(
|
56 | 97 | sql: string,
|
57 | 98 | params: any[] = [],
|
58 | 99 | ): Promise<T[]> {
|
59 |
| - return new Promise((resolve, reject) => { |
60 |
| - const isSelect = sql.trim().toUpperCase().startsWith('SELECT'); |
61 |
| - |
62 |
| - if (isSelect) { |
63 |
| - this.db.all(sql, params, (err, rows) => { |
64 |
| - if (err) reject(err); |
65 |
| - else resolve(rows as T[]); |
66 |
| - }); |
67 |
| - } else { |
68 |
| - this.db.run(sql, params, function (err) { |
69 |
| - if (err) reject(err); |
70 |
| - else resolve([{ affectedRows: this.changes } as any]); |
71 |
| - }); |
72 |
| - } |
73 |
| - }); |
| 100 | + return this.db.query(sql, params); |
74 | 101 | }
|
75 | 102 |
|
76 | 103 | synthesizeMemo(): string {
|
|
0 commit comments