You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 

30 lines
663 B

import knex from 'knex';
import path from 'path';
import fs from 'fs';
import config from './config';
// 确保数据库目录存在
const dbDir = path.join(process.cwd(), './database');
if (!fs.existsSync(dbDir)) {
fs.mkdirSync(dbDir, { recursive: true });
}
// 根据环境选择数据库文件
const dbFileName = config.isProduction ? 'prod.sqlite3' : 'dev.sqlite3';
const dbPath = path.join(dbDir, dbFileName);
const db = knex({
client: 'sqlite3',
connection: {
filename: dbPath
},
useNullAsDefault: true,
pool: {
afterCreate: (conn: any, cb: any) => {
conn.run('PRAGMA foreign_keys = ON', cb);
}
}
});
export default db;