-
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
Merged
Merged
Add middleware #105
Changes from all commits
Commits
Show all changes
28 commits
Select commit
Hold shift + click to select a range
5e341b7
Add the ability to trigger and to use middleware
5632c4c
Add some comments for talking points
914b2c6
Some cleanup and documentation updates
1a7760f
Fix naming of opLink to opId
14f7b0c
Add test for middleware
1f87aee
Add tests
15eab51
Fix linting issues
b1e6040
Add support for middleware on snapshot retrieval
e7b30a8
Remove only on test
3b6d227
Reduce information that is not currently needed on the request
38ca0ef
Rename queryFilter to query
03b3436
Add tests for definition of request object
96ed2aa
Small wording fix in comments
ca71737
Fix confusion on baz vs fuzz in commit tests
2f68ab9
Update documentation
d4a552e
Minor docs updates
12418b0
Update README.md
pypmannetjies 3ed9f54
Update middleware handler to use OOP approach instead of mixin
5ec4873
Update to move middleware to subpath in src
6291841
Don't save db on the test context
3a53f56
Add error handling for the `use` function
825d1fd
Minor refactor on variable names and linting issues
bdb11cc
Rename doc -> documentToWrite
3d69a06
Update index.js
pypmannetjies d81dfda
Rename beforeEdit -> beforeOverwrite
1619765
Merge branch 'add-middleware' of github.com:share/sharedb-mongo into …
546fead
Apply suggestions from code review
pypmannetjies d82a7dc
Update README.md
pypmannetjies File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,13 +1,13 @@ | ||
# sharedb-mongo | ||
|
||
[](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) | ||
[](https://travis-ci.org/share/sharedb-mongo) | ||
[](https://coveralls.io/github/share/sharedb-mongo?branch=master) | ||
|
||
MongoDB database adapter for [sharedb](https://github.com/share/sharedb). This | ||
driver can be used both as a snapshot store and oplog. | ||
|
||
Snapshots are stored where you'd expect (the named collection with _id=id). In | ||
Snapshots are stored where you'd expect (the named collection with \_id=id). In | ||
addition, operations are stored in `o_COLLECTION`. For example, if you have | ||
a `users` collection, the operations are stored in `o_users`. | ||
|
||
|
@@ -17,38 +17,36 @@ the form of `_v` and `_type`). It is safe to query documents directly with the | |
MongoDB driver or command line. Any read only mongo features, including find, | ||
aggregate, and map reduce are safe to perform concurrent with ShareDB. | ||
|
||
However, you must *always* use ShareDB to edit documents. Never use the | ||
However, you must _always_ use ShareDB to edit documents. Never use the | ||
MongoDB driver or command line to directly modify any documents that ShareDB | ||
might create or edit. ShareDB must be used to properly persist operations | ||
together with snapshots. | ||
|
||
|
||
## Usage | ||
|
||
`sharedb-mongo` uses the [MongoDB NodeJS Driver](https://github.com/mongodb/node-mongodb-native), and it supports the same configuration options. | ||
|
||
There are two ways to instantiate a sharedb-mongo wrapper: | ||
|
||
1. The simplest way is to invoke the module and pass in your mongo DB | ||
arguments as arguments to the module function. For example: | ||
|
||
```javascript | ||
const db = require('sharedb-mongo')('mongodb://localhost:27017/test', {mongoOptions: {...}}); | ||
const backend = new ShareDB({db}); | ||
``` | ||
1. The simplest way is to invoke the module and pass in your mongo DB | ||
arguments as arguments to the module function. For example: | ||
|
||
2. If you'd like to reuse a mongo db connection or handle mongo driver | ||
instantiation yourself, you can pass in a function that calls back with | ||
a mongo instance. | ||
```javascript | ||
const db = require('sharedb-mongo')('mongodb://localhost:27017/test', {mongoOptions: {...}}); | ||
const backend = new ShareDB({db}); | ||
``` | ||
|
||
```javascript | ||
const mongodb = require('mongodb'); | ||
const db = require('sharedb-mongo')({mongo: function(callback) { | ||
mongodb.connect('mongodb://localhost:27017/test', callback); | ||
}}); | ||
const backend = new ShareDB({db}); | ||
``` | ||
2. If you'd like to reuse a mongo db connection or handle mongo driver | ||
instantiation yourself, you can pass in a function that calls back with | ||
a mongo instance. | ||
|
||
```javascript | ||
const mongodb = require('mongodb'); | ||
const db = require('sharedb-mongo')({mongo: function(callback) { | ||
mongodb.connect('mongodb://localhost:27017/test', callback); | ||
}}); | ||
const backend = new ShareDB({db}); | ||
``` | ||
|
||
## Queries | ||
|
||
|
@@ -172,7 +170,7 @@ failed ops: | |
- v4: collision 4 | ||
- v5: unique | ||
- v6: unique | ||
... | ||
... | ||
- v1000: unique | ||
|
||
If I want to fetch ops v1-v3, then we: | ||
|
@@ -190,6 +188,34 @@ In the case where a valid op cannot be determined, we still | |
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 commentThe reason will be displayed to describe this comment to others. Learn more. This is the only real change to the docs |
||
|
||
Middlewares let you hook into the `sharedb-mongo` pipeline for certain actions. They are distinct from [middleware in `ShareDB`](https://github.com/share/sharedb) as they are closer to the concrete calls that are made to `MongoDB` itself. | ||
|
||
The original intent for middleware on `sharedb-mongo` is to support running in a sharded `MongoDB` cluster to satisfy the requirements on shard keys for versions 4.2 and greater of `MongoDB`. For more information see [the MongoDB docs](https://docs.mongodb.com/manual/core/sharding-shard-key/#shard-keys). | ||
|
||
#### Usage | ||
|
||
`share.use(action, fn)` | ||
Register a new middleware. | ||
|
||
- `action` _(String)_ | ||
One of: | ||
- `'beforeOverwrite'`: directly before the call to replace a document, can include edits as well as deletions | ||
- `'beforeSnapshotLookup'`: directly before the call to issue a query for one or more snapshots by ID | ||
- `fn` _(Function(context, callback))_ | ||
Call this function at the time specified by `action` | ||
- `context` will always have the following properties: | ||
- `action`: The action this middleware is handling | ||
- `collectionName`: The collection name being handled | ||
- `options`: Original options as they were passed into the relevant function that triggered the action | ||
- `'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, which should always include an ID and snapshot version e.g. `{_id: 'uuid', _v: 1}` | ||
- `'beforeSnapshotLookup'` actions have additional context properties: | ||
- `query` - A filter that will be used to lookup the snapshot. When a single snapshot is looked up the query will take the shape `{_id: docId}` while a bulk lookup by a list of IDs will resemble `{_id: {$in: docIdsArray}}`. | ||
|
||
### Limitations | ||
|
||
#### Integrity | ||
|
@@ -226,26 +252,26 @@ Mongo errors are passed back directly. Additional error codes: | |
|
||
#### 4100 -- Bad request - DB | ||
|
||
* 4101 -- Invalid op version | ||
* 4102 -- Invalid collection name | ||
* 4103 -- $where queries disabled | ||
* 4104 -- $mapReduce queries disabled | ||
* 4105 -- $aggregate queries disabled | ||
* 4106 -- $query property deprecated in queries | ||
* 4107 -- Malformed query operator | ||
* 4108 -- Only one collection operation allowed | ||
* 4109 -- Only one cursor operation allowed | ||
* 4110 -- Cursor methods can't run after collection method | ||
- 4101 -- Invalid op version | ||
- 4102 -- Invalid collection name | ||
- 4103 -- $where queries disabled | ||
- 4104 -- $mapReduce queries disabled | ||
- 4105 -- $aggregate queries disabled | ||
- 4106 -- $query property deprecated in queries | ||
- 4107 -- Malformed query operator | ||
- 4108 -- Only one collection operation allowed | ||
- 4109 -- Only one cursor operation allowed | ||
- 4110 -- Cursor methods can't run after collection method | ||
|
||
#### 5100 -- Internal error - DB | ||
|
||
* 5101 -- Already closed | ||
* 5102 -- Snapshot missing last operation field | ||
* 5103 -- Missing ops from requested version | ||
* 5104 -- Failed to parse query | ||
|
||
- 5101 -- Already closed | ||
- 5102 -- Snapshot missing last operation field | ||
- 5103 -- Missing ops from requested version | ||
- 5104 -- Failed to parse query | ||
|
||
## MIT License | ||
|
||
Copyright (c) 2015 by Joseph Gentle and Nate Smith | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,7 @@ | ||
module.exports = { | ||
// Triggers before the call to replace a document is made | ||
beforeOverwrite: 'beforeOverwrite', | ||
// Triggers directly before the call to issue a query for snapshots | ||
// Applies for both a single lookup by ID and bulk lookups by a list of IDs | ||
beforeSnapshotLookup: 'beforeSnapshotLookup' | ||
}; |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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