-
Notifications
You must be signed in to change notification settings - Fork 4
/
index.js
executable file
·60 lines (52 loc) · 1.63 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
module.exports = function (app, options) {
var remotes = app.remotes();
// Set X-Total-Count for all search requests
var applyXTotal = function (ctx, next) {
var filter;
if (ctx.args && ctx.args.filter) {
filter = ctx.args.filter.where;
}
if (!ctx.res._headerSent) {
this.count(filter, function (err, count) {
ctx.res.set('X-Total-Count', count);
next();
});
} else {
next();
}
};
var pattern = options && Array.isArray(options.pattern) ? options.pattern : ['*.find'];
for (var i=pattern.length-1; i>=0; i--) {
remotes.after(pattern[i], applyXTotal);
}
// Changes by Thomas Vié
// for nested relational models
var relationMethodNames = options.relationMethodNames;
// Function has to calculate count for nested pattern
// With __count__relatedModelName
var models = app.models();
for(let model of models) {
for(let relationMethodName of relationMethodNames) {
model.afterRemote(relationMethodName, function (ctx, output, next) {
// Get Model name (final model)
//TODO check if works in all cases
const relatedModel = app.models[ctx.resultType[0]];
var filter;
if (ctx.args && ctx.args.filter) {
filter = ctx.args.filter.where;
}
relatedModel.count(filter, function(err, count) {
if(err) {
throw new Error(err);
}
if (!ctx.res._headerSent) {
ctx.res.set('X-Total-Count', count);
next();
} else {
throw new Error('Headers already sent !');
}
});
});
}
}
};