/
index.js
38 lines (30 loc) · 1.04 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
'use strict';
const _ = require('lodash');
module.exports = function mongooseScopes(schema, opts) {
const defaultScope = schema.options && schema.options.defaultScope;
function filterScopes(data, scopes) {
let scopesArr = [];
if (_.isNil(scopes) && defaultScope) {
scopesArr.push(defaultScope);
} else if (_.isString(scopes)) {
scopesArr.push(scopes);
} else if (_.isArray(scopes)) {
scopesArr = scopes;
} else {
// Invalid input for scopes
return null;
}
const props = [];
_.forEach(schema.tree, (value, key) => {
let display = _.some(scopesArr, sc => _.includes(value.scopes || [], sc));
if (display || (key === 'id')) { // Show if visible or if id
props.push(key);
}
});
return _.pick(data, props);
}
schema.static('getScoped', filterScopes);
schema.method('getScoped', function (scopes) {
return filterScopes(instance, this);
});
};