Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/peerlibrary/meteor-middleware
Middleware support for Meteor publish functions
https://github.com/peerlibrary/meteor-middleware
meteor meteor-middleware
Last synced: 5 days ago
JSON representation
Middleware support for Meteor publish functions
- Host: GitHub
- URL: https://github.com/peerlibrary/meteor-middleware
- Owner: peerlibrary
- License: bsd-3-clause
- Created: 2014-09-28T23:07:41.000Z (about 10 years ago)
- Default Branch: master
- Last Pushed: 2019-09-21T05:48:01.000Z (about 5 years ago)
- Last Synced: 2024-04-15T03:06:58.388Z (7 months ago)
- Topics: meteor, meteor-middleware
- Language: CoffeeScript
- Homepage: https://atmospherejs.com/peerlibrary/middleware
- Size: 13.7 KB
- Stars: 14
- Watchers: 5
- Forks: 1
- Open Issues: 2
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
Meteor Middleware
=================Meteor smart package which provides middleware support for Meteor publish functions. It creates a system of
publish endpoints and stacked middleware which allows easy use of reusable components in the pipeline between
MongoDB and clients, in a reactive manner.Implemented features are:
* class-based publish endpoint
* class-based middleware which can post-process documents send to the subscriber by the publish endpoint
* stacking of middleware onto the publish endpointPlanned features are:
* auto-generation of public API documentation for publish endpoints
* integration of automatic arguments checkingAdding this package to your [Meteor](http://www.meteor.com/) application adds `PublishEndpoint` and `PublishMiddleware`
objects into the global scope.Server side only.
Installation
------------```
meteor add peerlibrary:middleware
```Publish endpoints
-----------------`PublishEndpoint` provides a base connection between the database and the rest of the stack. It can be seen as
traditional Meteor publish function and you define it in a similar, but class-based way:```coffee
myEndpoint = new PublishEndpoint 'my-endpoint', (argument1, argument2) ->
# Here you can define your base publish function in the same way as you
# would otherwise. For example, you can just return a cursor:
Posts.find()
```Publish middleware
------------------To define middleware, you extend the `PublishMiddleware` class with following methods:
```coffee
class PublishMiddleware
added: (publish, collection, id, fields) =>
publish.added collection, id, fieldschanged: (publish, collection, id, fields) =>
publish.changed collection, id, fieldsremoved: (publish, collection, id) =>
publish.removed collection, idonReady: (publish) =>
publish.ready()onStop: (publish) =>
publish.stop()onError: (publish, error) =>
publish.error error
````publish` argument is the publish context of the current subscription. It is passed as an argument because
the same middleware instance is reused among all subscriptions to the same publish endpoint. In addition to
[all official methods available for you otherwise](http://docs.meteor.com/#meteor_publish), there are few additional:* `publish.params()` – returns the arguments passed to the subscription by the client, an array
* `publish.set(key, value)` – sets a `value` for the `key` in the state for this subscription, you can use this to share state
between middleware
* `publish.get(key)` – retrieves the value for the `key` from the stateDefault implementation of all `PublishMiddleware` methods is to pass it on to the client. You can modify parameters,
decide to send something all, or simply to ignore and not do anything. You call `publish` methods you want.Example:
```coffee
class LogAllActionsMiddleware extends PublishMiddleware
added: (publish, collection, id, fields) =>
console.log "added", collection, id, fields
superchanged: (publish, collection, id, fields) =>
console.log "changed", collection, id, fields
superremoved: (publish, collection, id) =>
console.log "removed", collection, id
superonReady: (publish) =>
console.log "ready"
superonStop: (publish) =>
console.log "stop"
superonError: (publish, error) =>
console.log "error", error
super
```Stacking middleware
-------------------Once you defined your middleware class, you can add in onto the publish endpoint:
```coffee
myEndpoint.use new LogAllActionsMiddleware()
```Middleware is stacked in the order they were registered. Those registered earlier are called earlier. At each middleware,
only later middleware is processed, no matter what you call from the method. For example, if you are in `added` method
and you call `publish.changed`, only `changed` for the rest of middleware stack will be called, and will **not** go from the
top again.Examples
--------See [tests](https://github.com/peerlibrary/meteor-middleware/blob/master/tests.coffee) for some examples. See
[middleware definitions in PeerLibrary](https://github.com/peerlibrary/peerlibrary/tree/development/server/middlewares) for
real-world definitions, and [their endpoints](https://github.com/peerlibrary/peerlibrary/blob/development/server).