JSPM

mongoose-omit-common-fields

0.0.4
  • ESM via JSPM
  • ES Module Entrypoint
  • Export Map
  • Keywords
  • License
  • Repository URL
  • TypeScript Types
  • README
  • Created
  • Published
  • Downloads 54
  • Score
    100M100P100Q76529F
  • License MIT

Array of common fields to emit for Mongoose toObject/toJSON (helpful for security)

Package Exports

  • mongoose-omit-common-fields

This package does not declare an exports field, so the exports above have been automatically detected and optimized by JSPM instead. If any package subpath is missing, it is recommended to post an issue to the original package (mongoose-omit-common-fields) to support the "exports" field. If that is not possible, create a JSPM override to customize the exports field for this package.

Readme

mongoose-omit-common-fields

build status code coverage code style styled with prettier made with lass license

Array of common fields to emit for Mongoose toObject/toJSON (helpful for security)

Table of Contents

Install

npm:

npm install mongoose-omit-common-fields

yarn:

yarn add mongoose-omit-common-fields

Usage

const mongoose = require('mongoose');
const jsonSelect = require('mongoose-json-select');
const omitCommonFields = require('mongoose-omit-common-fields');

const Schema = new mongoose.Schema();

Schema.plugin(
  jsonSelect,
  // if your database fields are camelCased then
  // you will need to use `omitCommonFields.camelCased` variation
  // (e.g. `omitCommonFields.camelCased.str`)
  omitCommonFields.underscored.str
);

String notation

If you want to add additional keys to be omitted using String notation:

Schema.plugin(
  jsonSelect,
  [
    ...omitCommonFields.underscored.keys.map(key => `-${key}`),
    '-some_other_field',
    '-another_field_to_ignore'
  ].join(' ')
);

For older versions you can use Array.concat:

Schema.plugin(
  jsonSelect,
  omitCommonFields.underscored.keys.concat([
    'some_other_field',
    'another_field_to_ignore'
  ]).map(key => `-${key}`).join(' ')
);

Object notation

If you want to add additional keys to be omitted using Object notation:

Schema.plugin(
  jsonSelect,
  Object.assign({}, omitCommonFields.underscored.obj, {
    some_other_fields: 0,
    another_field_to_ignore: 0
  })
);

Reference

For more information on - and object notation, please see http://mongoosejs.com/docs/api.html#query_Query-select.

API

omitCommonFields.underscored.keys

[ '_id',
  '__v',
  'ip',
  'last_ips',
  'email',
  'api_token',
  'group',
  'attempts',
  'last',
  'hash',
  'password',
  'salt',
  'reset_token_expires_at',
  'reset_token',
  'google_profile_id',
  'google_access_token',
  'google_refresh_token' ]

omitCommonFields.underscored.str

"-_id -__v -ip -last_ips -email -api_token -group -attempts -last -hash -password -salt -reset_token_expires_at -reset_token -google_profile_id -google_access_token -google_refresh_token"

omitCommonFields.underscored.obj

{ _id: 0,
  __v: 0,
  ip: 0,
  last_ips: 0,
  email: 0,
  api_token: 0,
  group: 0,
  attempts: 0,
  last: 0,
  hash: 0,
  password: 0,
  salt: 0,
  reset_token_expires_at: 0,
  reset_token: 0,
  google_profile_id: 0,
  google_access_token: 0,
  google_refresh_token: 0 }

omitCommonFields.camelCased.keys

[ 'id',
  'v',
  'ip',
  'lastIps',
  'email',
  'apiToken',
  'group',
  'attempts',
  'last',
  'hash',
  'password',
  'salt',
  'resetTokenExpiresAt',
  'resetToken',
  'googleProfileId',
  'googleAccessToken',
  'googleRefreshToken' ]

omitCommonFields.camelCased.str

"-id -v -ip -lastIps -email -apiToken -group -attempts -last -hash -password -salt -resetTokenExpiresAt -resetToken -googleProfileId -googleAccessToken -googleRefreshToken"

omitCommonFields.camelCased.obj

{ id: 0,
  v: 0,
  ip: 0,
  lastIps: 0,
  email: 0,
  apiToken: 0,
  group: 0,
  attempts: 0,
  last: 0,
  hash: 0,
  password: 0,
  salt: 0,
  resetTokenExpiresAt: 0,
  resetToken: 0,
  googleProfileId: 0,
  googleAccessToken: 0,
  googleRefreshToken: 0 }

Tips

Note that this package is already built-in to mongoose-common-plugin.

Contributors

Name Website
Nick Baugh http://niftylettuce.com/

License

MIT © Nick Baugh