map.js
1009 Bytes
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
'use strict';
/*!
* ignore
*/
const MongooseMap = require('../types/map');
const SchemaMapOptions = require('../options/SchemaMapOptions');
const SchemaType = require('../schematype');
/*!
* ignore
*/
class Map extends SchemaType {
constructor(key, options) {
super(key, options, 'Map');
this.$isSchemaMap = true;
}
cast(val, doc, init) {
if (val instanceof MongooseMap) {
return val;
}
if (init) {
const map = new MongooseMap({}, this.path, doc, this.$__schemaType);
if (val instanceof global.Map) {
for (const key of val.keys()) {
map.$init(key, map.$__schemaType.cast(val.get(key), doc, true));
}
} else {
for (const key of Object.keys(val)) {
map.$init(key, map.$__schemaType.cast(val[key], doc, true));
}
}
return map;
}
return new MongooseMap(val, this.path, doc, this.$__schemaType);
}
}
Map.prototype.OptionsConstructor = SchemaMapOptions;
module.exports = Map;