dailiip/config/database.js

39 lines
1.1 KiB
JavaScript
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

const path = require('path');
const fs = require('fs');
// 数据库文件路径配置
// 在Docker环境中使用 /app/data本地开发使用项目根目录的data文件夹
let dbPath;
if (process.env.DB_PATH) {
// 如果设置了环境变量,使用环境变量的路径
dbPath = process.env.DB_PATH;
} else if (fs.existsSync('/app/data')) {
// Docker容器内使用/app/data目录
dbPath = '/app/data/proxies.db';
} else {
// 本地开发环境使用项目根目录的data文件夹
const dataDir = path.join(__dirname, '../data');
dbPath = path.join(dataDir, 'proxies.db');
// 确保data目录存在
if (!fs.existsSync(dataDir)) {
try {
fs.mkdirSync(dataDir, { recursive: true });
} catch (error) {
console.warn('无法创建data目录将使用项目根目录:', error.message);
// 如果创建失败,回退到项目根目录
dbPath = path.join(__dirname, '../proxies.db');
}
}
}
console.log(`数据库文件路径: ${dbPath}`);
const dbConfig = {
development: {
dialect: 'sqlite',
storage: dbPath
}
};
module.exports = dbConfig;