Class: Resource

Resource(definition)

new Resource(definition)

Constructs a resource.

Keys

  • model (object): The instance of the Mongoose model (required).
  • rel (string): The absolute path of the new resource (required).
  • create (boolean): Specifies if the resource should support create (POST) (default true).
  • update (boolean): Specifies if the resource should support update (PUT) (default true).
  • delete (boolean): Specifies if the resource should support delete (DELETE) (default true).
  • lean (boolean): Whether find[ById] queries should be 'lean' and return pojos (default true). If false then resource instances, prior to mapping an object for return, could make use of methods on the instance model.
  • populate (string||Array): Specifies a property, or list of properties, to populate into objects (deprecated use $expand).
  • count (boolean): Specifies if the resource should support counts on find and when traversed to by standard relationships from other types (default: false).

The following keys set defaults for possible query arguments.

  • $top (number): The default value for $top if not supplied on the query string (default none).
  • $skip (number): The default value for $skip if not supplied on the query string (default none).
  • $orderby (string): The default value for $orderby if not supplied on the query string (default none). This value must use odata syntax like "foo asc,bar desc,baz" rather than mongo syntax like "foo -bar baz".
  • $orderbyPaged (string): The name of an attribute to sort results by when clients are paging, send $top, but have not explicitly sent $orderby. (default '_id').
  • $select (string): The default value for $select if not supplied on the query string (default none, all properties). If a value is supplied then $select on the query string will be ignored to protect against the situation where the intent is to hide internal attributes (e.g. '-secret'). Unlike odata the syntax here is passed through to mongo so the '-' prefix will be honored.
  • $expand (string|Array): Specifies a property or list of properties to populate into objects. This value acts as the default value for the $expand URL argument. If the URL argument is supplied it over-rides this value. Nested expansion is supported. E.g. _book._author will end up in both the _book reference being expanded and its _author reference being expanded. The corresponding Mongoose model ObjectId properties must have their ref properties set properly or expansion cannot work.
Parameters:
Name Type Description
definition Object The resource definition.
Source:

Members

(static) parseFilter

Parse a $filter populating a mongoose query with its cotents.
Source:

Methods

(static) sendError(res, rc, message, err, nextopt)

Send a response error.
Parameters:
Name Type Attributes Description
res Object The express response object.
rc Number The response status code (default 500).
message String The response message.
err Object The error object.
next function <optional>
Optional next callback to invoke after the response is sent with the error object.
Source:
To Do:
  • currently this function unconditionally sends the error with the response. this may not be desirable since often exposing an error (e.g. stack trace) poses a potential security vulnerability.

count(req, res)

Executes a query for an entity type and returns the number of objects found. Resources may override this default functionality.
Parameters:
Name Type Description
req Object The express request object.
res Object The express response object.
Source:

create(req, res, nextopt)

Creates an instance of this entity type and returns the newly created object to the client.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:

delete(req, res, nextopt)

Deletes an instance of this entity type.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
next function <optional>
Optional next callback to invoke after successful delete with the model object.
Source:

find(req, res, nextopt)

Executes a query for an entity type and returns the response to the client. Resources may override this default functionality.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:

findById(req, res, nextopt)

Fetches and returns to the client an entity by id. Resources may override this default functionality.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:

getDefinition() → {Object}

Source:
Returns:
The resource definition.
Type
Object

getInstanceLinkNames() → {Array}

Source:
Returns:
The list of instance link names.
Type
Array

getMapper(postMapper) → {function}

Builds a 'mapper' object that can be used to translate mongoose objects into REST response objects. This function can be passed to Array.map given an array of mongoose objects. All object results returned to a client should pass through a mapper so that meta information like instance links can be attached.

Note: When sending custom responses you should use the listResponse or singleResponse functions to do so and those functions implicitly use a mapper.

Parameters:
Name Type Description
postMapper function Array.map callback that should be called after the underlying mapper does its work (optional).
Source:
Returns:
An Array.map callback.
Type
function

getModel() → {Object}

Source:
Returns:
The underlying mongoose model.
Type
Object

getRel() → {String}

Source:
Returns:
The resource relative path.
Type
String

getStaticLinkNames() → {Array}

Source:
Returns:
The list of static link names.
Type
Array

initQuery(query, req) → {Object}

Initializes a mongoose query from a user request.
Parameters:
Name Type Description
query Object The mongoose query.
req Object The express request object.
Source:
Returns:
The mongoose query (query input argument).
Type
Object

initRouter(app) → {object}

Initializes and returns an express router. If app is supplied then app.use is called to bind the resource's 'rel' to the router.
Parameters:
Name Type Description
app object Express app (optional).
Source:
Returns:
Express router configured for this resource.
Type
object
Add an instance link to this resource. The link argument can be either an object defining a simple relationship (based on a reference from the 'other side' object) or a function to be called to resolve the relationship. If a function is supplied then its arguments must be (req,res) which are the express request and response objects respectively. If an object is supplied then the necessary keys are: - otherSide (Object): The Resource instance of the other side entity. - key (string): The attribute name on the otherside object containing the id of this Resource's entity type.
Parameters:
Name Type Description
rel String The relative path of the link.
link Object | function The link definition.
Source:
Returns:
this
Type
Object

listResponse(req, res, objs, postMapperopt, nextopt)

Sends a list response.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
objs Array The array of objects to send.
postMapper function <optional>
Optional Array.map callback that will be called with each raw object instance.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:

relListResponse(req, res, objs, postMapperopt, nextopt)

Sends a list response when a relationship is traversed. This is used for standard relationships to allow the static count link to be handled.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
objs Array The array of objects to send.
postMapper function <optional>
Optional Array.map callback that will be called with each raw object instance.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:

singleResponse(req, res, objs, postMapperopt, nextopt)

Sends a single object instance response.
Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
objs Array The array of objects to send.
postMapper function <optional>
Optional Array.map callback that will be called with each raw object instance.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source:
Add a static link implementation to this resource.
Parameters:
Name Type Description
rel String The relative path of the link.
link function A function to call when the static link is requested. The arguments are (req,res) which are the express request and response objects respectively.
Source:
Returns:
this
Type
Object

update(req, res, nextopt)

Updates an instance of this entity type and returns the updated object to the client.

Note: This implementation of update is more similar to PATCH in that it doesn't require a complete object to update. It will accept a sparsely populated input object and update only the keys found within that object.

Parameters:
Name Type Attributes Description
req Object The express request object.
res Object The express response object.
next function <optional>
Optional next callback to invoke after the response is sent with the response object.
Source: