From 9847db5c8305a94f029f760c7bd96c1231d70cd3 Mon Sep 17 00:00:00 2001 From: SuYao Date: Thu, 3 Jul 2025 17:48:25 +0800 Subject: [PATCH] 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. --- .../src/services/QuickPhraseService.ts | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/renderer/src/services/QuickPhraseService.ts b/src/renderer/src/services/QuickPhraseService.ts index 876704cd07..3ec0439053 100644 --- a/src/renderer/src/services/QuickPhraseService.ts +++ b/src/renderer/src/services/QuickPhraseService.ts @@ -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 { + // 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): Promise { + 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 { const now = Date.now() + await QuickPhraseService.init() await Promise.all( phrases.map((phrase, index) => db.quick_phrases.update(phrase.id, {