-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
48 lines (39 loc) · 887 Bytes
/
index.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
const normalizeMongoose = schema => {
const {
toJSON,
normalizeId,
removeVersion,
removePrivatePaths,
toJSON: {transform} = {},
} = schema.options;
const json = {
transform(doc, returnValue, options) {
if (!removePrivatePaths) {
const {paths} = schema;
for (const path in paths) {
if (paths[path].options && paths[path].options.private && returnValue[path]) {
delete returnValue[path];
}
}
}
if (!removeVersion) {
const {__v} = returnValue;
if (!__v) {
delete returnValue.__v;
}
}
if (!normalizeId) {
const {_id, id} = returnValue;
if (_id && !id) {
returnValue.id = _id.toString();
delete returnValue._id;
}
}
if (transform) {
return transform(doc, returnValue, options);
}
},
};
schema.options.toJSON = {...toJSON, ...json};
};
export default normalizeMongoose;