-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathindex.js
44 lines (36 loc) · 1.12 KB
/
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
import {normalizeId, removeVersion, removePrivatePaths} from './helpers.js'
/**
* Default toJSON implementation for mongoose schema's
*/
export function toJSON(schema) {
//NOTE: this plugin is actually called *after* any schema's
//custom toJSON has been defined, so we need to ensure not to
//overwrite it. Hence, we remember it here and call it later
let transform
if (schema.options.toJSON && schema.options.toJSON.transform) {
transform = schema.options.toJSON.transform
}
//Extend toJSON options
schema.options.toJSON = Object.assign(schema.options.toJSON || {}, {
transform(doc, ret, options) {
//Remove private paths
if (schema.options.removePrivatePaths !== false) {
removePrivatePaths(ret, schema)
}
//Remove version
if (schema.options.removeVersion !== false) {
removeVersion(ret)
}
//Normalize ID
if (schema.options.normalizeId !== false) {
normalizeId(ret)
}
//Call custom transform if present
if (transform) {
return transform(doc, ret, options)
}
//Return
return ret
},
})
}