@probys/database
v0.0.2
Published
Overview The databasePlugin.ts file is a TypeScript module that defines a Fastify plugin responsible for connecting to and disconnecting from a MongoDB database using Mongoose. It utilizes the Fastify framework and incorporates hooks to manage the databa
Downloads
1
Readme
Overview The databasePlugin.ts file is a TypeScript module that defines a Fastify plugin responsible for connecting to and disconnecting from a MongoDB database using Mongoose. It utilizes the Fastify framework and incorporates hooks to manage the database connection lifecycle during Fastify's initialization and closing.
Dependencies Fastify: The web framework used for building web applications with a focus on speed and low overhead.
Import: FastifyInstance from "fastify" Import: fastifyPlugin from "fastify-plugin" Mongoose: An ODM (Object Data Modeling) library for MongoDB and Node.js.
Import: mongoose from "mongoose" Functions connectToDatabase(): Promise
Establishes a connection to the MongoDB database using the URI provided in the environment variables. Logs a success message upon successful connection. Throws an error if the connection fails. disconnectDatabase(): Promise
Disconnects from the MongoDB database using Mongoose. Logs a message indicating successful disconnection. databasePlugin(fastify: FastifyInstance): Promise
Fastify plugin function responsible for connecting to the MongoDB database and adding a hook to disconnect when Fastify is closing. Calls connectToDatabase() to establish the initial connection. Adds an "onClose" hook to invoke disconnectDatabase() when Fastify is closing.