-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathfind.js
More file actions
87 lines (75 loc) · 2.89 KB
/
find.js
File metadata and controls
87 lines (75 loc) · 2.89 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
/**
* Module dependencies
*/
var util = require('util'),
actionUtil = require('../../node_modules/sails/lib/hooks/blueprints/actionUtil');
/**
* Find Records
*
* get /:modelIdentity
* * /:modelIdentity/find
*
* An API call to find and return model instances from the data adapter
* using the specified criteria. If an id was specified, just the instance
* with that unique id will be returned.
*
* Optional:
* @param {Object} where - the find criteria (passed directly to the ORM)
* @param {Integer} limit - the maximum number of records to send back (useful for pagination)
* @param {Integer} skip - the number of records to skip (useful for pagination)
* @param {String} sort - the order of returned records, e.g. `name ASC` or `age DESC`
* @param {String} callback - default jsonp callback param (i.e. the name of the js function returned)
*/
module.exports = function findRecords (req, res) {
// Get and Set range info from header
var rangeHeader = req.get('Range') || req.get('X-Range') || undefined;
var rePattern = new RegExp(/^items=(\d+)-(\d+)$/);
var rangeStart, rangeEnd;
if(rangeHeader){
var array= rangeHeader.match(rePattern);
rangeStart = array[1];
rangeEnd = array[2];
}
req.options.limit = rangeEnd - rangeStart;
req.options.skip = rangeStart;
// Look up the model
var Model = actionUtil.parseModel(req);
// If an `id` param was specified, use the findOne blueprint action
// to grab the particular instance with its primary key === the value
// of the `id` param. (mainly here for compatibility for 0.9, where
// there was no separate `findOne` action)
if ( actionUtil.parsePk(req) ) {
return require('./findOne')(req,res);
}
// Lookup for records that match the specified criteria
var query = Model.find()
.where( actionUtil.parseCriteria(req) )
.limit( actionUtil.parseLimit(req) )
.skip( actionUtil.parseSkip(req) )
.sort( actionUtil.parseSort(req) );
// TODO: .populateEach(req.options);
query = actionUtil.populateEach(query, req);
query.exec(function found(err, matchingRecords) {
if (err) return res.serverError(err);
// Only `.watch()` for new instances of the model if
// `autoWatch` is enabled.
if (req._sails.hooks.pubsub && req.isSocket) {
Model.subscribe(req, matchingRecords);
if (req.options.autoWatch) { Model.watch(req); }
// Also subscribe to instances of all associated models
_.each(matchingRecords, function (record) {
actionUtil.subscribeDeep(req, record);
});
}
if(rangeHeader){
Model.count(actionUtil.parseCriteria(req))
.exec(function(err, total){
if (err) return res.serverError(err);
res.set('Content-Range','items ' + rangeStart + '-' + rangeEnd + '/' + total);
res.ok(matchingRecords);
});
} else {
res.ok(matchingRecords);
}
});
};