mortimer = MOngoose ResT
Gist
Mortimer is a tool that creates standard CRUD api endpoints on expressjs for predefined mongoose models.
Examples
Example showing a one-liner mortimer setup that gives you all standard CRUD endpoints. This is perfect for bootstraping projects.
var express = require('express');
var Mortimer = require('mortimer');
var Author = ..
var Book = ..
var app = express();
var mrt = new Mortimer();
mrt.router(Book).bind(app);
mrt.router(Author).bind(app);
app.listen(3000);
This example shows off a more complex api setup where Mortimer's middleware is be used in conjunction with other specialized middleware.
var express = require('express');
var Mortimer = require('mortimer');
var Author = ..
var Book = ..
var mrt = new Mortimer();
var app = express();
var isLoggedIn = ...
var isAdmin = ...
app.post( '/api/v1/authors', isLoggedIn, isAdmin, mrt.create(Author));
app.listen(3000);
API
Mortimer will generate endpoints to create, read, delete and update for both single resources and collections.
Mortimer is configurable, lightweight and supports pagination, fitlering and field sets for endpoints that make sense to have them.
For more flexibility it also exposes middleware for all rest verbs.
Mortimer
Constructor function that initializes the mortimer setup.
@param {String} [options.base] - defaults `/api`, mind the trainling slash.
@param {String} [options.version] - defaults to `v1`.
.router
Sets up a complete REST api both for single resources and collections.
@param {Object} model - an instance of the mongoose.Model class you want to create endpoints for.
@param {Object} options - the same options as the `Mortimer` constructor, these override options passed to constructor.
@param {String} [options.base] - defaults `/api`, mind the trainling slash.
@param {String} [options.version] - defaults to `v1`.
.middleware
Returns a middleware function that runs the specified query in express AND returns the response to the client.
@param {Object} model - instance of the mongoose.Model class you want to run the query on.
@param {String} action - the REST action to perform against the `model`
action = 'read' - returns a plain object representation of the specified `model`
- usefull for GET requests. You need to pass an `:modelNameId` url param.
action = 'create' - creates a new resource of type `model` with a specified `:modelNameId`
- usefull for POST requests, expects a request body to be sent as JSON.
action = 'update' - updates a resource of type `model` with a specified `:modelNameId`
action = 'delete' - removes a resource of type `model` specified by a `:modelNameId`
action = 'readAll' - return all resources of type `model`.
action = 'updateAll' - update all resources of type `model`. Expects a content body to be received as JSON object
action = 'deleteAll' - removes all resources of type `model`
Collection middleware (i.e. readAll
, updateAll
, deleteAll
) also support filtering, specific fields and pagination. Just add to your request urls:
?offset=0&limit=10
for pagination?fields=field1,field2,field3
to only select certain fields from a resource, coma separated.?field1=value1&field2=value2
to select only the resources that have field1=value1, field2=value2, etc.
.read | .create | .update | .delete | .readAll | .updateAll | .deleteAll
These are convenience methods for the .middleware
function above
@param {Object} model - instance of the mongoose.Model class you want to run the query on.
Development and Contribution
To run tests, you need an instance of mongo running on localhost on port 27017.
You can change the host and port for your mongo by manually editing /test/fixtures/index.js
git clone git@github.com:topliceanu/mortimer.git
cd mortimer
npm install
mocha
Resources
- W3C HTTP Method Descriptions
Licence
(The MIT License)
Copyright (c) 2012 Alexandru Topliceanu
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
'Software'), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED 'AS IS', WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.