Constructor
Creates a new MiftahDB instance.
- Parameters:
path
: The path to the database file. Defaults to ":memory:" if not provided.options
: Optional configuration options for the database. These options can be customized as follows:journalMode
: Determines the journal mode (default:"WAL"
).synchronousMode
: Controls the database's synchronization mode (default:"NORMAL"
).tempStoreMode
: Specifies where temporary tables are stored (default:"MEMORY"
).cacheSize
: The cache size for the database (default:-64000
).mmapSize
: The memory-mapped file size (default:30000000000
).lockingMode
: Determines the database locking mode (default:"NORMAL"
).autoVacuumMode
: Configures the auto-vacuum behavior (default:"OFF"
).autoCleanupOnClose
: Automatically cleans up expired keys when the database is closed (default:false
).autoCloseOnExit
: Automatically closes the database when the process exits (default:true
).
Example Usage
// New MiftahDB instance with disk-based database
const db1 = new MiftahDB("test.db");
// New MiftahDB instance with in-memory database
const db2 = new MiftahDB(":memory:");
// New MiftahDB instance with custom configuration
const db3 = new MiftahDB("test.db", {
journalMode: "WAL",
synchronousMode: "NORMAL",
tempStoreMode: "MEMORY",
cacheSize: -64000,
mmapSize: 30000000000,
lockingMode: "NORMAL",
autoVacuumMode: "OFF",
autoCleanupOnClose: false,
autoCloseOnExit: true,
});
Disk vs. In-Memory Databases
Disk-Based Database
- Persistence: Data is stored on disk, ensuring that it remains available even after the application restarts.
- Use Case: Best for applications where data persistence and durability are important, such as user profiles, logs, or long-term records.
- Performance: Generally slower than in-memory databases due to disk I/O operations. However, with
better-sqlite3
, performance is optimized for quick access and manipulation.
In-Memory Database
- Persistence: Data is stored in RAM and is lost when the application stops or restarts.
- Use Case: Ideal for scenarios where fast data access is critical, such as caching, temporary data processing, or testing.
- Performance: Faster than disk-based databases due to the absence of disk I/O. Operations are performed entirely in memory, resulting in reduced latency and higher throughput.
Performance Comparison
- Speed: In-memory databases are significantly faster because they avoid the overhead of disk I/O. They provide quicker read and write operations, making them suitable for scenarios where speed is crucial.
- Durability: Disk-based databases offer data persistence, making them the better choice for applications where data needs to be stored long-term.
- Usage: Choose an in-memory database for high-speed temporary storage and a disk-based database for reliable, long-term storage.
In summary, if you need rapid access and don't require data to persist beyond the application's runtime, go with an in-memory database. For durable, long-term storage, a disk-based database is the appropriate choice.