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

Function search

lib/helpers/populate/getSchemaTypes.js:49–219  ·  view source on GitHub ↗
(parts, schema, subdoc, nestedPath)

Source from the content-addressed store, hash-verified

47 }
48
49 function search(parts, schema, subdoc, nestedPath) {
50 let p = parts.length + 1;
51 let foundschema;
52 let trypath;
53
54 while (p--) {
55 trypath = parts.slice(0, p).join('.');
56 foundschema = schema.path(trypath);
57 if (foundschema == null) {
58 continue;
59 }
60
61 if (foundschema.embeddedSchemaType || foundschema.schema) {
62 // array of Mixed?
63 if (foundschema.embeddedSchemaType instanceof Mixed) {
64 return foundschema.embeddedSchemaType;
65 }
66
67 let schemas = null;
68 if (foundschema.schema?.discriminators != null) {
69 const discriminators = foundschema.schema.discriminators;
70 const discriminatorKeyPath = trypath + '.' +
71 foundschema.schema.options.discriminatorKey;
72 const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : [];
73 schemas = Object.keys(discriminators).
74 reduce(function(cur, discriminator) {
75 const tiedValue = discriminators[discriminator].discriminatorMapping.value;
76 if (doc == null || keys.indexOf(discriminator) !== -1 || keys.indexOf(tiedValue) !== -1) {
77 cur.push(discriminators[discriminator]);
78 }
79 return cur;
80 }, []);
81 }
82
83 // Now that we found the array, we need to check if there
84 // are remaining document paths to look up for casting.
85 // Also we need to handle array.$.path since schema.path
86 // doesn't work for that.
87 // If there is no foundschema.schema we are dealing with
88 // a path like array.$
89 if (p !== parts.length && foundschema.schema) {
90 let ret;
91 if (parts[p] === '$') {
92 if (p + 1 === parts.length) {
93 // comments.$
94 return foundschema;
95 }
96 // comments.$.comments.$.title
97 ret = search(
98 parts.slice(p + 1),
99 schema,
100 subdoc ? mpath.get(trypath, subdoc) : null,
101 nestedPath.concat(parts.slice(0, p))
102 );
103 if (ret) {
104 ret.$parentSchemaDocArray = ret.$parentSchemaDocArray ||
105 (foundschema.schema.$isSingleNested ? null : foundschema);
106 }

Callers 1

getSchemaTypes.jsFile · 0.70

Calls 2

getMethod · 0.80
getFunction · 0.50

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…