@waleetechnologies/common-im v1.0.44
Database Operations
This section provides documentation for the database operations in the repository.ts
file.
Functions
dbGet
This function retrieves data from a MongoDB collection. It can fetch either a single document or multiple documents based on the isSingle
parameter. It also supports advanced features like field selection, population of referenced documents, sorting, skipping, limiting, and lean queries.
dbCount
This function counts the number of documents in a MongoDB collection that match a certain query. It returns the count as a number.
dbDistinct
This function finds distinct values of a specified field across a MongoDB collection. It returns the distinct values as an array.
dbSave
This function saves a new document to a MongoDB collection. It returns the newly created document.
dbSaveMany
This function saves multiple new documents to a MongoDB collection. It doesn't return anything.
dbUpdate
This function updates an existing document in a MongoDB collection. It can update multiple documents if the options
parameter is set to { multi: true }
. It returns the result of the update operation.
dbUpdateMany
This function updates multiple documents in a MongoDB collection. It returns the result of the update operation.
dbRemove
This function removes documents from a MongoDB collection that match a certain query. It doesn't return anything.
dbDeleteMany
This function deletes multiple documents from a MongoDB collection that match a certain query. It doesn't return anything.
dbAggregate
This function performs an aggregation operation on a MongoDB collection. It returns the result of the aggregation operation.
dbRegister
This function saves a new document to a MongoDB collection and hashes its password field. It returns the newly created document.
mongoose.ts
This file contains the configuration for the MongoDB database connection using Mongoose. Here's a brief overview of the key parts:
Connection Events
Mongoose connection emits several events that you can listen to, as shown in the code:
reconnected
: This event is emitted when Mongoose successfully reconnects to the MongoDB server after it lost connection. In this case, a message "App DB reconnected" is logged to the console, unless the application is running in test mode.disconnected
: This event is emitted when Mongoose lost connection to the MongoDB server. In this case, a message "App DB disconnected" is logged to the console, unless the application is running in test mode.error
: This event is emitted if an error occurs while Mongoose is connected to the MongoDB server. In this case, the error message is logged to the console and aDatabaseConnectionError
is thrown with the error message.
Remember to replace the file name and descriptions with the actual file and its purposes in your project if they are different.
Errors
The errors
folder contains custom error classes that extend the base CustomError
class. Here's a brief overview of each file:
custom-error.ts
This file contains the CustomError
abstract base class. All custom errors in the application should extend this class. It has a statusCode
property and a serializeErrors
method that should be implemented by subclasses.
bad-request-error.ts
This file contains the BadRequestError
class. It extends the CustomError
class and sets the statusCode
to 400
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when a request is malformed.
database-connection-error.ts
This file contains the DatabaseConnectionError
class. It extends the CustomError
class and sets the statusCode
to 500
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when a connection to the database cannot be established.
not-authorized-error.ts
This file contains the NotAuthorizedError
class. It extends the CustomError
class and sets the statusCode
to 401
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when a user tries to access a resource they are not authorized to access.
action-not-authorized-error.ts
This file contains the ActionNotAuthorizedError
class. It extends the CustomError
class and sets the statusCode
to 401
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when a user tries to perform an action they are not authorized to perform.
not-found-error.ts
This file contains the NotFoundError
class. It extends the CustomError
class and sets the statusCode
to 404
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when a resource cannot be found.
request-validation-error.ts
This file contains the RequestValidationError
class. It extends the CustomError
class and sets the statusCode
to 400
. The serializeErrors
method returns an array with objects that contain the error messages. This error is thrown when request validation fails.
generic-server-error.ts
This file contains the GenericServerError
class. It extends the CustomError
class and sets the statusCode
to 500
. The serializeErrors
method returns an array with an object that contains the error message. This error is thrown when an unexpected server error occurs.
Remember to replace the file names and descriptions with the actual files and their purposes in your errors
folder if they are different.
Middlewares
Middlewares are functions that have access to the request object (req
), the response object (res
), and the next middleware function in the application’s request-response cycle. The next middleware function is commonly denoted by a variable named next
.
authenticationMiddleware
This middleware function is used to authenticate requests. It checks if the request has a valid JWT token and sets the req.user
property with the user's information from the token.
errorHandlingMiddleware
This middleware function is used to handle errors that occur in the application. It sends a response with the error message and status code.
loggingMiddleware
This middleware function is used to log the details of the request and the response. It logs the method, URL, status code, and response time of the request.
Libraries
The following libraries are used in this code:
Mongoose: Mongoose is a MongoDB object modeling tool designed to work in an asynchronous environment. Mongoose supports both promises and callbacks.
Express: Express is a minimal and flexible Node.js web application framework that provides a robust set of features for web and mobile applications.
bcrypt: bcrypt is a library to help you hash passwords.
jsonwebtoken: jsonwebtoken is a library that helps you work with JWTs.
dotenv: dotenv is a zero-dependency module that loads environment variables from a
.env
file intoprocess.env
.
Please refer to the respective library's documentation for more details.
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago
5 months ago