Skip to content

Instantly share code, notes, and snippets.

@intabulas
Last active January 11, 2016 18:33
Show Gist options
  • Save intabulas/0f1a529ecb255d7a4952 to your computer and use it in GitHub Desktop.
Save intabulas/0f1a529ecb255d7a4952 to your computer and use it in GitHub Desktop.
Nodal find_by example
/**
* 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