{"id":17831204,"url":"https://github.com/crsten/mongoose-trigger","last_synced_at":"2025-03-19T07:31:52.106Z","repository":{"id":57142006,"uuid":"91363881","full_name":"crsten/mongoose-trigger","owner":"crsten","description":null,"archived":false,"fork":false,"pushed_at":"2019-01-20T10:32:42.000Z","size":6,"stargazers_count":9,"open_issues_count":4,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2024-10-04T03:43:23.691Z","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/crsten.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-05-15T17:11:11.000Z","updated_at":"2024-05-14T08:57:52.000Z","dependencies_parsed_at":"2022-09-05T07:31:26.607Z","dependency_job_id":null,"html_url":"https://github.com/crsten/mongoose-trigger","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-trigger","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-trigger/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-trigger/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/crsten%2Fmongoose-trigger/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/crsten","download_url":"https://codeload.github.com/crsten/mongoose-trigger/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":221724227,"owners_count":16870236,"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-27T19:42:22.517Z","updated_at":"2024-10-27T19:42:23.099Z","avatar_url":"https://github.com/crsten.png","language":"JavaScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# mongoose-trigger\n[![Build Status](https://travis-ci.org/crsten/mongoose-trigger.svg?branch=master\u0026style=flat-square)](https://travis-ci.org/crsten/mongoose-trigger)\n[![npm](https://img.shields.io/npm/dt/mongoose-trigger.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-trigger)\n[![npm](https://img.shields.io/npm/v/mongoose-trigger.svg?style=flat-square)](https://www.npmjs.com/package/mongoose-trigger)\n\n[Mongoose](http://mongoosejs.com/) plugin to automatically emit needed events.\n\nThis modules lets you attach event listeners directly to your models and emit event at any mongoose hook you want in an extremly flexible way.\n\n## Installation\n\n`npm install --save mongoose-trigger`\n\n## Usage\n\n```js\nconst mongoose = require('mongoose');\nconst Schema = mongoose.Schema;\nconst MongooseTrigger = require('mongoose-trigger');\n\nlet UserSchema = new Schema({\n  name: String,\n  email: String,\n  something: Array\n});\n\nconst UserEvents = MongooseTrigger(UserSchema, {\n  events: {\n    create: {\n      select: 'email skills',\n      populate: {\n        path: 'skills',\n        select: 'name'\n      }\n    },\n    update: {\n      populate: 'skills'\n    },\n    remove: false\n  },\n  partials: [\n    {\n      eventName: 'custom_event',\n      triggers: 'name',\n      select: 'name email',\n      populate: 'something' //if it is a reference...\n    }\n  ],\n  debug: false\n});\n\nUserEvents.on('create', data =\u003e console.log('[create] says:', data));\nUserEvents.on('update', data =\u003e console.log('[update] says:', data));\nUserEvents.on('partial:skills', data =\u003e console.log('[partial:skills] says:', data));\nUserEvents.on('partial:x', data =\u003e console.log('[partial:x] says:', data));\nUserEvents.on('remove', data =\u003e console.log('[remove] says:', data));\n```\n\n## Initialization\n\nCall `mongoose-trigger` as a function to initialize.\nIt takes 2 arguments:\n\n* Schema\n* Options\n\nAnd returns an instance of [Node Events](https://nodejs.org/api/events.html) which you can attach listeners to.\n\nThe following events are supported:\n\n* create\n* update\n* remove\n* partials (you're free to define those and they are prefixed with 'partial')\n\n```js\nconst Listener = MongooseTrigger(YourSchema, YourOptions);\nListener.on('partial:custom_event', data =\u003e console.log('do something...'));\n```\n\n## Options\n\n### events\n\nDefine what events you want to emit. The following events are available:\n\n- create\n- update\n- remove\n\n#### Enabling / Disabling events\n\n```js\nevents: {\n  create: false || true\n}\n```\n\n#### Advanced configuration\n\nThis configuration uses the same syntax as mongoose. Actually it uses mongoose functionallity to make it possible. So you can include exclude single \u0026 multiple fields\n\n```js\nevents: {\n  create: {\n    select: 'name email',\n    populate: 'something'\n  }\n}\n```\n\n```js\nevents: {\n  create: {\n    select: 'name email',\n    populate: {\n      path: 'something',\n      select: 'example'\n    }\n  }\n}\n```\n\n### partials\n\nPartials gives you the power to create custom events with custom triggers and custom data. Very flexible...\nThe settings do also use mongoose built in functionality. So be sure to checkout mongoose documentation.\n\n```js\npartials: [\n  {\n    eventName: 'custom_eventname',\n    triggers: 'name email',\n    select: 'email',\n    populate: 'something'\n  }\n]\n```\n\n```js\npartials: [\n  {\n    eventName: 'custom_eventname',\n    triggers: 'something',\n    select: 'something name',\n    populate: {\n      path: 'something',\n      select: 'random'\n    }\n  }\n]\n```\n\n### debug\n\nSet this to true if you want to output info about all emitted events.\n\n## Development \u0026 Testing\n\n`npm run dev` starts a server at [localhost:3000](http://localhost:3000).\n\n## License\n\n[The MIT License](http://opensource.org/licenses/MIT)\nCopyright (c) Carsten Jacobsen\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-trigger","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fcrsten%2Fmongoose-trigger","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fcrsten%2Fmongoose-trigger/lists"}