-
Notifications
You must be signed in to change notification settings - Fork 15
/
index.js
146 lines (130 loc) · 4.25 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
'use strict';
const mpath = require('mpath');
module.exports = function mongooseLeanGetters(schema) {
const fn = applyGettersMiddleware(schema);
// 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) {
return function(res) {
applyGetters.call(this, schema, res);
};
}
function applyGetters(schema, res, path) {
if (res == null) {
return;
}
if (this._mongooseOptions.lean && this._mongooseOptions.lean.getters) {
if (Array.isArray(res)) {
const len = res.length;
for (let i = 0; i < len; ++i) {
applyGettersToDoc.call(this, schema, res[i], this._fields, path);
}
} else {
applyGettersToDoc.call(this, schema, res, this._fields, path);
}
for (let i = 0; i < schema.childSchemas.length; ++i) {
const childPath = path ? path + '.' + schema.childSchemas[i].model.path : schema.childSchemas[i].model.path;
const _schema = schema.childSchemas[i].schema;
const doc = mpath.get(schema.childSchemas[i].model.path, res);
if (doc == null) {
continue;
}
applyGetters.call(this, _schema, doc, childPath);
}
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, fields, prefix) {
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, fields, prefix);
continue;
}
const schemaForDoc = getSchemaForDoc(schema, currentDoc);
applyGettersToDoc.call(this, schemaForDoc, currentDoc, fields, prefix);
}
return;
}
const schemaForDoc = getSchemaForDoc(schema, doc);
schemaForDoc.eachPath((path, schematype) => {
const pathWithPrefix = prefix ? prefix + '.' + path : path;
if (this.selectedInclusively() &&
fields &&
fields[pathWithPrefix] == null &&
!this.isPathSelectedInclusive(pathWithPrefix)) { // fields[pathWithPrefix] should return false
return;
}
if (this.selectedExclusively() &&
fields &&
fields[pathWithPrefix] != null &&
!this.isPathSelectedInclusive(pathWithPrefix)) {
return;
}
const pathExists = mpath.has(path, doc);
if (pathExists) {
const val = schematype.applyGetters(mpath.get(path, doc), doc, true);
if (val && schematype.$isMongooseArray && !schematype.$isMongooseDocumentArray) {
mpath.set(
path,
val.map(subdoc => {
return schematype.caster.applyGetters(subdoc, doc);
}),
doc
);
} else {
mpath.set(path, val, doc);
}
}
});
}
module.exports.default = module.exports;
module.exports.mongooseLeanGetters = module.exports;