Created
March 25, 2020 15:04
-
-
Save tandavala/a96d93fd307f48af948f0ffc4d710af5 to your computer and use it in GitHub Desktop.
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
pageSchema.statics.findOneOrCreate = function findOneOrCreate(condition, doc, callback) { | |
const self = this; | |
self.findOne(condition, (err, result) => { | |
return result | |
? callback(err, result) | |
: self.create(doc, (err, result) => { | |
return callback(err, result); | |
}); | |
}); | |
} | |
Now when you have an instance of Page you can call findOneOrCreate: | |
Page.findOneOrCreate({id: 'somePageId'}, (err, page) => { | |
console.log(page); | |
}); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment