* Overwrites native Map's `set()` function to support setters, `populate()`, * and change tracking. Note that Mongoose maps _only_ support strings and * ObjectIds as keys. * * Keys also cannot: * - be named after special properties `prototype`, `constructor`, and `__proto__` * - st
(key, value)
| 87 | */ |
| 88 | |
| 89 | set(key, value) { |
| 90 | if (isBsonType(key, 'ObjectId')) { |
| 91 | key = key.toString(); |
| 92 | } |
| 93 | |
| 94 | checkValidKey(key); |
| 95 | value = handleSpreadDoc(value); |
| 96 | |
| 97 | // Weird, but because you can't assign to `this` before calling `super()` |
| 98 | // you can't get access to `$__schemaType` to cast in the initial call to |
| 99 | // `set()` from the `super()` constructor. |
| 100 | |
| 101 | if (this.$__schemaType == null) { |
| 102 | this.$__deferred = this.$__deferred || []; |
| 103 | this.$__deferred.push({ key: key, value: value }); |
| 104 | return; |
| 105 | } |
| 106 | |
| 107 | let _fullPath; |
| 108 | const parent = this.$__parent; |
| 109 | const populated = parent?.$__?.populated ? |
| 110 | parent.$populated(fullPath.call(this), true) || parent.$populated(this.$__path, true) : |
| 111 | null; |
| 112 | const priorVal = this.get(key); |
| 113 | |
| 114 | if (populated != null) { |
| 115 | if (this.$__schemaType.$isSingleNested) { |
| 116 | throw new MongooseError( |
| 117 | 'Cannot manually populate single nested subdoc underneath Map ' + |
| 118 | `at path "${this.$__path}". Try using an array instead of a Map.` |
| 119 | ); |
| 120 | } |
| 121 | if (Array.isArray(value) && this.$__schemaType.$isMongooseArray) { |
| 122 | value = value.map(v => { |
| 123 | if (v.$__ == null) { |
| 124 | v = new populated.options[populateModelSymbol](v); |
| 125 | } |
| 126 | // Doesn't support single nested "in-place" populate |
| 127 | v.$__.wasPopulated = { value: v._doc._id }; |
| 128 | return v; |
| 129 | }); |
| 130 | } else if (value != null) { |
| 131 | if (value.$__ == null) { |
| 132 | value = new populated.options[populateModelSymbol](value); |
| 133 | } |
| 134 | // Doesn't support single nested "in-place" populate |
| 135 | value.$__.wasPopulated = { value: value._doc._id }; |
| 136 | } |
| 137 | } else { |
| 138 | try { |
| 139 | const options = this.$__schemaType.$isMongooseDocumentArray || this.$__schemaType.$isSingleNested || this.$__schemaType.$isMongooseArray || this.$__schemaType.$isSchemaMap ? |
| 140 | { path: fullPath.call(this) } : |
| 141 | null; |
| 142 | value = this.$__schemaType.applySetters( |
| 143 | value, |
| 144 | this.$__parent, |
| 145 | false, |
| 146 | this.get(key), |
no test coverage detected