From 7a7624866fdad27f111085cb818d6db2b51c7e8f Mon Sep 17 00:00:00 2001 From: Yaosanqi137 Date: Wed, 8 Apr 2026 00:17:40 +0800 Subject: [PATCH] perf(web-db): stop re-encrypting records on read --- apps/web/src/services/local-ai-chat-repo.ts | 13 +----------- apps/web/src/services/local-sync-repo.ts | 21 +------------------ .../web/src/services/local-task-draft-repo.ts | 10 +-------- apps/web/src/services/local-task-repo.ts | 15 +------------ 4 files changed, 4 insertions(+), 55 deletions(-) diff --git a/apps/web/src/services/local-ai-chat-repo.ts b/apps/web/src/services/local-ai-chat-repo.ts index 1425a50..17fb7a9 100644 --- a/apps/web/src/services/local-ai-chat-repo.ts +++ b/apps/web/src/services/local-ai-chat-repo.ts @@ -2,8 +2,7 @@ import { localDb, type LocalAiChatSessionRecord } from "@/services/local-db"; import type { WebAiChannel } from "@/services/ai-api"; import { decryptAiChatSessionRecord, - encryptAiChatSessionRecord, - shouldEncryptAiChatSessionRecord + encryptAiChatSessionRecord } from "@/services/local-sensitive-codec"; export type LocalAiChatMessageRecord = { @@ -69,16 +68,6 @@ export async function listLocalAiChatSessions( userId: string ): Promise { const records = await localDb.aiChatSessions.where("userId").equals(userId).toArray(); - const encryptedRecords = await Promise.all( - records - .filter(shouldEncryptAiChatSessionRecord) - .map((record) => encryptAiChatSessionRecord(record)) - ); - - if (encryptedRecords.length > 0) { - await localDb.aiChatSessions.bulkPut(encryptedRecords); - } - const decryptedRecords = await Promise.all( records.map((record) => decryptAiChatSessionRecord(record)) ); diff --git a/apps/web/src/services/local-sync-repo.ts b/apps/web/src/services/local-sync-repo.ts index 267eca3..3567a64 100644 --- a/apps/web/src/services/local-sync-repo.ts +++ b/apps/web/src/services/local-sync-repo.ts @@ -7,10 +7,7 @@ import { decryptOpLogRecord, decryptSyncInboxRecord, - encryptOpLogRecord, - encryptSyncInboxRecord, - shouldEncryptOpLogRecord, - shouldEncryptSyncInboxRecord + encryptSyncInboxRecord } from "@/services/local-sensitive-codec"; import type { SyncPullItem } from "@/services/sync-api"; @@ -18,14 +15,6 @@ export const MAX_SYNC_RETRY_COUNT = 5; export async function listPendingSyncOperations(limit = 20): Promise { const records = await localDb.opLogs.orderBy("clientTs").toArray(); - const encryptedRecords = await Promise.all( - records.filter(shouldEncryptOpLogRecord).map((record) => encryptOpLogRecord(record)) - ); - - if (encryptedRecords.length > 0) { - await localDb.opLogs.bulkPut(encryptedRecords); - } - const pendingRecords = records .filter((record) => record.syncedAt === null && record.retryCount < MAX_SYNC_RETRY_COUNT) .slice(0, limit); @@ -144,14 +133,6 @@ export async function listPendingRemoteOperations( limit = 100 ): Promise { const records = await localDb.syncInbox.where("userId").equals(userId).toArray(); - const encryptedRecords = await Promise.all( - records.filter(shouldEncryptSyncInboxRecord).map((record) => encryptSyncInboxRecord(record)) - ); - - if (encryptedRecords.length > 0) { - await localDb.syncInbox.bulkPut(encryptedRecords); - } - const pendingRecords = records .filter((record) => record.appliedAt === null) .sort((left, right) => { diff --git a/apps/web/src/services/local-task-draft-repo.ts b/apps/web/src/services/local-task-draft-repo.ts index e34446e..19dd3ad 100644 --- a/apps/web/src/services/local-task-draft-repo.ts +++ b/apps/web/src/services/local-task-draft-repo.ts @@ -1,9 +1,5 @@ import { localDb, type LocalTaskDraftRecord } from "@/services/local-db"; -import { - decryptTaskDraftRecord, - encryptTaskDraftRecord, - shouldEncryptTaskDraft -} from "@/services/local-sensitive-codec"; +import { decryptTaskDraftRecord, encryptTaskDraftRecord } from "@/services/local-sensitive-codec"; export type SaveLocalTaskDraftInput = { taskId: string; @@ -22,10 +18,6 @@ export async function getLocalTaskDraft(taskId: string): Promise { const tasks = await localDb.tasks.where("userId").equals(userId).toArray(); - const encryptedTasks = await Promise.all( - tasks.filter(shouldEncryptTaskRecord).map((task) => encryptTaskRecord(task)) - ); - - if (encryptedTasks.length > 0) { - await localDb.tasks.bulkPut(encryptedTasks); - } - const decryptedTasks = await Promise.all(tasks.map((task) => decryptTaskRecord(task))); return decryptedTasks .filter((task) => task.deletedAt === null) @@ -109,10 +100,6 @@ export async function getLocalTaskById(id: string): Promise