@nerd-coder/fastify-mongoose
Advanced tools
Comparing version 0.1.1 to 0.2.0
{ | ||
"name": "@nerd-coder/fastify-mongoose", | ||
"version": "0.1.1", | ||
"version": "0.2.0", | ||
"description": "A Fastify plugin to connect to a MongoDB instance via the Mongoose ODM", | ||
@@ -5,0 +5,0 @@ "main": "src/index.js", |
const fp = require('fastify-plugin') | ||
const mongoose = require('mongoose') | ||
const parseEnv = require('./parseEnv') | ||
const config = { | ||
host: parseEnv('MONGO_HOST', 'mongodb://localhost'), | ||
name: parseEnv('MONGO_NAME', 'ndx'), | ||
user: parseEnv('MONGO_USER', ''), | ||
pass: parseEnv('MONGO_PASS', ''), | ||
} | ||
module.exports = fp( | ||
async function(fastify, { url, connectionOptions, models = {} }) { | ||
const { user, pass, dbName, ...otherOptions } = connectionOptions || {} | ||
const cnn = await mongoose.createConnection(url || config.host, { | ||
useNewUrlParser: true, | ||
dbName: dbName || config.name, | ||
...(user || config.user ? { user: user || config.user } : {}), | ||
...(pass || config.pass ? { pass: pass || config.pass } : {}), | ||
...otherOptions, | ||
}) | ||
const cnn = await mongoose.createConnection(url, connectionOptions) | ||
for (const key in models) | ||
if (models.hasOwnProperty(key)) cnn.model(key, models[key]) | ||
for (const key in models) if (models.hasOwnProperty(key)) cnn.model(key, models[key]) | ||
@@ -26,0 +10,0 @@ fastify.decorate('mongoose', cnn) |
4031
5
119