fix: auto-create database directory using fs and path

This commit is contained in:
2026-02-06 15:22:15 +08:00
parent c6fc32818e
commit 28543f5979
2 changed files with 14 additions and 0 deletions

View File

@@ -1,8 +1,15 @@
import Database from 'better-sqlite3';
import bcrypt from 'bcryptjs';
import path from 'path';
import fs from 'fs';
const dbPath = path.join(process.cwd(), 'data/database.sqlite');
const dbDir = path.dirname(dbPath);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
const db = new Database(dbPath, { verbose: console.log });
const createTables = (): void => {

View File

@@ -1,5 +1,6 @@
import Database from 'better-sqlite3';
import path from 'path';
import fs from 'fs';
class DatabaseWrapper {
private db: Database.Database;
@@ -7,6 +8,12 @@ class DatabaseWrapper {
constructor() {
this.dbPath = process.env.DB_PATH || path.join(process.cwd(), 'data/database.sqlite');
const dbDir = path.dirname(this.dbPath);
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
this.db = new Database(this.dbPath, { verbose: console.log });
}