-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.js
More file actions
133 lines (120 loc) · 5 KB
/
index.js
File metadata and controls
133 lines (120 loc) · 5 KB
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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
const error = require('./src/error');
const SearchQueryBuilder = require('./src/SearchQueryBuilder');
const { UnknownPropertyError } = require('./src/error');
/**
* Loopback component that allows filtering over related models using the where filter.
*/
module.exports = function(loopbackApp, settings) {
Object
.values(loopbackApp.models)
.forEach((model) => {
const searchConfig = getSearchSettings(model, settings);
if (searchConfig.enabled === true) {
model.observe('access', extendedFindQuery(model, loopbackApp.models, searchConfig));
model.afterRemote('find', extendedFindQueryHandleOrder(model, loopbackApp.models, searchConfig));
}
});
};
// Export the component specific error classes.
module.exports.error = error;
/**
* Creates the function which is invoked for the 'find' and 'findOne' remote hook of loopback.
*
* For more information on remote hooks see https://loopback.io/doc/en/lb3/Remote-hooks.html
*
* @param model a loopback model
* @param models the loopback models object
* @returns {Function}
*/
function extendedFindQuery(model, models, { rejectUnknownProperties = false, preserveColumnCase = true, joinMethod = 'inner',joinOptions } = {}) {
return function(ctx, next) {
const originalFilter = getFilter(ctx);
if (!originalFilter) {
next();
} else {
const builder = new SearchQueryBuilder(models, { rejectUnknownProperties, preserveColumnCase, joinMethod, joinOptions });
const filter = Object.assign({}, originalFilter);
try {
const idName = model.getIdName();
const databaseQuery = builder.buildQuery(model.modelName, filter);
const sqlString = databaseQuery.toString();
model.dataSource.connector.execute(sqlString, (err, result) => {
if (err) {
next(err);
} else if (!result || result.length === 0) {
// no results match our query, prevent loopback from returning a result
// setting it to false would cause loopback to throw an error because
// it is no integer
ctx.query.where = {[idName]: -1};
next();
} else {
const resultIds = result.map(entry => entry[idName]);
// Removed the check for an existing id query, since the result of the
// database query should include the corresponding id already!
// Therefore we remove all the other constrains since they could lead to
// contradicting statements!
ctx.query.where = {[idName]: { inq: resultIds }};
// Remove the order,limit,skip from the original query
delete ctx.query.order;
delete ctx.query.limit;
delete ctx.query.skip;
delete ctx.query.offset;
next();
}
});
} catch (err) {
if (err instanceof UnknownPropertyError) {
err.status = 400;
}
next(err);
}
}
};
}
/**
* Creates the function which is invoked for the 'find' and 'findOne' after loading document for handling order.
*
* For more information on remote hooks see https://loopback.io/doc/en/lb3/Remote-hooks.html
*
* @param model a loopback model
* @param models the loopback models object
* @returns {Function}
*/
function extendedFindQueryHandleOrder(model, models, { rejectUnknownProperties = false, preserveColumnCase = true } = {}) {
return function(ctx, result, next) {
if (!ctx.result && !Array.isArray(ctx.result)) {
next();
} else {
const idName = model.getIdName();
if(ctx.args && ctx.args.filter && ctx.args.filter.where && ctx.args.filter.where[idName] && ctx.args.filter.where[idName].inq){
const idsOrder = ctx.args.filter.where[idName].inq;
ctx.result.sort(function(a, b){
return idsOrder.indexOf(a[idName]) - idsOrder.indexOf(b[idName]);
});
}
next();
}
};
}
/**
* Returns the filter query (either sent via API or remote method invocation).
*
* @param context the loopback request context
* @returns {null}
*/
function getFilter(context = {}) {
const query = context.query;
return query;
}
/**
* Gets the relationFilter settings from the models configuration (setting) and merges
* them with the basic component settings.
*
* @param model a loopback model
* @param componentSettings general settings of the component
* @returns {*}
*/
function getSearchSettings(model, componentSettings = {}) {
const modelSettings = model.definition.settings.relationFilter || {};
return Object.assign({}, componentSettings, modelSettings);
}