0.2.0 • Published 8 years ago

fh-rest-mongodb-adapter v0.2.0

Weekly downloads
-
License
MIT
Repository
-
Last release
8 years ago

fh-rest-mongodb-adapter

A MongoDB adapater compatible with the fh-rest-express-router. Allows one to easily create a RESTful API that uses MongoDB as a datastore.

Example

The code below will generate routes that can be used like this

'use strict';

var app = require('express')()
  , fhRestRouter = require('fh-rest-express-router')
  , fhRestMongoAdapter = require('fh-rest-mongodb-adapter');

app.use(
  '/tasks',
  fhRestMongoAdapter({
    // Collection to expose
    collection: 'user-tasks',

    // Optional mongo connection string. When on RHMAP this is filled for you
    mongoUrl: 'mongodb://host-name.com/27017/database-name',

    // Indexes you'd like to apply to collections in use
    indexes: [{
      userId: 1
    }]
  })
);

app.listen(3001);

Direct API

Works just like the memory adapter, with the exception being params.query passed to adapter.list can contain Mongo specific operators, e.g $gte. For example:

var payloads = fhRestMongoAdapter({
  // Collection to expose
  collection: 'payloads'
});

payloads.list({
  query: {
    createDate: {
      $lte: new Date()
    }
  }
}, function (err, payloads) {});