-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Felipe Santos
committed
Mar 13, 2019
0 parents
commit 78d3ed4
Showing
10 changed files
with
2,446 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
node_modules | ||
coverage | ||
.nyc_output | ||
yarn-error.log |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
language: node_js | ||
node_js: | ||
- "8" | ||
- "6" | ||
env: | ||
- MONGOOSE_VERSION=5 | ||
services: mongodb | ||
cache: yarn | ||
git: | ||
depth: 3 | ||
before_script: | ||
- sleep 15 | ||
install: | ||
- yarn | ||
- yarn add --dev mongoose@^$MONGOOSE_VERSION | ||
script: | ||
- yarn coverage | ||
jobs: | ||
include: | ||
- stage: deploy | ||
if: env(MONGOOSE_VERSION) = 5 | ||
node_js: "8" | ||
script: | ||
- yarn coverage | ||
- yarn coveralls |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
The MIT License | ||
|
||
Copyright (c) 2019 Felipe Augusto dos Santos | ||
|
||
Permission is hereby granted, free of charge, to any person obtaining a copy | ||
of this software and associated documentation files (the "Software"), to deal | ||
in the Software without restriction, including without limitation the rights | ||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell | ||
copies of the Software, and to permit persons to whom the Software is | ||
furnished to do so, subject to the following conditions: | ||
|
||
The above copyright notice and this permission notice shall be included in | ||
all copies or substantial portions of the Software. | ||
|
||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR | ||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, | ||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE | ||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER | ||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, | ||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN | ||
THE SOFTWARE. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
# Mongoose Fields Filter | ||
|
||
## About | ||
|
||
Mongoose plugin that provides private paths and custom permissions filtering | ||
|
||
## Requirements | ||
|
||
Mongo Fields Filter is compatible with mongoose 5. | ||
|
||
## Install | ||
|
||
```sh | ||
$ npm i -S mongo-fields-filter | ||
// or | ||
$ yarn add mongo-fields-filter | ||
``` | ||
|
||
## Use | ||
|
||
Register the plugin on the relevant mongoose schema. | ||
|
||
```javascript | ||
const mongoose = require('mongoose'); | ||
const mongoFields = require('mongoose-fields-filter'); | ||
|
||
const MySchema = new mongoose.Schema({}); | ||
MySchema.plugin(mongoFields); | ||
|
||
const MyModel = mongoose.model('MyModel', MySchema); | ||
``` | ||
|
||
Specify which fields are `private` and which `access` a person must has to retrieve the field: | ||
|
||
|
||
```javascript | ||
const UserSchema = new mongoose.Schema({ | ||
name: String, | ||
password: { | ||
type: String, | ||
private: true, // indicates that this field is private, it never returns | ||
}, | ||
phone: { | ||
type: String, | ||
access: ['admin', 'support'] // the `access` the person needs (or) to obtain this fields | ||
}, | ||
document: { | ||
type: String, | ||
access: ['admin'] | ||
} | ||
}) | ||
|
||
``` | ||
|
||
Scope query by passing which `access` you want the query to have, and then make the query: | ||
|
||
``` | ||
const AccessBoundModel = UserModel.byAccess(['support', 'financial']) | ||
const user = AccessBoundModel.findOne({}) | ||
// filtering works here | ||
{ | ||
name: 'Some name', | ||
phone: '(11) 1234-5678' | ||
} | ||
``` | ||
|
||
|
||
### Configuration | ||
|
||
Everything works out of the box, but you can customize as follows: | ||
|
||
```javascript | ||
const config = { | ||
/** | ||
* If you want to include virtuals | ||
*/ | ||
virtuals: true, | ||
|
||
/** | ||
* Max depth allowed when using recursive populate | ||
*/ | ||
depth: 3, | ||
|
||
/** | ||
* The field that the plugin will look in the model in order | ||
* to find which permissions are necessary | ||
*/ | ||
accessKey: 'access', | ||
|
||
/** | ||
* The name of the filter bound model getter method | ||
*/ | ||
accessorMethod: 'byAccess', | ||
|
||
/** | ||
* The name of the access getter method | ||
*/ | ||
accessIdGetter: 'getAccess' | ||
}; | ||
|
||
SomeSchema.plugin(schema, config); | ||
``` | ||
|
||
|
||
### LICENSE | ||
|
||
The files in this archive are released under MIT license. | ||
You can find a copy of this license in [LICENSE](). |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,50 @@ | ||
const _pick = require('lodash.pick') | ||
|
||
module.exports = class Filter { | ||
constructor(opts = {}) { | ||
this.virtuals = 'virtuals' in opts ? opts.virtuals : true | ||
} | ||
|
||
isObject(obj) { | ||
return Object.prototype.toString.call(obj) === '[object Object]' | ||
} | ||
|
||
// this function receives an object and the fields it should pick | ||
pick(obj, fields) { | ||
// append prefixes | ||
fields.push(...fields.map(a => a.split('.')[0])) | ||
|
||
// we reached a leaf, need to return | ||
if(fields == null || (Array.isArray(fields) && fields.length == 0)) { | ||
return obj | ||
} | ||
|
||
// if passing an array, call pick for each item | ||
if(Array.isArray(obj)) { | ||
return obj.map(a => this.pick(a, fields)) | ||
} | ||
|
||
if(this.isObject(obj)) { | ||
if(obj.toObject instanceof Function) obj = obj.toObject({ virtuals: this.virtuals }) | ||
|
||
// filter the object | ||
obj = _pick(obj, fields) | ||
|
||
// iterate on object keys | ||
Object.keys(obj).forEach(key => { | ||
const nestedFields = fields | ||
.filter(f => f.startsWith(`${key}.`)) | ||
.map(f => f.replace(new RegExp(`^${key}\\.`),'')) | ||
|
||
// if it's an array, filter each item of the array | ||
if(Array.isArray(obj[key])) { | ||
obj[key] = obj[key].map(item => this.pick(item, nestedFields)) | ||
} else { | ||
obj[key] = this.pick(obj[key], nestedFields) | ||
} | ||
}) | ||
} | ||
|
||
return obj | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
function castArray(...args) { | ||
if (!args.length) { | ||
return [] | ||
} | ||
const value = args[0] | ||
return Array.isArray(value) ? value : [value] | ||
} | ||
|
||
module.exports = { | ||
castArray | ||
} |
Oops, something went wrong.