refactor: migrate entire project to TypeScript

This commit is contained in:
2026-02-06 14:44:14 +08:00
parent e98dbcb0f4
commit a80c479027
14 changed files with 1141 additions and 462 deletions

View File

@@ -1,50 +1,49 @@
const Database = require('better-sqlite3');
const path = require('path');
import Database from 'better-sqlite3';
import path from 'path';
class DatabaseWrapper {
private db: Database.Database;
private dbPath: string;
constructor() {
this.dbPath = process.env.DB_PATH || path.join(__dirname, '../data/database.sqlite');
this.db = new Database(this.dbPath, { verbose: console.log });
}
// 查询单个记录
get(sql, params = []) {
get<T = any>(sql: string, params: any[] = []): T | undefined {
try {
const stmt = this.db.prepare(sql);
return stmt.get(params);
return stmt.get(params) as T | undefined;
} catch (error) {
console.error('数据库查询错误:', error);
throw error;
}
}
// 查询多个记录
all(sql, params = []) {
all<T = any>(sql: string, params: any[] = []): T[] {
try {
const stmt = this.db.prepare(sql);
return stmt.all(params);
return stmt.all(params) as T[];
} catch (error) {
console.error('数据库查询错误:', error);
throw error;
}
}
// 执行插入、更新、删除操作
run(sql, params = []) {
run(sql: string, params: any[] = []): { id: number; changes: number } {
try {
const stmt = this.db.prepare(sql);
const result = stmt.run(params);
return { id: result.lastInsertRowid, changes: result.changes };
return { id: result.lastInsertRowid as number, changes: result.changes };
} catch (error) {
console.error('数据库操作错误:', error);
throw error;
}
}
// 关闭数据库连接
close() {
close(): void {
this.db.close();
}
}
module.exports = new DatabaseWrapper();
export default new DatabaseWrapper();