Skip to content
This repository has been archived by the owner on Oct 21, 2023. It is now read-only.
/ mongo Public archive

Mongo database connection and session store for Waigo

License

Notifications You must be signed in to change notification settings

waigo/mongo

Repository files navigation

Build Status

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.

Installation

$ npm install waigo-mongo

Using the database connection

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'
    }
  }
  
  ...
}

Using the session store

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.

Using the Mongoose schema constructor

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 ([]).

Startup step to check Mongoose indexes

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...

License

MIT - see LICENSE.md

About

Mongo database connection and session store for Waigo

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published