{"id":19737989,"url":"https://github.com/devsu/loopback-setup-hooks-mixins","last_synced_at":"2025-04-30T05:30:56.452Z","repository":{"id":92793986,"uuid":"75883067","full_name":"devsu/loopback-setup-hooks-mixins","owner":"devsu","description":"Mixins for Loopback, to easily configure operation and remote hooks from the model configuration file.","archived":false,"fork":false,"pushed_at":"2016-12-08T23:40:35.000Z","size":13,"stargazers_count":6,"open_issues_count":0,"forks_count":0,"subscribers_count":6,"default_branch":"master","last_synced_at":"2024-10-06T04:40:03.101Z","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/devsu.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}},"created_at":"2016-12-07T23:05:22.000Z","updated_at":"2017-12-28T06:24:44.000Z","dependencies_parsed_at":"2023-03-13T17:25:13.131Z","dependency_job_id":null,"html_url":"https://github.com/devsu/loopback-setup-hooks-mixins","commit_stats":{"total_commits":10,"total_committers":1,"mean_commits":10.0,"dds":0.0,"last_synced_commit":"8f265a656dff9fa071cd0bb3bc96072d090e120c"},"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-hooks-mixins","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-hooks-mixins/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-hooks-mixins/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/devsu%2Floopback-setup-hooks-mixins/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/devsu","download_url":"https://codeload.github.com/devsu/loopback-setup-hooks-mixins/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":224197880,"owners_count":17271999,"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-11-12T01:13:00.651Z","updated_at":"2024-11-12T01:13:01.278Z","avatar_url":"https://github.com/devsu.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# loopback-setup-hooks-mixins\nMixin for Loopback, to easily configure operation and remote hooks from the model configuration file.\n\nIt works with Loopback 2 and Loopback 3.\n\n## Installation\n\n```bash\nnpm install --save loopback-setup-hooks-mixins\n```\n\n## Configuration\n\nFirst, modify your `server/model-config.json` to include the path to this module:\n\n```json\n{\n  \"mixins\": [\n    \"loopback/common/mixins\",\n    \"loopback/server/mixins\",\n    \"../common/mixins\",\n    \"./mixins\",\n    \"../node_modules/loopback-setup-hooks-mixins\"\n  ]\n}\n```\n\nThen you can [use the mixins](https://loopback.io/doc/en/lb3/Defining-mixins.html#enable-a-model-with-mixins) from your model definition files:\n\n```json\n...\n  \"mixins\": {\n    \"SetupOperationHooks\": {\n      \"source\": \"./common/models/employee-hooks.js\",\n      \"before save\": [\"doSomething\"],\n      \"before delete\": [\"checkSomething\"]\n    },\n    \"SetupRemoteHooks\": {\n      \"source\": \"./common/models/employee-hooks.js\",\n      \"beforeRemote\": {\n        \"deleteById\": [\"checkSomethingElse\"],\n      },\n      \"afterRemote\": {\n        \"create\": [\"doSomethingElse\"]\n      }\n    }\n  }\n...\n```\n\n\n## Available mixins\n\n- [SetupOperationHooks](#setupoperationhooks)\n- [SetupRemoteHooks](#setupremotehooks)\n\n## SetupOperationHooks\n\nConfigures the defined operation hooks in the model.\n\n### Options\n\n- source (optional)\n- name of the operation hooks (e.g. \"before save\", \"after save\", etc.)\n\n### Usage\n\nFor each [operation hook](http://loopback.io/doc/en/lb3/Operation-hooks.html) that you want to configure, you will need to define a **string** or an **array** with the name(s) of the method(s) that should be called.\n\nIf you define `source` option, it will search the methods in that file, otherwise it will search the methods in the Model.\n\n```json\n  \"mixins\": {\n    \"SetupOperationHooks\": {\n      \"source\": \"./common/models/employee-hooks.js\",\n      \"before save\": \"doSomething\",\n      \"before delete\": [\"checkSomething\", \"doSomethingElse\"]\n    }\n  }\n```\n\nThe source file (`employee-hooks.js` in our example) would look like this:\n\n```javascript\nconst Promise = require('bluebird');\n\nmodule.exports = {\n  doSomething,\n  checkSomething,\n  doSomethingElse,\n};\n\nfunction doSomething(context) {\n  return new Promise((resolve, reject) =\u003e {\n    resolve();\n  });\n}\n\nfunction checkSomething(context) {\n  return new Promise((resolve, reject) =\u003e {\n    resolve();\n  });\n}\n\nfunction doSomethingElse(context) {\n  return new Promise((resolve, reject) =\u003e {\n    resolve();\n  });\n}\n```\n\nThe example is using promises, but if your prefer you could use the next callback as well.\n\n## SetupRemoteHooks\n\nConfigures the defined remote hooks in the model.\n\n### Options\n- source (optional)\n- beforeRemote\n- afterRemote\n- afterRemoteError\n\n### Usage\n\nThe `beforeRemote`, `afterRemote` and `afterRemoteError` options should have the names of the remote methods you want to apply the hooks to. \n\nEach remote method name is an object property that should contain a **string** or an **array** with the name(s) of the method(s) to be called.\n \nIf you define `source`, it will search the methods on that file, otherwise it will search the methods in the Model.\n\n```json\n  \"mixins\": {\n    \"SetupRemoteHooks\": {\n      \"source\": \"./common/models/employee-hooks.js\",\n      \"beforeRemote\": {\n        \"deleteById\": [\"checkSomething\", \"logSomething\"],\n      },\n      \"afterRemote\": {\n        \"create\": \"doSomething\"\n      },\n      \"afterRemoteError\": {\n        \"create\": \"doSomethingElse\"\n      }\n    }\n  }\n```\n\nThe source file (`employee-hooks.js` in our example) would look like this:\n\n```javascript\nmodule.exports = {\n  doSomething,\n  checkSomething,\n  logSomething,\n};\n\nfunction doSomething(context, instance, next) {\n  next();\n}\n\nfunction checkSomething(context, instance, next) {\n  next();\n}\n\nfunction logSomething(context, instance, next) {\n  next();\n}\n```\n\nThis example is not using promises, since it looks like they are not supported yet for remote hooks.\n\n## Credits\n\nCreated by [c3s4r](https://github.com/c3s4r) for [Devsu](http://devsu.com/).\n\nCopyright Devsu LLC, 2016.\n\nLicense: MIT\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Floopback-setup-hooks-mixins","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fdevsu%2Floopback-setup-hooks-mixins","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fdevsu%2Floopback-setup-hooks-mixins/lists"}