Phase 5 구현: SQLite DB + History/Dictionary 서비스 + UI
- DB: better-sqlite3 + drizzle-orm (history/dictionary/stats, WAL 모드) - HistoryService: CRUD + 검색 + 통계 + 30일 보존 정책, 세션 완료 시 자동 저장 - DictionaryService: CRUD + 검색 + 사용 횟수 추적 + STT 프롬프트 힌트 - HistoryPage: 목록 + 검색 + 삭제 + 복사 + 페이지네이션 - DictionaryPage: 목록 + 검색 + 추가 다이얼로그 + 삭제 - DashboardPage: 실데이터 통계 (총/오늘 세션, 시간, 단어수, 연속일수) - IPC: history/dictionary/stats 핸들러 + preload API - Bootstrap: DB 초기화 (critical) + 이력 자동 저장 연동
This commit is contained in:
parent
4a5cf6c819
commit
291e2a29d0
17 changed files with 3111 additions and 35 deletions
107
src/main/db/index.ts
Normal file
107
src/main/db/index.ts
Normal file
|
|
@ -0,0 +1,107 @@
|
|||
// src/main/db/index.ts
|
||||
// 설계서 03의 DB 초기화 코드
|
||||
|
||||
import Database from 'better-sqlite3'
|
||||
import { drizzle, type BetterSQLite3Database } from 'drizzle-orm/better-sqlite3'
|
||||
import { app } from 'electron'
|
||||
import path from 'path'
|
||||
import * as schema from './schema'
|
||||
import { getLogger } from '../services/LoggerService'
|
||||
|
||||
const logger = getLogger('database')
|
||||
|
||||
let db: BetterSQLite3Database<typeof schema> | null = null
|
||||
let sqlite: Database.Database | null = null
|
||||
|
||||
export function initDatabase(): BetterSQLite3Database<typeof schema> {
|
||||
const dbPath = path.join(app.getPath('userData'), 'd3ro-voice.db')
|
||||
logger.info(`Initializing database at: ${dbPath}`)
|
||||
|
||||
sqlite = new Database(dbPath)
|
||||
|
||||
sqlite.pragma('journal_mode = WAL')
|
||||
sqlite.pragma('foreign_keys = ON')
|
||||
sqlite.pragma('busy_timeout = 5000')
|
||||
|
||||
// 테이블 생성 (마이그레이션 대신 직접 생성)
|
||||
sqlite.exec(`
|
||||
CREATE TABLE IF NOT EXISTS history (
|
||||
id TEXT PRIMARY KEY,
|
||||
original_text TEXT NOT NULL,
|
||||
polished_text TEXT,
|
||||
focused_app TEXT,
|
||||
focused_app_name TEXT,
|
||||
focused_app_window_title TEXT,
|
||||
mode TEXT NOT NULL DEFAULT 'dictation',
|
||||
status TEXT NOT NULL DEFAULT 'completed',
|
||||
error_code TEXT,
|
||||
audio_local_path TEXT,
|
||||
duration REAL NOT NULL,
|
||||
detected_language TEXT,
|
||||
mic_device TEXT,
|
||||
word_count INTEGER NOT NULL DEFAULT 0,
|
||||
stt_model TEXT,
|
||||
llm_model TEXT,
|
||||
stt_latency_ms INTEGER,
|
||||
llm_latency_ms INTEGER,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL,
|
||||
app_version TEXT NOT NULL DEFAULT '1.0.0'
|
||||
);
|
||||
|
||||
CREATE INDEX IF NOT EXISTS idx_history_created_at ON history(created_at DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_status ON history(status);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_detected_language ON history(detected_language);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_focused_app_name ON history(focused_app_name);
|
||||
CREATE INDEX IF NOT EXISTS idx_history_mode ON history(mode);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS dictionary (
|
||||
id TEXT PRIMARY KEY,
|
||||
word TEXT NOT NULL,
|
||||
pronunciation TEXT,
|
||||
category TEXT NOT NULL DEFAULT 'user',
|
||||
usage_count INTEGER NOT NULL DEFAULT 0,
|
||||
last_used_at INTEGER,
|
||||
created_at INTEGER NOT NULL,
|
||||
updated_at INTEGER NOT NULL
|
||||
);
|
||||
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS idx_dictionary_word_category ON dictionary(word, category);
|
||||
CREATE INDEX IF NOT EXISTS idx_dictionary_created_at ON dictionary(created_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_dictionary_usage_count ON dictionary(usage_count DESC);
|
||||
|
||||
CREATE TABLE IF NOT EXISTS stats (
|
||||
id INTEGER PRIMARY KEY CHECK (id = 1),
|
||||
total_duration REAL NOT NULL DEFAULT 0,
|
||||
total_words INTEGER NOT NULL DEFAULT 0,
|
||||
session_count INTEGER NOT NULL DEFAULT 0,
|
||||
streak_days INTEGER NOT NULL DEFAULT 0,
|
||||
last_session_at INTEGER,
|
||||
last_updated INTEGER NOT NULL
|
||||
);
|
||||
|
||||
INSERT OR IGNORE INTO stats (id, total_duration, total_words, session_count, streak_days, last_updated)
|
||||
VALUES (1, 0, 0, 0, 0, ${Date.now()});
|
||||
`)
|
||||
|
||||
db = drizzle(sqlite, { schema })
|
||||
|
||||
logger.info('Database initialized')
|
||||
return db
|
||||
}
|
||||
|
||||
export function getDatabase(): BetterSQLite3Database<typeof schema> {
|
||||
if (!db) {
|
||||
throw new Error('Database not initialized. Call initDatabase() first.')
|
||||
}
|
||||
return db
|
||||
}
|
||||
|
||||
export function closeDatabase(): void {
|
||||
if (sqlite) {
|
||||
sqlite.close()
|
||||
sqlite = null
|
||||
db = null
|
||||
logger.info('Database closed')
|
||||
}
|
||||
}
|
||||
83
src/main/db/schema.ts
Normal file
83
src/main/db/schema.ts
Normal file
|
|
@ -0,0 +1,83 @@
|
|||
// src/main/db/schema.ts
|
||||
// 설계서 03의 drizzle-orm 스키마 정의
|
||||
|
||||
import { sqliteTable, text, integer, real, index, uniqueIndex } from 'drizzle-orm/sqlite-core'
|
||||
|
||||
// ── history ──────────────────────────────────────────────
|
||||
export const history = sqliteTable(
|
||||
'history',
|
||||
{
|
||||
id: text('id').primaryKey(),
|
||||
originalText: text('original_text').notNull(),
|
||||
polishedText: text('polished_text'),
|
||||
focusedApp: text('focused_app'),
|
||||
focusedAppName: text('focused_app_name'),
|
||||
focusedAppWindowTitle: text('focused_app_window_title'),
|
||||
mode: text('mode', { enum: ['dictation', 'translate', 'command'] })
|
||||
.notNull()
|
||||
.default('dictation'),
|
||||
status: text('status', { enum: ['completed', 'cancelled', 'error'] })
|
||||
.notNull()
|
||||
.default('completed'),
|
||||
errorCode: text('error_code'),
|
||||
audioLocalPath: text('audio_local_path'),
|
||||
duration: real('duration').notNull(),
|
||||
detectedLanguage: text('detected_language'),
|
||||
micDevice: text('mic_device'),
|
||||
wordCount: integer('word_count').notNull().default(0),
|
||||
sttModel: text('stt_model'),
|
||||
llmModel: text('llm_model'),
|
||||
sttLatencyMs: integer('stt_latency_ms'),
|
||||
llmLatencyMs: integer('llm_latency_ms'),
|
||||
createdAt: integer('created_at').notNull(),
|
||||
updatedAt: integer('updated_at').notNull(),
|
||||
appVersion: text('app_version').notNull().default('1.0.0')
|
||||
},
|
||||
(table) => [
|
||||
index('idx_history_created_at').on(table.createdAt),
|
||||
index('idx_history_status').on(table.status),
|
||||
index('idx_history_detected_language').on(table.detectedLanguage),
|
||||
index('idx_history_focused_app_name').on(table.focusedAppName),
|
||||
index('idx_history_mode').on(table.mode)
|
||||
]
|
||||
)
|
||||
|
||||
// ── dictionary ───────────────────────────────────────────
|
||||
export const dictionary = sqliteTable(
|
||||
'dictionary',
|
||||
{
|
||||
id: text('id').primaryKey(),
|
||||
word: text('word').notNull(),
|
||||
pronunciation: text('pronunciation'),
|
||||
category: text('category', { enum: ['user', 'auto', 'technical'] })
|
||||
.notNull()
|
||||
.default('user'),
|
||||
usageCount: integer('usage_count').notNull().default(0),
|
||||
lastUsedAt: integer('last_used_at'),
|
||||
createdAt: integer('created_at').notNull(),
|
||||
updatedAt: integer('updated_at').notNull()
|
||||
},
|
||||
(table) => [
|
||||
uniqueIndex('idx_dictionary_word_category').on(table.word, table.category),
|
||||
index('idx_dictionary_created_at').on(table.createdAt),
|
||||
index('idx_dictionary_usage_count').on(table.usageCount)
|
||||
]
|
||||
)
|
||||
|
||||
// ── stats ────────────────────────────────────────────────
|
||||
export const stats = sqliteTable('stats', {
|
||||
id: integer('id').primaryKey(),
|
||||
totalDuration: real('total_duration').notNull().default(0),
|
||||
totalWords: integer('total_words').notNull().default(0),
|
||||
sessionCount: integer('session_count').notNull().default(0),
|
||||
streakDays: integer('streak_days').notNull().default(0),
|
||||
lastSessionAt: integer('last_session_at'),
|
||||
lastUpdated: integer('last_updated').notNull()
|
||||
})
|
||||
|
||||
// ── 타입 추출 ────────────────────────────────────────────
|
||||
export type History = typeof history.$inferSelect
|
||||
export type NewHistory = typeof history.$inferInsert
|
||||
export type Dictionary = typeof dictionary.$inferSelect
|
||||
export type NewDictionary = typeof dictionary.$inferInsert
|
||||
export type Stats = typeof stats.$inferSelect
|
||||
Loading…
Add table
Add a link
Reference in a new issue