-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch.js
47 lines (43 loc) · 1.11 KB
/
search.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
const config = require('./../../../config')
const debug = require('debug')('api:model:search')
/**
* Searches for documents in the database and returns a
* metadata object.
*
* @param {Object} query - the search query
* @param {Object} options - an options object
* @returns {Promise<Metadata>}
*/
module.exports = function ({
client,
options = {}
} = {}) {
let err
if (!this.searchHandler.canUse()) {
err = new Error('Not Implemented')
err.statusCode = 501
err.json = {
errors: [{
message: `Search is disabled or an invalid data connector has been specified.`
}]
}
} else if (!options.search || options.search.length < config.get('search.minQueryLength')) {
err = new Error('Bad Request')
err.statusCode = 400
err.json = {
errors: [{
message: `Search query must be at least ${config.get('search.minQueryLength')} characters.`
}]
}
}
if (err) {
return Promise.reject(err)
}
return this.validateAccess({
client,
type: 'read'
}).then(() => {
debug(options.search)
return this.searchHandler.find(options.search)
})
}