Local Database
A built-in, file-based persistence layer for Moonlink.js. It stores data in JSON format with Write-Ahead Logging (WAL) for durability.
The Local database provider offers persistent storage by saving data to the local file system. It is robust, featuring a Write-Ahead Log (WAL) to prevent data loss and periodic compaction to keep file sizes manageable. This is the default provider if database.type is set to 'local'.
Properties
Local Database Properties
Internal state of the local storage engine.
Paths
The directory where data files are stored. Defaults to ../datastore.
Path to the main .json data file.
Path to the .wal write-ahead log file.
Configuration
Interval for compacting the WAL into the snapshot (default: 60000ms).
Interval for flushing the WAL buffer to disk (default: 500ms).
Methods
init(manager, options)
init(manager, options)#
→Promise<void>Initializes the local storage. Creates the data directory if it doesn't exist, loads the snapshot, replays the WAL, and starts background maintenance tasks (compaction, flushing).
The Moonlink Manager instance.
Optional configuration (e.g., path).
// Called automatically by DatabaseManager
await localDb.init(manager);
set(key, value)
set(key, value)#
→Promise<void>Stores a value. Supports dot-notation for nested keys (e.g., players.123.volume). Writes the operation to the WAL buffer.
The key to store.
The value to store.
await localDb.set("user.settings.theme", "dark");
get(key)
Retrieves a value from the in-memory store. Supports dot-notation.
The key to retrieve.
const theme = await localDb.get("user.settings.theme");
remove(key)
remove(key)#
→Promise<boolean>Deletes a key from the store. Supports dot-notation. Logs the deletion to the WAL.
The key to remove.
await localDb.remove("user.settings");
has(key)
Checks if a key exists in the store.
The key to check.
if (await localDb.has("session.id")) { ... }
keys(pattern)
keys(pattern)#
→Promise<string[]>Returns all keys in the store flattened (dot-notation). Supports a simple regex-like pattern matching.
Optional pattern (e.g., players.*).
const allPlayers = await localDb.keys("players.*");
clear()
Clears the entire database, resets the in-memory store, and empties the WAL file.
await localDb.clear();
shutdown()
shutdown()#
→Promise<void>Gracefully shuts down the database. Performs a final compaction, flushes buffers, and closes file streams.
await localDb.shutdown();