Files
yx-nav/backend/src/database.js
T
Agent 2b8f7d9947 feat: 后端SQLite→MySQL改造,数据库110.42.189.169:9109
- 使用 mysql2 pool 连接外部 MySQL
- 初始化表结构 + 默认管理员 + 示例数据
- 清理 unused routes, 移走 db 文件
2026-07-14 15:07:35 +08:00

57 lines
4.1 KiB
JavaScript

import mysql from 'mysql2/promise';
export const pool = mysql.createPool({
host: process.env.DB_HOST || '110.42.189.169',
port: Number(process.env.DB_PORT) || 9109,
user: process.env.DB_USER || 'yx-nav',
password: process.env.DB_PASS || 'yx-nav',
database: process.env.DB_NAME || 'yx-nav',
waitForConnections: true,
connectionLimit: 10,
queueLimit: 0,
});
export async function initDB() {
const conn = await pool.getConnection();
try {
await conn.query('CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY AUTO_INCREMENT, username VARCHAR(50) UNIQUE NOT NULL, password VARCHAR(255) NOT NULL, nickname VARCHAR(50) DEFAULT "管理员", role VARCHAR(20) DEFAULT "admin", created_at DATETIME DEFAULT CURRENT_TIMESTAMP)');
await conn.query('CREATE TABLE IF NOT EXISTS categories (id INTEGER PRIMARY KEY AUTO_INCREMENT, name VARCHAR(50) NOT NULL, icon VARCHAR(50) DEFAULT "🔗", sort INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP)');
await conn.query('CREATE TABLE IF NOT EXISTS links (id INTEGER PRIMARY KEY AUTO_INCREMENT, title VARCHAR(100) NOT NULL, url VARCHAR(255) NOT NULL, category_id INTEGER NOT NULL, description TEXT, icon_url VARCHAR(255), sort INTEGER DEFAULT 0, is_show TINYINT DEFAULT 1, views INTEGER DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, FOREIGN KEY (category_id) REFERENCES categories(id) ON DELETE CASCADE)');
await conn.query('CREATE TABLE IF NOT EXISTS settings (id INTEGER PRIMARY KEY AUTO_INCREMENT, key VARCHAR(50) UNIQUE NOT NULL, value TEXT NOT NULL, description VARCHAR(100) DEFAULT "")');
const [hasAdmin] = await conn.query("SELECT 1 FROM users WHERE username = 'admin'");
if (hasAdmin.length === 0) {
import('bcryptjs').then(({ default: bcrypt }) => {
conn.query("INSERT INTO users (username, password, nickname) VALUES (?, ?, ?)", ['admin', bcrypt.hashSync('admin123', 10), '管理员']);
});
}
const [hasCat] = await conn.query("SELECT 1 FROM categories WHERE name = '常用网站'");
if (hasCat.length === 0) {
await conn.query("INSERT INTO categories (name, icon, sort) VALUES ('常用网站','🌟',0)");
await conn.query("INSERT INTO categories (name, icon, sort) VALUES ('开发工具','💻',1)");
await conn.query("INSERT INTO categories (name, icon, sort) VALUES ('新闻资讯','📰',2)");
await conn.query("INSERT INTO categories (name, icon, sort) VALUES ('娱乐休闲','🎮',3)");
await conn.query("INSERT INTO categories (name, icon, sort) VALUES ('学习资源','📚',4)");
}
const [hasLinks] = await conn.query('SELECT 1 FROM links');
if (hasLinks.length === 0) {
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('百度','https://www.baidu.com','全球最大的中文搜索引擎',1,0)");
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('GitHub','https://github.com','全球最大的代码托管平台',2,0)");
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('掘金','https://juejin.cn','技术社区',2,1)");
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('知乎','https://www.zhihu.com','问答社区',3,0)");
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('Bilibili','https://www.bilibili.com','视频网站',4,0)");
await conn.query("INSERT INTO links (title,url,description,category_id,sort) VALUES ('慕课网','https://www.imooc.com','在线学习平台',5,0)");
}
await conn.query("INSERT IGNORE INTO settings (key,value) VALUES ('site_name','言逍导航')");
await conn.query("INSERT IGNORE INTO settings (key,value) VALUES ('site_description','精心整理的网址导航')");
await conn.query("INSERT IGNORE INTO settings (key,value) VALUES ('theme_color','#6366F1')");
await conn.query("INSERT IGNORE INTO settings (key,value) VALUES ('layout_mode','grid')");
await conn.query("INSERT IGNORE INTO settings (key,value) VALUES ('footer_text','© 2026 言逍导航')");
} finally {
conn.release();
}
}