npm package discovery and stats viewer.

Discover Tips

  • General search

    [free text search, go nuts!]

  • Package details

    pkg:[package-name]

  • User packages

    @[username]

Sponsor

Optimize Toolset

I’ve always been into building performant and accessible sites, but lately I’ve been taking it extremely seriously. So much so that I’ve been building a tool to help me optimize and monitor the sites that I build to make sure that I’m making an attempt to offer the best experience to those who visit them. If you’re into performant, accessible and SEO friendly sites, you might like it too! You can check it out at Optimize Toolset.

About

Hi, 👋, I’m Ryan Hefner  and I built this site for me, and you! The goal of this site was to provide an easy way for me to check the stats on my npm packages, both for prioritizing issues and updates, and to give me a little kick in the pants to keep up on stuff.

As I was building it, I realized that I was actually using the tool to build the tool, and figured I might as well put this out there and hopefully others will find it to be a fast and useful way to search and browse npm packages as I have.

If you’re interested in other things I’m working on, follow me on Twitter or check out the open source projects I’ve been publishing on GitHub.

I am also working on a Twitter bot for this site to tweet the most popular, newest, random packages from npm. Please follow that account now and it will start sending out packages soon–ish.

Open Software & Tools

This site wouldn’t be possible without the immense generosity and tireless efforts from the people who make contributions to the world and share their work via open source initiatives. Thank you 🙏

© 2024 – Pkg Stats / Ryan Hefner

waigo-mongo

v1.3.0

Published

Mongo database connection and session store for Waigo

Downloads

20

Readme

Build Status

This waigo plugin provides:

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