Files
gitea-jira-task-bot/src/db/connection.js
2026-01-29 15:38:49 +08:00

48 lines
1.2 KiB
JavaScript

const Database = require('better-sqlite3');
const config = require('../config/env');
const path = require('path');
const logger = require('../utils/logger');
//自动创建数据库连接
const dbPath = config.app.dbPath || path.join(__dirname, '../../sync.sqlite');
const db = new Database(dbPath);
db.exec(`
CREATE TABLE IF NOT EXISTS issue_mapping (
id INTEGER PRIMARY KEY AUTOINCREMENT,
repo_key TEXT NOT NULL, -- 仓库标识 (owner/repo格式)
gitea_id INTEGER NOT NULL, -- Gitea Issue Number
jira_key TEXT NOT NULL, -- Jira Key (e.g., LTM-123)
jira_id TEXT NOT NULL, -- Jira Internal ID
UNIQUE(repo_key, gitea_id) -- 同一仓库的Issue不能重复
)
`);
const cleanup = () => {
try {
logger.info('Closing database connection...');
db.close();
logger.info('Database connection closed');
} catch (err) {
logger.error('Failed to close database', err.message);
}
};
process.on('SIGINT', () => {
cleanup();
process.exit(0);
});
process.on('SIGTERM', () => {
cleanup();
process.exit(0);
});
process.on('exit', () => {
if (db.open) {
cleanup();
}
});
logger.info(`Database connected at ${dbPath}`);
module.exports = db;