fix: auto-create data/ directory in connection.ts
This commit is contained in:
1 parent
4656203e4d
commit
ecec178400
1 file changed
+6
-1
@@ -1,14 +1,19 @@
|
|||||||
import Database from 'better-sqlite3'
|
import Database from 'better-sqlite3'
|
||||||
|
import fs from 'fs'
|
||||||
import path from 'path'
|
import path from 'path'
|
||||||
import { fileURLToPath } from 'url'
|
import { fileURLToPath } from 'url'
|
||||||
|
|
||||||
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
const __dirname = path.dirname(fileURLToPath(import.meta.url))
|
||||||
const DB_PATH = path.resolve(__dirname, '../../data/production.db')
|
const DATA_DIR = path.resolve(__dirname, '../../data')
|
||||||
|
const DB_PATH = path.join(DATA_DIR, 'production.db')
|
||||||
|
|
||||||
let db: Database.Database | null = null
|
let db: Database.Database | null = null
|
||||||
|
|
||||||
export function getDb(): Database.Database {
|
export function getDb(): Database.Database {
|
||||||
if (!db) {
|
if (!db) {
|
||||||
|
if (!fs.existsSync(DATA_DIR)) {
|
||||||
|
fs.mkdirSync(DATA_DIR, { recursive: true })
|
||||||
|
}
|
||||||
db = new Database(DB_PATH)
|
db = new Database(DB_PATH)
|
||||||
db.pragma('journal_mode = WAL')
|
db.pragma('journal_mode = WAL')
|
||||||
db.pragma('foreign_keys = ON')
|
db.pragma('foreign_keys = ON')
|
||||||
|
|||||||
Reference in new issue
Block a user