Last active
January 11, 2016 18:33
-
-
Save intabulas/0f1a529ecb255d7a4952 to your computer and use it in GitHub Desktop.
Nodal find_by example
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
/** | |
* Finds a model with a provided column value, otherwise returns a notFound error. | |
* @param {object} A single key object that contains the model column name and value you're looking for | |
* @param {function({Error} err, {Nodal.Model} model)} callback The callback to execute upon completion | |
*/ | |
static find_by(findObject, callback) { | |
return new Composer(this) | |
.where(queryObject) | |
.end((err, models) => { | |
if (!err && !models.length) { | |
let queryBy = Object.keys(findObject)[0]; | |
let err = new Error(`Could not find ${this.name} with ${queryBy} "${findObject[queryBy]}".`); | |
err.notFound = true; | |
return callback(err); | |
} | |
callback(err, models); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment