API Configuration

End-point configuration options:

To configure end-point you should to specify appropriate router-level paramter with end-point configuration object. For instance, like that:

var api = new merest.ModelAPIExpress();
api.expose(models.Book, {
  details: { populate: 'author' }
});

All end-point configuration parameters are the following:

  • path: String - additional-path of end-point
  • method: String -- HTTP-method to mount appropriate end-point
  • filter: Object|Function - Mongoose query-object or function that returns such object. The function receives request (Express Incoming Message) as only parameter. The function is executing in synchronous mode.
  • fields: Object|Array|String - Mongoose field-selection parameter
  • readonly: reserved,
  • queryFields: Object - keys of the objects are names of fields. If value of the key is equal to false, the correspondent field will be excluded from the query. Affects on search end-point only.
  • populate: Array|Object|String - Mongoose field population parameter
  • skip: Boolean - allows or disables skipping documents in the search result. Affects on search end-point only.
  • limit: Boolean - allows on denies limitation of documents in the search result. Affects on search end-point only.
  • sort: Boolean|Object -- allows or denies (in case of false) the sorting. Object keys are names of the fields. The value of appropriate key allows or denies to sort by this field. Affects on search end-point only. The mongodb field paths could be used as keys of sort-object.
  • middlewares: Function|Array - middleware function or array of such functions. The middleware(s) will be mounted to the end-point route as usual express middleware
  • title: String - the description of the end-point

Allmost all of described above options (excl. method, path, title) could be assigned also on the router-level. In this case it will be applied to all applicable end-points if end-point doesn't override appropriate parameter directly.

var api = new merest.ModelAPIExpress();
api.expose(models.Vector, { // Model-routes level
  options: false, // end-point level
  update: 'put', // end-point level - update controller will be mounted on the PUT HTTP-method
  search: { // end-point level - search controller will be mounted
    method: 'post',  // on the POST HTTP-method
    path: '/search'  // on /search
  }
  fields: { // Model-routes level
    x: true,
    y: true,
    _id: false
  }
});

Calling API:

curl -X OPTIONS http://localhost:1337/api/v1/

Output:

[
  ["options", "/api/v1/", "List all end-points of current application"],
  ["post", "/api/v1/vectors/search", "List/Search all vectors"],
  ["post", "/api/v1/vectors/", "Create a new Vector"],
  ["get", "/api/v1/vectors/:id", "Find a Vector by Id"],
  ["put", "/api/v1/vectors/:id", "Find a Vector by Id and update it (particulary)"],
  ["delete", "/api/v1/vectors/:id", "Find a Vector by Id and delete it."]
]

If paraneter path is assigned on the router-level it will be used as end-point sub-path instead of Model collection name (plural).



Next (Installation) >