Mongster

Client

Connect, transact, and work with one or more MongoDB databases

The client layer comes in two forms:

  • mongster, a shared singleton client,
  • MongsterClient, a class for dedicated client instances.
import { ,  } from "mongster";

await .("mongodb://127.0.0.1:27017/app");

const  = new ();
await .("mongodb://127.0.0.1:27017/reporting");

const  = await .();

await .();

Constructor

new MongsterClient(uri?: string, options?: MongsterClientOptions)

You can pass the URI and options up front, or call connect() later. The options object is the same in both places.

Methods

MethodWhat it does
connect(uri?, options?)Connect to MongoDB and optionally configure retries or auto index sync
disconnect()Close the underlying MongoDB client
syncIndexes()Sync indexes for every model registered on this client
ping(dbToPing?)Check connectivity
isConnected()Read the connection flag
getClient()Access the raw MongoDB client
getDb(name?)Access a database handle
getOptions()Read the merged options object (constructor + connect() overrides)
model(name, schema)Create a model on this client
transaction(callback, options?)Run a MongoDB transaction with automatic cleanup
startSession(options?)Create a manual session

Connection options

OptionTypeDefaultWhat it does
retryConnectionnumber1Number of connection attempts (including the first) before failing.
retryDelayMsnumber200Base delay between retries. Random jitter is added on top.
autoIndexboolean | { syncOnConnect?: boolean }trueWhen true, schema indexes are synced lazily on first model operation. With { syncOnConnect: true }, the sync runs as soon as connect() resolves.

These are layered on top of the MongoDB driver's own connection options.

Transactions

transaction(callback, options?) wraps session.withTransaction(...) and routes failures through TransactionError. The callback receives a context with session and a use(Model) helper that returns a transaction-scoped model. See the Transactions guide.

Manual sessions

startSession(options?) returns a raw ClientSession. You take responsibility for endSession() and any retry semantics. See the Transactions guide for when to reach for this.

On this page