-
-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
map.js
201 lines (167 loc) · 4.65 KB
/
map.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
'use strict';
const Mixed = require('../schema/mixed');
const get = require('../helpers/get');
const util = require('util');
const utils = require('../utils');
/*!
* ignore
*/
class MongooseMap extends Map {
constructor(v, path, doc, schemaType) {
if (v != null && v.constructor.name === 'Object') {
v = Object.keys(v).reduce((arr, key) => arr.concat([[key, v[key]]]), []);
}
super(v);
this.$__parent = doc != null && doc.$__ != null ? doc : null;
this.$__path = path;
this.$__schemaType = schemaType == null ? new Mixed(path) : schemaType;
this.$__runDeferred();
}
$init(key, value) {
checkValidKey(key);
super.set(key, value);
if (value != null && value.$isSingleNested) {
value.$basePath = this.$__path + '.' + key;
}
}
$__set(key, value) {
super.set(key, value);
}
set(key, value) {
checkValidKey(key);
// Weird, but because you can't assign to `this` before calling `super()`
// you can't get access to `$__schemaType` to cast in the initial call to
// `set()` from the `super()` constructor.
if (this.$__schemaType == null) {
this.$__deferred = this.$__deferred || [];
this.$__deferred.push({ key: key, value: value });
return;
}
const fullPath = this.$__path + '.' + key;
const populated = this.$__parent != null && this.$__parent.$__ ?
this.$__parent.populated(fullPath) || this.$__parent.populated(this.$__path) :
null;
if (populated != null) {
if (value.$__ == null) {
value = new populated.options.model(value);
}
value.$__.wasPopulated = true;
} else {
try {
value = this.$__schemaType.
applySetters(value, this.$__parent, false, this.get(key));
} catch (error) {
if (this.$__parent != null && this.$__parent.$__ != null) {
this.$__parent.invalidate(fullPath, error);
return;
}
throw error;
}
}
super.set(key, value);
if (value != null && value.$isSingleNested) {
value.$basePath = this.$__path + '.' + key;
}
if (this.$__parent != null && this.$__parent.$__) {
this.$__parent.markModified(this.$__path + '.' + key);
}
}
delete(key) {
this.set(key, undefined);
super.delete(key);
}
toBSON() {
return new Map(this);
}
toObject(options) {
if (get(options, 'flattenMaps')) {
const ret = {};
const keys = this.keys();
for (const key of keys) {
ret[key] = this.get(key);
}
return ret;
}
return new Map(this);
}
toJSON() {
const ret = {};
const keys = this.keys();
for (const key of keys) {
ret[key] = this.get(key);
}
return ret;
}
inspect() {
return new Map(this);
}
$__runDeferred() {
if (!this.$__deferred) {
return;
}
for (let i = 0; i < this.$__deferred.length; ++i) {
this.set(this.$__deferred[i].key, this.$__deferred[i].value);
}
this.$__deferred = null;
}
}
if (util.inspect.custom) {
Object.defineProperty(MongooseMap.prototype, util.inspect.custom, {
enumerable: false,
writable: false,
configurable: false,
value: MongooseMap.prototype.inspect
});
}
Object.defineProperty(MongooseMap.prototype, '$__set', {
enumerable: false,
writable: true,
configurable: false
});
Object.defineProperty(MongooseMap.prototype, '$__parent', {
enumerable: false,
writable: true,
configurable: false
});
Object.defineProperty(MongooseMap.prototype, '$__path', {
enumerable: false,
writable: true,
configurable: false
});
Object.defineProperty(MongooseMap.prototype, '$__schemaType', {
enumerable: false,
writable: true,
configurable: false
});
Object.defineProperty(MongooseMap.prototype, '$isMongooseMap', {
enumerable: false,
writable: false,
configurable: false,
value: true
});
Object.defineProperty(MongooseMap.prototype, '$__deferredCalls', {
enumerable: false,
writable: false,
configurable: false,
value: true
});
/*!
* Since maps are stored as objects under the hood, keys must be strings
* and can't contain any invalid characters
*/
function checkValidKey(key) {
const keyType = typeof key;
if (keyType !== 'string') {
throw new TypeError(`Mongoose maps only support string keys, got ${keyType}`);
}
if (key.startsWith('$')) {
throw new Error(`Mongoose maps do not support keys that start with "$", got "${key}"`);
}
if (key.includes('.')) {
throw new Error(`Mongoose maps do not support keys that contain ".", got "${key}"`);
}
if (utils.specialProperties.has(key)) {
throw new Error(`Mongoose maps do not support reserved key name "${key}"`);
}
}
module.exports = MongooseMap;