GitXplorerGitXplorer
f

mongoose-fields-filter

public
6 stars
0 forks
0 issues

Commits

List of commits on branch master.
Verified
4699b27bcedc2627303c103e633a4f3962b9c39a

Update README.md

ffelipe-augusto committed 4 years ago
Unverified
f0163302874c1f8f64c1f201fb097044c2924f2a

Release 1.0.2

committed 6 years ago
Unverified
85f6b702bdccdbe342ea45b3d2f81d88b810db8b

Add keywords

committed 6 years ago
Unverified
48afd207160e9b73f865933335edfea9904f6d43

Release 1.0.1

committed 6 years ago
Unverified
8f8338543f45a041ed26932a1abfd8fdde1824fb

Add release-it

committed 6 years ago
Unverified
cf36982a8a9cbd6fb436b1c2b0b3b2f5bd06b73c

Replace build image

committed 6 years ago

README

The README file for this repository.

Mongoose Fields Filter

Build Status Coverage Status npm version GitHub license

About

Mongoose plugin that provides private paths and custom permissions filtering

Requirements

Mongo Fields Filter is compatible with mongoose 5.

Install

$ npm i -S mongoose-fields-filter
// or
$ yarn add mongoose-fields-filter

Use

Register the plugin on the relevant mongoose schema.

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:

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:

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.