Primate LogoPrimate

MongoDB

MongoDB is a document database with flexible schemas and rich querying.

Setup

Install

npm install @primate/mongodb

You need a running MongoDB server.

Configure

// config/db.ts
import mongodb from "@primate/mongodb";

export default mongodb({
  database: "app",
  // host: "localhost",
  // port: 27017,
  // username: "mongo",
  // password: "secret",
  // pool: { max: 100, connectTimeout: 30_000, idleTimeout: 0 },
});

Options

option type default description
database string — Database name
host string "localhost" MongoDB host
port number 27017 MongoDB port
username string — Username
password string — Password
pool object driver defaults Connection pool controls

Pool controls use the shared max, connectTimeout, and idleTimeout options documented in Connection pools. Timeouts are milliseconds. max is an integer from 1 to 65,535. The timeout maximum is 2,147,483,647; connectTimeout starts at 1 and idleTimeout starts at 0 (0 disables MongoDB idle expiry). Omitted values preserve MongoDB driver defaults.

Use db.fork({ pool: { max: 2 } }) to create another MongoClient and pool that privately inherit the connection configuration. Close the fork when finished; its close() does not affect the parent pool.

Previous
JSONDB
Next
MySQL