Created
January 19, 2018 14:16
-
-
Save MitsuhaKitsune/4d73baad6d4e134e66b6cba9d47d0a2e to your computer and use it in GitHub Desktop.
Update or create helper for waterline on SailsJS
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
// Include this code on your config/models.js | |
updateOrCreate: function(criteria, values){ | |
var self = this; | |
if (!values) values = criteria.where ? criteria.where : criteria; | |
return this.findOne(criteria).then(function (result){ | |
if (result) { | |
return self.update(criteria, values); | |
} else { | |
return self.create(values); | |
} | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment