@hiki9/rich-domain
Version:
Rich Domain is a library that provides a set of tools to help you build complex business logic in NodeJS using Domain Driven Design principles.
42 lines • 1.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.Pagination = void 0;
class Pagination {
constructor(criteria, paginationResult) {
this.result = paginationResult.result;
this.query = {
timestamp: Date.now(),
currentPage: Math.floor(criteria.offset / criteria.limit) + 1,
totalPages: Math.ceil(paginationResult.total / criteria.limit),
totalResults: paginationResult.total,
config: {
search: criteria.search,
offset: criteria.offset,
limit: criteria.limit,
filter: criteria?.filter,
businessFilter: criteria?.businessFilter,
orderBy: criteria?.orderBy,
},
};
}
toJSON(transformer) {
if (typeof transformer === 'function') {
this.result = this.result.map(transformer);
return this;
}
if (!this.result.length) {
return this;
}
const [item] = this.result;
if (typeof item !== 'object') {
return this;
}
if (typeof item?.toJSON !== 'function') {
throw new Error('toJSON method is not implemented in the pagination item. Please provide a transformer function in pagination.toJSON() method. Example pagination.toJSON((item) => item.toJSON())');
}
this.result = this.result.map((item) => item.toJSON());
return this;
}
}
exports.Pagination = Pagination;
//# sourceMappingURL=pagination.js.map