├── .gitignore ├── app.js ├── package.json ├── model ├── team.js └── db.js ├── pages.js └── readme.md /.gitignore: -------------------------------------------------------------------------------- 1 | npm-debug.log 2 | node_modules -------------------------------------------------------------------------------- /app.js: -------------------------------------------------------------------------------- 1 | var http = require('http'), 2 | db = require('./model/db'), 3 | pages = require('./pages'); 4 | 5 | http.createServer(function (req, res) { 6 | pages.index(req, res); 7 | }).listen(8888, '127.0.0.1'); -------------------------------------------------------------------------------- /package.json: -------------------------------------------------------------------------------- 1 | { 2 | "name": "MongooseCOnnectionTest", 3 | "version": "0.0.1", 4 | "private": true, 5 | "scripts": { 6 | "start": "node app" 7 | }, 8 | "dependencies": { 9 | "mongoose": "3.6.x" 10 | } 11 | } -------------------------------------------------------------------------------- /model/team.js: -------------------------------------------------------------------------------- 1 | var mongoose = require( 'mongoose' ); 2 | 3 | var teamSchema = new mongoose.Schema({ 4 | Country: String, 5 | GroupName: String, 6 | CreatedOn: Date 7 | }); 8 | 9 | var Team = module.exports = mongoose.model('Team', teamSchema); 10 | -------------------------------------------------------------------------------- /pages.js: -------------------------------------------------------------------------------- 1 | var mongoose = require( 'mongoose' ), 2 | Team = mongoose.model('Team'); 3 | 4 | exports.index = function (req, res) { 5 | Team.create({ 6 | Country : "England", 7 | GroupName: "D", 8 | CreatedOn: Date.now() 9 | }, function(err, team) { 10 | var strOutput; 11 | res.writeHead(200, { 12 | 'Content-Type': 'text/plain' 13 | }); 14 | if (err) { 15 | console.log(err); 16 | strOutput = 'Oh dear, we\'ve got an error'; 17 | } else { 18 | console.log('Team created: ' + team); 19 | strOutput = team.Country + ' created in Group ' + team.GroupName + '\nat ' + team.CreatedOn; 20 | } 21 | res.write(strOutput); 22 | res.end(); 23 | }); 24 | }; -------------------------------------------------------------------------------- /model/db.js: -------------------------------------------------------------------------------- 1 | // Bring Mongoose into the app 2 | var mongoose = require( 'mongoose' ); 3 | 4 | // Build the connection string 5 | var dbURI = 'mongodb://localhost/ConnectionTest'; 6 | 7 | // Create the database connection 8 | mongoose.connect(dbURI); 9 | 10 | // CONNECTION EVENTS 11 | // When successfully connected 12 | mongoose.connection.on('connected', function () { 13 | console.log('Mongoose default connection open to ' + dbURI); 14 | }); 15 | 16 | // If the connection throws an error 17 | mongoose.connection.on('error',function (err) { 18 | console.log('Mongoose default connection error: ' + err); 19 | }); 20 | 21 | // When the connection is disconnected 22 | mongoose.connection.on('disconnected', function () { 23 | console.log('Mongoose default connection disconnected'); 24 | }); 25 | 26 | // If the Node process ends, close the Mongoose connection 27 | process.on('SIGINT', function() { 28 | mongoose.connection.close(function () { 29 | console.log('Mongoose default connection disconnected through app termination'); 30 | process.exit(0); 31 | }); 32 | }); 33 | 34 | 35 | // BRING IN YOUR SCHEMAS & MODELS 36 | // For example 37 | require('./team'); 38 | -------------------------------------------------------------------------------- /readme.md: -------------------------------------------------------------------------------- 1 | Mongoose Default Connection 2 | =========================== 3 | 4 | A quick guide to the best practice for creating, maintaining and using a default Mongoose connection. 5 | 6 | The connection is managed in `/model/db.js`. It is opened at application start, and closed on application termination. In this file we also monitor the `connected`, `error` and `disconnected` events. 7 | 8 | A sample Mongoose schema is defined and model built in `/model/team.js`. 9 | All of the relevant code is in /model/db.js. 10 | 11 | `app.js` is just a minimal Node.js skeleton, rendering a page defined in `pages.js`. `pages.js` brings in Mongoose and the model defined in `/model/team.js`. 12 | 13 | This only requires Mongoose in the application, as noted in package.json. If you download this you can install Mongoose into the application by running `npm install` 14 | 15 | More detailed discussion around using this is available on my blog: ~theholmesoffice.com/mongoose-connection-best-practice/~ 16 | 17 | This link is now dead as the domain was poached from me 🤦‍♂️ 18 | 19 | Thanks to [3willows](https://github.com/3willows) for spotting it and providing the Web Archive link: [https://web.archive.org/web/20140330182958/http://theholmesoffice.com/mongoose-connection-best-practice/](https://web.archive.org/web/20140330182958/http://theholmesoffice.com/mongoose-connection-best-practice/) 20 | 21 | It doesn't show my nice original formatting, but you can probably figure it out. 22 | --------------------------------------------------------------------------------