-
Notifications
You must be signed in to change notification settings - Fork 2
/
index.js
138 lines (119 loc) · 3.73 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
const mongooseErrorMessages = require('@ladjs/mongoose-error-messages');
const mongooseHidden = require('mongoose-hidden')();
const mongooseOmitCommonFields = require('mongoose-omit-common-fields');
const uniqueValidator = require('@ladjs/mongoose-unique-validator');
const validationErrorTransform = require('mongoose-validation-error-transform');
const { boolean } = require('boolean');
const mongooseCommonPlugin = (schema, options = {}) => {
options = {
object: '',
camelCase: false,
locale: true,
omitCommonFields: true,
defaultLocale: 'en',
omitExtraFields: [],
// <https://github.com/blakehaswell/mongoose-unique-validator>
uniqueValidator: {
message: mongooseErrorMessages.general.unique
},
// <https://github.com/niftylettuce/mongoose-validation-error-transform>
validationErrorTransform: {},
// <https://github.com/mblarsen/mongoose-hidden>
mongooseHidden: {},
uniqueId: true,
...options
};
if (typeof options.object !== 'string' || options.object.trim() === '')
throw new Error(
"You must define an `object` name (e.g. `{ object: 'user' }`)"
);
schema.add({
id: {
type: String,
index: true,
unique: boolean(options.uniqueId)
},
object: {
type: String,
trim: true
}
});
if (options.locale)
schema
.virtual('locale')
.get(function () {
return this.__locale || options.defaultLocale;
})
.set(function (locale) {
this.__locale = locale;
});
schema.pre('save', function (next) {
this.id = this._id.toString();
this.object = options.object;
next();
});
const hidden = {};
let select = '';
const field =
mongooseOmitCommonFields[options.camelCase ? 'camelCased' : 'underscored'];
if (Array.isArray(options.omitExtraFields)) {
select = [];
if (options.omitCommonFields) select = [...select, ...field.keys];
select = [...select, ...options.omitExtraFields]
.map((key) => {
key = key.trim();
if (key.startsWith('-')) {
hidden[key.slice(1)] = true;
return key;
}
hidden[key] = true;
return `-${key}`;
})
.join(' ');
} else if (typeof options.omitExtraFields === 'object') {
select = options.omitExtraFields;
for (const prop in options.omitExtraFields) {
if (Object.prototype.hasOwnProperty.call(options.omitExtraFields, prop))
hidden[prop] = !boolean(options.omitExtraFields[prop]);
}
if (options.omitCommonFields) {
Object.assign(select, field.obj);
for (const prop in field.obj) {
if (Object.prototype.hasOwnProperty.call(field.obj, prop))
hidden[prop] = !boolean(field.obj[prop]);
}
}
} else if (options.omitCommonFields) {
select = field.str;
for (const prop in field.obj) {
if (Object.prototype.hasOwnProperty.call(field.obj, prop))
hidden[prop] = !boolean(field.obj[prop]);
}
}
//
// TODO: we should probably set a few of the props to `select: false`
// by default (as similar to `passport-local-mongoose` on `hash` and `password`)
//
schema.set('toJSON', {
getters: true,
virtuals: true,
...schema.options.toJSON
});
schema.set('toObject', {
getters: true,
virtuals: true,
...schema.options.toObject
});
schema.set('timestamps', {
createdAt: options.camelCase ? 'createdAt' : 'created_at',
updatedAt: options.camelCase ? 'updatedAt' : 'updated_at'
});
schema.plugin(uniqueValidator, options.uniqueValidator);
schema.plugin(mongooseHidden, {
hidden,
...options.mongooseHidden
});
schema.plugin(validationErrorTransform, options.validationErrorTransform);
return schema;
};
module.exports = mongooseCommonPlugin;