Database Class
The Database class provides a robust key-value storage system for Moonlink.js, used internally to store player, node, and queue data. It utilizes a Write-Ahead Log (WAL) for data persistence and recovery.
The Database class is automatically initialized by the Manager and can be accessed through manager.database
. It is instantiated using the static Database.create()
method.
Overview
Initialization
The Database is initialized asynchronously and should be accessed via the static create
method.
Methods
Methods
Methods available in the Database class.
Available Methods
set
Set Value
Stores a value associated with a key. If the key points to an object and the new value is also an object, they will be merged. Supports optional logging to WAL.
Parameters
settings.volume
, players.guildId.volume
) true
. Returns & Example
Returns
• void
get
Get Value
Retrieves a value by its key.
Parameters
Returns & Example
Returns
• T | undefined
- The stored value or undefined if not found
delete
Delete Value
Removes a value by its key. Supports optional logging to WAL.
Parameters
true
. Returns & Example
Returns
• boolean
- True if the key was deleted, false otherwise
keys
Get All Keys
Retrieves all top-level and nested keys stored in the database.
Returns & Example
Returns
• string[]
- An array of all keys.
clear
Clear All Data
Clears all data from the database, effectively resetting it. Also clears the Write-Ahead Log.
Returns & Example
Returns
• void
shutdown
Shutdown Database
Gracefully shuts down the database, ensuring all pending writes are flushed and resources are released.
This should be called before your application exits.
Returns & Example
Returns
• Promise<void>
File Storage
Write-Ahead Log (WAL) Persistence
The Database class implements a Write-Ahead Log (WAL) system for robust data persistence and recovery.
- Log Files: Changes are appended to
data.{clientId}.wal
. - Snapshot Files: Periodically, the in-memory state is saved to
data.{clientId}.json
. - Automatic Recovery: On initialization, the database replays the WAL to restore the latest state.
The file storage system is handled automatically. You should not need to interact with these files directly. Manual modification can lead to data corruption.
Usage Examples
Basic Usage
Examples of using the Database class.