-
Notifications
You must be signed in to change notification settings - Fork 64
Add middleware #105
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add middleware #105
Conversation
[](https://npmjs.org/package/sharedb-mongo) | ||
[](https://travis-ci.org/share/sharedb-mongo) | ||
[](https://coveralls.io/github/share/sharedb-mongo?branch=master) | ||
[](https://npmjs.org/package/sharedb-mongo) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
My linter kicked in on this README
index.js
Outdated
var succeeded = !!result.modifiedCount; | ||
callback(null, succeeded); | ||
request.query = {_id: id, _v: request.doc._v - 1}; | ||
self.trigger(self.MIDDLEWARE_ACTIONS.beforeEdit, request, function(middlewareErr) { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just so I'm clear: this doesn't need to be triggered on create?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No it does not at the moment, and I believe that if we wanted to, we should make another middleware for beforeCreate
. They are different because beforeEdit
has a query (for an existing document) but beforeCreate
would not.
Now that you mention it though, we probably do want to add the beforeCreate
for our purposes to ensure we never create documents with missing shard keys 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
But I would suggest let's leave the beforeCreate
for now - I will add it in another PR if we need it
Co-authored-by: Alec Gibson <12036746+alecgibson@users.noreply.github.com>
fall back to fetching all ops and working backwards from the | ||
current version. | ||
|
||
### Middlewares |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is the only real change to the docs
README.md
Outdated
- `'beforeOverwrite'` actions have additional context properties: | ||
- `documentToWrite` - The document to be written | ||
- `op` - The op that represents the changes that will be made to the document | ||
- `query` - A filter that will be used to lookup the document that is about to be edited |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
- `query` - A filter that will be used to lookup the document that is about to be edited | |
- `query` - A filter that will be used to lookup the document that is about to be edited, which should always include an ID and snapshot version e.g. `{_id: 'uuid', _v: 1}` |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
The addition of middleware is done in order to support sharding, in order for consumers of this library to satisfy any requirements for shard keys as defined by the MongoDB docs: https://docs.mongodb.com/manual/core/sharding-shard-key
Usage follows the same pattern as middlewares on ShareDB with differences on what the middleware context looks like depending on the actions.
Currently only adding a limited set of actions and properties on the context in order to reduce scope of this work.