Automattic / Automattic/mongoose

Pass doc as parameter to pre and post hooks

Open
#8,964 4 comments 0 reactions 0 assignees View on GitHub
enhancement
Dominant language
JavaScript
Stars
27.5k
Forks
4k
Avg merge
2d 5h
Merged PRs (30d)
32

Description

It would be handy to have the document itself available as an parameter passed to the function rather than the this context so we can use arrow functions rather than the long form syntax.
Sort of like this
```js
UserSchema.pre('save', (next, doc) => {
doc.password = await hash(doc.password)
next()
})
```

Contributor guide

Open the contributing guide

Research direction

Start by tracing the UserSchema.pre('save') hook API and the corresponding pre and post middleware behavior. Confirm the intended callback argument order and how arrow functions should receive the document, then add coverage for both hook types. Done means the document is available as a function argument without relying on this.

Written by the indexing model from the issue text.

Assessment

Tech stack
javascript, mongodb, node.js
Domain
backend, databases
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.