/mongoose-rest-helper

Some helper functions to expose mongoose models through rest. Very specific to our apps.

Primary LanguageCoffeeScriptMIT LicenseMIT

Build Status Coverage Status NPM Version Dependency Status NPM Downloads Issues API Documentation

mongoose-rest-helper

Provides some convenience functions that can bridge mongoose and your REST endpoints. Nothing special really.

How to get started:

npm install mongoose-rest-helper

Usage

The following illustrates a typical use case where we map CRUD functions to the mongoose-rest-helper. This is verbose, you could easily create this for all your models.

mongooseRestHelper = require 'mongoose-rest-helper'

module.exports = class RoleMethods
  UPDATE_EXCLUDEFIELDS = ['_id']

  constructor:(@model) ->

Return all objects (with pagination, and scoping through accountId)

  all: (accountId,options = {},cb = ->) =>
    return cb new Error "accountId parameter is required." unless accountId

    settings = 
        baseQuery:
          accountId : mongooseRestHelper.asObjectId accountId
        defaultSort: 'name'
        defaultSelect: null
        defaultCount: 1000
    mongooseRestHelper.all @model,settings,options, cb

Get an entity for it's id

  get: (id,options = {}, cb = ->) =>
    return cb new Error "id parameter is required." unless id
    mongooseRestHelper.getById @model,id,null,options, cb

Destroy an entity

  destroy: (id, options = {}, cb = ->) =>
    return cb new Error "id parameter is required." unless id
    settings = {}
    mongooseRestHelper.destroy @model,id, settings,{}, cb

Create an entity

  create:(accountId,objs = {}, options = {}, cb = ->) =>
    return cb new Error "accountId parameter is required." unless accountId
    settings = {}
    objs.accountId = mongooseRestHelper.asObjectId accountId
    mongooseRestHelper.create @model,settings,objs,options,cb

Update an entity

  patch: (id, obj = {}, options = {}, cb = ->) =>
    settings =
      exclude : UPDATE_EXCLUDEFIELDS
    mongooseRestHelper.patch @model,id, settings, obj, options, cb

Stuff

  • npm install
  • grunt watch
  • grunt deploy

See also

and additionally

Contributing to mongoose-rest-helper

  • Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet
  • Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it
  • Fork the project
  • Start a feature/bugfix branch
  • Commit and push until you are happy with your contribution
  • Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.
  • Please try not to mess with the package.json, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.

Copyright

Copyright (c) 2013-2014 Martin Wawrusch See LICENSE for further details.