{"id":16219316,"url":"https://github.com/ivanseidel/geddy-rest","last_synced_at":"2025-04-03T01:33:22.737Z","repository":{"id":20007065,"uuid":"23274592","full_name":"ivanseidel/geddy-rest","owner":"ivanseidel","description":"REST Api made easy for Geddy","archived":false,"fork":false,"pushed_at":"2014-09-02T05:35:50.000Z","size":184,"stargazers_count":3,"open_issues_count":0,"forks_count":2,"subscribers_count":2,"default_branch":"master","last_synced_at":"2025-03-18T04:16:49.864Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":null,"language":"JavaScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/ivanseidel.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2014-08-24T06:09:24.000Z","updated_at":"2015-12-30T15:32:23.000Z","dependencies_parsed_at":"2022-08-27T03:40:45.882Z","dependency_job_id":null,"html_url":"https://github.com/ivanseidel/geddy-rest","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2Fgeddy-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2Fgeddy-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2Fgeddy-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/ivanseidel%2Fgeddy-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/ivanseidel","download_url":"https://codeload.github.com/ivanseidel/geddy-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":246922243,"owners_count":20855341,"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":"2024-10-10T11:54:19.796Z","updated_at":"2025-04-03T01:33:22.420Z","avatar_url":"https://github.com/ivanseidel.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# GeddyJs REST Helper\n\nUse this simple library to avoid code repetition for Controllers REST actions. With one single line, you provide all the methods you need for your controller, in order to have a fully REST api.\n\nIt is made to be used with [GeddyJs](http://geddyjs.org/).\n\n## Instalation\n\n1. Run: `npm install --save geddy-rest`\n2. Add the folowing line inside your controller:\n   \n  ```\n  require('geddy-test').RESTify(this, MyModel);\n  ```\n\n3. Add the folowing line inside `/config/routes.js`:\n\n  ```\n  require('geddy-test').route(router, 'mymodel');\n  ```\n\n4. You are done, and you can access trough:\n  ```\n  GET /:model         ---\u003e Find All\n  GET /:model/:id     ---\u003e Find First\n  POST /:model/:id    ---\u003e Create\n  PUT /:model/:id     ---\u003e Update\n  DELETE /:model/:id  ---\u003e Destroy\n  ```\n  \n  _If you want `named` paths with `GET` method, read below._\n\n\n## Nested findings\n\nImagine the folowing situation: `User` `hasMany` `Photos`. If you need to read the User photos, you would need to go to an endpoint like `GET /photos?userId=\u003ctheUserId\u003e`, after getting the user information in `GET /users/\u003ctheUserId\u003e`.\n\nWith that in mind, I made a simple helper that allows you to do even more with Geddy. This situation will become `GET /users/\u003ctheUserId\u003e/photos`, in a really simple way.\n\nWe present you, nested associations with REST:\n\n```\n// Add this to your config/routes.js to allow use of nested findings\nrequire('geddy-test').RestApi.route(router, 'users', {\n  find: {\n    // Enables Nested finding route\n    nested: true\n  }\n});\n\n// User Controller\n// We are nesting the model method 'getPhotos' as 'photos'\nrequire('geddy-test').RESTify(this, MyModel, {\n  find: {\n    nested: {\n      photos: 'getPhotos'\n    }\n  }\n});\n\n// You can even nest multiple actions, and provide a custom callback like:\nfunction specialMethod(cb){\n  // Send back data to be replaced\n  cb({custom: 'data', to: 'be', shown: true});\n}\n\nrequire('geddy-test').RESTify(this, MyModel, {\n  find: {\n    nested: {\n      photos: 'getPhotos',\n      friends: 'getFriends',\n      address: 'getAddress',\n      special: specialMethod\n    }\n  }\n});\n```\n\nThe example above would generate the folowing routes:\n```\nGET /users/:id/photos  -\u003e find then getPhotos\nGET /users/:id/friends -\u003e find then getFriends\nGET /users/:id/address -\u003e find then getAddress\nGET /users/:id/special -\u003e find then run specialMethod\n```\n\n## Using it as a middleware\n\nYou can also use those methods for your own purpose. Look at some examples:\n\n```\nvar Users = function (){\n  // Inject methods into controller\n  require('geddy-rest').RESTify(this, geddy.model.User);\n\n  // Should route to: /users/:id/resume\n  this.resume = function (req, res, params){\n\n    // Execute custom find method\n    this.find(req, res, params, function afterFind(err, model, cb){\n\n      var data = {\n        userName: model.name,\n        userFriendsCount: model.getFriends().length\n      };\n\n      // Continue with it...\n      return cb(data);\n\n      // Or render it yourself\n      respond(data);\n\n    });\n  }\n}\n```\n\n## Advanced usage\n\n- `RESTify(controller, model, opts)`\n\n  Restify creates methods inside your given `controller`. It uses the `model` to _create/find/delete/update_.\n  \n  You can also assign some options:\n    \n  - `opts.[create|find|update|destroy]`: [`false` | `Object`]\n    If set to false, will not generate the method.\n    \n    example:\n    ```\n    // Only enables find method\n    RESTify(this, MyModel, {\n      create: false,\n      destroy: false,\n      update: false,\n    });\n    ```\n    \n  - `opts.[create|find|update|destroy].action`: `String`\n    Use this to change the desired method to be saved. You can, for example, generate it and use it the way you want:\n    ```\n    RESTify(this, MyModel, {\n      find: {\n        action: 'myFindAction'\n      }\n    });\n    \n    this.find = function (req, res, params){\n      // Do whatever you want....\n      var a = 2*9;\n      // Delegate it to the REST find method\n      this.myFindAction(req, res, params);\n    }\n    ```\n    \n    \n  - `opts.[create|find|update|destroy].beforeRender`: `function`\n    \n    Use this property to receive actions just before rendering the data. You can either render it yourself (just don't call the `next` function), or process something before rendering and delegate it to the REST action egain:\n    ```\n    this._checkDbFirst = function (err, models, next){\n      // You render the content here. Just don't call next.\n      respond(models);\n      \n      // But if you do, pass the models to render\n      next(models);\n    }\n    \n    RESTify(this, MyModel, {\n      create: {\n        action: 'find',\n        beforeRender: this._checkDbFirst\n      }\n    });\n    ```\n    \n  - `opts.find.nested`: `Object`\n\n    Put each nested method as the key, and the callback as the value. (Read above for more info);\n\n    \n  - GeddyJs is a well tought Framework. If you need to perform actions either `before` or `after` a call to the REST endpoint, use the methods `.before` and `.after`:\n    ```\n    // Calls prepareThings before find and create\n    this.before(prepareThings, {only: ['find', 'create']});\n    // Calls finishThingsUp after update and destroy\n    this.after(finishThingsUp, {only: ['update', 'destroy']});\n    ```\n      \n- `route(Router, ModelName, opts)`\n  \n  This will generate routes acordingly to your needs. It should be placed inside `/config/routes.js`.\n\n  If you need to alter the `default routes`, or `HTTP methods` you can access it exacly like the `RESTify` method:\n  ```\n  // Change the default route of 'find' to 'get'\n  // Change the destroy method to 'GET'\n  // Disables update route\n  route(router, MyModelName, {\n    find: {\n      action: 'get'\n    },\n    destroy: {\n      method: 'GET'\n    },\n    update: false\n  });\n  ```\n    \n  One last usefull option, is the `opts.strict`, which defaults is set to `true`.\n  \n  If set to false, will generate PATHS to facilitate your life (during development?):\n  ```\n  GET /:model/find\n  GET /:model/find/:id\n  GET /:model/create\n  GET /:model/destroy/:id\n  GET /:model/update/:id\n  ```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanseidel%2Fgeddy-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fivanseidel%2Fgeddy-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fivanseidel%2Fgeddy-rest/lists"}