0.1.0 • Published 8 years ago

restify-microservice-mongoose v0.1.0

Weekly downloads
4
License
MIT
Repository
github
Last release
8 years ago

resetify-microservice-mongoose

a mongoose.js adapter for restify-microservice

Install

npm install --save restify-microservice-mongoose

Getting Started

First, define one or more mongoose connections

// in /config/connections.js

var mongooseAdapter = require('resetify-microservice-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) {
    var blogSchema = new Schema({
          title:  String,
          author: String,
          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: function(req, res) {
        var microservice = req.microservice;
            Posts = microservice.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});
        });
    }
}