-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathdatabase.js
102 lines (99 loc) · 3.13 KB
/
database.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
const sqlite3 = require('sqlite3').verbose();
const path = require('path');
const db = new sqlite3.Database(path.join(__dirname, 'database.sqlite'), (err) => {
if (err) {
console.error('数据库连接失败:', err);
} else {
console.log('数据库连接成功');
initDatabase();
}
});
function initDatabase() {
db.run(`
CREATE TABLE IF NOT EXISTS users (
id TEXT PRIMARY KEY,
username TEXT UNIQUE NOT NULL,
email TEXT UNIQUE NOT NULL,
password TEXT NOT NULL,
email_verified BOOLEAN DEFAULT 0,
role TEXT DEFAULT 'user',
avatar TEXT DEFAULT '/images/default-avatar.png',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
last_login DATETIME,
status TEXT DEFAULT 'pending'
)
`, (err) => {
if (err) {
console.error('创建用户表失败:', err);
} else {
console.log('用户表创建成功或已存在');
}
});
// 创建验证码表
db.run(`
CREATE TABLE IF NOT EXISTS verification_codes (
id INTEGER PRIMARY KEY AUTOINCREMENT,
email TEXT NOT NULL,
code TEXT NOT NULL,
type TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
expires_at DATETIME NOT NULL,
used BOOLEAN DEFAULT 0
)
`, (err) => {
if (err) {
console.error('创建验证码表失败:', err);
} else {
console.log('验证码表创建成功或已存在');
}
});
// 添加点赞记录表
db.run(`
CREATE TABLE IF NOT EXISTS likes (
user_id TEXT NOT NULL,
image_id TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
PRIMARY KEY (user_id, image_id)
)
`);
// 创建评论表
db.run(`
CREATE TABLE IF NOT EXISTS comments (
id TEXT PRIMARY KEY,
image_id TEXT NOT NULL,
user_id TEXT NOT NULL,
content TEXT NOT NULL,
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
FOREIGN KEY (user_id) REFERENCES users(id),
FOREIGN KEY (image_id) REFERENCES images(id)
)
`, (err) => {
if (err) {
console.error('创建评论表失败:', err);
} else {
console.log('评论表创建成功或已存在');
}
});
// 创建图片表
db.run(`
CREATE TABLE IF NOT EXISTS images (
id TEXT PRIMARY KEY,
filename TEXT NOT NULL,
originalname TEXT NOT NULL,
path TEXT NOT NULL,
category TEXT DEFAULT 'recent',
created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
likes INTEGER DEFAULT 0,
tags TEXT,
uploader_id TEXT NOT NULL,
FOREIGN KEY (uploader_id) REFERENCES users(id)
)
`, (err) => {
if (err) {
console.error('创建图片表失败:', err);
} else {
console.log('图片表创建成功或已存在');
}
});
}
module.exports = db;