map.js
1.67 KB
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
'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;
}
set(option, value) {
return SchemaType.set(option, value);
}
cast(val, doc, init) {
if (val instanceof MongooseMap) {
return val;
}
const path = this.path;
if (init) {
const map = new MongooseMap({}, path, doc, this.$__schemaType);
if (val instanceof global.Map) {
for (const key of val.keys()) {
let _val = val.get(key);
if (_val == null) {
_val = map.$__schemaType._castNullish(_val);
} else {
_val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
}
map.$init(key, _val);
}
} else {
for (const key of Object.keys(val)) {
let _val = val[key];
if (_val == null) {
_val = map.$__schemaType._castNullish(_val);
} else {
_val = map.$__schemaType.cast(_val, doc, true, null, { path: path + '.' + key });
}
map.$init(key, _val);
}
}
return map;
}
return new MongooseMap(val, path, doc, this.$__schemaType);
}
clone() {
const schematype = super.clone();
if (this.$__schemaType != null) {
schematype.$__schemaType = this.$__schemaType.clone();
}
return schematype;
}
}
Map.prototype.OptionsConstructor = SchemaMapOptions;
Map.defaultOptions = {};
module.exports = Map;