This repository has been archived by the owner on Jan 28, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
74 lines (55 loc) · 1.51 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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
var async = require('async');
var collections = {};
exports.search = function (query, callback) {
'use strict';
var lastError;
var requestedCollections;
if (! (query.collections && query.collections instanceof Array)) {
requestedCollections = Object.keys(collections);
} else {
requestedCollections = query.collections;
}
var maxResults = query.maxResults;
var pluginCallback = query.pluginCallback;
var results = [];
var iterator = function (collection, done) {
var myCollection = collections[collection];
if (! myCollection) {
return process.nextTick(function () {
lastError = new Error('Collection "' + collection + '" does not exist');
done();
});
}
myCollection.search(query, function (err, value) {
if (maxResults && value && value.data instanceof Array) {
value.data = value.data.slice(0, maxResults);
}
if (value) {
value.name = collection;
results.push(value);
}
if (err) {
lastError = err;
}
if (pluginCallback) {
if (err) {
pluginCallback(err);
} else {
pluginCallback(null, value);
}
}
done();
});
};
if (typeof callback !== 'function') {
callback = function () {};
}
var wrappedCallback = function () {
callback(lastError, results);
};
async.each(requestedCollections, iterator, wrappedCallback);
};
exports.add = function (name, plugin) {
'use strict';
collections[name] = plugin;
};