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

mycro-mongoose

v0.5.0

Published

mongoose adapter for mycro

Downloads

2

Readme

mycro-mongoose

a mongoose.js adapter for mycro

Install

npm install --save mycro-mongoose

Getting Started

First, make sure both the connections and models hooks are enabled. (These are enabled by default, however, if you've defined your own /config/hooks.js file, make sure it includes both of these)

// in /config/hooks.js
module.exports = [
    // ..
    'connections',
    'models'
    // ..
]

Then, define one or more mongoose connections

// in /config/connections.js

var mongooseAdapter = require('mycro-mongoose');

module.exports = {
    // ..
    mongo: {
        adapter: mongooseAdapter,
        config: {
            host: process.env.MONGO_HOST || 'localhost',
            port: process.env.MONGO_PORT || 27017,
            user: process.env.MONGO_USER,
            password: process.env.MONGO_PASSWORD,
            database: process.env.MONGO_DATABASE
        }
    },
    // ..
};

Next, define a mongoose model

// in /app/models/post.js

module.exports = function(connection, Schema) {
    let options = {
        collection: 'posts'
    };

    let schema = new Schema({
          title:  String,
          author: {
              type: Schema.Types.ObjectId,
              ref: 'users'
          },
          body:   String,
          comments: [{
              body: String,
              date: Date
          }],
          date: {
              type: Date,
              default: Date.now
          },
          hidden: Boolean,
          meta: {
                votes: Number,
                favs:  Number
          }
    });

    return connection.model('post', blogSchema);
}

Lastly, use it in your app!

// in /app/controllers/post.js

module.exports = {
    findPosts(req, res) {
        var Posts = req.mycro.models['post'];

        Posts.find({
            hidden: false,
            date: {
                $lte: new Date()
            }
        }, function(err, posts) {
            if (err) {
                return res.json(500, {error: err});
            }
            res.json(200, {data: posts});
        });
    }
}

Config

The connection configuration object is described in more detail below

// in config/connections.js
const mongooseAdapter = require('mycro-mongoose');

module.exports = {
    // ..
    mongo: {
        // in order to specify a mongodb connection, use this adapter as the adapter object
        adapter: mongooseAdapter,

        // all config is specified in the 'config' top level key. the key can be an object
        // or a synchronous function (executed at runtime) that is passed the mycro instance and expects
        // a config object in return
        config: {
            // either a valid mongodb url connection string can be provided
            url: 'mongodb://sampleuser:correct-horse-batter-staple@localhost:27017/test',

            // or the following are used to build a valid connection string
            host: 'localhost'
            port: 27017,
            user: 'sampleuser',
            password: 'correct-horse-batter-staple',
            database: 'test'

            // any additional connection options can be specified in an optional 'options' keys
            options: {
                replSet: {
                    sslValidate: false
                }
            }
        }
    }
    // ..
}

Testing

running the tests:

  1. Update the connection info in test/test-app/config/connections.js
  2. Run the mongod server
  3. npm test

to view coverage:

  1. Update the connection info in test/test-app/config/connections.js
  2. Run the mongod server
  3. grunt coverage

Contributing

  1. Fork it
  2. Create your feature branch (git checkout -b my-new-feature)
  3. Commit your changes (git commit -am 'Add some feature')
  4. Push to the branch (git push origin my-new-feature)
  5. Create new Pull Request

License

Copyright (c) 2015 Chris Ludden. Licensed under the MIT license.