1.4.1 • Published 4 years ago

@postilion/events v1.4.1

Weekly downloads
-
License
MIT
Repository
-
Last release
4 years ago

Event-Framework

event-framework is a nodejs wrapper around MongoDB change streams to support per-Collection events based on a set of listeners with filters

Installation

Use the package manager npm to install event-framework.

npm install @postilion/event-framework

Usage

  1. Import the EventFramework constructor
import { EventFramework } from '@postilion/event-framework';
  1. Define subscriptions with a model, operation, handler, filters and queue options
const subscriptions: Array<Subscription> = [
    {
		// a named subscription is one used by a scheduled job that
		// runs at periodic intervals or is manually pushed to from
		// another queue
        name: 'SyncFilingsByTicker',
        model: models.Company,
        operation: Operation.named,
        handler: filingManager.syncSecFilingFeedByTicker,
        filters: [],
        options: {}
    },
    {
		// a collection-based subscription listens for a change to
		// the model with an operationType that matches this operation
		// and meets the filters/pipeline query
        name: 'GetFilingDocumentsForFiling',
        model: models.Filing,
        operation: Operation.create,
        handler: filingManager.getDocumentsForFiling,
        filters: [
            {
                $match: {
                    status: 'unseeded'
                }
            }
        ],
        options: {}
    }
];
  1. Pass your connection string and subscriptions to the event framework constructor
const eventFramework = new EventFramework('mongodb://user:pass@localhost/db', subscriptions);
  1. Change a document in a collection you've created a subscription for
db.collection.insert({ ... })
  1. The handler attached to each matching subscription should receive an event
{
  id: "cde20f20-28d9-11ea-9735-99b5e82d5a99",
  name: "GetFilingDocumentsForFiling",
  operation: "insert",
  data: {
    _id: "5e06528b29734aab3823235d",
    status: "unseeded",
    company: "5e065276aeee4f3833517b6b",
    publishedAt: "2019-02-01T01:22:40.000Z",
    fiscalYearEnd: "1231-01-01T00:00:00.000Z",
    source: "sec",
    type: "10-K",
    refId: "0001018724-19-000004",
    period: "2018-12-31T00:00:00.000Z",
    url: "https://www.sec.gov/Archives/edgar/data/1018724/000101872419000004/0001018724-19-000004-index.htm",
    name: "Form 10-K",
    filedAt: "2019-02-01T00:00:00.000Z",
    acceptedAt: "2019-02-01T04:22:40.000Z",
    __v: 0
  }
}

Contributing

Pull requests are welcome. For major changes, please open an issue first to discuss what you would like to change.

License

MIT