PageRenderTime 33ms CodeModel.GetById 24ms app.highlight 5ms RepoModel.GetById 1ms app.codeStats 0ms

/node_modules/mongoose/lib/services/query/selectPopulatedFields.js

https://bitbucket.org/coleman333/smartsite
JavaScript | 45 lines | 34 code | 5 blank | 6 comment | 15 complexity | 8638e7c367188d634889e701f7ecd5f2 MD5 | raw file
 1'use strict';
 2
 3/*!
 4 * ignore
 5 */
 6
 7module.exports = function selectPopulatedFields(query) {
 8  var opts = query._mongooseOptions;
 9
10  if (opts.populate != null) {
11    var paths = Object.keys(opts.populate);
12    var i;
13    var userProvidedFields = query._userProvidedFields || {};
14    if (query.selectedInclusively()) {
15      for (i = 0; i < paths.length; ++i) {
16        if (!isPathInFields(userProvidedFields, paths[i])) {
17          query.select(paths[i]);
18        }
19      }
20    } else if (query.selectedExclusively()) {
21      for (i = 0; i < paths.length; ++i) {
22        if (userProvidedFields[paths[i]] == null) {
23          delete query._fields[paths[i]];
24        }
25      }
26    }
27  }
28};
29
30/*!
31 * ignore
32 */
33
34function isPathInFields(userProvidedFields, path) {
35  var pieces = path.split('.');
36  var len = pieces.length;
37  var cur = pieces[0];
38  for (var i = 1; i < len; ++i) {
39    if (userProvidedFields[cur] != null) {
40      return true;
41    }
42    cur += '.' + pieces[i];
43  }
44  return userProvidedFields[cur] != null;
45}