refactor: migrate entire project to TypeScript

This commit is contained in:
2026-02-06 14:44:14 +08:00
parent e98dbcb0f4
commit a80c479027
14 changed files with 1141 additions and 462 deletions

View File

@@ -1,14 +1,11 @@
const Database = require('better-sqlite3');
const bcrypt = require('bcryptjs');
const path = require('path');
import Database from 'better-sqlite3';
import bcrypt from 'bcryptjs';
import path from 'path';
// 创建数据库连接
const dbPath = path.join(__dirname, '../data/database.sqlite');
const db = new Database(dbPath, { verbose: console.log });
// 创建表
const createTables = () => {
// 用户表
const createTables = (): void => {
db.exec(`
CREATE TABLE IF NOT EXISTS users (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -22,18 +19,16 @@ const createTables = () => {
)
`);
// 企业表
db.exec(`
CREATE TABLE IF NOT EXISTS companies (
id INTEGER PRIMARY KEY AUTOINCREMENT,
company_name TEXT UNIQUE NOT NULL,
is_active BOOLEAN DEFAULT 1,
is_active BOOLEAN BOOLEAN DEFAULT 1,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
updated_at DATETIME DEFAULT CURRENT_TIMESTAMP
)
`);
// 序列号表
db.exec(`
CREATE TABLE IF NOT EXISTS serials (
id INTEGER PRIMARY KEY AUTOINCREMENT,
@@ -49,7 +44,6 @@ const createTables = () => {
)
`);
// 创建索引
db.exec('CREATE INDEX IF NOT EXISTS idx_company_name_companies ON companies (company_name)');
db.exec('CREATE INDEX IF NOT EXISTS idx_serial_number ON serials (serial_number)');
db.exec('CREATE INDEX IF NOT EXISTS idx_company_name_serials ON serials (company_name)');
@@ -58,20 +52,16 @@ const createTables = () => {
console.log('数据库表创建完成');
};
// 创建默认管理员用户
const createDefaultUser = async () => {
const createDefaultUser = async (): Promise<void> => {
const username = 'admin';
const password = 'Beifan@2026';
const name = '系统管理员';
// 检查用户是否已存在
const user = db.prepare('SELECT * FROM users WHERE username = ?').get(username);
if (!user) {
// 哈希密码
const hashedPassword = await bcrypt.hash(password, 10);
// 创建用户
db.prepare(
'INSERT INTO users (username, password, name, email, role) VALUES (?, ?, ?, ?, ?)'
).run(username, hashedPassword, name, 'admin@example.com', 'admin');
@@ -84,14 +74,11 @@ const createDefaultUser = async () => {
}
};
// 初始化数据库
const initDatabase = async () => {
const initDatabase = async (): Promise<void> => {
createTables();
await createDefaultUser();
// 关闭数据库连接
db.close();
console.log('数据库连接已关闭');
};
initDatabase();
initDatabase();