Skip to content

Commit 8d4d727

Browse files
committed
supported search argument in options
1 parent a48fbd2 commit 8d4d727

3 files changed

Lines changed: 54 additions & 36 deletions

File tree

src/routes/projects/index.js

Lines changed: 5 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -7,28 +7,12 @@ const getDatabase = require('../../database');
77
// Get the project formatter
88
const projectFormatter = require('../../utils/project-formatter');
99
// Get auxiliar functions
10-
const { parseJSON, getConfig } = require('../../utils/auxiliar-functions');
10+
const { parseJSON, getConfig, parseType, getSearchQuery } = require('../../utils/auxiliar-functions');
1111
// Standard HTTP response status codes
1212
const { BAD_REQUEST, INTERNAL_SERVER_ERROR } = require('../../utils/status-codes');
1313

1414
const projectRouter = Router();
1515

16-
// Convert a string input into int, float or boolean type if possible
17-
const parseType = input => {
18-
// Booleans
19-
if (input === 'false') return false;
20-
if (input === 'true') return true;
21-
// Numbers
22-
if (+input) return +input;
23-
// Other strings
24-
return input;
25-
};
26-
27-
// Escape all regex sensible characters
28-
const escapeRegExp = input => {
29-
return input.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
30-
};
31-
3216
// Root
3317
projectRouter.route('/').get(
3418
handler({
@@ -42,24 +26,10 @@ projectRouter.route('/').get(
4226
// Look for the search text in the accession and some metadata/pdbInfo fields
4327
const search = request.query.search;
4428
if (search) {
45-
// trim() removes surrounding white spaces
46-
const tsearch = escapeRegExp(search.trim());
47-
// $regex is a mongo command to search for regular expressions inside fields
48-
// $options: 'i' stands for the search to be case insensitive
49-
if (!finder.$and) finder.$and = [];
50-
finder.$and.push( {
51-
$or: [
52-
{ accession: { $regex: tsearch, $options: 'i' } },
53-
{ 'metadata.NAME': { $regex: tsearch, $options: 'i' } },
54-
{ 'metadata.DESCRIPTION': { $regex: tsearch, $options: 'i' } },
55-
{ 'metadata.AUTHORS': { $regex: tsearch, $options: 'i' } },
56-
{ 'metadata.GROUPS': { $regex: tsearch, $options: 'i' } },
57-
{ 'metadata.REFERENCES': { $regex: tsearch, $options: 'i' } },
58-
{ 'metadata.INCHIKEYS': { $regex: tsearch, $options: 'i' } },
59-
{ 'metadata.PDBIDS': { $regex: tsearch, $options: 'i' } },
60-
{ 'metadata.SYSKEYS': { $regex: tsearch, $options: 'i' } },
61-
],
62-
});
29+
// Parse the search text to a predefined smart query
30+
const searchQuert = getSearchQuery(search);
31+
if (!finder.$and) finder.$and = [ searchQuert ];
32+
else finder.$and.push(searchQuert);
6333
}
6434
// Then, filter by 'filter' parameters
6535
// Look for a specified value in any database field

src/routes/projects/options/index.js

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ const getDatabase = require('../../../database');
66
// Standard HTTP response status codes
77
const { BAD_REQUEST, NOT_FOUND } = require('../../../utils/status-codes');
88
// Set a error-proof JSON parser
9-
const { getValueGetter } = require('../../../utils/auxiliar-functions');
9+
const { getValueGetter, getSearchQuery } = require('../../../utils/auxiliar-functions');
1010
// Import references configuration
1111
const { REFERENCES, REFERENCE_HEADER, TOPOLOGY_HEADER } = require('../../../utils/constants');
1212

@@ -21,6 +21,14 @@ router.route('/').get(
2121
// Set an object with all the parameters to performe the mongo query
2222
// Start filtering by published projects only if we are in production environment
2323
const finder = database.getBaseFilter();
24+
// Handle when there is an automatic query
25+
let search = request.query.search;
26+
if (search) {
27+
// Process the mongo query to convert references and topology queries
28+
const searchQuery = getSearchQuery(search);
29+
if (!finder.$and) finder.$and = [ searchQuery ];
30+
else finder.$and = finder.$and.concat(searchQuery);
31+
}
2432
// Handle when there is a mongo query
2533
let query = request.query.query;
2634
if (query) {

src/utils/auxiliar-functions/index.js

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -166,6 +166,43 @@ const min = array => array.reduce((cv, nv) => Math.min(cv, nv), Infinity);
166166
const round2tenths = number => Math.round(number * 10) / 10;
167167
const round2cents = number => Math.round(number * 100) / 100;
168168

169+
// Convert a string input into int, float or boolean type if possible
170+
const parseType = input => {
171+
// Booleans
172+
if (input === 'false') return false;
173+
if (input === 'true') return true;
174+
// Numbers
175+
if (+input) return +input;
176+
// Other strings
177+
return input;
178+
};
179+
180+
// Escape all regex sensible characters
181+
const escapeRegExp = input => {
182+
return input.replace(/[-[/\]{}()*+?.,\\^$|#\s]/g, '\\$&');
183+
};
184+
185+
// Set the defualt smart query when the user uses the "search" parameters
186+
const getSearchQuery = searchText => {
187+
// Removes surrounding white spaces and escape regular expression characters
188+
const trimedText = escapeRegExp(searchText.trim());
189+
// $regex is a mongo command to search for regular expressions inside fields
190+
// $options: 'i' stands for the search to be case insensitive
191+
return {
192+
$or: [
193+
{ accession: { $regex: trimedText, $options: 'i' } },
194+
{ 'metadata.NAME': { $regex: trimedText, $options: 'i' } },
195+
{ 'metadata.DESCRIPTION': { $regex: trimedText, $options: 'i' } },
196+
{ 'metadata.AUTHORS': { $regex: trimedText, $options: 'i' } },
197+
{ 'metadata.GROUPS': { $regex: trimedText, $options: 'i' } },
198+
{ 'metadata.REFERENCES': { $regex: trimedText, $options: 'i' } },
199+
{ 'metadata.INCHIKEYS': { $regex: trimedText, $options: 'i' } },
200+
{ 'metadata.PDBIDS': { $regex: trimedText, $options: 'i' } },
201+
{ 'metadata.SYSKEYS': { $regex: trimedText, $options: 'i' } },
202+
],
203+
};
204+
}
205+
169206
module.exports = {
170207
getRequestUrl,
171208
parseJSON,
@@ -183,4 +220,7 @@ module.exports = {
183220
min,
184221
round2tenths,
185222
round2cents,
223+
parseType,
224+
escapeRegExp,
225+
getSearchQuery,
186226
}

0 commit comments

Comments
 (0)