fix: auto-create database directory using fs and path
This commit is contained in:
@@ -1,8 +1,15 @@
|
|||||||
import Database from 'better-sqlite3';
|
import Database from 'better-sqlite3';
|
||||||
import bcrypt from 'bcryptjs';
|
import bcrypt from 'bcryptjs';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
const dbPath = path.join(process.cwd(), 'data/database.sqlite');
|
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 db = new Database(dbPath, { verbose: console.log });
|
||||||
|
|
||||||
const createTables = (): void => {
|
const createTables = (): void => {
|
||||||
|
|||||||
@@ -1,5 +1,6 @@
|
|||||||
import Database from 'better-sqlite3';
|
import Database from 'better-sqlite3';
|
||||||
import path from 'path';
|
import path from 'path';
|
||||||
|
import fs from 'fs';
|
||||||
|
|
||||||
class DatabaseWrapper {
|
class DatabaseWrapper {
|
||||||
private db: Database.Database;
|
private db: Database.Database;
|
||||||
@@ -7,6 +8,12 @@ class DatabaseWrapper {
|
|||||||
|
|
||||||
constructor() {
|
constructor() {
|
||||||
this.dbPath = process.env.DB_PATH || path.join(process.cwd(), 'data/database.sqlite');
|
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 });
|
this.db = new Database(this.dbPath, { verbose: console.log });
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user