-
Notifications
You must be signed in to change notification settings - Fork 7
Expand file tree
/
Copy pathDefaultModelMethods.js
More file actions
340 lines (306 loc) · 11.4 KB
/
DefaultModelMethods.js
File metadata and controls
340 lines (306 loc) · 11.4 KB
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
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
class DefaultModelMethods {
/**
* [apiSubRoutes description]
* @return {[type]} [description]
*/
static apiSubRoutes() {
/// this points to model (schema.statics.)
const subRoutes = {};
let fPopulated = pathname => {
return async doc => {
const populate = doc.populate(pathname);
if (populate.execPopulate) {
await populate.execPopulate();
} else {
await populate;
}
// await doc.populate(pathname).execPopulate();
return doc[pathname];
};
};
let fExternal = (externalModel, refKey) => {
return doc => {
const whereOptions = {};
whereOptions[refKey] = doc;
return externalModel.find().where(whereOptions);
};
};
this.schema.eachPath((pathname, schematype) => {
if (
schematype &&
schematype.instance &&
schematype.instance.toLowerCase() == 'objectid' &&
schematype.options &&
schematype.options.ref
) {
/// there is Ref ObjectId in this model
subRoutes[pathname] = fPopulated(pathname);
}
});
for (let key of Object.keys(this.db.models)) {
let model = this.db.models[key];
model.schema.eachPath((refKey, schematype) => {
if (
schematype &&
schematype.instance &&
schematype.instance.toLowerCase() == 'objectid' &&
schematype.options &&
schematype.options.ref &&
schematype.options.ref == this.modelName
) {
//// there is Ref to this model in other model
let pathname = model.prototype.collection.name;
if (!subRoutes[pathname]) {
/// set up route as default name, /author/ID/books
subRoutes[pathname] = fExternal(model, refKey);
} else {
/// if there're few refs to same model, as Author and co-Author in book, set up additional routes
/// as /author/ID/books_as_coauthor
/// keeping the first default one
pathname += '_as_' + refKey;
subRoutes[pathname] = fExternal(model, refKey);
}
} else if (
schematype &&
schematype.instance == 'Array' &&
schematype.options &&
schematype.options.type &&
schematype.options.type[0] &&
schematype.options.type[0].ref == this.modelName
) {
//// Refs as the array of ObjectId
//// like:
//// inspired: [{ type : mongoose.Schema.Types.ObjectId, ref: 'Book' }],
//// there is Ref to this model in other model
let pathname = model.prototype.collection.name;
if (!subRoutes[pathname]) {
/// set up route as default name, /author/ID/books
subRoutes[pathname] = fExternal(model, refKey);
} else {
/// if there're few refs to same model, as Author and co-Author in book, set up additional routes
/// as /author/ID/books_as_coauthor
/// keeping the first default one
pathname += '_as_' + refKey;
subRoutes[pathname] = fExternal(model, refKey);
}
}
});
}
return subRoutes;
}
/**
* [apiPost description]
* @param Object data [description]
* @return Document [description]
*/
static async apiPost(data) {
/// this points to model (schema.statics.)
let doc = new this();
doc = assignDataToDocPostForReplace(this.schema, doc, data);
await doc.save();
return doc;
}
/**
* [apiCoU description]
* @param Object data [description]
* @return Document [description]
*/
static async apiCoU(data) {
/// this points to model (schema.statics.)
let doc = new this();
doc = assignDataToDocForUpdate(doc, data);
await doc.validate();
// in save-mode, a field set to null is deleted, in update mode, it requires to use $unset
// so we need to delete it from doc._doc and add it to $unset
const keysToDelete = Object.keys(data).filter(key => doc[key] === null);
keysToDelete.forEach(key => delete doc._doc[key]);
doc = await this.findByIdAndUpdate(
doc._id,
{
$set: doc,
$unset: Object.fromEntries(keysToDelete.map(key => [key, '']))
},
{
upsert: true,
new: true,
runValidators: true,
setDefaultsOnInsert: true
}
);
return doc;
}
/**
* [apiCoR description]
* @param Object data [description]
* @return Document [description]
*/
static async apiCoR(data) {
/// this points to model (schema.statics.)
let doc = new this();
doc = assignDataToDocPostForReplace(this.schema, doc, data);
await doc.validate();
doc = await this.findOneAndReplace({ _id: doc._id }, doc, {
upsert: true,
new: true,
runValidators: true,
setDefaultsOnInsert: true
});
return doc;
}
/**
* [apiValues description]
* @return {[type]} [description]
*/
async apiValues(request) {
// complex logic below is to be sure deeper populated object are covered by their .apiValues()
// method.
// If you don't need this, just use simple:
// return this.toObject({depopulate: true});
// instead
// But please be sure you understand it doesn't proceed populated documents with apiValues() and
// you may end showing off data you wanted to keep private.
let areThereDeepObjects = false;
let deepObjectsPromisesArray = [];
const deepObjectsPromisesResults = {};
let versionKey = true;
if (this.__api && this.__api._exposeVersionKey === false) {
versionKey = false;
}
let modelNameField = null;
if (this.__api && this.__api._exposeModelName) {
if (typeof this.__api._exposeModelName == 'string') {
modelNameField = this.__api._exposeModelName;
} else {
modelNameField = '__modelName';
}
}
// 2 steps
// 1 - run regular toObject processing and check if there're deeper documents we may need to transform
// 2 - run toObject processing updaing deeper objects with their .apiValues() results
const transform = (doc, ret) => {
if (doc === this) {
if (modelNameField) {
ret[modelNameField] = doc.constructor.modelName;
}
return ret;
} else {
areThereDeepObjects = true;
const deeperApiValues = doc.apiValues(request);
if (typeof deeperApiValues?.then === 'function') {
deepObjectsPromisesArray.push(
deeperApiValues.then(res => {
if (modelNameField) {
res[modelNameField] = doc.constructor.modelName;
}
deepObjectsPromisesResults[doc.id] = res;
})
);
} else {
if (modelNameField) {
deeperApiValues[modelNameField] =
doc.constructor.modelName;
}
deepObjectsPromisesResults[doc.id] = deeperApiValues;
}
return null; // to be covered later
}
};
const firstResult = this.toObject({
transform: transform,
versionKey: versionKey
});
if (!areThereDeepObjects) {
// return data after 1st step if there's nothing deeper
return firstResult;
}
// await for promises, if any
await Promise.all(deepObjectsPromisesArray);
const transformDeeper = (doc, ret) => {
if (doc === this) {
if (modelNameField) {
ret[modelNameField] = doc.constructor.modelName;
}
return ret;
} else {
return deepObjectsPromisesResults[doc.id];
}
};
return this.toObject({
transform: transformDeeper,
versionKey: versionKey
});
}
/**
* [apiPut description]
* @param Object data [description]
* @return Document [description]
*/
async apiPut(data) {
//// this points to document (schema.methods.)
const model = assignDataToDocForUpdate(this, data);
await model.save();
}
/**
* [apiDelete description]
* @return Boolean success
*/
async apiDelete() {
//// this points to document (schema.methods.)
await this.deleteOne();
}
}
/**
* Assigns values from data to doc based on schema paths, handling nested paths.
* @param {Schema} schema - Mongoose schema object
* @param {Object} doc - Document to assign values to
* @param {Object} data - Source data object
*/
const assignDataToDocPostForReplace = (schema, doc, data) => {
schema.eachPath(pathname => {
if (data[pathname] !== undefined) {
if (pathname.includes('.')) {
// nested document
const keys = pathname.split('.');
const lastKey = keys.pop();
const lastObj = keys.reduce(
(doc, key) => (doc[key] = doc[key] || {}),
doc
);
lastObj[lastKey] = data[pathname];
} else {
doc[pathname] = data[pathname];
}
}
});
return doc;
};
const assignDataToDocForUpdate = (model, data) => {
model.schema.eachPath(pathname => {
let newValue = undefined;
let isChanged = false;
if (data[pathname] !== undefined) {
newValue = data[pathname];
isChanged = true;
} else if (data[pathname] === null) {
newValue = undefined;
isChanged = true;
}
if (isChanged) {
if (pathname.includes('.')) {
let doc = model;
// nested document
const keys = pathname.split('.');
const lastKey = keys.pop();
const lastObj = keys.reduce(
(doc, key) => (doc[key] = doc[key] || {}),
doc
);
lastObj[lastKey] = newValue;
} else {
model[pathname] = newValue;
}
}
});
return model;
};
module.exports = DefaultModelMethods;