2026-05-23 14:15:45 +08:00
|
|
|
const mysql = require('mysql2/promise')
|
2026-05-26 09:18:48 +08:00
|
|
|
require('dotenv').config()
|
2026-05-23 14:15:45 +08:00
|
|
|
|
2026-05-26 09:30:17 +08:00
|
|
|
function requireEnv(name, fallback) {
|
|
|
|
|
const value = process.env[name] || fallback
|
|
|
|
|
if (!value && !fallback) {
|
|
|
|
|
throw new Error(`Missing required environment variable: ${name}. Check .env file.`)
|
|
|
|
|
}
|
|
|
|
|
return value
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 14:15:45 +08:00
|
|
|
const config = {
|
2026-05-26 09:30:17 +08:00
|
|
|
host: requireEnv('DB_HOST', 'localhost'),
|
|
|
|
|
port: parseInt(requireEnv('DB_PORT', '3306')),
|
|
|
|
|
user: requireEnv('DB_USER', 'root'),
|
|
|
|
|
password: requireEnv('DB_PASSWORD', ''),
|
|
|
|
|
database: requireEnv('DB_NAME', 'miniprogram'),
|
2026-05-23 14:15:45 +08:00
|
|
|
waitForConnections: true,
|
|
|
|
|
connectionLimit: 10,
|
|
|
|
|
queueLimit: 0
|
|
|
|
|
}
|
2026-05-26 09:30:17 +08:00
|
|
|
|
2026-05-23 14:15:45 +08:00
|
|
|
const pool = mysql.createPool(config)
|
|
|
|
|
|
|
|
|
|
async function query(sql, params = []) {
|
|
|
|
|
const connection = await pool.getConnection()
|
|
|
|
|
try {
|
|
|
|
|
const [rows, fields] = await connection.execute(sql, params)
|
|
|
|
|
return rows
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async function initDatabase() {
|
|
|
|
|
try {
|
|
|
|
|
const connection = await mysql.createConnection({
|
|
|
|
|
host: config.host,
|
|
|
|
|
port: config.port,
|
|
|
|
|
user: config.user,
|
|
|
|
|
password: config.password,
|
|
|
|
|
multipleStatements: true
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
await connection.query(`CREATE DATABASE IF NOT EXISTS ${config.database} CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci`)
|
|
|
|
|
await connection.query(`USE ${config.database}`)
|
|
|
|
|
|
|
|
|
|
const fs = require('fs')
|
|
|
|
|
const path = require('path')
|
|
|
|
|
const schema = fs.readFileSync(path.join(__dirname, 'schema.sql'), 'utf8')
|
|
|
|
|
const statements = schema.split(';').filter(s => s.trim())
|
|
|
|
|
|
|
|
|
|
for (const statement of statements) {
|
|
|
|
|
await connection.query(statement)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await connection.end()
|
|
|
|
|
console.log('Database initialized successfully')
|
|
|
|
|
} catch (error) {
|
|
|
|
|
console.error('Failed to initialize database:', error)
|
|
|
|
|
throw error
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-26 13:37:55 +08:00
|
|
|
async function transaction(callback) {
|
|
|
|
|
const connection = await pool.getConnection()
|
|
|
|
|
try {
|
|
|
|
|
await connection.beginTransaction()
|
|
|
|
|
const result = await callback(connection)
|
|
|
|
|
await connection.commit()
|
|
|
|
|
return result
|
|
|
|
|
} catch (error) {
|
|
|
|
|
await connection.rollback()
|
|
|
|
|
throw error
|
|
|
|
|
} finally {
|
|
|
|
|
connection.release()
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-23 14:15:45 +08:00
|
|
|
module.exports = {
|
|
|
|
|
pool,
|
|
|
|
|
query,
|
2026-05-26 13:37:55 +08:00
|
|
|
transaction,
|
2026-05-23 14:15:45 +08:00
|
|
|
initDatabase,
|
|
|
|
|
config
|
|
|
|
|
}
|