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
})
-
get
Defaults 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. -
getOne
Defaults tofindOne
. This also can be overridden like theget
method to customize the way you show your model. -
getOneOrMany
Based on yourget
methods, if the result holds a single match, it returns the single match, else it returns the array as is. -
getOrFail
returns a rejected promise if result from theget
method is an empty array. Usefull for verification and Authentication. -
getOneOrFail
returns a rejected promise if result fromgetOne
method is null. -
getOrCreate
It creates a record if it fails to find a match, based on yourget
query. The result is returned using yourgetOne
method. 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. -
createThenGet
Used for making newly created result consistent with the ones gotten fromgetOne
. -
getOneAndEdit
Since (at the time of writing), there was no provision for conditionalupdate
, this allows you set different query forgetOne
andupdate
. 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 duringupdate
operations 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 }
-
getManyAndEdit
same withgetOneAndEdit
but deals withupdateMany
and many results. returns the result usingget
; -
edit
performs anupdate
, then returns the result usinggetOne
. Alternatively you can use thefindOneAndUpdate
but this one maintains the result you would achieve with yourgetOne
method. -
editMany
performs anupdateMany
, the returns the result usingget
. -
editOrFail
performs anedit
, then returns a rejected promise if no match was found. The use case for this is rare, I think. -
editManyOrFail
performs aneditMany
, then returns a rejected promise no match was found. -
deleteOrFail
Tries to delete a record, returns a rejected promise if it no match was found -
deleteManyorFail
Tries to delete many records, returns a failed promise if no match was found