Bedwetter
Auto-generated, RESTful, CRUDdy route handlers for Waterline models in hapi
Install / Use
/learn @devinivy/BedwetterREADME

Auto-generated, RESTful, CRUDdy route handlers
to be used with hapi 8 (and 7) and its Waterline plugin, dogwater.
What it does
Bedwetter registers route handlers based upon the method and path of your route. It turns them into RESTful API endpoints that automatically interact with the model defined using dogwater. The route handler is based on one of eight bedwetters:
POSTis used forcreate,addwhenaddis used to create a record then add it to a relation, and forupdatePATCHis also used forupdatePUTis used foraddwhen it's used to simply add a record to a relationGETis used forfind,findOne, andpopulate(get related records or check an association)DELETEis used fordestroyandremove(remove a record from a relation)
For now, see SailsJs's documentation on Blueprints for info about parameters for the bedwetters. A portion of the code is adapted from this SailsJs hook.
Bedwetter also allows you to manage resources/records with owners. There are options to act on behalf of a user via hapi authentication. You can set owners automatically on new records, only display records when owned by the authenticated user, and make bedwetters behave like the primary record is the authenticated user.
Bedwetting Patterns
Suppose users are associated with comments via dogwater/Waterline. The user model associates comments in an attribute named comments. Here are some examples as to how the plugin will deduce which of the eight bedwetters to use, based upon route method and path definition.
-
GET /users↦findReturns an array of users with an
HTTP 200 OKresponse. -
GET /users/count↦findwith/countReturns the integer number of users matched with an
HTTP 200 OKresponse. -
GET /users/{id}↦findOneReturns user
idwith anHTTP 200 OKresponse. Responds with anHTTP 404 Not Foundresponse if the user is not found. -
GET /users/{id}/comments↦populateReturns an array of comments associated with user
id. ReturnsHTTP 200 OKif that user is found. Returns anHTTP 404 Not Foundresponse if that user is not found. -
GET /users/{id}/comments/count↦populatewith/countReturns the integer number of comments associated with user
id. ReturnsHTTP 200 OKif that user is found. Returns anHTTP 404 Not Foundresponse if that user is not found. -
GET /users/{id}/comments/{childId}↦populateReturns
HTTP 204 No Contentif commentchildIdis associated with userid. Returns anHTTP 404 Not Foundresponse if that user is not found or that comment is not associated with the user. -
POST /users↦createCreates a new user using the request payload and returns it with an
HTTP 201 Createdresponse. -
POST /users/{id}/comments↦addCreates a new comment using the request payload and associates that comment with user
id. Returns that comment with anHTTP 201 Created response. If that user is not found, returns anHTTP 404 Not Foundresponse. -
PUT /users/{id}/comments/{childId}↦addAssociates comment
childIdwith userid. Returns anHTTP 204 No Contentresponse on success. If the user or comment are not found, returns anHTTP 404 Not Foundresponse. -
DELETE /users/{id}↦destroyDestroys user
id. Returns anHTTP 204 No Contentresponse on success. If the user doesn't exist, returns anHTTP 404 Not Foundresponse. -
DELETE /users/{id}/comment/{childId}↦removeRemoves association between user
idand commentchildId. Returns anHTTP 204 No Contentresponse on success. If the user or comment doesn't exist, returns anHTTP 404 Not Foundresponse. -
PATCH /users/{id}orPOST /user/{id}↦updateUpdates user
idusing the request payload (which will typically only contain the attributes to update) and responds with the updated user. Returns anHTTP 200 OKresponse on success. If the user doesn't exist, returns anHTTP 404 Not Foundresponse.
Options
Options can be passed to the plugin when registered or defined directly on the route handler. Those defined on the route handler override those passed to the plugin on a per-route basis.
Acting as a User
These options allow you to act on behalf of the authenticated user. Typically the user info is taken directly off the credentials object without checking the Request.auth.isAuthenticated flag. This allows you to use authentication modes however you wish. For examples, for now please see tests at test/options/actAsUser.js.
-
actAsUser(boolean, defaultsfalse). Applies tofindOne,find,create,update,destroy,add,remove, andpopulate.This must be set to
truefor the following options in the section to take effect. The acting user is defined by hapi authentication credentials and theuserIdPropertyoption. -
userIdProperty(string, defaults"id"). Applies tofindOne,find,create,update,destroy,add,remove, andpopulate.When
actAsUseristruethis option takes effect. It defines a path intoRequest.auth.credentialsto determine the acting user's id. For example, if the credentials object equals{user: {info: {id: 17}}}then"user.info.id"would grab user id17. SeeHoek.reach, which is used to convert the string to a deep property in the hapi credentials object. -
userUrlPrefix(string, defaults"/user"). Applies tofindOne,update,destroy,add,remove, andpopulate.When
actAsUseristruethis option takes effect. This option works in tandem withuserModel. When a route path begins withuserUrlPrefix(after any other inert prefix has been stripped via theprefixoption), the URL is transformed to begin/:userModel/:actingUserIdbefore matching for a bedwetter; it essentially sets the primary record to the acting user. -
userModel(string, defaults"users"). Applies tofindOne,update,destroy,add,remove, andpopulate.When
actAsUseristruethis option takes effect. This option works in tandem withuserUrlPrefix. When a route path begins withuserUrlPrefix(after any other inert prefix has been stripped via theprefixoption), the URL is transformed to begin/:userModel/:actingUserIdbefore matching for a bedwetter; it essentially sets the primary record to the acting user. E.g., by default whenactAsUseris enabled, route pathPUT /user/following/10would internally be considered asPUT /users/17/following/10, which corresponds to theaddbedwetter applied to the authenticated user. -
requireOwner(boolean, defaultsfalse). Applies tofindOne,find,create,update,destroy,add,remove, andpopulate.When
actAsUseristruethis option takes effect. The option forces any record to that's being viewed or modified (including associations) to be owned by the user. Ownership is determined by matching the acting user's id against the attribute of the record determined byownerAttrorchildOwnerAttr. -
setOwner(boolean, defaultsfalse). Applies tocreate,update,add.When
actAsUseristruethis option takes effect. The option forces any record to that's being created or updated (including associated records) to be owned by the acting user. The owner is set on the record's attribute determined byownerAttrorchildOwnerAttr. -
ownerAttr(string orfalse, defaults"owner"). Applies tofindOne,find,update,destroy,add,remove, andpopulate.When
actAsUseristruethis option takes effect. Iffalse,requireOwnerandsetOwnerare disabled on the primary record. Otherwise,requireOwnerandsetOwneroptions act using the primary record's attribute with name specified byownerAttr. -
childOwnerAttr(string orfalse, defaults"owner"). Applies toadd,remove, andpopulate.When
actAsUseristruethis option takes effect. Iffalse,requireOwnerandsetOwnerare disabled on the child record. Otherwise,requireOwnerandsetOwneroptions act using the child record's attribute with name specified bychildOwnerAttr.
Other Options
-
prefix(string). Applies tofindOne,find,create,update,destroy,add,remove, andpopulate.Allows one to specify a prefix to the route path that will be ignored when determining which bedwetter to apply.
-
createdLocation(string). Applies tocreateand sometimes toadd.When this set (typically as a route-level option rather than a plugin-level option), a
Locationheader will be added to responses with a URL pointing to the created record. This option will act as the first argument toutil.formatwhen set, and there should be a single placeholder for the created record's id. -
model(string). Applies tofindOne,find,create,update,destroy,add,remove, andpopulate.Name of the model's Waterline identity. If not provided as an option, it is deduced from the route path.
Ex:
/user/1/files/3has the modeluser. -
associationAttr(string). Applies toadd,remove, andpopulateName of the association's Waterline attribute. If not provided as an option, it is deduced from the route path.
Ex:
/user/1/files/3has the association attributefiles(i.e., the Waterline modeluserhas an attribute,filescontaining records in a one-to-many relationship). -
criteria(object). Applies tofindandpopulate.
