@schamane/small-graphql-mongoose-middleware
Version:

58 lines • 1.83 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.MongoPagedDataSource = void 0;
const lodash_1 = require("lodash");
const _1 = require(".");
const PER_PAGE = 10;
class MongoPagedDataSource extends _1.MongoDataSource {
constructor(entity, fieldTranslations, exts) {
super(entity, fieldTranslations, exts);
this.setLimit(PER_PAGE);
}
async listPaged(sort, pages) {
if (pages && pages > 1) {
this.setLimit(PER_PAGE * pages);
}
const all = (await this.list(sort));
const totalCount = await this.Entity.countDocuments();
const count = all.length;
const totalPages = (0, lodash_1.ceil)(totalCount / PER_PAGE);
if (pages && pages > 1) {
this.setLimit(PER_PAGE);
}
return {
count,
totalCount,
currentPage: 0,
pages: pages || 1,
totalPages,
data: all
};
}
async filterPaged(filters, sort, pages) {
if (pages && pages > 1) {
this.setLimit(PER_PAGE * pages);
}
const all = (await this.filter(filters, sort));
const totalCount = await this.Entity.countDocuments();
const queryCount = await this.count(filters);
const count = all.length;
const totalPages = (0, lodash_1.ceil)(queryCount / PER_PAGE);
if (pages && pages > 1) {
this.setLimit(PER_PAGE);
}
return {
count,
totalCount,
currentPage: 0,
pages: pages || 1,
totalPages,
data: all
};
}
async byId(id) {
return super.findById(id);
}
}
exports.MongoPagedDataSource = MongoPagedDataSource;
//# sourceMappingURL=mongoPagedDataSource.js.map