https://github.com/mongoosejs/mongoose-throw-unselected
Throws an error whenever you access a field on a mongoose document that is projected out
https://github.com/mongoosejs/mongoose-throw-unselected
Last synced: about 1 month ago
JSON representation
Throws an error whenever you access a field on a mongoose document that is projected out
- Host: GitHub
- URL: https://github.com/mongoosejs/mongoose-throw-unselected
- Owner: mongoosejs
- License: apache-2.0
- Created: 2017-10-24T21:12:35.000Z (over 7 years ago)
- Default Branch: master
- Last Pushed: 2018-12-18T23:32:15.000Z (over 6 years ago)
- Last Synced: 2024-10-16T03:31:29.165Z (8 months ago)
- Language: JavaScript
- Homepage:
- Size: 18.6 KB
- Stars: 0
- Watchers: 2
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# mongoose-throw-unselected
Throws an error whenever you access a field on a mongoose document that is not selected
## Usage
```javascript
const mongooseThrowUnselected = require('mongoose-throw-unselected');
```# examples
## It throws an error if accessing a field that is not included in the projection
If you explicitly exclude a field (like via `.select()` on a query) and you access that
field, this plugin will throw an error. This plugin adds a custom setter on each field that checks
if the [field is included by checking `isSelected()`](http://mongoosejs.com/docs/api.html#document_Document-isSelected).```javascript
const schema = new mongoose.Schema({
name: {
first: String,
last: String,
},
age: Number
});schema.plugin(mongooseThrowUnselected);
const Model = mongoose.model('Person', schema);
return Model.create({ name: { first: 'Valeri', last: 'Karpov' }, age: 28 }).
// Explicitly exclude 'name.first'
then(() => Model.findOne().select({ 'name.first': false })).
then(doc => {
assert.throws(() => doc.name.first);
assert.doesNotThrow(() => doc.name);
assert.equal(doc.name.last, 'Karpov');
assert.equal(doc.age, 28);
});
```