Skip to content

Commit 6e20126

Browse files
author
santizz
committed
Breaking Changes: Post Search
1 parent 72e0821 commit 6e20126

File tree

10 files changed

+758
-192
lines changed

10 files changed

+758
-192
lines changed

.gitignore

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,3 +13,5 @@ typings
1313
*.log
1414

1515
.DS_Store
16+
17+
test-ri-backoffice.sh

lib/app/internals/ModelsLoader.js.map

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

lib/app/internals/ModelsLoader.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
///<reference path='../../../typings/index.d.ts'/>
22

3-
import FSUtils =require('../../utils/FSUtils');
3+
import FSUtils = require('../../utils/FSUtils');
44
import mongoose = require('mongoose');
55
import PluginRegistry = require("./PluginRegistry");
66
import RouteInjector = require("../RouteInjector");

lib/engine/routeinjector/rest/search.js

Lines changed: 88 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@ var statusCode = require('http-status-codes');
33
var utils = require('../utils');
44
var async = require('async');
55
var _ = require('lodash');
6+
var Hooks = require("../../../utils/HooksUtils");
7+
//var Hooks = new HooksUtils();
68

79
var injector = require('../../../');
810
var log = injector.log;
@@ -56,13 +58,13 @@ module.exports.getNDocumentsPost = function (Model) {
5658
options.limit = limit;
5759

5860
function checkOverrideHardlimit() {
59-
if(!req.user) return false;
61+
if (!req.user) return false;
6062
return req.user.role == perm_conf.adminRole;
6163
}
6264

6365
//Keep a hard-limit for the total items returned unless you are the adminRole
6466
var hardLimit = perm_conf.hardLimit || 50;
65-
if((!options.limit || options.limit > hardLimit) && !checkOverrideHardlimit()) {
67+
if ((!options.limit || options.limit > hardLimit) && !checkOverrideHardlimit()) {
6668
options.limit = hardLimit;
6769
}
6870

@@ -98,7 +100,7 @@ module.exports.getNDocumentsPost = function (Model) {
98100
delete queryCount.limit;
99101
}
100102

101-
Model.count(queryCount).exec(function (err, searchCount) {
103+
Model.count(queryCount).exec(async function (err, searchCount) {
102104

103105
if (err) {
104106
log.error(err);
@@ -108,45 +110,88 @@ module.exports.getNDocumentsPost = function (Model) {
108110
return res.end();
109111
}
110112

111-
// This deletes the limit in the query because when post middleware is executed, all the results
112-
// are queried and the skip/limit is done at the end. probably not the most intelligent way to do it
113-
if (config.post && config.post.length > 0) {
114-
delete options.limit;
115-
}
113+
try {
114+
// This deletes the limit in the query because when post middleware is executed, all the results
115+
// are queried and the skip/limit is done at the end. probably not the most intelligent way to do it
116+
var limit;
116117

117-
// TODO: In order to add a restriction in this case the body and the config JSON should be merged.
118-
Model.find(query, config.mongo.projection, options, function (err, result) {
119-
if (err) {
120-
log.error(err);
121-
res.statusCode = statusCode.INTERNAL_SERVER_ERROR;
122-
return res.end();
118+
if (config.post && config.post.length > 0) {
119+
limit = options.limit;
120+
delete options.limit;
123121
}
124122

125-
(config.populate) ? utils.dynamicPopulate(config.populate, Model, result, okCallbackTask) : okCallbackTask();
123+
var MongoQuery = Model.find(query, config.mongo.projection, options);
126124

127-
function okCallbackTask() {
128-
if (config.post && config.post.length > 0) {
129-
var funcArray = _.clone(config.post);
130-
var func = funcArray[0];
131-
funcArray[0] = startTask;
125+
if (config.populate) {
126+
if (!(config.populate instanceof Array)) throw 'populate field in ' + Model.modelName + ' routerInjector configuration should be an array';
132127

133-
function startTask(callback) {
134-
func(config, req, res, result, callback);
135-
}
128+
MongoQuery.populate(config.populate);
129+
}
136130

137-
async.waterfall(funcArray, function (err, _config, _req, _res, _result) {
138-
searchCount = skip ? _result.length + skip : _result.length;
139-
if (limit) {
140-
limit = (limit > _result.length || limit < 1) ? _result.length : limit;
141-
_result = _result.slice(0, limit);
131+
if (config.post && config.post.length > 0) {
132+
var cursor = MongoQuery.cursor();
133+
var acum = [];
134+
135+
acum = await Hooks.iterateCursor(
136+
cursor,
137+
limit || Infinity,
138+
Hooks.funcMerge(config.post),
139+
{
140+
ctx: {
141+
req,
142+
res,
143+
config
142144
}
143-
endOK(_result);
144-
});
145-
} else {
146-
endOK(result);
147-
}
145+
}
146+
);
147+
148+
try {
149+
cursor.close();
150+
} catch(err) { }
151+
152+
endOK(acum);
153+
154+
} else {
155+
156+
endOK(await MongoQuery.exec());
157+
148158
}
149159

160+
161+
// TODO: In order to add a restriction in this case the body and the config JSON should be merged.
162+
// Model.find(query, config.mongo.projection, options, function (err, result) {
163+
// if (err) {
164+
// log.error(err);
165+
// res.statusCode = statusCode.INTERNAL_SERVER_ERROR;
166+
// return res.end();
167+
// }
168+
169+
// (config.populate) ? utils.dynamicPopulate(config.populate, Model, result, okCallbackTask) : okCallbackTask();
170+
171+
// function okCallbackTask() {
172+
// if (config.post && config.post.length > 0) {
173+
// var funcArray = _.clone(config.post);
174+
// var func = funcArray[0];
175+
// funcArray[0] = startTask;
176+
177+
// function startTask(callback) {
178+
// func(config, req, res, result, callback);
179+
// }
180+
181+
// async.waterfall(funcArray, function (err, _config, _req, _res, _result) {
182+
// searchCount = skip ? _result.length + skip : _result.length;
183+
// if (limit) {
184+
// limit = (limit > _result.length || limit < 1) ? _result.length : limit;
185+
// _result = _result.slice(0, limit);
186+
// }
187+
// endOK(_result);
188+
// });
189+
// } else {
190+
// endOK(result);
191+
// }
192+
// }
193+
// }
194+
150195
function endOK(_result) {
151196
res.statusCode = statusCode.OK;
152197
var sendObj = {};
@@ -176,7 +221,16 @@ module.exports.getNDocumentsPost = function (Model) {
176221
return res.end();
177222
}
178223
}
179-
});
224+
225+
} catch (err) {
226+
227+
log.error(err);
228+
utils.runErrorCallbacks(config, req, err);
229+
res.statusCode = statusCode.INTERNAL_SERVER_ERROR;
230+
res.json(err);
231+
return res.end();
232+
233+
}
180234
});
181235
}
182236
});

0 commit comments

Comments
 (0)