This repository has been archived by the owner on Jul 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
1 changed file
with
52 additions
and
0 deletions.
There are no files selected for viewing
This file contains 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,52 @@ | ||
/** | ||
* BaseModelService | ||
* | ||
* @by Vunb | ||
* @date 09/12/2016 | ||
* | ||
*/ | ||
class BaseModelService { | ||
constructor(model) { | ||
// init default model | ||
this.model = model; | ||
} | ||
|
||
/** | ||
* get model | ||
*/ | ||
getModel() { | ||
return this.model; | ||
} | ||
|
||
/** | ||
* Allow search and pagination | ||
*/ | ||
bySearch(criterias, pageSize, pageIndex) { | ||
|
||
let limit = (pageSize || 0) * 1; | ||
let skip = (pageIndex || 0) * limit; | ||
return this.model.find(criterias) | ||
.skip(skip) | ||
.limit(limit) | ||
.then((results) => { | ||
let count = this.model.count(criterias) | ||
.then(result => { | ||
return result; | ||
}); | ||
// return an Array promises | ||
return [results, count]; | ||
}) | ||
.spread((results, count) => { | ||
// return an object | ||
return { | ||
total: count, | ||
List: results | ||
}; | ||
}); | ||
} | ||
} | ||
|
||
// Export module | ||
module.exports = new BaseModelService(); | ||
// Backwards-compat with node 0.10.x | ||
exports.BaseModelService = BaseModelService; |