cruder
CRUD for express and mongoose.
Usage
mongoose = require "mongoose"express = require "express"cruder = require "cruder" db = mongoosecreateConnection "mongodb://localhost/test" UserSchema = username: type: Stringrequired: true password: type: Stringrequired: true User = dbmodel "users"UserSchema app = expressappuse expressbodyParser resource = cruder appdb # first argument is model name or model class, second is resource options resource "users" collection: get: : -> @Modelfindsortusername: 1 applisten 3000 if requiremain is module
Available options and default values
Default values here is pseudo-values used for understanding.
options = # Document field name used for Last-Modified header. # If model has date field and this field updated on every change # then you should use name of this field. modificationTimeField: undefined # Express-like url pattern used for Location header. # Url params replaced by document fields. # Required if options.document.url is RegExp. # Example behaviour: # if options.locationUrl is "/:group/users/:_id" # and doc._id is "777" # and doc.group is "admins" # then Location header is "/admins/users/777" locationUrl: optionsdocumenturl # Whole collection actions. collection: # Collection actions url. # Can be express-like url pattern or RegExp. url: "/" + ModelmodelName # Url params definition using for model queries. # Requied if options.collection.url is RegExp. # Example behaviour: # if options.collection.url is "/users/:paramName" # and options.collection.params is {"paramName": "modelField"} # then options.collection.get.query is # Model.find({modelField: req.params.paramName}) params: /:/gexec optionscollectionurl # GET / get: # Respond with 405 status if disabled. _disabled: false # Model query. # Can be factory function or query object. # Can use @Model for access to resource model. : Modelfind # Function used for state changes before sending response. # Should return modified documents array. : docs # Function used to perform actions after response is sent. : # POST / post: # Respond with 405 status if disabled. _disabled: false # New document factory function. # Must return new document object. : reqbody # Function used for state changes before sending response. # Should return modified document object. : doc # Function used to perform actions after response is sent. : # PUT / # Responds with 405 status. put: # Function used for state changes before sending response. : # Function used to perform actions after response is sent. : # DELETE / delete: # Respond with 405 status if disabled. _disabled: false # Model query. # Can be factory function or query object. # Can use @Model for access to resource model. : Modelremove # Function used for state changes before sending response. : # Function used to perform actions after response is sent. : # One document actions. document: # Document actions url. # Can be express-like url pattern or RegExp. url: "/" + ModelmodelName + "/:id" # Url params definition using for model queries. # Requied if options.document.url is RegExp. # Example behaviour: # if options.document.url is "/users/:id" # and options.document.params is {"id": "_id"} # then options.document.get.query is # Model.findOne({_id: req.params.id}) params: /:/gexec optionsdocumenturl # GET /:id get: # Respond with 405 status if disabled. _disabled: false # Model query. # Can be factory function or query object. # Can use @Model for access to resource model. : ModelfindOne _id: reqparamsid # Function used for state changes before sending response. # Should return modified document. : doc # Function used to perform actions after response is sent. : # POST /:id # Responds with 405 status. post: # Function used for state changes before sending response. : # Function used to perform actions after response is sent. : # PUT /:id put: # Respond with 405 status if disabled. _disabled: false # Model query. # Can be factory function or query object. # Can use @Model for access to resource model. : ModelfindOne _id: reqparamsid # Function used for state changes before saving document. # Must return modified document. : data = reqbody delete data_id dockey= value for keyvalue of data doc # Function used for state changes before sending response. # Should return modified document. : doc # Function used to perform actions after response is sent. : # DELETE /:id delete: # Respond with 405 status if disabled. _disabled: false # Model query. # Can be factory function or query object. # Can use @Model for access to resource model. : ModelfindOne _id: reqparamsid # Function used for state changes before sending response. : # Function used to perform actions after response is sent. :
Events
Every controller emits event before calling beforeSaving
, beforeSending
,
or afterSending
. You can subscribe to events in four ways:
- Subscribe to event from all controllers:
resource"users"on "beforeSending" resset "Expires"Datenow + 1000 * 60 * 60toGMTString
-
Subscribe to event from collection or document controllers:
resource"users"on "document:beforeSending"if docresset "X-Doc-Id"doc_id -
Subscribe to event from concrete method from collection and document controllers:
resource"users"on "get:afterSending" consolelog requrldata
- Subscribe to event from concrete controller:
resource"users"on "post:collection:afterSending" consolelog "Created new document"doc