This waigo plugin provides:
- Mongo database connection (mongoose)
- Mongo session store (koa-session-mongo)
- A Mongoose schema constructor which makes it easy to create view objects from model instances.
- Startup step to ensure indexes are ready.
For your convenience mongoose-q is used as a wrapper around mongoose, so that you can easily obtain Promises from model queries.
$ npm install waigo-mongo
In your configuration file enable the database startup step and add configuration info:
module.exports = function(config) {
...
config.startupSteps = [
...
'database'
...
];
config.db = {
mongo: {
host: '127.0.0.1',
port: '27017',
db: 'your_db_name'
}
}
...
}
Edit the session configuration:
module.exports = function(config) {
...
config.middleware.options.sessions = {
...
store: {
type: 'mongo',
config: {
/* see koa-session-mongo module for configuration options */
}
}
...
}
...
}
Note: it is possible to pass your Mongoose db connection to the mongo session store, see koa-session-mongo for details.
First define your model somewhere. Here will do it in a startup step:
// file: support/startup/defineModels.js
"use strict";
var waigo = require('waigo'),
schema = waigo.load('support/db/mongoose/schema');
module.exports = function*(app) {
app.models = {};
var schema = schema.create({
name: String,
}, {
/** Automatically add `created_at` and `updated_at` fields and manage them on save() */
addTimestampFields: true
});
app.models[modelClass.modelName] = app.db.model('MyModel', schema);
};
Then you can use the model and render its instances:
// file: controllers/main.js
var waigo = require('waigo');
exports.index = function*() {
yield this.render('show', {
item: this.app.models.MyModel.findOne(this.params.id);
});
};
In the above controller the model instance gets rendered into a view object. Its _id
, name
, created_at
and updated_at
keys will all be rendered.
You can control which keys get rendered, and you can even choose to render each key differently:
// file: support/startup/defineModels.js
"use strict";
var waigo = require('waigo'),
schema = waigo.load('support/db/mongoose/schema');
module.exports = function*(app) {
app.models = {};
var schema = schema.create({
name: String,
}, {
addTimestampFields: true
});
schema.method('viewObjectKeys', function() {
return ['name']; // only render the `name` key in the view object
});
schema.method('formatForViewObject', function*(ctx, key, val) {
if ('name' === key) {
return '[' + name + ']';
} else {
return val;
}
});
app.models[modelClass.modelName] = app.db.model('MyModel', schema);
};
Now the controller render call will result in a view object containing just the name
key, and the value of this key will be the name
model instance value surrounded by square brackets ([]
).
If you enable this startup step then the mongoose ensureIndexes()
call will be used to ensure that MongoDB has actually setup the indexes you've specified in your models, for maximum performance.
In your configuration file enable the checkMongooseIndexes
startup step after the models
step:
module.exports = function(config) {
...
config.startupSteps = [
...
'database',
'models',
'checkMongooseIndexes'
...
];
config.db = {
mongo: {
host: '127.0.0.1',
port: '27017',
db: 'your_db_name'
}
}
...
}
Now during startup you will see something like:
2014-09-17T17:07:38.074Z - debug: Checking Mongoose db indexes...
MIT - see LICENSE.md