{"_id":"@data-processing.solutions/grit-events-mongoose","_rev":"1-d39a0fa730001b58030283eb9e6e585b","name":"@data-processing.solutions/grit-events-mongoose","dist-tags":{"latest":"0.0.2"},"versions":{"0.0.2":{"name":"@data-processing.solutions/grit-events-mongoose","main":"lib/index.js","author":{"name":"Linus Gubenis, Data Processing Solutions, Inc."},"scripts":{"test":""},"private":false,"dependencies":{"debug":"^4.1.0","mongoose":"^5.3.13"},"version":"0.0.2","devDependencies":{"babel-eslint":"^10.0.1","eslint":"^5.16.0","eslint-plugin-babel":"^5.3.0"},"gitHead":"c957fd993aee8ed0de32f9901138615036b1cb6e","description":"## Create, Fetch, Acquire and Handle - events being saved as documents in MongoDB.","_id":"@data-processing.solutions/grit-events-mongoose@0.0.2","_nodeVersion":"12.1.0","_npmVersion":"6.9.0","dist":{"integrity":"sha512-v/ymDZ8HiQ+RM+eZpRnJXhP/TuLhCc5YzNaeMFghhjyrF7Da26Do8lqT23A67aU/XE2gzQvXDwN84pvYb8kfNA==","shasum":"eb395e1b9edb9aafa67221145e10d90af725aa05","tarball":"https://registry.npmjs.org/@data-processing.solutions/grit-events-mongoose/-/grit-events-mongoose-0.0.2.tgz","fileCount":11,"unpackedSize":16689,"npm-signature":"-----BEGIN PGP SIGNATURE-----\r\nVersion: OpenPGP.js v3.0.4\r\nComment: https://openpgpjs.org\r\n\r\nwsFcBAEBCAAQBQJcycFkCRA9TVsSAnZWagAAGVIQAIuFMJFoUBm98Yd+nE9D\nzgbGw/6rmdKZQY/ip0OUea2613akGLGf+RBmLMlw8Se8UNNsnIb2CznwN4Eb\nYL49pARC3T20tXVl8CxZg28yjZXJPVwzsMfbtlqENo0Otg4lR7cqCiCcb2Fq\n7SLB1cbf+CVu3tQY6qmSK6vJuP4ubbJy2bnS9plMIxc0/R4O0WiTOGBskLIe\nuqwWTB6mMOYdOWi2tZgnDgozIFUk+Muiukj+YYsauzwqFsfam2eytWwH9NDP\nTVccT0JaxwNc6tSXe0DwFhqebTvWVZj4lnLjreaW1CwxDAXTsQMjvYU+Tp7k\njbjxrMeq5RFhLpN0N5+iKI40Wzc2vGZgL89ONMACJgr9um3sq/q+ou59qMr7\n8yfelqjYlDg5mVZnYg1vEnICVi2R2y8xIVI10Lbft1wvaMNHswLPWPj4Dp15\nNEn+EjpEuniaXeCZjp+EKt2aScZ6CFikPG0fiHa+4nidoetVaA8HQSFwSvAz\nO0ibyO2xuEu31CQ+nh4GvYwRLbpVvNDPT1fsDT/HtW1aMXMSsQAHKWSiqhwh\ntnLJWMgN4Ysdzdn6lUrfmdU3MOVcK6XgSZ0UyoZ8XIu5AYTmBwMJxMUxqrX3\nk74D11TNdAgUVio8Lo+PNQPBpQDpjhlCfNRMfCuTwFn66U+Nzh0vg/jtjFdN\noQ1H\r\n=KvzB\r\n-----END PGP SIGNATURE-----\r\n","signatures":[{"keyid":"SHA256:jl3bwswu80PjjokCgh0o2w5c2U4LhQAE57gj9cz1kzA","sig":"MEUCIHtyY8LhvRgLFnppU2vdF03KxGVjpfe9nXQSmBfp9d99AiEA7CewN6V0ZvUdLoVFw1wnU6Kk4ClAAEoDtZKOwBKQODw="}]},"maintainers":[{"name":"beist","email":"linus.gubenis@gmail.com"}],"_npmUser":{"name":"beist","email":"linus.gubenis@gmail.com"},"directories":{},"_npmOperationalInternal":{"host":"s3://npm-registry-packages","tmp":"tmp/grit-events-mongoose_0.0.2_1556726115894_0.7715706179169508"},"_hasShrinkwrap":false}},"time":{"created":"2019-05-01T15:55:15.706Z","0.0.2":"2019-05-01T15:55:16.008Z","modified":"2022-04-05T02:50:38.301Z"},"maintainers":[{"name":"beist","email":"linus.gubenis@gmail.com"}],"description":"## Create, Fetch, Acquire and Handle - events being saved as documents in MongoDB.","author":{"name":"Linus Gubenis, Data Processing Solutions, Inc."},"readme":"# Grit Events Mongoose\n\n## Create, Fetch, Acquire and Handle - events being saved as documents in MongoDB.\n\n### What is Grit Events Mongoose\n\nGrit Events Mongoose is a class to instantiate event producers and consumers. It can be used to develop all kinds of event-based systems, for example an engine to execute BPMN processes. When initialized with event handlers it functions as an event-consumer, otherwise as an event-producer. In both cases a connection to a MongoDB database is opened.\n\n### How does Grit Events Mongoose work under the hood\n\nWhen created with handlers - once an instance of the class is listening - it creates a backoff function which checks for new events to handle (it is looking for events which were not yet acquired). Whenever a new (not acquired) event is found, the machine tries to acquire it. It could happen that another machine instance is also trying to acquire it but only one will win. Whoever acquires the event will handle it and when handled set it to status: handled. A producer can be used to create new events.\n\n### How to install\n\n```sh\n$ npm install @data-processing.solutions/grit-events-mongoose\n```\n\nor\n\n```sh\n$ yarn add @data-processing.solutions/grit-events-mongoose\n```\n\n### Examples\n\n#### Require\n\n```js\nconst GritEvents = require('@data-processing.solutions/grit-events-mongoose');\n```\n\n#### Producer\n\n```js\nconst gritEvents = new GritEvents({\n  dbUrl: 'mongodb://localhost:27017',\n  DEFAULT_PRIORITY: 50, // default: 50\n  DEFAULT_MAX_RETRIES: 0 // default: 0\n});\n\ngritEvents.listen().catch(err => console.error(err));\n\nfor (let i = 0; i <= 1; i++) {\n  gritEvents.createNewEvent({\n    intent: 'CREATE_SOMETHING',\n    time: new Date().getTime() - 1 // optional/default\n  });\n}\n```\n\nWhen producing events, note that the `intent` is the name of the handler method you provide your consumer with. `time` is the desired time of executing this event, the `(current timestamp - 1)` should make it being picked up right away (if there are no other events, which are waiting for longer, or have higher `priority`). You can control how often a failed event gets re-scheduled for retry with `maxRetries`, by default it is set to `0` so failed events won't get retried.\n\n#### Consumer\n\n```js\nconst gritEvents = new GritEvents(\n  {\n    dbUrl: 'mongodb://localhost:27017',\n    DEFAULT_PRIORITY: 50,\n    batchSize: 1, // Optional (default: 1), set how many events you want to fetch, acquire and handle at once\n    intentScope: ['CREATE_SOMETHING'] // Optional, this worker/consumer only fetches, acquires and handles events with the intent CREATE_SOMETHING, [] means nothing will be handled, not defining it means all handlers will be handled\n  },\n  {\n    CREATE_SOMETHING: event => {\n      console.log('create something', event._id);\n    }\n  }\n);\n\ngritEvents.listen().catch(err => console.error(err));\n```\n\nWhen consuming events, note that you can define `batchSize` to acquire and handle multiple events at once, the default is `1`. You can also define an `intentScope` so you might have an instance of the Event Machine only handling certain events, even though it is capable of more, handler-wise. This could be used to scale (TODO: elaborate).\n\n#### Directly access the database schema\n\nWhenever you have the need to read an existing event directly, you can invoke `mongoose` methods on the Event Model, like described following:\n\n```js\nconst Event = require('@grit/grit-events-mongoose/lib/schemas/Event');\n\nconst specificEvent = await Event.findById('...');\n```\n\n#### EventEmitter\n\nThe GritEventMongoose class extends the native Node.JS EventEmitter (require('events')), there are some before and after events emitted for the process to listen to. Those are side-effects and it is not intended nor possible (?) to interrupt fetching, acquisition or handling of events like this.\nSuch a desire is only possible through the handlers themselves, they are promises and if they fail the event won't get marked as handled.\n\n```js\n// before acquiring an event\ngritEvents.on('before.acquire.CREATE_SOMETHING', event => {});\n\n// after successfully acquiring an event\ngritEvents.on('after.acquire.CREATE_SOMETHING', event => {});\n\n// before handling an event\ngritEvents.on('before.handle.CREATE_SOMETHING', event => {});\n\n// after successfully handling an event\ngritEvents.on('after.handle.CREATE_SOMETHING', event => {});\n\n// the event got scheduled for a retry\ngritEvents.on('retry.handle.CREATE_SOMETHING', event => {});\n\n// when an event never (after retries) was able to be handled\ngritEvents.on('failed.handle.CREATE_SOMETHING', event => {});\n```\n\n#### Retries\n\nWhen creating events via `createNewEvent` you can define `maxRetries` (default: 0), so when handling the event fails, it gets retried `n` times. By default failed events won't get retried. Note: the handler gets executed 1 + maxRetries times in total.\n","readmeFilename":"README.md"}