refactor: use function instead of an object for create nedb#9594
Conversation
✅ Circular References ReportGenerated at: 2026-03-06T07:34:56.130Z Summary
Click to view all circular references in PR (76)Click to view all circular references in base branch (76)Analysis✅ No Change: This PR does not introduce or remove any circular references. This report was generated automatically by comparing against the |
6547665 to
14fb8ea
Compare
There was a problem hiding this comment.
Pull request overview
This PR refactors the NeDB-backed database implementation to be created via a factory function rather than exported as a singleton object, and extracts the database repair logic into its own module. This aligns the Node/main-process database implementation with a more explicit construction/wrapping pattern.
Changes:
- Replaced the
nedbDatabaseexported object with acreateNedbDatabase()factory (with an optional wrapper hook). - Extracted database repair routines into
repair-database.tsand updated tests accordingly. - Updated main process + in-memory send-request initialization to use
createNedbDatabase().
Reviewed changes
Copilot reviewed 6 out of 7 changed files in this pull request and generated no comments.
Show a summary per file
| File | Description |
|---|---|
| packages/insomnia/src/main/database.main.ts | Uses createNedbDatabase() wrapper to customize init and flushChanges in the Electron main process. |
| packages/insomnia/src/insomnia-data/node-src/index.ts | Switches node entrypoint export from nedbDatabase to createNedbDatabase. |
| packages/insomnia/src/insomnia-data/node-src/database/repair-database.ts | New standalone module containing the database repair routines. |
| packages/insomnia/src/insomnia-data/node-src/database/database.test.ts | Updates repair-related tests to call repairDatabase() from the new module. |
| packages/insomnia/src/insomnia-data/node-src/database/database-nedb.ts | Core refactor: introduces createNedbDatabase() factory and imports repairDatabase. |
| packages/insomnia/src/common/send-request.ts | Initializes an in-memory DB for send-request via createNedbDatabase(). |
| packages/insomnia/src/common/database.ts | Adds a small re-export module for database + related types. |
Comments suppressed due to low confidence (1)
packages/insomnia/src/insomnia-data/node-src/database/database-nedb.ts:560
createNedbDatabase()is now a factory, but the backing state (nedbBucket,bufferingChanges,changeBuffer,changeListeners, etc.) is still module-scoped. That means multiple database instances created via this factory will share state and can interfere with each other (e.g., listeners/buffers leaking across instances, one init overwriting another instance’s buckets). Consider moving this state insidecreateNedbDatabase()(or otherwise keying it per instance) so each created database is actually isolated, or explicitly document/enforce that only a single instance may exist per process.
let nedbBucket: Record<AllTypes, NeDB> = {} as Record<AllTypes, NeDB>;
// ~~~~~~~~~~~~~~~~ //
// Change Listeners //
// ~~~~~~~~~~~~~~~~ //
let bufferingChanges = false;
let bufferChangesId = 1;
let changeBuffer: ChangeBufferEvent[] = [];
let changeListeners: ChangeListener[] = [];
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
Background
In the previous commit, the database was an object that inherits the database-nedb, now update it to be a create function to be more reasonable.
Changes