Skip to content
On this page

Database

This class provides methods to interact with the database. It supports both MongoDB and SQLite backends and offers operations for creating, updating, deleting, and querying user and level-role documents. Methods set createdAt and lastUpdated timestamps when creating or updating records.

Returns a collection from the database.

Parameter Type Required Description
collection string ✅ The name of the collection to get.

Creates a new document in the database. The method automatically adds createdAt and lastUpdated ISO timestamps. Many fields in the data object are optional (for example name, user, level, xp, and flags for simply-xps). The method supports both MongoDB and SQLite backends.

Parameter Type Required Description
query UserOptions LevelRoleOptions ✅ The options for user or level role.

Deletes multiple documents from the database. Returns true when the underlying driver reports a result object (for SQLite the run() result is treated as truthy when successful).

Parameter Type Required Description
query UserOptions LevelRoleOptions ✅ The options for user or level role.

Returns Promise<boolean> - true if documents were deleted, otherwise false

Section titled “Returns Promise<boolean> - true if documents were deleted, otherwise false”

Deletes a single document from the database.

Parameter Type Required Description
query UserOptions LevelRoleOptions ✅ The options for user or level role.

Returns Promise<boolean> - true if a document was deleted, otherwise false

Section titled “Returns Promise<boolean> - true if a document was deleted, otherwise false”

Finds a document from the database.

Parameter Type Required Description
query UserOptions LevelRoleOptions ✅ The options for user or level role.

Finds documents from the database for a given guild. For SQLite simply-xps rows will have the flags column parsed from JSON back into an array. The collection parameter must be either simply-xps or simply-xp-levelroles.

Parameter Type Required Description
collection simply-xps
simply-xp-levelroles
✅ The name of the collection to get.
guild string ✅ The guild ID.
limit number ❌ Optional max number of documents to return.

Finds all documents from the database.

Parameter Type Required Description
collection simply-xps
simply-xp-levelroles
✅ The name of the collection to get.
limit number ❌ Optional max number of documents to return.

Finds the global leaderboard across every guild, keeping one row per user: whichever of their guild rows has the highest XP (ties broken by most recently updated, then most recently created). Used internally by leaderboard() when called without a guildId.

Parameter Type Required Description
limit number ❌ Optional max number of users to return.

Updates a document in the database.

Parameter Type Required Description
filter UserOptions LevelRoleOptions ✅ The options for user or level role.
update UserOptions LevelRoleOptions ✅ The update to apply to the document.
options { upsert?: boolean } ❌ MongoDB upsert options.

Returns a private key/value store for a plugin, so plugins can persist their own data without touching xp.database or the simply-xp tables directly.

Works on both MongoDB and SQLite. Storage is created lazily on the first write, so plugins that never store anything add nothing to your database.

Parameter Type Required Description
name string ✅ Namespace, usually your plugin’s package name.
Method Returns Description
get(key) Promise<T | null> Reads a value, or null when the key is not set.
set(key, value) Promise<void> Writes a value. Anything JSON-serialisable is accepted.
delete(key) Promise<boolean> Removes a key, returning whether it existed.
keys() Promise<string[]> Lists every key in this namespace.
clear() Promise<number> Removes every key in this namespace, returning how many.
  • XpFatal - If no name is provided, or if there is no database connection.
const { Database } = require("simply-xp");
const store = Database.namespace("simply-xp-rate-limits");
await store.set(`cooldown:${userId}`, { until: Date.now() + 60_000 });
const cooldown = await store.get(`cooldown:${userId}`);
if (cooldown && cooldown.until > Date.now()) return; // still cooling down

Counts users in a guild with strictly more XP than a given value. Used internally by fetch() and rankCard() to compute a user’s leaderboard position without loading the full guild into memory.

Parameter Type Required Description
guildId string ✅ The guild ID.
xpValue number ✅ Count users with XP greater than this value.

Returns Promise<number> - The number of users with more XP.

Section titled “Returns Promise<number> - The number of users with more XP.”