{"id":15984221,"url":"https://github.com/droideveloper/restful","last_synced_at":"2025-03-27T03:30:41.490Z","repository":{"id":57355016,"uuid":"80046705","full_name":"droideveloper/RESTful","owner":"droideveloper","description":"Restful Resources for express + sequelize orm","archived":false,"fork":false,"pushed_at":"2018-09-14T12:22:31.000Z","size":45,"stargazers_count":3,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-22T23:11:20.762Z","etag":null,"topics":["nodejs","orm","restfull"],"latest_commit_sha":null,"homepage":null,"language":"TypeScript","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/droideveloper.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":"2017-01-25T18:58:49.000Z","updated_at":"2020-10-30T15:49:49.000Z","dependencies_parsed_at":"2022-08-28T13:11:00.764Z","dependency_job_id":null,"html_url":"https://github.com/droideveloper/RESTful","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/droideveloper%2FRESTful","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droideveloper%2FRESTful/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droideveloper%2FRESTful/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/droideveloper%2FRESTful/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/droideveloper","download_url":"https://codeload.github.com/droideveloper/RESTful/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":245778338,"owners_count":20670682,"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":["nodejs","orm","restfull"],"created_at":"2024-10-08T02:05:09.781Z","updated_at":"2025-03-27T03:30:41.230Z","avatar_url":"https://github.com/droideveloper.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"##  RESTful - is easy to use restful service implementation for express and sequelize ##\n\n\n### How to install ###\n\nwith node package manager aka npm;\n\n`npm install --save restful-express-sequelize`\n\nDocumentation for `express` or `sequelize`\n\n[Express](https://expressjs.com/en/4x/api.html) or [Sequelize](http://sequelize.readthedocs.io/en/v3/)\n\n\n(PS: for database example check [model](https://github.com/droideveloper/RESTful#models))\n\nin your server.js or index.js file;\n\n```javascript\n//imports\nvar express = require(\"express\");\nvar bodyParser = require(\"body-parser\");\nvar gzip = require(\"compression\");\nvar context = require(\"restful-express-sequelize\");\n// model generted by sequelize-cli\n// (sequelize model:create --name Framework --attributes name:string,lang:string)\nvar dbContext = require(\"./models\");\n// bind over ip and port instead of 127.0.0.1\nvar port = process.env.PORT || 52192;\nvar host = process.env.HOST || \"192.168.1.100\";\n// express instance\nvar server = express();\n// register body-parser middleware\nserver.use(bodyParser.json());\nserver.use(bodyParser.urlencoded({ extended: true }));\n// register compression middleware\nserver.use(gzip({ filter: function (req, res) {\n  return !req.headers[\"x-no-gzip\"];\n}}));\n\n// get items from models\nvar models = [];\nfor (var property in dbContext) {\n  // register as options you can add { model: xxx, methods: [\"get\", \"post\"] } \n  // methods are (optional) defaults all registered [\"get\", \"post\", \"put\", \"delete\"] \n  models.push({ model: dbContext[property] });  \n}\n// finally register your method(s) on base as '/v1/endpoint'\n// base is (optional) context.Resource.register(server, model)\n// port is (optional) context.Resource.register(server, model) if port is not 80 then we bind\n// if you use it in local project or port specified on others it will be useful.\ncontext.Resource.register(server, models, \"/v1/endpoint\", port);\n// start serving\nserver.listen(port, host, function () {\n  console.log(\"Server Running...\");\n});\n```\n### Models ###\n\nin `/model` folder \n\nas Country.js \n\n```javascript\n'use strict';\nmodule.exports = function(sequelize, DataTypes) {\n  var Country = sequelize.define('Country', {\n    countryName: { type: DataTypes.STRING, allowNull: false }\n  }, {\n    classMethods: {\n      associate: function(models) {\n        Country.hasMany(models.City, { foreignKey: \"countryId\" });\n        // Country.map is for api will show assosiations\n        Country.map = [models.City];\n      }\n    }\n  });\n  return Country;\n};\n```\n\nas City.js \n\n```javascript\n'use strict';\nmodule.exports = function(sequelize, DataTypes) {\n  var City = sequelize.define('City', {\n    cityName: { type: DataTypes.STRING, allowNull: false }\n  }, {\n    classMethods: {\n      associate: function(models) {\n        City.belongsTo(models.Country, { as: \"country\" });\n      }\n    }\n  });\n  return City;\n};\n```\n\nas index.js \n\n```javascript\n'use strict';\nvar fs        = require('fs');\nvar path      = require('path');\nvar Sequelize = require('sequelize');\nvar basename  = path.basename(module.filename);\nvar config    = require(path.join(__dirname, '../config/config.json'));\n// placeholder for all\nvar dbContext = {};\n// connect\nvar sequelize = new Sequelize(config.database, config.username, config.password, config.options);\n// imports everything in this directory into entities and register relations later.\nfs.readdirSync(__dirname)\n  .filter(function(f) {\n    return (f.indexOf('.') !== 0) \u0026\u0026 (f !== basename) \u0026\u0026 (f.slice(-3) === '.js');\n  })\n  .forEach(function(f) {\n    var model = sequelize.import(path.join(__dirname, f));\n    dbContext[model.name] = model;  \n  });\n// invoke associate methods on models\nObject.keys(dbContext)\n  .forEach(function(key) {\n    if(dbContext[key].associate) {\n      // this will invoke our relationships\n      dbContext[key]associate(dbContext);\n    }\n  });\n// sync context once\nsequelize.sync();\n// exports\nmodule.exports = dbContext;\n```\n\n## For More and What we support ##\n\nRegisters your database context on restful definitions, and service is created with it at github [link.](https://github.com/droideveloper/RESTfulExample)\n\nFor instance your database table is \"Frameworks\" in mysql registered as `/frameworks` for methods:\n  \n  * GET     /frameworks\n  * GET     /frameworks/:id\n  * POST    /frameworks\n  * PUT     /frameworks/:id\n  * DELETE  /frameworks/:id\n\nand by passing methods args on your registeration in array `[\"get\", \"post\", \"put\", \"delete\"]`\nyou are allowed to manipulate proper methods or register only your needs. P.S. ( as defaults all registered )\n\nsupports some default query options;\n  \n- for array Response:\n  * select=property1,property2 (any property of model itself and some extras: id, href, createdAt, updatedAt)\n  * sort=property,type (any property of model and type as 'desc' or 'asc' is default )\n  * limit=number (25 is default)\n  * offset=number (0 is default)  \n    \n- for object Response:\n  * select=property1,property2 (any property of model itself and some extras: id, href, createdAt, updatedAt)  \n\nresponse are wrapped as follows;\n\n  for array responses:\n\n  `next` or `previous` properties might not exists depending on context. (optionals)\n  `data` property can be `null` or `[]`.\n\n  ```json\n  {\n    \"code\": 200,\n    \"message\": \"success\",\n    \"data\": [{ }],\n    \"count\": 1, \n    \"href\": \"$href\", \n    \"next\": \"$next\", \n    \"previous\": \"$previous\", \n    \"limit\": 25,\n    \"offset\": 0\n  }\n  ```\n\n  for object or primitive responses:\n\n  `data` property can be `null` or `{}`.\n\n  ```json\n  {\n    \"code\": 200,\n    \"message\": \"success\",\n    \"data\": { } \n  }\n  ```\n\n## Changes ##\n- optional port added for local projects.\n- bug fix.\n\n## License ##\n\nCopyright 2017 Fatih Şen and contributors.\n\nPermission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the \"Software\"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:\n\nThe above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.\n\nTHE SOFTWARE IS PROVIDED \"AS IS\", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdroideveloper%2Frestful","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdroideveloper%2Frestful","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdroideveloper%2Frestful/lists"}