{"id":24493062,"url":"https://github.com/open-node/open-rest-helper-rest","last_synced_at":"2025-04-14T01:40:41.188Z","repository":{"id":57315146,"uuid":"67470487","full_name":"open-node/open-rest-helper-rest","owner":"open-node","description":null,"archived":false,"fork":false,"pushed_at":"2020-07-21T12:35:08.000Z","size":107,"stargazers_count":2,"open_issues_count":2,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-27T15:52:15.149Z","etag":null,"topics":["rest","rest-api","resthelper"],"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/open-node.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":"2016-09-06T03:34:40.000Z","updated_at":"2019-08-26T13:44:16.000Z","dependencies_parsed_at":"2022-09-18T20:51:23.189Z","dependency_job_id":null,"html_url":"https://github.com/open-node/open-rest-helper-rest","commit_stats":null,"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-rest","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-rest/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-rest/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/open-node%2Fopen-rest-helper-rest/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/open-node","download_url":"https://codeload.github.com/open-node/open-rest-helper-rest/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":248809039,"owners_count":21164893,"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":["rest","rest-api","resthelper"],"created_at":"2025-01-21T19:19:01.280Z","updated_at":"2025-04-14T01:40:41.163Z","avatar_url":"https://github.com/open-node.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# open-rest-helper-rest\n\nopen-rest 的 helper 插件，用来实现 CRUD 的标准操作\n\n[![Build status](https://api.travis-ci.org/open-node/open-rest-helper-rest.svg?branch=master)](https://travis-ci.org/open-node/open-rest-helper-rest)\n[![codecov](https://codecov.io/gh/open-node/open-rest-helper-rest/branch/master/graph/badge.svg)](https://codecov.io/gh/open-node/open-rest-helper-rest)\n\n## Node version\n\u003cpre\u003e \u003e= 6 \u003c/pre\u003e\n\n# Usage\n\n```bash\nnpm instsall open-rest-helper-rest --save\n```\n\n```js\nconst rest = require('open-rest');\nconst restHelper = require('open-rest-helper-rest');\n\nrest.plugin(restHelper);\n\n// restHelper Equivalent to rest.helper.rest\n```\n\n## restHelper.list\n标准的列表方法\n```js\n// Model 必选 Sequelize 定义的 Model, 表明要从哪个表获取数据\n// opt 可选 特殊的 Model.findAll(options) options 的 hook 名称\n// allowAttrs 可选，数组类型，指定允许返回的列，不指定则返回全部\n// hook 可选，直接输出或者暂时寄存在 hooks 上\n\nrestHelper.list(Model, opt, allowAttrs, hook);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .list\n  .Model(User)\n  .exec();\n```\n\n## restHelper.detail\n标准的输出详情方法\n```js\n// hook 必选，要输出的数据在 req.hooks 的什么位置\n// attachs 可选，要附加输出的数据格式为 key =\u003e value, value 是 req 上的路径字符串\n// statusCode 可选，输出使用的http状态码, 默认值 200\n// attrFilter 可选, 是否允许过滤属性, 默认 true\n\nrestHelper.detail(hook, attachs, statusCode, attrFilter);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .detail\n  .hook('user')\n  .statusCode(201)\n  .attachs({address: 'hooks.address'})\n  .exec();\n```\n\n## restHelper.remove\n标准删除资源的方法\n```js\n// hook 必选，要删除的实例在 req.hooks 的什么位置\n\nrestHelper.remove(hook);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .remove\n  .hook('user')\n  .exec();\n```\n\n## restHelper.beforeModify\n修改资源的前期准备，不包括save到数据库的操作\n```js\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// hook 必选, 实例的存放位置\n// cols 可选, 允许修改的字段\n\nrestHelper.beforeModify(Model, hook, cols);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .beforeModify\n  .Model(User)\n  .cols(cols)\n  .hook('user')\n  .exec();\n```\n\n## restHelper.save\n修改save到数据库的操作\n\n```js\n// 修改某个资源描述的后置方法, 将变化保存到数据库\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// hook 必选, 实例的存放位置\n\nrestHelper.save(hook);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .save\n  .hook('user')\n  .exec();\n```\n\n## restHelper.modify\n标准的修改某个资源的操作\n\n```js\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// hook 必选, 实例的存放位置\n// cols 可选, 允许修改的字段\n\nrestHelper.modify(Model, hook, cols);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .modify\n  .Model(User)\n  .cols(cols)\n  .hook('user')\n  .exec();\n```\n\n## restHelper.beforeAdd\n创建资源的操作，不包括 res.send 返回\n\n```js\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// cols 可选, 允许设置的字段\n// hook 必选, 生成实例的存放位置\n\nrestHelper.beforeAdd(Model, cols, hook);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .beforeAdd\n  .Model(User)\n  .cols(['name', 'age', 'gender'])\n  .hook('user')\n  .exec();\n```\n\n## restHelper.add\n标准的创建资源方法\n\n```js\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// cols 可选, 允许设置的字段\n// hook 可选, 生成实例的存放位置\n// attachs 可选，要附加输出的数据格式为 key =\u003e value, value 是 req 上的路径字符串\n\nrestHelper.add(Model, cols, hook, attachs);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .add\n  .Model(User)\n  .cols(['name', 'age', 'gender'])\n  .hook('user')\n  .exec();\n```\n\n## restHelper.batchAdd\n批量创建资源方法\n\n```js\n// Model 必选, Sequlize 定义的Model，表明数据的原型\n// cols 可选, 允许设置的字段\n// hook 可选, 生成实例的存放位置\n// attachs 可选，要附加输出的数据格式为 key =\u003e value, value 是 req 上的路径字符串\n\nrestHelper.batchAdd(Model, cols, hook, attachs);\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .batchAdd\n  .Model(User)\n  .cols(['name', 'age', 'gender'])\n  .hook('user')\n  .exec();\n```\n\n## restHelper.statistics\n标准的资源统计功能方法\n\n```js\n// Model 必选，Sequlize 定义的Model，表明数据从哪里获取\n// where 可选，额外的条件, req 对象上的路径，例如 'hooks.option.where',\n// hook 可选, 默认为空，如果指定了hook，则数据不直接输出而是先挂在 hook上\n// conf 可选，统计功能的配置，req 对象上值的路径例如 'hooks.user.conf'\n\nrestHelper.statistics(Model, 'hooks.opt.where', 'report', 'hooks.conf');\n\n// return\n// function(req, res, next) { ... };\n\n//or 链式调用\nrestHelper\n  .statistics\n  .Model(User)\n  .where('hooks.options.where')\n  .conf('hooks.stats.conf')\n  .hook('data')\n  .exec();\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-node%2Fopen-rest-helper-rest","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fopen-node%2Fopen-rest-helper-rest","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fopen-node%2Fopen-rest-helper-rest/lists"}