Skip to content

Conversation

@hadv
Copy link
Owner

@hadv hadv commented Dec 8, 2025

Summary

Migrate the backend database layer from sqlite3 to better-sqlite3 for improved performance and simpler API.

Changes

backend/package.json

  • Replaced sqlite3 with better-sqlite3 (v12.5.0)

backend/database.js

  • Import: Changed from sqlite3 to better-sqlite3
  • Database initialization: Converted from async callbacks/promises to synchronous API
  • Pragmas: Now use db.pragma() instead of promise-wrapped db.run()
  • Helper functions: Replaced runAsync/getAsync/allAsync with synchronous run/get/all using prepared statements
  • All exported functions: Converted from async function to regular function
  • Transaction handling: Used better-sqlite3's native transaction API (db.transaction()) for atomic operations in activateDevice()
  • Backup: Changed from VACUUM INTO to better-sqlite3's db.backup() API
  • Close: Changed from callback-based to synchronous db.close()
  • Graceful shutdown: Converted from async to sync

Benefits

Aspect sqlite3 better-sqlite3
API Async (callback/Promise) Synchronous
Performance Slower due to async overhead 2-10x faster for typical workloads
Simplicity More complex with callbacks Cleaner, simpler code
Transactions Complex to handle properly Easy: db.transaction(() => {...})()
Native builds Often problematic Generally more reliable
Maintenance Less actively maintained Actively maintained

Testing

  • Backend server starts successfully
  • Database initialization works
  • FIDO MDS cache loads and refreshes correctly
  • Graceful shutdown works properly
  • No diagnostics/lint errors

Related Issue

Closes #170


Pull Request opened by Augment Code with guidance from the PR author

- Replace sqlite3 with better-sqlite3 for improved performance
- Convert all async database operations to synchronous API
- Use better-sqlite3's native transaction support for atomic operations
- Simplify database initialization with synchronous pragmas
- Update backup to use better-sqlite3's backup() API
- Convert closeDatabase to synchronous operation

Benefits:
- 2-10x performance improvement for typical workloads
- Simpler, cleaner code without callback/promise wrappers
- Better prepared statement API for parameterized queries
- More reliable native builds compared to sqlite3

Closes #170
@hadv hadv merged commit b0a451a into main Dec 8, 2025
4 checks passed
@hadv hadv deleted the feature/migrate-to-better-sqlite3 branch December 8, 2025 01:47
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Migrate from sqlite3 to better-sqlite3

3 participants