@colucom/osseus-mongo
v2.0.3
Published
Osseus mongo
Downloads
9
Keywords
Readme
Osseus Mongo
mongoose based osseus mongodb connection module
Install
$ npm install @colucom/osseus-mongo
Usage
Configuration
OSSEUS_MONGO_URI
- connection uri stringOSSEUS_MONGO_OPTIONS
- connection options object, default is:
{
"keepAlive": 1,
"connectTimeoutMS": 30000,
"socketTimeoutMS": 30000,
"useMongoClient": true
}
Simple usage
- Configuration file / secret:
OSSEUS_MONGO_URI: 'mongodb://<mongosever>:<port>/<databasename>'
- Model file (example: models/MyModel.js):
module.exports = (osseus) => {
const db = osseus.mongo
const mongoose = osseus.mongo.mongoose
const Schema = mongoose.Schema
const myModelSchema = new Schema({
name: String
});
const MyModel = db.model('MyModel', myModelSchema)
osseus.mongo.appModels = osseus.mongo.appModels || {}
osseus.mongo.appModels['MyModel'] = MyModel
}
- Require model after osseus init:
require('./models/MyModel')(osseus);
- You can access models by osseus.mongo.appModels['modelName'] from anywhere in the application. For example:
module.exports = (osseus) => {
return {
examplePOST: (req, res, next) => {
const newMyModel = new osseus.mongo.appModels.MyModel({
name: 'Someone'
})
newMyModel.save()
.then(() => {
console.log('Document successfully saved')
})
.catch(err => {
throw err
})
}
}
}
Contributing
Please see contributing guidelines.
License
Code released under the MIT License.