config
- Description:
Configuration and utility functions for managing MongoDB connections.
- Source:
Members
(static) client :MongoClient|null
- Description:
MongoDB client instance. Initialized as
null
and set upon first connection.
- Source:
MongoDB client instance. Initialized as null
and set upon first connection.
Type:
- MongoClient | null
(static) clientPromise :Promise.<MongoClient>|null
- Description:
Promise for the MongoDB client instance. Ensures only one connection is created.
- Source:
Promise for the MongoDB client instance. Ensures only one connection is created.
Type:
- Promise.<MongoClient> | null
(static, constant) mongoUrl :string
- Description:
MongoDB connection URI from the environment variable
MONGO_URI
. Throws an error if not set.
- Source:
MongoDB connection URI from the environment variable MONGO_URI
.
Throws an error if not set.
Type:
- string
(static, constant) swaggerOptions :swaggerJsdoc.Options
- Description:
Swagger configuration options for generating the OpenAPI documentation. Defines metadata, servers, and paths for the API documentation.
- Source:
Properties:
Name | Type | Description | |||||||||||||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
definition |
object | OpenAPI specification details. Properties
|
|||||||||||||||||||||||||||
apis |
string | Glob pattern to locate route files for Swagger documentation. |
Swagger configuration options for generating the OpenAPI documentation. Defines metadata, servers, and paths for the API documentation.
Type:
- swaggerJsdoc.Options
Example
const swaggerOptions = {
definition: {
openapi: '3.0.0',
info: {
title: 'My API',
version: '1.0.0',
description: 'API documentation',
},
servers: [{ url: 'http://localhost:5000' }],
},
apis: ['./src/routes/*.ts'],
};
(static, constant) swaggerSpec :object
- Description:
Generates the Swagger specification using the provided options. This specification can be used to configure Swagger-UI or similar tools.
- Source:
Properties:
Name | Type | Description |
---|---|---|
paths |
object | Contains the paths and endpoints documented in your API. |
components |
object | Holds reusable components such as schemas or responses. |
Generates the Swagger specification using the provided options. This specification can be used to configure Swagger-UI or similar tools.
Type:
- object
Example
import swaggerSpec from './swaggerConfig';
app.use('/api-docs', swaggerUi.serve, swaggerUi.setup(swaggerSpec));
Methods
(async, static) closeClient() → {Promise.<void>}
- Description:
Closes the MongoDB client connection. Ensures the client and its promise are reset to avoid stale connections.
- Source:
Example
await closeClient();
console.log('Connection closed');
Throws:
-
Throws an error if the client fails to close.
- Type
- Error
Returns:
Resolves when the client is successfully closed.
- Type
- Promise.<void>
(async, static) connectToDatabase(dbNameopt, nullable) → {Promise.<Db>}
- Description:
Connects to the specified database using the MongoDB client. If no database name is provided, the default database is used.
- Source:
Example
const db = await connectToDatabase("myDatabase");
console.log(db.databaseName);
Parameters:
Name | Type | Attributes | Description |
---|---|---|---|
dbName |
string |
<optional> <nullable> |
Name of the database to connect to. Optional. |
Throws:
-
Throws an error if the database connection fails.
- Type
- Error
Returns:
Resolves to the database instance.
- Type
- Promise.<Db>
(async, static) getClient() → {Promise.<MongoClient>}
- Description:
Retrieves the MongoDB client instance, connecting if necessary. Ensures only one connection is created across multiple calls.
- Source:
Example
const client = await getClient();
console.log(client.isConnected());
Throws:
-
Throws an error if the client cannot be connected.
- Type
- Error
Returns:
Resolves to the MongoDB client instance.
- Type
- Promise.<MongoClient>