Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

CORE - Mostrar el numero total de seguimientos filtrados en el buscador #52

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions api-tool/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export function apiOptions(req: ERequest): IOptions {
options.skip = req.query.skip ? parseInt(req.query.skip as string, 10) : null;
options.sort = req.query.sort as string;
options.populate = req.query.populate as string;
options.total = req.query.total as string;
return options;
}

Expand All @@ -34,6 +35,7 @@ export interface IOptions {
limit?: number;
sort?: string;
populate?: string;
total?: string;
}

export interface Request extends ERequest {
Expand Down
24 changes: 21 additions & 3 deletions core/src/model-builder/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -163,11 +163,12 @@ export abstract class ResourceBase<T extends Document = any> {
const preconditions = await this.presearch(data, req);
const conditions = MongoQuery.buildQuery(data, this.searchFileds);
options = options || {};
const { fields, skip, limit, sort, populate } = options;
const { fields, skip, limit, sort, populate, total } = options;
let query = this.Model.find({
...preconditions,
...conditions
});
let response;

if (fields) {
query.select(fields);
Expand All @@ -184,8 +185,25 @@ export abstract class ResourceBase<T extends Document = any> {
if (populate) {
query.populate(populate);
}

return await this.Model.find(query);
if (total) {
const queryPromises = [this.Model.find(query)];
if (skip === 0) {
delete query.options;
queryPromises.push(this.Model.find(query).count());
}
const [results, total] = await Promise.all(queryPromises);
response = {
pagination: {
offset: skip,
limit: limit,
total: total
},
data: results
};
} else {
response = await this.Model.find(query);
}
return response;
}

public async findOne(data: any, options: IOptions = {}, req: Request = null) {
Expand Down