-
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,6 +4,7 @@ var ShareDbMongo = require('..'); | |
|
||
var mongoUrl = process.env.TEST_MONGO_URL || 'mongodb://localhost:27017/test'; | ||
var BEFORE_EDIT = ShareDbMongo.MiddlewareActions.beforeOverwrite; | ||
var BEFORE_CREATE = ShareDbMongo.MiddlewareActions.beforeCreate; | ||
var BEFORE_SNAPSHOT_LOOKUP = ShareDbMongo.MiddlewareActions.beforeSnapshotLookup; | ||
|
||
function create(callback) { | ||
|
@@ -155,6 +156,63 @@ describe('mongo db middleware', function() { | |
}); | ||
}); | ||
|
||
describe(BEFORE_CREATE, function() { | ||
it('has the expected properties on the request object', function(done) { | ||
db.use(BEFORE_CREATE, function(request, next) { | ||
expect(request).to.have.all.keys([ | ||
'action', | ||
'collectionName', | ||
'documentToWrite', | ||
'op', | ||
'options' | ||
]); | ||
expect(request.action).to.equal(BEFORE_CREATE); | ||
expect(request.collectionName).to.equal('testcollection'); | ||
expect(request.documentToWrite.foo).to.equal('bar'); | ||
expect(request.op).to.exist; | ||
expect(request.options.testOptions).to.equal('baz'); | ||
next(); | ||
done(); | ||
}); | ||
|
||
var snapshot = {type: 'json0', id: 'test1', v: 1, data: {foo: 'bar'}}; | ||
|
||
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, {testOptions: 'baz'}, function(err) { | ||
if (err) return done(err); | ||
}); | ||
}); | ||
|
||
it('should augment the written document when commit is called', function(done) { | ||
// Change the written value of foo to be `fuzz` | ||
db.use(BEFORE_CREATE, function(request, next) { | ||
request.documentToWrite.foo = 'fuzz'; | ||
next(); | ||
}); | ||
|
||
var snapshot = {type: 'json0', id: 'test1', v: 1, data: {foo: 'bar'}}; | ||
|
||
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, function(err) { | ||
if (err) return done(err); | ||
expectDocumentToContainFoo('fuzz', done); | ||
}); | ||
}); | ||
|
||
it('returns without writing when there was a middleware error', function(done) { | ||
db.use(BEFORE_CREATE, function(_, next) { | ||
next(new Error('Oh no!')); | ||
}); | ||
|
||
var snapshot = {type: 'json0', id: 'test1', v: 1, data: {foo: 'bar'}}; | ||
|
||
db.commit('testcollection', snapshot.id, {v: 0, create: {}}, snapshot, null, function(err) { | ||
expectDocumentNotToExist(function() { | ||
if (err) return done(); | ||
pypmannetjies marked this conversation as resolved.
Show resolved
Hide resolved
|
||
done('Expected an error, did not get one'); | ||
}); | ||
}); | ||
}); | ||
}); | ||
|
||
describe(BEFORE_SNAPSHOT_LOOKUP, function() { | ||
it('has the expected properties on the request object before getting a single snapshot', function(done) { | ||
db.use(BEFORE_SNAPSHOT_LOOKUP, function(request, next) { | ||
|
@@ -284,4 +342,14 @@ describe('mongo db middleware', function() { | |
cb(); | ||
}); | ||
}; | ||
|
||
function expectDocumentNotToExist(cb) { | ||
var query = {_id: 'test1'}; | ||
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 function feels weird to me:
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 commentThe reason will be displayed to describe this comment to others. Learn more. As discussed, will fix this in another PR |
||
|
||
db.query('testcollection', query, null, null, function(err, results) { | ||
if (err) return cb(err); | ||
expect(results).to.be.empty; | ||
cb(); | ||
}); | ||
}; | ||
}); |
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 inbeforeSnapshotLookup
)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 renamedbeforeWrite
), wherequery
happens to benull
, 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!