{"id":24087119,"url":"https://github.com/hapipal/tandy","last_synced_at":"2025-05-05T17:55:04.663Z","repository":{"id":53156540,"uuid":"84329400","full_name":"hapipal/tandy","owner":"hapipal","description":"Auto-generated, RESTful, CRUDdy route handlers for Objection models in hapi","archived":false,"fork":false,"pushed_at":"2023-02-28T00:45:51.000Z","size":160,"stargazers_count":27,"open_issues_count":6,"forks_count":3,"subscribers_count":7,"default_branch":"main","last_synced_at":"2025-05-02T15:11:21.408Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/hapipal.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2017-03-08T14:30:03.000Z","updated_at":"2024-01-07T14:22:38.000Z","dependencies_parsed_at":"2024-10-31T20:35:55.760Z","dependency_job_id":"b73cbc98-f8ef-49c4-aeb1-65325f26771e","html_url":"https://github.com/hapipal/tandy","commit_stats":{"total_commits":70,"total_committers":7,"mean_commits":10.0,"dds":0.6142857142857143,"last_synced_commit":"fc43360fe5d726f60cc4012df44ad9d006ba2dce"},"previous_names":[],"tags_count":6,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Ftandy","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Ftandy/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Ftandy/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/hapipal%2Ftandy/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/hapipal","download_url":"https://codeload.github.com/hapipal/tandy/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":252546588,"owners_count":21765802,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2025-01-10T03:02:10.076Z","updated_at":"2025-05-05T17:55:04.656Z","avatar_url":"https://github.com/hapipal.png","language":"JavaScript","readme":"# tandy\n\nAuto-generated, RESTful, CRUDdy route handlers\n\n[![Build Status](https://travis-ci.org/hapipal/tandy.svg?branch=master)](https://travis-ci.org/hapipal/tandy) [![Coverage Status](https://coveralls.io/repos/github/hapipal/tandy/badge.svg?branch=master)](https://coveralls.io/github/hapipal/tandy?branch=master)\n\nLead Maintainer - [Matt Boutet](https://github.com/mattboutet)\n\n## Installation\n```sh\nnpm install @hapipal/tandy\n```\n\n\u003e **Note**\n\u003e\n\u003e This plugin is intended to work with [hapi](https://github.com/hapijs/hapi) v19+ and its [Objection ORM](https://github.com/Vincit/objection.js/) plugin, [schwifty](https://github.com/hapipal/schwifty).\n\n## What it does\nTandy registers route handlers based upon the `method` and `path` of your route.  It turns them into RESTful API endpoints that automatically interact with models defined through Schwifty.  By default Tandy will infer which models to use from the request path.  The route handler is based on one of eight Tandys:\n\n - `POST` is used for `create`, `add` (create a record then add it to a relation), and for `update`.\n - `PATCH` may also be used for `update`.\n - `PUT` is used for `add` in order to add an existing record to a relation.\n - `GET` is used for `find`, `findOne`, and `populate` (get related records or check an association).\n - `DELETE` is used for `destroy` and `remove` (remove a record from a relation).\n\n## Tandy Patterns\nSuppose users are associated with comments via an Objection relation.  The user model associates comments in an relation named `comments`.  Here are some examples as to how the plugin will deduce which of the eight Tandys to use, based upon route method and path definition.\n\n - `GET /users` ↦ `find`\n\n    Returns an array of users with an `HTTP 200 OK` response.\n\n - `GET /users/count` ↦ `find` with `/count`\n\n    Returns the integer number of users matched with an `HTTP 200 OK` response.\n\n - `GET /users/{id}` ↦ `findById`\n\n    Returns user `id` with an `HTTP 200 OK` response.  Responds with an `HTTP 404 Not Found` response if the user is not found.\n\n - `GET /users/{id}/comments` ↦ `findById`\n\n    Returns an array of comments associated with user `id`.  Returns `HTTP 200 OK` if that user is found.  Returns an `HTTP 404 Not Found` response if that user is not found.\n\n - `GET /users/{id}/comments/count` ↦ `populate` with `/count`\n\n    Returns the integer number of comments associated with user `id`.  Returns `HTTP 200 OK` if that user is found.  Returns an `HTTP 404 Not Found` response if that user is not found.\n\n - `GET /users/{id}/comments/{childId}` ↦ `populate`\n\n    Returns `HTTP 204 No Content` if comment `childId` is associated with user `id`.  Returns an `HTTP 404 Not Found` response if that user is not found or that comment is not associated with the user.\n\n - `POST /users` ↦ `create`\n\n    Creates a new user using the request payload and returns it with an `HTTP 201 Created` response.\n\n - `POST /users/{id}/comments` ↦ `add`\n\n    Creates a new comment using the request payload and associates that comment with user `id`.  Returns that comment with an `HTTP 201 Created response`.  If that user is not found, returns an `HTTP 404 Not Found` response.\n\n - `PUT /users/{id}/comments/{childId}` ↦ `add`\n\n    Associates comment `childId` with user `id`.  Returns an `HTTP 204 No Content` response on success.  If the user or comment are not found, returns an `HTTP 404 Not Found` response.\n\n - `DELETE /users/{id}` ↦ `destroy`\n\n    Destroys user `id`.  Returns an `HTTP 204 No Content` response on success.  If the user doesn't exist, returns an `HTTP 404 Not Found` response.\n\n - `DELETE /users/{id}/comment/{childId}` ↦ `remove`\n\n    Removes association between user `id` and comment `childId`.  Returns an `HTTP 204 No Content` response on success.  If the user or comment doesn't exist, returns an `HTTP 404 Not Found` response.\n\n - `PATCH /users/{id}` or `POST /user/{id}` ↦ `update`\n\n    Updates user `id` using the request payload (which will typically only contain the attributes to update) and responds with the updated user.  Returns an `HTTP 200 OK` response on success.  If the user doesn't exist, returns an `HTTP 404 Not Found` response.\n\n\n## Options\nOptions 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.\n\n### Acting as a User\nThese 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`](https://github.com/hapijs/hapi/blob/master/API.md#request.auth) flag.  This allows you to use authentication modes however you wish.\n\n - `actAsUser` (boolean, defaults `false`).  Applies to `findOne`, `find`, `create`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    This must be set to `true` for the following options in the section to take effect.  The acting user is defined by hapi authentication credentials and the `userIdProperty` option.\n\n - `userIdProperty` (string, defaults `'id'`).  Applies to `findOne`, `find`, `create`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    When `actAsUser` is `true` this option takes effect.  It defines a path into `Request.auth.credentials` to determine the acting user's id.  For example, if the credentials object equals `{user: {info: {id: 17}}}` then `'user.info.id'` would grab user id `17`.  See [`Hoek.reach`](https://github.com/hapijs/hoek#reachobj-chain-options), which is used to convert the string to a deep property in the hapi credentials object.\n\n - `userUrlPrefix` (string, defaults `'/user'`).  Applies to `findOne`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    When `actAsUser` is `true` this option takes effect.  This option works in tandem with `userModel`.  When a route path begins with `userUrlPrefix` (after any other inert prefix has been stripped via the `prefix` option), the URL is transformed to begin `/:userModel/:actingUserId` before matching for a Tandy; it essentially sets the primary record to the acting user.\n\n - `userModel` (string, defaults `'users'`).  Applies to `findOne`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    When `actAsUser` is `true` this option takes effect.  This option works in tandem with `userUrlPrefix`.  When a route path begins with `userUrlPrefix` (after any other inert prefix has been stripped via the `prefix` option), the URL is transformed to begin `/:userModel/:actingUserId` before matching for a Tandy; it essentially sets the primary record to the acting user.  E.g., by default when `actAsUser` is enabled, route path `PUT /user/following/10` would internally be considered as `PUT /users/17/following/10`, which corresponds to the `add` Tandy applied to the authenticated user.\n\n### Other Options\n\n - `prefix` (string).  Applies to `findOne`, `find`, `create`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    Allows one to specify a prefix to the route path that will be ignored when determining which Tandy to apply.\n\n\n - `model` (string). Applies to `findOne`, `find`, `create`, `update`, `destroy`, `add`, `remove`, and `populate`.\n\n    Name of the model's Objection identity.  If not provided as an option, it is deduced from the route path.\n\n    Ex: `/user/1/files/3` has the model `user`.\n\n - `associationAttr` (string). Applies to `add`, `remove`, and `populate`\n\n    Name of the association's Objection attribute.  If not provided as an option, it is deduced from the route path.\n\n    Ex: `/user/1/files/3` has the association attribute `files` (i.e., the Objection model `user` has an attribute, `files` containing records in a one-to-many relationship).\n\n - `limit` (positive integer). Applies to `find` and `populate`.\n\n    Set default limit of records returned in a list.  If not provided, this defaults to 30.\n\n - `skip` (positive integer). Applies to `find` and `populate`.\n\n    Sets default number of records to skip in a list (overridden by `skip` query parameter).  Defaults to 0.\n\n - `sort` (string). Applies to `find` and `populate`.\n\n    Sets default sorting criteria (i.e. `createdDate ASC`) (overridden by `sort` query parameter).  Defaults to no sort applied.\n\n - `where` (string). Applies to `find`.\n\n    Extracts only those records that fulfill a specified condition. (i.e. `createdDate = '2019-08-19'`)(overridden by `where` query parameter).\n\n## Usage\nHere's an (over)simplified example.\n\n```js\n// Assume `server` is a hapi server with the Tandy plugin registered.\n// Models with names 'Zoo' and 'Treat' exist via Schwifty.\n// Zoos and Treats are in a many-to-many correspondence with each other.\n// Check-out the test/ folder for further details.\n\nserver.route([\n    { // findOne\n        method: 'GET',\n        path: '/zoo/{id}',\n        handler: {\n            tandy: options\n        }\n    },\n    { // find\n        method: 'GET',\n        path: '/treat',\n        handler: {\n            tandy: options\n        }\n    },\n    { // destroy\n        method: 'DELETE',\n        path: '/treat/{id}',\n        handler: {\n            tandy: options\n        }\n    },\n    { // create\n        method: 'POST',\n        path: '/zoo',\n        handler: {\n            tandy: options\n        }\n    },\n    { // update\n        method: ['PATCH', 'POST'],\n        path: '/treat/{id}',\n        handler: {\n            tandy: options\n        }\n    },\n    { // remove\n        method: 'DELETE',\n        path: '/zoo/{id}/treats/{childId}',\n        handler: {\n            tandy: options\n        }\n    },\n    { // create then add\n        method: 'POST',\n        path: '/zoo/{id}/treats',\n        handler: {\n            tandy: options\n        }\n    },\n    { // add\n        method: 'PUT',\n        path: '/zoo/{id}/treats/{childId}',\n        handler: {\n            tandy: options\n        }\n    },\n    { // populate\n        method: 'GET',\n        path: '/zoo/{id}/treats/{childId?}',\n        handler: {\n            tandy: options\n        }\n    }\n]);\n```\n\n## Extras\n - based on [bedwetter](https://github.com/devinivy/bedwetter), which offers similar functionality for Waterline ORM.\n","funding_links":[],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapipal%2Ftandy","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fhapipal%2Ftandy","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fhapipal%2Ftandy/lists"}