Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
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: 1 addition & 1 deletion api-tool/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andes/api-tool",
"version": "2.3.0",
"version": "2.4.0",
"description": "",
"main": "build/index.js",
"module": "build/index.js",
Expand Down
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
2 changes: 1 addition & 1 deletion core/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@andes/core",
"version": "1.11.0",
"version": "1.12.0",
"description": "",
"main": "build/index.js",
"module": "build/index.js",
Expand Down
26 changes: 22 additions & 4 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,14 +185,31 @@ export abstract class ResourceBase<T extends Document = any> {
if (populate) {
query.populate(populate);
}

return await this.Model.find(query);
if (total) {
const queryPromises: any[] = [this.Model.find(query)];
if (skip === 0) {
delete (query as any).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) {
options.limit = 1;
const documents = await this.search(data, options, req);
if (documents.length > 0) {
if ((documents as any).length > 0) {
return documents[0];
}
return null;
Expand Down
6 changes: 3 additions & 3 deletions core/src/query-builder/in-mongo.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import * as moment from 'moment';
import { makePattern } from './utils';
import { Types } from 'mongoose';
import * as mongoose from 'mongoose';
import { isNullOrUndefined } from 'util';

export function matchDate(value: string) {
Expand Down Expand Up @@ -115,8 +115,8 @@ export function queryMatch(value: string, keyName: string, valueName: string) {
filtro[keyName] = ids[0];
}
if (ids[1]) {
if (Types.ObjectId.isValid(ids[1])) {
filtro[valueName] = Types.ObjectId(ids[1]);
if (mongoose.isValidObjectId(ids[1])) {
filtro[valueName] = mongoose.isValidObjectId(ids[1]);
} else {
filtro[valueName] = partialString(ids[1]);
}
Expand Down
Loading