Star ✨ on GitHub

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

dirstring

The directory where data files are stored. Defaults to ../datastore.

snapshotPathstring

Path to the main .json data file.

logPathstring

Path to the .wal write-ahead log file.

Configuration

compactionIntervalMsnumber

Interval for compacting the WAL into the snapshot (default: 60000ms).

walFlushIntervalMsnumber

Interval for flushing the WAL buffer to disk (default: 500ms).

Methods

init(manager, options)

init(manager, options)#

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).

managerManager required

The Moonlink Manager instance.

optionsobject

Optional configuration (e.g., path).

Example
// Called automatically by DatabaseManager
await localDb.init(manager);

set(key, value)

set(key, value)#

Stores a value. Supports dot-notation for nested keys (e.g., players.123.volume). Writes the operation to the WAL buffer.

keystring required

The key to store.

valueany required

The value to store.

Example
await localDb.set("user.settings.theme", "dark");

get(key)

get(key)#

Retrieves a value from the in-memory store. Supports dot-notation.

keystring required

The key to retrieve.

Example
const theme = await localDb.get("user.settings.theme");

remove(key)

remove(key)#

Deletes a key from the store. Supports dot-notation. Logs the deletion to the WAL.

keystring required

The key to remove.

Example
await localDb.remove("user.settings");

has(key)

has(key)#

Checks if a key exists in the store.

keystring required

The key to check.

Example
if (await localDb.has("session.id")) { ... }

keys(pattern)

keys(pattern)#

Returns all keys in the store flattened (dot-notation). Supports a simple regex-like pattern matching.

patternstring

Optional pattern (e.g., players.*).

Example
const allPlayers = await localDb.keys("players.*");

clear()

clear()#

Clears the entire database, resets the in-memory store, and empties the WAL file.

Example
await localDb.clear();

shutdown()

shutdown()#

Gracefully shuts down the database. Performs a final compaction, flushes buffers, and closes file streams.

Example
await localDb.shutdown();