-
Notifications
You must be signed in to change notification settings - Fork 0
β‘ Bolt: Optimize IndexedDB storage with bulk save #2
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. Weβll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
davidraehles
merged 3 commits into
main
from
bolt-optimize-idb-bulk-save-2171306501365949358
Feb 18, 2026
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| ## 2025-05-15 - [Initial Bottleneck Hunting] | ||
| **Learning:** Found a performance anti-pattern in IndexedDB usage where multiple notes are saved individually, creating N separate transactions instead of one. Also noticed that PBKDF2 is used per note with unique salts, which is secure but expensive during initial load. | ||
| **Action:** Implement a bulk save function for IndexedDB to use a single transaction when loading multiple notes from the server. |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| /** | ||
| * Unit tests for offline service | ||
| */ | ||
|
|
||
| import { describe, it, expect, beforeEach, vi } from 'vitest'; | ||
| import { saveNoteToDB, saveNotesToDB, getNotesFromDB } from '../offline'; | ||
| import type { Note } from '../../models/types'; | ||
|
|
||
| // Mock the idb openDB | ||
| vi.mock('idb', () => ({ | ||
| openDB: vi.fn(), | ||
| })); | ||
|
|
||
| import { openDB } from 'idb'; | ||
|
|
||
| describe('Offline Service', () => { | ||
| const mockNote: Note = { | ||
| id: '1', | ||
| title: 'Test Note', | ||
| content: 'Test Content', | ||
| createdAt: new Date().toISOString(), | ||
| updatedAt: new Date().toISOString(), | ||
| }; | ||
|
|
||
| const mockDb = { | ||
| put: vi.fn(), | ||
| getAll: vi.fn(), | ||
| transaction: vi.fn(), | ||
| }; | ||
|
|
||
| const mockStore = { | ||
| put: vi.fn(), | ||
| clear: vi.fn(), | ||
| }; | ||
|
|
||
| const mockTx = { | ||
| objectStore: vi.fn(() => mockStore), | ||
| done: Promise.resolve(), | ||
| }; | ||
|
|
||
| beforeEach(() => { | ||
| vi.clearAllMocks(); | ||
| vi.mocked(openDB).mockResolvedValue(mockDb as any); | ||
| mockDb.transaction.mockReturnValue(mockTx as any); | ||
| }); | ||
|
|
||
| it('should save a single note', async () => { | ||
| await saveNoteToDB(mockNote); | ||
| expect(mockDb.put).toHaveBeenCalledWith('notes', mockNote); | ||
| }); | ||
|
|
||
| it('should save multiple notes in a single transaction', async () => { | ||
| const notes = [mockNote, { ...mockNote, id: '2' }]; | ||
| await saveNotesToDB(notes); | ||
|
|
||
| expect(mockDb.transaction).toHaveBeenCalledTimes(1); | ||
| expect(mockDb.transaction).toHaveBeenCalledWith('notes', 'readwrite'); | ||
| expect(mockTx.objectStore).toHaveBeenCalledWith('notes'); | ||
| expect(mockStore.put).toHaveBeenCalledTimes(2); | ||
| expect(mockStore.put).toHaveBeenCalledWith(notes[0]); | ||
| expect(mockStore.put).toHaveBeenCalledWith(notes[1]); | ||
| }); | ||
|
|
||
| it('should get all notes', async () => { | ||
| const notes = [mockNote]; | ||
| mockDb.getAll.mockResolvedValue(notes); | ||
|
|
||
| const result = await getNotesFromDB(); | ||
| expect(result).toEqual(notes); | ||
| expect(mockDb.getAll).toHaveBeenCalledWith('notes'); | ||
| }); | ||
|
|
||
| it('should return early if saving empty notes array', async () => { | ||
| await saveNotesToDB([]); | ||
| expect(mockDb.transaction).not.toHaveBeenCalled(); | ||
| }); | ||
| }); | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The test claims to verify βsingle transactionβ behavior, but it doesnβt assert the transaction call count.
toHaveBeenCalledWithwill still pass iftransaction('notes','readwrite')is invoked multiple times. Add an explicittoHaveBeenCalledTimes(1)(or equivalent) so this test would fail if the implementation regresses to multiple transactions.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
expect(mockDb.transaction).toHaveBeenCalledTimes(1)assertion before the existingtoHaveBeenCalledWithcheck to explicitly verify single-transaction usage.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good suggestion. I've added the
expect(mockDb.transaction).toHaveBeenCalledTimes(1)assertion to the test to explicitly verify that only one transaction is used for bulk operations.