preston (master) doxdox documentation

Serves Mongoose models on a flexible, RESTful API.

# Model(restifier, model)



Name Types Description
restifier RestAPI - The RestAPI instance
model mongoose.Model - The mongoose model that is being restified.

# Model.applyModifiers(request)

Applies the modifiers of this model onto a request.


Name Types Description
request Request - The request

# Model.applyTransforms(req, doc, cb)

Applies all of the transforms defined on this model on a document.


Name Types Description
req Request - The request.
doc Document - The document(s) to apply the transforms to.
cb Function - The cb(err, result) where result is the transformed document.

# Model.filter(name, filter)

Adds a filter to this model. Filters are user-defined functions that modify the query. They work very similarly to AngularJS filters. They can be chained and take parameters, allowing immense flexibility for developers to add features to APIs. Filters are called like this: GET /people?filter=children They can also be chained like this: GET /people?filter=children | proximity 5


Name Types Description
name String - The name of the filter (case sensitive).
filter Function - The filter function (req, query, params...) that will be applied to queries.

# Model.getUriBases()

Gets the URI bases of the model.

# Model.limit(num)

Modifies the limit parameter. Internally, this applies a {@link modifyParam} that restricts the limit parameter to be no greater than what is given here.


Name Types Description
num Number - The maximum number of documents that can be returned.

# Model.middleware()

Creates a middleware of this model. It is preferable to use {@link RestAPI#middleware} as that function provides the necessary error handling and invalid method middlewares.

# Model.modifyParam(param, modifier)

Modifies a query parameter. Modifiers alter the query parameters that will be passed to the pipeline. Modifiers are applied before any logic is called. To modify a parameter, pass the name of the parameter you wish to modify and a callback that returns the modified value of the parameter.


Name Types Description
param String - The param to modify. Can be limit, skip, populate, sort, any document field, or a parameter handled by a custom middleware.
modifier Function - The modifier(req, value[, cb]) function. Should return the new value.

# Model.printRoutes()

Prints the routes corresponding to this model.

# Model.serve(router)

Serves this model on a RESTful API. Don't use this method; use the middleware instead via {@link RestAPI#middleware}.


Name Types Description
router express.Router - The Express router to serve the model on.

# Model.submodel(field, correspondsTo, model)

Makes a field a submodel, meaning that field has its own routes for query/crud. Note that you cannot create a submodel of a submodel.


Name Types Description
field - The field to submodel
correspondsTo - The submodel's field that corresponds to this model.
model - The mongoose model

# Model.transform(transformer)

Adds a transformer to this model. Transformers change the returned results. One transformer is built in, the restricted transformer, and cannot be changed.


Name Types Description
transformer Function - The transformer(req, doc[, next]).

# Model.transformPopulate(field, transformer)

Adds a population transformer to this model. Population transformers are transformers that operate on populated fields. They can be used to make your application more secure by removing fields you don't want people to see.


Name Types Description
field String - The populated field to transform.
transformer Function - The transform function(req, doc[, next])

# Model.use(route, middleware)

Uses a middleware on a certain route. These middlewares are called in the order that they are added to this model.


Name Types Description
route String - The route. Can be one of all, query, create, get, update, destroy.
middleware Function - The middleware(req, res, next).

# RestAPI()

Represents a RESTful API powered by Restifier.

# RestAPI.addModels(mongs)

Adds multiple models to this RestAPI.


Name Types Description
mongs mongoose.Model[] The mongoose models

# RestAPI.asFunction()

Creates a setup function for this RestAPI.

# RestAPI.finish()

Should be called after the app has been created.

# RestAPI.middleware()

Creates middleware that encompasses all registered models. This is equivalent to calling model.middleware() for each individual model, plus calling restifier.initialize() and restifier.finish().

# RestAPI.model(mong)

Adds a model to this RestAPI.


Name Types Description
mong mongoose.Model The mongoose model

# RestAPI.modelsMiddleware(mongs)

Creates a middleware out of multiple models.


Name Types Description
mongs mongoose.Model[] The mongoose models

# RestAPI.printRoutes()

Prints all routes corresponding to this RestAPI.

# RestAPI.setup()

Gateway function that can add model(s) or return initialization middleware depending on the arguments passed to it.

# RestAPI.use()

Applies middleware to this RestAPI. Uses express.Router#use.

# restifier()

The main function. Wraps {@link RestAPI#setup}.

# restifier.api()

Factory function to create a new {@link RestAPI}.

# exports.default()

The default route that will be called if nothing else is.

# exports.query(model)

The query route.


Name Types Description
model Model - The {@link Model}.