-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathindex.js
129 lines (114 loc) · 3.76 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
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
'use strict';
const mpath = require('mpath');
module.exports = function mongooseLeanGetters(schema, options) {
const fn = applyGettersMiddleware(schema, options);
// Use `pre('find')` so this also works with `cursor()`
// and `eachAsync()`, because those do not call `post('find')`
schema.pre('find', function() {
if (typeof this.map === 'function') {
this.map((res) => {
fn.call(this, res);
return res;
});
} else if (typeof this.transform === 'function') {
this.transform((res) => {
fn.call(this, res);
return res;
});
} else {
this.options.transform = (res) => {
fn.call(this, res);
return res;
};
}
});
schema.post('findOne', fn);
schema.post('findOneAndUpdate', fn);
schema.post('findOneAndDelete', fn);
schema.post('findOneAndReplace', fn);
};
function applyGettersMiddleware(schema, options) {
return function(res) {
this._mongooseLeanGettersOptions = options || {};
applyGetters.call(this, schema, res);
};
}
function applyGetters(schema, res) {
if (res == null) {
return;
}
const { defaultLeanOptions } = this._mongooseLeanGettersOptions;
const shouldCallGetters = this._mongooseOptions?.lean?.getters ?? defaultLeanOptions?.getters ?? false;
if (shouldCallGetters) {
if (Array.isArray(res)) {
const len = res.length;
for (let i = 0; i < len; ++i) {
applyGettersToDoc.call(this, schema, res[i]);
}
} else {
applyGettersToDoc.call(this, schema, res);
}
return res;
} else {
return res;
}
}
function getSchemaForDoc(schema, res) {
if (!schema.discriminatorMapping || !schema.discriminatorMapping.key || !schema.discriminators) {
return schema;
}
const discriminatorValue = res[schema.discriminatorMapping.key];
let childSchema = undefined;
for (const name of Object.keys(schema.discriminators)) {
const matchValue = schema.discriminators[name].discriminatorMapping.value;
if (matchValue === discriminatorValue) {
childSchema = schema.discriminators[name];
break;
}
}
// If no discriminator schema found, return the root schema (#39)
return childSchema || schema;
}
function applyGettersToDoc(schema, doc) {
if (doc == null) {
return;
}
if (Array.isArray(doc)) {
for (let i = 0; i < doc.length; ++i) {
const currentDoc = doc[i];
// If the current doc is null/undefined, there's nothing to do
if (currentDoc == null) continue;
// If it is a nested array, apply getters to each subdocument (otherwise it would attempt to apply getters to the array itself)
if (Array.isArray(currentDoc)) {
applyGettersToDoc.call(this, schema, currentDoc);
continue;
}
const schemaForDoc = getSchemaForDoc(schema, currentDoc);
applyGettersToDoc.call(this, schemaForDoc, currentDoc);
}
return;
}
const schemaForDoc = getSchemaForDoc(schema, doc);
schemaForDoc.eachPath((path, schematype) => {
if (!mpath.has(path, doc)) {
// The path is not present (likely from projection)
return;
}
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
if (Array.isArray(val) && schematype.$isMongooseArray) {
if (schematype.$isMongooseDocumentArray) {
val.forEach((subdoc) => applyGettersToDoc.call(this, schematype.schema, subdoc));
} else {
for (let i = 0; i < val.length; ++i) {
val[i] = schematype.caster.applyGetters(val[i], doc);
}
}
} if (val && typeof val === 'object' && schematype.$isSingleNested) {
applyGettersToDoc.call(this, schematype.schema, val);
} else {
mpath.set(path, val, doc);
}
});
}
module.exports.default = module.exports;
module.exports.mongooseLeanGetters = module.exports;