Created
September 12, 2017 11:29
-
-
Save talesluna-zz/eccd810912d224fc78847bb0fb33da07 to your computer and use it in GitHub Desktop.
Mongoose Merge Update with Lodash
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
import _ from 'lodash'; | |
class MergeUpdate { | |
save(original, update) { | |
// Update timestamp manually | |
if (original.updatedAt) { | |
update.updatedAt = new Date().toISOString(); | |
} | |
// Merge objects | |
_.merge(original, update); | |
// Save document | |
return original.save(); | |
} | |
} | |
export default new MergeUpdate() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment