MCPcopy
hub / github.com/Automattic/mongoose / analyzePath

Function analyzePath

lib/queryHelpers.js:299–374  ·  view source on GitHub ↗
(path, type)

Source from the content-addressed store, hash-verified

297 }
298
299 function analyzePath(path, type) {
300 if (fields == null) {
301 return;
302 }
303
304 // If schema-level selected not set, nothing to do
305 if (typeof type.selected !== 'boolean') {
306 return;
307 }
308
309 // User overwriting default exclusion
310 if (type.selected === false && fields[path]) {
311 if (sanitizeProjection) {
312 fields[path] = 0;
313 }
314
315 return;
316 }
317
318 // If set to 0, we're explicitly excluding the discriminator key. Can't do this for all fields,
319 // because we have tests that assert that using `-path` to exclude schema-level `select: true`
320 // fields counts as an exclusive projection. See gh-11546
321 if (!exclude && type.selected && path === schema.options.discriminatorKey && fields[path] != null && !fields[path]) {
322 delete fields[path];
323 return;
324 }
325
326 if (exclude === false && type.selected && fields[path] != null && !fields[path]) {
327 delete fields[path];
328 return;
329 }
330
331 const plusPath = '+' + path;
332 const hasPlusPath = fields && plusPath in fields;
333 if (hasPlusPath) {
334 // forced inclusion
335 delete fields[plusPath];
336
337 // if there are other fields being included, add this one
338 // if no other included fields, leave this out (implied inclusion)
339 if (exclude === false && keys.length > 1 && !~keys.indexOf(path) && !sanitizeProjection) {
340 fields[path] = 1;
341 } else if (exclude == null && sanitizeProjection && type.selected === false) {
342 fields[path] = 0;
343 }
344
345 return;
346 }
347
348 // check for parent exclusions
349 const pieces = path.split('.');
350 let cur = '';
351 for (let i = 0; i < pieces.length; ++i) {
352 cur += cur.length ? '.' + pieces[i] : pieces[i];
353 if (excluded.indexOf(cur) !== -1) {
354 return;
355 }
356 }

Callers 1

analyzeSchemaFunction · 0.85

Calls 1

getFunction · 0.70

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…