-
Notifications
You must be signed in to change notification settings - Fork 64
Trigger a beforeCreate
middleware before inserting a new document
#107
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
Conversation
return callback(null, false); | ||
} | ||
return callback(err); | ||
self._middleware.trigger(MiddlewareHandler.Actions.beforeCreate, 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.
I don't know if we can clarify that this is talking about creating a MongoDB document (not a ShareDB document, which might be "created" with a version > 1).
I guess it's probably fine, so long as all of our actions are talking specifically about MongoDB operations (which I guess is already implied with beforeOverwrite
, and differentiated in beforeSnapshotLookup
)
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.
(I also can't help but wonder if this could have just been a "special case" of beforeOverwrite
(maybe renamed beforeWrite
), where query
happens to be null
, but I guess it's a bit late for that now anyway.)
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.
Also, as discussed, this is probably all fine.
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.
As discussed in the weekly meeting, I do still like it this way but open to adding another middleware for both at some point!
}; | ||
|
||
function expectDocumentNotToExist(cb) { | ||
var query = {_id: 'test1'}; |
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 function feels weird to me:
- it uses magic strings
- it assumes it's the only assertion, which is always run at the end (what if I want to assert many things?)
I'd normally prefer something like:
describe('', () => {
var docId;
beforeEach(() => {
docId = 'test1';
});
function expectStuff() {
// has access to docId
}
});
But better yet, can't we just make a helper method:
function docCount(collection, id, cb) {
var query = {_id: id};
db.query('collection', query, null, null, function(err, results) {
var count = null;
if (!err) count = results.length;
cb(err, count);
}
}
Still has some boilerplate in the code itself, but that's the sad fact of callbacks, I guess:
docCount(collection, id, function(error, count) {
if (error) return done(error);
expect(count).to.equal(0);
return done();
});
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.
As discussed, will fix this in another PR
This middleware is similar to the
beforeOverwrite
, but it does not have aquery
since it is a brand new document