Last active
December 11, 2017 20:13
-
-
Save BideoWego/470edf9b3484b1a182cff5a84658abf4 to your computer and use it in GitHub Desktop.
Mongoose - Setup and boilerplate files for Mongoose with seeds, config for multiple environment databases and a REPL
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 characters
// Put this before your routes | |
// ---------------------------------------- | |
// Mongoose | |
// ---------------------------------------- | |
const mongoose = require('mongoose'); | |
app.use((req, res, next) => { | |
if (mongoose.connection.readyState) { | |
next(); | |
} else { | |
require('./mongo')().then(() => next()); | |
} | |
}); |
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 characters
module.exports = { | |
development: { | |
database: "YOUR_DATABASE_NAME_development", | |
host: "localhost" | |
}, | |
test: { | |
database: "YOUR_DATABASE_NAME_test", | |
host: "localhost" | |
}, | |
production: { | |
use_env_variable: "MONGO_URL" | |
} | |
}; |
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 characters
const mongoose = require('mongoose'); | |
const bluebird = require('bluebird'); | |
mongoose.Promise = bluebird; | |
// Disable logging while testing and seeding | |
if (process.env.NODE_ENV !== 'test' && | |
!process.argv[process.argv.length - 1].match(/seed/)) { | |
mongoose.set('debug', true); | |
} | |
const models = {}; | |
// Require models here | |
models.User = require('./user'); | |
module.exports = models; |
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 characters
const mongoose = require('mongoose'); | |
const env = process.env.NODE_ENV || 'development'; | |
const config = require('./config/mongoose')[env]; | |
module.exports = () => { | |
const envUrl = process.env[config.use_env_variable]; | |
const localUrl = `mongodb://${ config.host }/${ config.database }`; | |
const mongoUrl = envUrl ? envUrl : localUrl; | |
return mongoose.connect(mongoUrl); | |
}; |
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 characters
npm install --save mongoose mongooseeder bluebird |
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 characters
const repl = require('repl').start({}); | |
const mongoose = require('mongoose'); | |
const models = require('./models/mongoose'); | |
require('./mongo')().then(() => { | |
// ---------------------------------------- | |
// Models | |
// ---------------------------------------- | |
repl.context.models = models; | |
Object.keys(models.mongoose).forEach((modelName) => { | |
repl.context[modelName] = mongoose.model(modelName); | |
}); | |
// ---------------------------------------- | |
// Logging | |
// ---------------------------------------- | |
repl.context.lg = console.log; | |
}); |
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 characters
const models = require('../models/'); | |
// ---------------------------------------- | |
// Seeds | |
// ---------------------------------------- | |
const seeds = () => {}; | |
// ---------------------------------------- | |
// Run Seeds | |
// ---------------------------------------- | |
const mongoose = require('mongoose'); | |
const mongooseeder = require('mongooseeder'); | |
const env = process.env.NODE_ENV || 'development'; | |
const config = require('../config/mongoose')[env]; | |
let mongodbUrl = config.use_env_variable ? | |
process.env[config.use_env_variable] : | |
`mongodb://${ config.host }/${ config.database }`; | |
mongooseeder.seed({ | |
mongodbUrl: mongodbUrl, | |
models: models, | |
clean: true, | |
mongoose: mongoose, | |
seeds: seeds | |
}); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment