-
Notifications
You must be signed in to change notification settings - Fork 409
Expand file tree
/
Copy pathDatabaseService.ts
More file actions
355 lines (315 loc) · 11 KB
/
Copy pathDatabaseService.ts
File metadata and controls
355 lines (315 loc) · 11 KB
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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
// Copyright (c) 2025 Beijing Volcano Engine Technology Co., Ltd.
// SPDX-License-Identifier: Apache-2.0
// electron/database.ts - SQLite Database Manager (ES Module Compatible)
import Database from 'better-sqlite3'
import path from 'path'
import fs from 'fs'
import { isValidIsoString, toSqliteDatetime } from '../utils/time'
import { DB } from './Database'
import { TODOActivity } from '@interface/db/todo'
import { getLogger } from '@shared/logger/main'
import { VaultDatabaseService } from './VaultDatabaseService'
import { resolveSqliteDbPath } from './dbPath'
const logger = getLogger('DatabaseManager')
class DatabaseManager extends VaultDatabaseService {
private db: Database.Database | null = null
private dbPath: string
private isInitialized: boolean = false
private initPromise: Promise<void> | null = null
constructor() {
super()
this.dbPath = resolveSqliteDbPath()
logger.info('📁 Database path:', this.dbPath)
}
// Asynchronously initialize the database
public async initialize(): Promise<void> {
if (this.isInitialized) {
return
}
if (this.initPromise) {
return this.initPromise
}
this.initPromise = this._initialize()
return this.initPromise
}
private async _initialize(): Promise<void> {
try {
// Wait for the database directory to be created
await this.waitForDatabaseDirectory()
this.db = new Database(this.dbPath)
// Enable WAL mode for better performance
this.db!.pragma('journal_mode = WAL')
// Only initialize the table structure when needed
this.ensureTablesExist()
this.isInitialized = true
logger.info('✅ Database initialized successfully')
} catch (error) {
logger.error('❌ Database initialization failed:', error)
throw error
}
}
// Wait for the database directory to be created
private async waitForDatabaseDirectory(): Promise<void> {
const maxRetries = 30 // Wait for a maximum of 30 seconds
const retryDelay = 1000 // Check once per second
for (let i = 0; i < maxRetries; i++) {
const dbDir = path.dirname(this.dbPath)
if (fs.existsSync(dbDir)) {
logger.info('📁 Database directory found:', dbDir)
return
}
logger.info(`⏳ Waiting for database directory... (${i + 1}/${maxRetries})`)
await new Promise((resolve) => setTimeout(resolve, retryDelay))
}
throw new Error(`Database directory not found after ${maxRetries} seconds: ${path.dirname(this.dbPath)}`)
}
// Ensure the database is initialized
private ensureInitialized(): void {
if (!this.isInitialized || !this.db) {
throw new Error('Database not initialized. Call initialize() first.')
}
}
private ensureTablesExist() {
try {
// Check if the table exists
const tableExists = this.db!.prepare("SELECT name FROM sqlite_master WHERE type='table' AND name='vaults'").get()
if (!tableExists) {
logger.info('📊 Creating vaults table for the first time...')
// this.createVaultsTable()
}
} catch (error) {
logger.error('❌ Failed to ensure tables exist:', error)
throw error
}
}
// // todo: Move table definitions to the backend
// private createVaultsTable() {
// this.db!.exec(`
// CREATE TABLE vaults (
// id INTEGER PRIMARY KEY AUTOINCREMENT,
// parent_id INTEGER,
// sort_order INTEGER,
// title TEXT,
// content TEXT,
// summary TEXT,
// tags TEXT,
// is_folder INTEGER DEFAULT 0,
// is_deleted INTEGER DEFAULT 0,
// created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
// updated_at DATETIME DEFAULT CURRENT_TIMESTAMP,
// FOREIGN KEY (parent_id) REFERENCES vaults(id)
// );
// `)
// // Create indexes to improve query performance
// this.db!.exec(`
// CREATE INDEX IF NOT EXISTS idx_vaults_title ON vaults(title);
// CREATE INDEX IF NOT EXISTS idx_vaults_parent_id ON vaults(parent_id);
// CREATE INDEX IF NOT EXISTS idx_vaults_order ON vaults(sort_order);
// CREATE INDEX IF NOT EXISTS idx_vaults_is_folder ON vaults(is_folder);
// CREATE INDEX IF NOT EXISTS idx_vaults_is_deleted ON vaults(is_deleted);
// `)
// logger.info('✅ Vaults table created successfully')
// }
// private createTodoTable() {
// this.db!.exec(`
// CREATE TABLE todo (
// id INTEGER PRIMARY KEY AUTOINCREMENT,
// content TEXT,
// created_at DATETIME DEFAULT CURRENT_TIMESTAMP,
// start_time DATETIME,
// end_time DATETIME,
// status INTEGER DEFAULT 0,
// urgency INTEGER DEFAULT 0
// );
// `)
// this.db!.exec(`
// CREATE INDEX IF NOT EXISTS idx_todo_status ON todo(status);
// CREATE INDEX IF NOT EXISTS idx_todo_urgency ON todo(urgency);
// `)
// logger.info('✅ Todo table created successfully')
// }
// private createActivityTable() {
// this.db!.exec(`
// CREATE TABLE activity (
// id INTEGER PRIMARY KEY AUTOINCREMENT,
// title TEXT,
// content TEXT,
// resources JSON,
// start_time DATETIME,
// end_time DATETIME
// );
// `)
// this.db!.exec(`
// CREATE INDEX IF NOT EXISTS idx_activity_status ON activity(title);
// CREATE INDEX IF NOT EXISTS idx_activity_urgency ON activity(start_time);
// `)
// logger.info('✅ Activity table created successfully')
// }
// private createTipsTable() {
// this.db!.exec(`
// CREATE TABLE tips (
// id INTEGER PRIMARY KEY AUTOINCREMENT,
// content TEXT,
// created_at DATETIME DEFAULT CURRENT_TIMESTAMP
// );
// `)
// this.db!.exec(`
// CREATE INDEX IF NOT EXISTS idx_tips_status ON tips(content);
// `)
// logger.info('✅ Tips table created successfully')
// }
// Get all activities
public getAllActivities() {
try {
this.ensureInitialized()
const stmt = this.db!.prepare('SELECT * FROM activity')
const rows = stmt.all()
logger.info('✅ All activities retrieved successfully:', rows)
return rows
} catch (error) {
logger.error('❌ Failed to get all activities:', error)
throw error
}
}
// Get new activities
// "2024-01-15T10:30:00.000Z" or "2024-01-15 10:30:00" format
public getNewActivities(startTime: string, endTime: string = '2099-12-31 00:00:00') {
try {
this.ensureInitialized()
const start = toSqliteDatetime(new Date(startTime))
const end = toSqliteDatetime(new Date(endTime))
const stmt = this.db!.prepare(
'SELECT * FROM activity WHERE start_time > ? AND start_time < ? ORDER BY start_time ASC'
)
const rows = stmt.all(start, end)
return rows
} catch (error) {
logger.error('❌ Failed to get new activities:', error)
throw error
}
}
public getTasks(startTime: string, endTime: string) {
try {
this.ensureInitialized()
if (!isValidIsoString(startTime)) {
logger.error('❌ Invalid startTime format:', startTime)
throw new Error('Invalid startTime format. Expected ISO 8601 string.')
}
if (!isValidIsoString(endTime)) {
logger.error('❌ Invalid endTime format:', endTime)
throw new Error('Invalid endTime format. Expected ISO 8601 string.')
}
const db = DB.getInstance(DB.dbName)
const sql = 'SELECT * FROM todo WHERE start_time >= ? AND start_time < ? ORDER BY start_time ASC'
const rows = db.query<TODOActivity>(sql, [startTime, endTime])
return rows
} catch (error) {
logger.error('❌ Failed to get tasks:', error)
throw error
}
}
// Add to the appropriate place in the file
public addTask(
taskData: Partial<{ content?: string; status?: number; start_time?: string; end_time?: string; urgency?: number }>
) {
try {
this.ensureInitialized()
const db = DB.getInstance(DB.dbName)
const info = db.insert('todo', taskData)
return info
} catch (error) {
logger.error('❌ Failed to add task:', error)
throw error
}
}
public toggleTaskStatus(taskId: number) {
try {
this.ensureInitialized()
const stmt = this.db!.prepare('UPDATE todo SET status = 1 - status WHERE id = ?')
const result = stmt.run(taskId)
logger.info('✅ Task status toggled successfully')
return result
} catch (error) {
logger.error('❌ Failed to toggle task status:', error)
throw error
}
}
public updateTask(
taskId: number,
taskData: Partial<{ content: string; urgency: number; start_time: string; end_time: string }>
) {
try {
this.ensureInitialized()
// Build the dynamic update statement
const updateFields: string[] = []
const values: any[] = []
if (taskData.content !== undefined) {
updateFields.push('content = ?')
values.push(taskData.content)
}
if (taskData.urgency !== undefined) {
updateFields.push('urgency = ?')
values.push(taskData.urgency)
}
if (taskData.start_time !== undefined) {
updateFields.push('start_time = ?')
values.push(taskData.start_time)
}
if (taskData.end_time !== undefined) {
updateFields.push('end_time = ?')
values.push(taskData.end_time)
}
if (updateFields.length === 0) {
throw new Error('No fields provided to update')
}
values.push(taskId) // Parameter for the WHERE clause
const stmt = this.db!.prepare(`UPDATE todo SET ${updateFields.join(', ')} WHERE id = ?`)
const result = stmt.run(...values)
logger.info(`✅ Task updated successfully: ID ${taskId}, rows affected: ${result.changes}`)
return result
} catch (error) {
logger.error('❌ Failed to update task:', error)
throw error
}
}
public deleteTask(taskId: number) {
try {
this.ensureInitialized()
// Check if the task exists
const existingTask = this.db!.prepare('SELECT * FROM todo WHERE id = ?').get(taskId)
if (!existingTask) {
throw new Error(`Task with ID ${taskId} not found`)
}
const stmt = this.db!.prepare('DELETE FROM todo WHERE id = ?')
const result = stmt.run(taskId)
logger.info(`✅ Task deleted successfully: ID ${taskId}, rows affected: ${result.changes}`)
return result
} catch (error) {
logger.error('❌ Failed to delete task:', error)
throw error
}
}
public getAllTips() {
try {
this.ensureInitialized()
const stmt = this.db!.prepare('SELECT * FROM tips')
const rows = stmt.all()
logger.info('✅ Tips retrieved successfully')
return rows
} catch (error) {
logger.error('❌ Failed to get tips:', error)
throw error
}
}
close() {
try {
logger.info('🔒 Closing database connection...')
this.db?.close()
} catch (error) {
logger.error('❌ Error closing database:', error)
}
}
}
// 导出单例实例
const databaseManager = new DatabaseManager()
export default databaseManager