moloquent
v1.0.1
Published
Wrapper for mongoose queries
Maintainers
Readme
Moloquent
Inspired by laravel's eloquent.
To use, add it to your model class
const {Model} = require('moloquent');
thatModelSchema.loadClass(class thatModelClass extends Model{
})This package is useful for projects where you have a complex query for getting a model by default instead of the traditional find or findOne query.
It assumes you will use the get and getOne methods to wrap your custom query. This could be an aggregate, mapReduce or maybe find or findOne with lots of projection and other set up. This way you only get to write them once and resuse them for operations like updates (instead of using findOneAndUpdate) and create.
A common example is a model that includes rating. Normally you dont show it as is; you have to aggregate to show the average rating. Then your get query becomes..
const {Model} = require('moloquent');
//mongoose schema and all the other setups
thatModel.loadClass(class thatModelClass extends Model{
static get(){
return this.aggregate([
//calculate avergage rating, paginate the actual rating array
])
}
static getMany(){
return this.aggregate([
//paginate results, calculate average rating, paginate actula rating array
])
}
//other custom model methods
})Methods
getDefaults tofind. This can be overrriden to become your custom method of showing your model to the user. It returns an array of mathces or an empy array if none was matched.getOneDefaults tofindOne. This also can be overridden like thegetmethod to customize the way you show your model.getOneOrManyBased on yourgetmethods, if the result holds a single match, it returns the single match, else it returns the array as is.getOrFailreturns a rejected promise if result from thegetmethod is an empty array. Usefull for verification and Authentication.getOneOrFail
returns a rejected promise if result fromgetOnemethod is null.getOrCreateIt creates a record if it fails to find a match, based on yourgetquery. The result is returned using yourgetOnemethod. It is useful if you dont want to the modification effect of upserts but want to always get a result as you would with upserts.createThenGetUsed for making newly created result consistent with the ones gotten fromgetOne.getOneAndEditSince (at the time of writing), there was no provision for conditionalupdate, this allows you set different query forgetOneandupdate. I use it alot in array of subdocuments which are suppose to be unique, example rating. Since there is no way to check if the record exists duringupdateoperations and you want to return the result as you would withgetOne, you use the method.thatModel.getOneThenEdit(getQuery, editQuery, body){ //perform update operation //the return getOne }getManyAndEditsame withgetOneAndEditbut deals withupdateManyand many results. returns the result usingget;editperforms anupdate, then returns the result usinggetOne. Alternatively you can use thefindOneAndUpdatebut this one maintains the result you would achieve with yourgetOnemethod.editMany
performs anupdateMany, the returns the result usingget.editOrFailperforms anedit, then returns a rejected promise if no match was found. The use case for this is rare, I think.editManyOrFailperforms aneditMany, then returns a rejected promise no match was found.deleteOrFailTries to delete a record, returns a rejected promise if it no match was founddeleteManyorFailTries to delete many records, returns a failed promise if no match was found
