メインコンテンツまでスキップ

connect()

This function is used to connect simply-xp to a database, and configure client options.

If options.type is omitted, connect() defaults to mongodb at runtime.

When provided, xp_rate in options must be "slow", "normal", "fast", or a finite number greater than 0.

Parameters

ParameterTypeRequiredDescription
uristringThe URI to connect to the database.
optionsConnectionOptionsThe options to configure the client.

Returns

  • Promise<boolean> - If the connection was successful.

Throws

  • XpFatal - If the connection was unsuccessful, or if the URI is invalid.
  • XpFatal with error.code === "SX_ADAPTER_MISSING" - If the adapter package for the selected type (mongodb or better-sqlite3) is not installed. The message includes the exact install command to run.

[JS] Example

const { connect } = require("simply-xp");

connect("path/to/file.sqlite", { type: "sqlite" }) // Returns Promise<boolean>
.then(console.log) // Returns true if connection was successful
.catch((err) => console.log(err)); // Returns the error