Created
January 19, 2022 18:28
-
-
Save brianzelip/6deb77132751a0363f3b07ca62399329 to your computer and use it in GitHub Desktop.
Revisions
-
brianzelip created this gist
Jan 19, 2022 .There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters. Learn more about bidirectional Unicode charactersOriginal file line number Diff line number Diff line change @@ -0,0 +1,18 @@ const dotenv = require('dotenv').config(); const mongoose = require('mongoose'); const fp = require('fastify-plugin'); async function mongooseConnect(fastify, options, done) { try { await mongoose.connect(process.env.DB_URI); console.log('DB connected!'); } catch (err) { console.log(err); } fastify.decorate('db', mongoose.connection); // usage example: `fastify.db.users.find({})` done(); } module.exports = fp(mongooseConnect);