deno.land / x / mongoose@6.7.5 / lib / helpers / populate / getSchemaTypes.js

getSchemaTypes.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
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
'use strict';
/*! * ignore */
const Mixed = require('../../schema/mixed');const get = require('../get');const getDiscriminatorByValue = require('../discriminator/getDiscriminatorByValue');const leanPopulateMap = require('./leanPopulateMap');const mpath = require('mpath');
const populateModelSymbol = require('../symbols').populateModelSymbol;
/** * Given a model and its schema, find all possible schema types for `path`, * including searching through discriminators. If `doc` is specified, will * use the doc's values for discriminator keys when searching, otherwise * will search all discriminators. * * @param {Model} model * @param {Schema} schema * @param {Object} doc POJO * @param {string} path * @api private */
module.exports = function getSchemaTypes(model, schema, doc, path) { const pathschema = schema.path(path); const topLevelDoc = doc; if (pathschema) { return pathschema; }
const discriminatorKey = schema.discriminatorMapping && schema.discriminatorMapping.key; if (discriminatorKey && model != null) { if (doc != null && doc[discriminatorKey] != null) { const discriminator = getDiscriminatorByValue(model.discriminators, doc[discriminatorKey]); schema = discriminator ? discriminator.schema : schema; } else if (model.discriminators != null) { return Object.keys(model.discriminators).reduce((arr, name) => { const disc = model.discriminators[name]; return arr.concat(getSchemaTypes(disc, disc.schema, null, path)); }, []); } }
function search(parts, schema, subdoc, nestedPath) { let p = parts.length + 1; let foundschema; let trypath;
while (p--) { trypath = parts.slice(0, p).join('.'); foundschema = schema.path(trypath); if (foundschema == null) { continue; }
if (foundschema.caster) { // array of Mixed? if (foundschema.caster instanceof Mixed) { return foundschema.caster; }
let schemas = null; if (foundschema.schema != null && foundschema.schema.discriminators != null) { const discriminators = foundschema.schema.discriminators; const discriminatorKeyPath = trypath + '.' + foundschema.schema.options.discriminatorKey; const keys = subdoc ? mpath.get(discriminatorKeyPath, subdoc) || [] : []; schemas = Object.keys(discriminators). reduce(function(cur, discriminator) { const tiedValue = discriminators[discriminator].discriminatorMapping.value; if (doc == null || keys.indexOf(discriminator) !== -1 || keys.indexOf(tiedValue) !== -1) { cur.push(discriminators[discriminator]); } return cur; }, []); }
// Now that we found the array, we need to check if there // are remaining document paths to look up for casting. // Also we need to handle array.$.path since schema.path // doesn't work for that. // If there is no foundschema.schema we are dealing with // a path like array.$ if (p !== parts.length && foundschema.schema) { let ret; if (parts[p] === '$') { if (p + 1 === parts.length) { // comments.$ return foundschema; } // comments.$.comments.$.title ret = search( parts.slice(p + 1), schema, subdoc ? mpath.get(trypath, subdoc) : null, nestedPath.concat(parts.slice(0, p)) ); if (ret) { ret.$isUnderneathDocArray = ret.$isUnderneathDocArray || !foundschema.schema.$isSingleNested; } return ret; }
if (schemas != null && schemas.length > 0) { ret = []; for (const schema of schemas) { const _ret = search( parts.slice(p), schema, subdoc ? mpath.get(trypath, subdoc) : null, nestedPath.concat(parts.slice(0, p)) ); if (_ret != null) { _ret.$isUnderneathDocArray = _ret.$isUnderneathDocArray || !foundschema.schema.$isSingleNested; if (_ret.$isUnderneathDocArray) { ret.$isUnderneathDocArray = true; } ret.push(_ret); } } return ret; } else { ret = search( parts.slice(p), foundschema.schema, subdoc ? mpath.get(trypath, subdoc) : null, nestedPath.concat(parts.slice(0, p)) );
if (ret) { ret.$isUnderneathDocArray = ret.$isUnderneathDocArray || !foundschema.schema.$isSingleNested; } return ret; } } else if (p !== parts.length && foundschema.$isMongooseArray && foundschema.casterConstructor.$isMongooseArray) { // Nested arrays. Drill down to the bottom of the nested array. let type = foundschema; while (type.$isMongooseArray && !type.$isMongooseDocumentArray) { type = type.casterConstructor; }
const ret = search( parts.slice(p), type.schema, null, nestedPath.concat(parts.slice(0, p)) ); if (ret != null) { return ret; }
if (type.schema.discriminators) { const discriminatorPaths = []; for (const discriminatorName of Object.keys(type.schema.discriminators)) { const _schema = type.schema.discriminators[discriminatorName] || type.schema; const ret = search(parts.slice(p), _schema, null, nestedPath.concat(parts.slice(0, p))); if (ret != null) { discriminatorPaths.push(ret); } } if (discriminatorPaths.length > 0) { return discriminatorPaths; } } } } else if (foundschema.$isSchemaMap && foundschema.$__schemaType instanceof Mixed) { return foundschema.$__schemaType; }
const fullPath = nestedPath.concat([trypath]).join('.'); if (topLevelDoc != null && topLevelDoc.$__ && topLevelDoc.$populated(fullPath) && p < parts.length) { const model = doc.$__.populated[fullPath].options[populateModelSymbol]; if (model != null) { const ret = search( parts.slice(p), model.schema, subdoc ? mpath.get(trypath, subdoc) : null, nestedPath.concat(parts.slice(0, p)) );
if (ret) { ret.$isUnderneathDocArray = ret.$isUnderneathDocArray || !model.schema.$isSingleNested; } return ret; } }
const _val = get(topLevelDoc, trypath); if (_val != null) { const model = Array.isArray(_val) && _val.length > 0 ? leanPopulateMap.get(_val[0]) : leanPopulateMap.get(_val); // Populated using lean, `leanPopulateMap` value is the foreign model const schema = model != null ? model.schema : null; if (schema != null) { const ret = search( parts.slice(p), schema, subdoc ? mpath.get(trypath, subdoc) : null, nestedPath.concat(parts.slice(0, p)) );
if (ret != null) { ret.$isUnderneathDocArray = ret.$isUnderneathDocArray || !schema.$isSingleNested; return ret; } } } return foundschema; } } // look for arrays const parts = path.split('.'); for (let i = 0; i < parts.length; ++i) { if (parts[i] === '$') { // Re: gh-5628, because `schema.path()` doesn't take $ into account. parts[i] = '0'; } } return search(parts, schema, doc, []);};
mongoose

Version Info

Tagged at
a year ago