Skip to content
This repository has been archived by the owner on Jul 19, 2023. It is now read-only.

Commit

Permalink
add happ base service
Browse files Browse the repository at this point in the history
  • Loading branch information
vunb committed Dec 9, 2016
1 parent f8b675f commit aaccfc4
Showing 1 changed file with 52 additions and 0 deletions.
52 changes: 52 additions & 0 deletions lib/app/services.js
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;

0 comments on commit aaccfc4

Please sign in to comment.