@colucom/osseus-mongo

2.0.3 • Public • Published

JavaScript Style Guide

Osseus Mongo

mongoose based osseus mongodb connection module

Install

$ npm install @colucom/osseus-mongo

Usage

Configuration

  • OSSEUS_MONGO_URI - connection uri string
  • OSSEUS_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.

Readme

Keywords

none

Package Sidebar

Install

npm i @colucom/osseus-mongo

Weekly Downloads

1

Version

2.0.3

License

MIT

Unpacked Size

9.19 kB

Total Files

9

Last publish

Collaborators

  • liorrabin
  • grebenaha
  • valery-colu
  • leonprou
  • quilamir
  • rottev