Skip to content
This repository has been archived by the owner on Apr 17, 2023. It is now read-only.

feedhenry-staff/fh-rest-mongodb-adapter

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

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) {});