Back to Top

preston master

Serves Mongoose models on a flexible, RESTful API.

Model(restifier, model)

Constructor.

Parameters

Name Type Description
restifier RestAPI
  • The RestAPI instance
model mongoose.Model
  • The mongoose model that is being restified.

Returns

Void

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.

Parameters

Name Type 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.

Examples

// A modifier that forces sorting by name ascending
model.modifyParam('sort', function(req, value) {
  value.name = 1;
  return value;
});

Returns

Model

The instance of this model

Model.applyModifiers(request)

Applies the modifiers of this model onto a request.

Parameters

Name Type Description
request Request
  • The request

Returns

Void

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.

Parameters

Name Type Description
num Number
  • The maximum number of documents that can be returned.

Examples

model.limit(5); // Now only 5 documents can be returned max

// GET /model?limit=4 - Returns 4 documents
// GET /model?limit=5 - Returns 5 documents
// GET /model?limit=6 - Returns 5 documents

Returns

Model

The instance of this model

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

Parameters

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

Examples

// A simple filter
model.filter('children', function(req, query) {
  query.where('age').lt(18);
});

// A filter that takes parameters:
model.filter('proximity', function(req, query, distance) {
  query.where('location').maxDistance(distance);
});

Returns

Void

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.

Parameters

Name Type Description
transformer Function
  • The transformer(req, doc[, next]).

Examples

model.transform(function(req, doc) {
  delete doc._id;
  delete doc.password;
  doc.type = 'This is a string that isn\'t in the database!';
});

Returns

Model

The instance of this model

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.

Parameters

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

Examples

model.transformPopulate('owners', function(req, doc) {
  delete doc._id;
  delete doc.password;
});

Returns

Model

The instance of this model

Model.applyTransforms(req, doc[, cb])

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

Parameters

Name Type 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.
Optional

Returns

Object

The transformed document as a JavaScript object.

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.

Parameters

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

Returns

Model

The instance of this model

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.

Parameters

Name Type Description
field
  • The field to submodel
correspondsTo
  • The submodel's field that corresponds to this model.
model
  • The mongoose model

Returns

Model

A model which is prefixed with this model's route.

Model.getUriBases()

Gets the URI bases of the model.

Returns

Void

Model.serve(router)

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

Parameters

Name Type Description
router express.Router
  • The Express router to serve the model on.

Returns

Model

The instance of this model

Model.printRoutes()

Prints the routes corresponding to this model.

Returns

Void

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.

Returns

Middleware

The middleware corresponding to this model.

RestAPI()

Represents a RESTful API powered by Restifier.

Returns

Void

RestAPI.setup()

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

Returns

Model Function

A model or middleware depending on the arguments. * setup() - Returns this.router

  • setup(mong) - Returns model(mong)
  • setup(mong1, mong2) - Returns modelsMiddleware(mong1, mong2)

RestAPI.asFunction()

Creates a setup function for this RestAPI.

Returns

Void

RestAPI.use()

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

Returns

RestAPI

This RestAPI.

RestAPI.finish()

Should be called after the app has been created.

Returns

Function

Express middleware

RestAPI.model(mong)

Adds a model to this RestAPI.

Parameters

Name Type Description
mong mongoose.Model

The mongoose model

Returns

Void

RestAPI.addModels(mongs)

Adds multiple models to this RestAPI.

Parameters

Name Type Description
mongs Array.<mongoose.Model>

The mongoose models

Returns

Void

RestAPI.modelsMiddleware(mongs)

Creates a middleware out of multiple models.

Parameters

Name Type Description
mongs Array.<mongoose.Model>

The mongoose models

Returns

Void

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().

Returns

Router

Returns an Express router containing all middlewares.

RestAPI.printRoutes()

Prints all routes corresponding to this RestAPI.

Returns

Void

restifier()

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

Returns

Void

restifier.api()

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

Returns

RestAPI A new RestAPI.

exports.query(model)

The query route.

Parameters

Name Type Description
model Model
  • The {@link Model}.

Returns

Function

A middleware corresponding to this route.

exports.default()

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

Returns

Void