AppDB.ts

1import sqlite3 from 'sqlite3';
2import path from 'path';
3import fs from 'fs';
4import { getAppDataDir } from '../utils/system';
5
6class AppDB {
7	private static instance: AppDB | null = null;
8	private db: sqlite3.Database;
9
10	private constructor() {
11		const dbPath = path.join(getAppDataDir(), 'app.db');
12
13		if (!fs.existsSync(dbPath)) {
14			this.db = new sqlite3.Database(
15				dbPath,
16				sqlite3.OPEN_READWRITE | sqlite3.OPEN_CREATE,
17				(err) => {
18					if (err) {
19						console.error('Error opening database:', err);
20					}
21				},
22			);
23		} else {
24			this.db = new sqlite3.Database(dbPath, sqlite3.OPEN_READWRITE, (err) => {
25				if (err) {
26					console.error('Error opening database:', err);
27				}
28			});
29		}
30	}
31
32	public static getInstance(): AppDB {
33		if (!AppDB.instance) {
34			AppDB.instance = new AppDB();
35		}
36
37		return AppDB.instance;
38	}
39
40	public close(): void {
41		this.db.close((err) => {
42			if (err) {
43				console.error('Error closing database:', err);
44			}
45		});
46	}
47
48	public static destroy(): void {
49		if (AppDB.instance) {
50			AppDB.instance.close();
51		}
52
53		AppDB.instance = null;
54	}
55
56	public getDb(): sqlite3.Database {
57		return this.db;
58	}
59
60	public async statement(query: string, params: any[] = []): Promise<any[]> {
61		return new Promise((resolve, reject) => {
62			this.db.all(query, params, (err, rows) => {
63				if (err) {
64					reject(err);
65				} else {
66					resolve(rows);
67				}
68			});
69		});
70	}
71
72	public async createDatabase(): Promise<void> {
73		try {
74			const initSQLPath = path.join(__dirname, '..', '..', 'sql');
75
76			const initFiles = fs.readdirSync(initSQLPath).filter((file) => {
77				return file.endsWith('-init.sql');
78			});
79
80			for (const file of initFiles) {
81				console.log('Creating database from file:', file);
82				const sql = fs.readFileSync(path.join(initSQLPath, file), 'utf-8');
83				await this.statement(sql);
84			}
85		} catch (error) {
86			console.error('Error creating database:', error);
87			throw error; // Rethrow the error to signal that the database creation failed
88		}
89	}
90}
91
92export default AppDB;
93