Created
August 9, 2016 07:32
-
-
Save svnlto/13552766131087e5fadb156c35ba6d81 to your computer and use it in GitHub Desktop.
This file contains 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
import mongoose from 'mongoose'; | |
import grid from 'gridfs-stream'; | |
import config from 'config'; | |
import url from 'url'; | |
const mongodb = config.get('mongodb'); | |
const { Schema } = mongoose; | |
grid.mongo = mongoose.mongo; | |
const mongodbUri = url.format({ | |
slashes: true, | |
protocol: mongodb.protocol, | |
hostname: mongodb.host, | |
port: mongodb.port | |
}); | |
export default (() => { | |
const connectDb = (uri, useGridFS=false) => { | |
let db = mongoose.createConnection(mongodbUri); | |
db.on('error', console.error.bind(console, 'connection error')); | |
db.once('open', () => { | |
console.log(`Connection with database \'${uri}\' succeeded.`); | |
}); | |
if (!useGridFS) { | |
return db; | |
} else { | |
return grid(db.db); | |
} | |
}; | |
return Object.freeze({ | |
connectDb, | |
Schema | |
}); | |
})(); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment