Sur cette page
Database
Ce contenu n’est pas encore disponible dans votre langue.
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.
Methods
Section titled “Methods”Database.getCollection()
Section titled “Database.getCollection()”Returns a collection from the database.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| collection | string | ✅ | The name of the collection to get. |
Returns Collection - MongoDB Collection
Section titled “Returns Collection - MongoDB Collection”Database.createOne()
Section titled “Database.createOne()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| query | UserOptions LevelRoleOptions | ✅ | The options for user or level role. |
Returns Promise<UserResult | LevelRoleResult> - User or Level Role
Section titled “Returns Promise<UserResult | LevelRoleResult> - User or Level Role”Database.deleteMany()
Section titled “Database.deleteMany()”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).
Parameters
Section titled “Parameters”| 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”Database.deleteOne()
Section titled “Database.deleteOne()”Deletes a single document from the database.
Parameters
Section titled “Parameters”| 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”Database.findOne()
Section titled “Database.findOne()”Finds a document from the database.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| query | UserOptions LevelRoleOptions | ✅ | The options for user or level role. |
Returns Promise<UserResult | LevelRoleResult> - User or Level Role
Section titled “Returns Promise<UserResult | LevelRoleResult> - User or Level Role”Database.find()
Section titled “Database.find()”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.
Parameters
Section titled “Parameters”| 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. |
Returns Promise< UserResult[] | LevelRoleResult[] > - User or Level Role
Section titled “Returns Promise< UserResult[] | LevelRoleResult[] > - User or Level Role”Database.findAll()
Section titled “Database.findAll()”Finds all documents from the database.
Parameters
Section titled “Parameters”| 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. |
Returns Promise< UserResult[] | LevelRoleResult[] > - User or Level Role
Section titled “Returns Promise< UserResult[] | LevelRoleResult[] > - User or Level Role”Database.findGlobalLeaderboard()
Section titled “Database.findGlobalLeaderboard()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| limit | number | ❌ | Optional max number of users to return. |
Returns Promise< UserResult[] > - One row per user, sorted by XP descending.
Section titled “Returns Promise< UserResult[] > - One row per user, sorted by XP descending.”Database.updateOne()
Section titled “Database.updateOne()”Updates a document in the database.
Parameters
Section titled “Parameters”| 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 Promise<UserResult | LevelRoleResult> - User or Level Role
Section titled “Returns Promise<UserResult | LevelRoleResult> - User or Level Role”Database.namespace()
Section titled “Database.namespace()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| name | string | ✅ | Namespace, usually your plugin’s package name. |
Returns PluginStore
Section titled “Returns PluginStore”| 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. |
Throws
Section titled “Throws”XpFatal- If no name is provided, or if there is no database connection.
Example
Section titled “Example”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 downDatabase.countUsersWithMoreXp()
Section titled “Database.countUsersWithMoreXp()”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.
Parameters
Section titled “Parameters”| Parameter | Type | Required | Description |
|---|---|---|---|
| guildId | string | ✅ | The guild ID. |
| xpValue | number | ✅ | Count users with XP greater than this value. |