HotFix/dexie error (#7778)

* fix(dexieError): initialize database connection before fetching phrases

- Added an `init` method to the `QuickPhraseService` to ensure the Dexie database is opened before retrieving all quick phrases.
- Updated the `getAll` method to call the `init` method, improving reliability in data retrieval.

* fix(QuickPhraseService): ensure database initialization before updating phrases

- Added calls to the `init` method in the `update` and `updateOrder` methods to guarantee the database connection is established before performing updates, enhancing data integrity and reliability.

* fix(QuickPhraseService): prevent multiple database initializations

- Added a static flag to ensure the database initialization occurs only once, preventing redundant calls to the `init` method and improving performance.
This commit is contained in:
SuYao 2025-07-03 17:48:25 +08:00 committed by GitHub
parent 4c353f4eee
commit 9847db5c83
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

View File

@ -3,7 +3,24 @@ import { QuickPhrase } from '@renderer/types'
import { v4 as uuidv4 } from 'uuid'
export class QuickPhraseService {
private static _isInitialized: boolean = false
static async init() {
if (QuickPhraseService._isInitialized) {
return
}
try {
await db.open()
QuickPhraseService._isInitialized = true
} catch (error) {
console.error('Failed to open Dexie database:', error)
}
}
static async getAll(): Promise<QuickPhrase[]> {
// Ensure database is initialized before
await QuickPhraseService.init()
const phrases = await db.quick_phrases.toArray()
return phrases.sort((a, b) => (b.order ?? 0) - (a.order ?? 0))
}
@ -34,6 +51,7 @@ export class QuickPhraseService {
}
static async update(id: string, data: Pick<QuickPhrase, 'title' | 'content'>): Promise<void> {
await QuickPhraseService.init()
await db.quick_phrases.update(id, {
...data,
updatedAt: Date.now()
@ -54,6 +72,7 @@ export class QuickPhraseService {
static async updateOrder(phrases: QuickPhrase[]): Promise<void> {
const now = Date.now()
await QuickPhraseService.init()
await Promise.all(
phrases.map((phrase, index) =>
db.quick_phrases.update(phrase.id, {