Package Exports
- @lykmapipo/mongoose-connection
This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (@lykmapipo/mongoose-connection) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.
Readme
mongoose-connection
simplified mongoose connection setup and model loading.
It parse process.env.MONGODB_URI
or process.env.MONGODB_URL
to obtain
connection options if none specified.
Requirements
- NodeJS v9.3.0+
- mongoose v5.0.9+
Install
$ npm install --save @lykmapipo/mongoose-connection
Usage
const mongoose = require('@lykmapipo/mongoose-connection');
mongoose.loadModels();
mongoose.open(uri);
API
open([uri:String],[options:Object],[done:Function])
Establish mongoose connection
Options
uri:String
- valid mongodb connection uri. Default toprocess.env.MONGODB_URI
orprocess.env.MONGODB_URL
options:Object
- valid mongoose connection optionsdone:Function
- a callback to invoke on success or error
Examples
mongoose.open();
...
mongoose.open('mongodb://localhost/test');
...
mongoose.open({
username: 'user',
password: 'pass',
host: 'localhost',
port: 27017,
database: 'test'
});
loadModels([options:Object])
Perform recursive loading of mongoose model(s) based on specified options.
Best to be called during application setup or boot phase
.
Options
cwd:String
- currect working directory. Default to 'process.cwd()'. Used as root path to resolve 'valid mongoose model' to load.path:String|[String]
- path or collection of pathrelative
tocwd
where valid mongoose model definition resides. Default toprocess.cwd()
suffix:String
- model file name suffix. Default to_model
e.guser_model.js
Examples
mongoose.loadModels({ path: './models' });
...
mongoose.loadModels({ path: ['./feature/product', './feature/payment'] });
Testing
Clone this repository
Install all development dependencies
$ npm install
- Then run test
$ npm test
Contribute
It will be nice, if you open an issue first so that we can know what is going on, then, fork this repo and push in your ideas. Do not forget to add a bit of test(s) of what value you adding.
Licence
The MIT License (MIT)
Copyright (c) 2018 lykmapipo & Contributors
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.