-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathindex.js
42 lines (33 loc) · 1.06 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
'use strict';
const _ = require('lodash');
const moment = require('moment');
function findPaths(schema) {
return Object.keys(schema.paths).filter((path) => {
return schema.paths[path].instance === 'Date';
});
}
module.exports = 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) {
// find date field
const paths = findPaths(schema);
// format
_.each(paths, (path, index) => {
const date = _.get(ret, path);
_.set(ret, path, moment(date).format('YYYY-MM-DD HH:mm:ss'));
});
//Call custom transform if present
if (transform) {
return transform(doc, ret);
}
}
});
};