MongoDB initializers for actionHero backed by MongoSkin
npm install actionhero-mongoskin**WORK IN PROGRESS
- What & Why
- How to
- Extend
- Database Seed
- Configuration
- Exposed API
```
npm install actionHero-mongoskin --save
Create an initializer, require actionHero-mongoskin then expose your initializer with mongoinit
`javascript
// initializers/mongodb.js
mongo = require('actionHero-mongoskin').mongoinit;
exports.mongodb = mongo
`
That's it! Now you can access various mongoskin reference from api.mongo.* as listed in Exposed API.
to your liking.
For that you need to manually expose your initializer rather than using actionHero-mongoskin built-in initializer as shown above.`javascript
// initializers/mongodb.js
mongo = require('actionHero-mongoskin').mongo;exports.mongodb = function(api, next) {
// Add a collection object shortcut:
mongo.mycollection = mongo.db.collection('mycollection');
api.mongo = mongo; // Now you can access
mycollection by using api.mongo.mycollection
return next();
};
`Database Seeding
By default the initializer will also run database seed in server start, useful for providing sample data in development time. All you need to do is override the api.mongo.dbSeed function to setup the database.`javascript
// initializers/mongodb.js
mongo = require('actionHero-mongoskin').mongo;exports.mongodb = function(api, next) {
mongo.dbSeed = function(next) {
api.mongo.collection('mycollection').insert({foo:'bar'}, function(error, result) {
// ... further setup
next() // <- IMPORTANT, dont forget to call this when you've finished seeding the database.
})
}
api.mongo = mongo;
return next();
};
`If you don't want to run the dbSeed, set the
seed config option of mongo section to false. See Configuration explanation below.Configuration
To provide connection configuration you need to add mongo property to your config.js file. Shown below is the default value if no configData.mongo available in configuration`javascript
configData.mongo = {
// Run database seed if true. Set to false in production.
seed: true,
host: "localhost",
port: 27017,
db: "test",
user: "",
pass: ""
}
``