Skip to content

Commit c1564e0

Browse files
refactor: migrate from sqlite3 to better-sqlite3 for improved performance and simplicity
- Updated package.json files to replace sqlite3 with better-sqlite3 in shared and web packages. - Refactored sqlite-storage.ts to utilize better-sqlite3 API, simplifying database operations and initialization. - Adjusted test-database.ts to accommodate changes in database handling with better-sqlite3. - Modified tests in sqlite-storage.test.ts to mock better-sqlite3 instead of sqlite3. - Updated next-env.d.ts to reference the correct types for Next.js.
1 parent cbce88a commit c1564e0

8 files changed

Lines changed: 298 additions & 1114 deletions

File tree

package-lock.json

Lines changed: 165 additions & 787 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,8 @@
1818
"test": "npm run test --workspace=shared && npm run test --workspace=web && npm run test --workspace=mcp-server",
1919
"test:watch": "npm run test:watch --workspace=shared",
2020
"test:coverage": "npm run test:coverage --workspace=shared && npm run test:coverage --workspace=web && npm run test:coverage --workspace=mcp-server",
21-
"lint": "eslint .",
22-
"lint:fix": "eslint . --fix",
21+
"lint": "npm run lint --workspace=shared && npm run lint --workspace=web && npm run lint --workspace=mcp-server",
22+
"lint:fix": "npm run lint:fix --workspace=shared && npm run lint:fix --workspace=web && npm run lint:fix --workspace=mcp-server",
2323
"lint:all": "npm run lint --workspace=shared && npm run lint --workspace=web && npm run lint --workspace=mcp-server",
2424
"ci:tests": "npm run test --workspace=@turn-based-mcp/shared && npm run test --workspace=@turn-based-mcp/web && npm run test --workspace=@turn-based-mcp/mcp-server",
2525
"ci": "npm run build --workspace=@turn-based-mcp/shared && npm run type-check && npm run build --workspace=@turn-based-mcp/web && npm run build --workspace=@turn-based-mcp/mcp-server && npm run ci:tests && npm run lint --workspace=@turn-based-mcp/web"

shared/package.json

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,9 @@
4444
"vitest": "^4.0.18"
4545
},
4646
"dependencies": {
47-
"sqlite3": "^5.1.7"
47+
"better-sqlite3": "^12.6.2"
48+
},
49+
"optionalDependencies": {
50+
"@types/better-sqlite3": "^7.6.13"
4851
}
4952
}

shared/src/storage/sqlite-storage.test.ts

Lines changed: 14 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,24 @@
11
import { vi } from 'vitest'
22
import * as sqliteStorage from './sqlite-storage';
33

4-
// Mock sqlite3 to prevent actual database operations
4+
// Mock better-sqlite3 to prevent actual database operations
55
// In vitest v4, mocks used as constructors must use 'function' or 'class' syntax
6-
vi.mock('sqlite3', () => ({
7-
default: {
8-
Database: vi.fn(function() {
6+
vi.mock('better-sqlite3', () => {
7+
const mockStatement = {
8+
run: vi.fn(() => ({ lastInsertRowid: 1, changes: 1 })),
9+
get: vi.fn(() => undefined),
10+
all: vi.fn(() => []),
11+
}
12+
return {
13+
default: vi.fn(function() {
914
return {
10-
run: vi.fn(function(sql: string, params: unknown, callback?: (this: { lastID: number; changes: number }) => void) {
11-
if (callback) {
12-
// Mock the 'this' context with lastID and changes
13-
callback.call({ lastID: 1, changes: 1 });
14-
}
15-
}),
16-
get: vi.fn((sql: string, params: unknown, callback?: (err: Error | null, row: unknown) => void) => {
17-
if (callback) callback(null, null);
18-
}),
19-
all: vi.fn((sql: string, params: unknown, callback?: (err: Error | null, rows: unknown[]) => void) => {
20-
if (callback) callback(null, []);
21-
}),
22-
close: vi.fn((callback?: () => void) => {
23-
if (callback) callback();
24-
}),
25-
serialize: vi.fn((fn?: () => void) => {
26-
if (fn) fn();
27-
})
28-
};
15+
prepare: vi.fn(() => mockStatement),
16+
exec: vi.fn(),
17+
close: vi.fn(),
18+
}
2919
})
3020
}
31-
}));
21+
});
3222

3323
// Integration-style tests for SQLite storage
3424
// These tests verify the public API without heavy mocking

0 commit comments

Comments
 (0)