1.0.44 • Published 5 months ago

@waleetechnologies/common-im v1.0.44

Weekly downloads
-
License
MIT
Repository
-
Last release
5 months ago

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 a DatabaseConnectionError 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 into process.env.

Please refer to the respective library's documentation for more details.

1.0.44

5 months ago

1.0.43

5 months ago

1.0.42

5 months ago

1.0.41

5 months ago

1.0.39

5 months ago

1.0.38

5 months ago

1.0.40

5 months ago

1.0.22

5 months ago

1.0.21

5 months ago

1.0.20

5 months ago

1.0.26

5 months ago

1.0.25

5 months ago

1.0.24

5 months ago

1.0.23

5 months ago

1.0.29

5 months ago

1.0.28

5 months ago

1.0.27

5 months ago

1.0.33

5 months ago

1.0.32

5 months ago

1.0.31

5 months ago

1.0.30

5 months ago

1.0.37

5 months ago

1.0.36

5 months ago

1.0.35

5 months ago

1.0.34

5 months ago

1.0.19

5 months ago

1.0.18

5 months ago

1.0.17

5 months ago

1.0.16

5 months ago

1.0.15

5 months ago

1.0.14

5 months ago

1.0.13

5 months ago

1.0.12

5 months ago

1.0.9

5 months ago

1.0.8

5 months ago

1.0.6

5 months ago

1.0.3

5 months ago