Skip to content

Commit

Permalink
Merge pull request #297 from jdgjsag67251/296-after-update-direct-find
Browse files Browse the repository at this point in the history
#296: After update direct find
  • Loading branch information
StorytellerCZ authored Mar 31, 2023
2 parents fb94439 + 1fd054b commit 46caa3c
Show file tree
Hide file tree
Showing 5 changed files with 64 additions and 3 deletions.
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -158,6 +158,8 @@ effectively disable the pre-fetching of documents.
It is instead recommended to use the collection-wide options (e.g.
`MyCollection.hookOptions.after.update = {fetchPrevious: false};`).

This hook will always be called with the new documents; even if the updated document gets modified in a way were it would normally not be able to be found because of `before.find` hooks (see https://github.com/Meteor-Community-Packages/meteor-collection-hooks/pull/297).

--------------------------------------------------------------------------------

### .after.remove(userId, doc)
Expand Down Expand Up @@ -193,6 +195,8 @@ test.before.find(function (userId, selector, options) {
});
```

__Important:__ This hook does not get called for `after.update` hooks (see https://github.com/Meteor-Community-Packages/meteor-collection-hooks/pull/297).

--------------------------------------------------------------------------------

### .after.find(userId, selector, options, cursor)
Expand Down
4 changes: 2 additions & 2 deletions collection-hooks.js
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ CollectionHooks.initOptions = (options, pointcut, method) =>
CollectionHooks.extendOptions = (source, options, pointcut, method) =>
({ ...options, ...source.all.all, ...source[pointcut].all, ...source.all[method], ...source[pointcut][method] })

CollectionHooks.getDocs = function getDocs (collection, selector, options, fetchFields = {}) {
CollectionHooks.getDocs = function getDocs (collection, selector, options, fetchFields = {}, { useDirect = false } = {}) {
const findOptions = { transform: null, reactive: false }

if (Object.keys(fetchFields).length > 0) {
Expand Down Expand Up @@ -173,7 +173,7 @@ CollectionHooks.getDocs = function getDocs (collection, selector, options, fetch

// Unlike validators, we iterate over multiple docs, so use
// find instead of findOne:
return collection.find(selector, findOptions)
return (useDirect ? collection.direct : collection).find(selector, findOptions)
}

// This function normalizes the selector (converting it to an Object)
Expand Down
1 change: 1 addition & 0 deletions tests/common.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,3 +20,4 @@ import './hooks_in_loop.js'
import './upsert.js'
import './trycatch.js'
import './meteor_1_4_id_object.js'
import './find_after_hooks'
56 changes: 56 additions & 0 deletions tests/find_after_hooks.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
import { Mongo } from 'meteor/mongo'
import { Tinytest } from 'meteor/tinytest'

Tinytest.addAsync('issue #296 - after update hook always finds all updated', function (test, next) {
const collection = new Mongo.Collection(null)

collection.before.find((userId, selector) => {
selector.removedAt = { $exists: false }

return true
})

let beforeCalled = false
collection.before.update(() => {
beforeCalled = true
})

let afterCalled = false
collection.after.update(() => {
afterCalled = true
})

const id = collection.insert({ test: true })

collection.update(id, { $set: { removedAt: new Date() } }, () => {
test.equal(beforeCalled, true)
test.equal(afterCalled, true)
next()
})
})

Tinytest.addAsync('issue #296 - after insert hook always finds all inserted', function (test, next) {
const collection = new Mongo.Collection(null)

collection.before.find((userId, selector) => {
selector.removedAt = { $exists: false }

return true
})

let beforeCalled = false
collection.before.insert(() => {
beforeCalled = true
})

let afterCalled = false
collection.after.insert(() => {
afterCalled = true
})

collection.insert({ removedAt: new Date() }, () => {
test.equal(beforeCalled, true)
test.equal(afterCalled, true)
next()
})
})
2 changes: 1 addition & 1 deletion update.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ CollectionHooks.defineAdvice('update', function (userId, _super, instance, aspec
if (aspectFetchFields || globalFetchFields) {
Object.assign(fetchFields, globalFetchFields || {}, ...aspectFetchFields.map(a => a.fetchFields))
}
docs = CollectionHooks.getDocs.call(this, instance, { _id: { $in: docIds } }, options, fetchFields).fetch()
docs = CollectionHooks.getDocs.call(this, instance, { _id: { $in: docIds } }, options, fetchFields, { useDirect: true }).fetch()
}

aspects.after.forEach((o) => {
Expand Down

0 comments on commit 46caa3c

Please sign in to comment.