New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

mortimer

Package Overview
Dependencies
Maintainers
1
Versions
11
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

mortimer

rest interface for mongoose models, built on top of express

  • 0.1.8
  • Source
  • npm
  • Socket score

Version published
Weekly downloads
6
decreased by-64.71%
Maintainers
1
Weekly downloads
 
Created
Source

mortimer = MOngoose ResT

Build Status

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');

// Define your mongoose models.
var Author = ..
var Book = ..

// Initialize express.
var app = express();

// Initialize Mortimer.
var mrt = new Mortimer();

// Let mortimer generate standard endpoints for your models.
// It will automatically bind get, post, put, del endpoints for your models
// as well as collections.
mrt.router(Book).bind(app);
mrt.router(Author).bind(app);

/*
// This is essentially the same as saying:
app.get('/api/v1/books', getAllBooks);
app.post('/api/v1/books', createBook);
app.put('/api/v1/books', updateAllBooks);
app.del('/api/v1/books', deleteAllBooks);
app.get('/api/v1/books/:bookId', getBookById);
app.put('/api/v1/books/:bookId', updateBookById);
app.del('/api/v1/books/:bookId', deleteBookById);
// The difference is the route handlers are generated by Mortimer.
*/

// Finally, bind app to a port.
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');

// Define your mongoose models.
var Author = ..
var Book = ..

// Initialize Mortimer.
var mrt = new Mortimer();

// Initialize express.
var app = express();

// Setup a custom POST api for creating a new Author model
// but only if the user is logged in and is an administrator.
var isLoggedIn = ...
var isAdmin = ...
app.post( '/api/v1/authors', isLoggedIn, isAdmin, mrt.create(Author));

// Finally bind app to a port.
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

  1. 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.

FAQs

Package last updated on 08 Dec 2012

Did you know?

Socket

Socket for GitHub automatically highlights issues in each pull request and monitors the health of all your open source dependencies. Discover the contents of your packages and block harmful activity before you install or update your dependencies.

Install

Related posts

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc