- 添加3个复合索引提升查询性能 (user_date, user_category, destination) - 添加GZIP压缩和静态资源缓存 (maxAge: 1h) - 添加API响应no-cache头 - 添加stats端点2分钟内存缓存 (per-user key, LRU淘汰) - 合并多次数据库查询为单次查询 - 添加滚动防抖改善移动端体验 - 修复res.once内存泄漏问题 Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
96 lines
3.2 KiB
JavaScript
96 lines
3.2 KiB
JavaScript
const Database = require('better-sqlite3');
|
|
const bcrypt = require('bcryptjs');
|
|
const path = require('path');
|
|
|
|
const db = new Database(path.join(__dirname, '..', 'data.db'));
|
|
|
|
db.pragma('journal_mode = WAL');
|
|
db.pragma('foreign_keys = ON');
|
|
|
|
// 创建表
|
|
db.exec(`
|
|
CREATE TABLE IF NOT EXISTS users (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
username TEXT UNIQUE NOT NULL,
|
|
password TEXT NOT NULL,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP
|
|
);
|
|
|
|
CREATE TABLE IF NOT EXISTS records (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
type TEXT NOT NULL CHECK(type IN ('income', 'expense')),
|
|
category TEXT NOT NULL,
|
|
amount REAL NOT NULL,
|
|
quantity INTEGER,
|
|
unit_price REAL,
|
|
boss TEXT,
|
|
partner TEXT,
|
|
source TEXT,
|
|
destination TEXT,
|
|
note TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
);
|
|
|
|
CREATE INDEX IF NOT EXISTS idx_records_user_id ON records(user_id);
|
|
CREATE INDEX IF NOT EXISTS idx_records_category ON records(category);
|
|
CREATE INDEX IF NOT EXISTS idx_records_created_at ON records(created_at);
|
|
CREATE INDEX IF NOT EXISTS idx_records_type ON records(type);
|
|
CREATE INDEX IF NOT EXISTS idx_records_boss ON records(boss);
|
|
CREATE INDEX IF NOT EXISTS idx_records_partner ON records(partner);
|
|
CREATE INDEX IF NOT EXISTS idx_records_source ON records(source);
|
|
CREATE INDEX IF NOT EXISTS idx_records_user_date ON records(user_id, created_at);
|
|
CREATE INDEX IF NOT EXISTS idx_records_user_category ON records(user_id, category, created_at);
|
|
CREATE INDEX IF NOT EXISTS idx_records_destination ON records(destination);
|
|
`);
|
|
|
|
// 迁移:去掉 category 的 CHECK 约束
|
|
try {
|
|
const tableInfo = db.prepare("SELECT sql FROM sqlite_master WHERE type='table' AND name='records'").get();
|
|
if (tableInfo && tableInfo.sql.includes("CHECK(category IN")) {
|
|
db.exec(`
|
|
CREATE TABLE records_new (
|
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
user_id INTEGER NOT NULL,
|
|
type TEXT NOT NULL CHECK(type IN ('income', 'expense')),
|
|
category TEXT NOT NULL,
|
|
amount REAL NOT NULL,
|
|
quantity INTEGER,
|
|
unit_price REAL,
|
|
boss TEXT,
|
|
partner TEXT,
|
|
source TEXT,
|
|
destination TEXT,
|
|
note TEXT,
|
|
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
|
|
FOREIGN KEY (user_id) REFERENCES users(id)
|
|
);
|
|
INSERT INTO records_new SELECT * FROM records;
|
|
DROP TABLE records;
|
|
ALTER TABLE records_new RENAME TO records;
|
|
`);
|
|
}
|
|
} catch (e) {
|
|
console.log('Migration skipped:', e.message);
|
|
}
|
|
|
|
// 迁移:添加 rebate 列(派单返点)
|
|
try {
|
|
const cols = db.prepare("PRAGMA table_info(records)").all();
|
|
if (!cols.find(c => c.name === 'rebate')) {
|
|
db.exec("ALTER TABLE records ADD COLUMN rebate REAL");
|
|
}
|
|
} catch (e) {
|
|
console.log('Rebate migration skipped:', e.message);
|
|
}
|
|
|
|
// 初始化默认用户 lizhilun
|
|
const existingUser = db.prepare('SELECT id FROM users WHERE username = ?').get('lizhilun');
|
|
if (!existingUser) {
|
|
const hash = bcrypt.hashSync('lizhilun', 10);
|
|
db.prepare('INSERT INTO users (username, password) VALUES (?, ?)').run('lizhilun', hash);
|
|
}
|
|
|
|
module.exports = db;
|