项目作者: DouglasGabr

项目描述 :
Attach defaults to the results of mongoose queries when using .lean()
高级语言: TypeScript
项目地址: git://github.com/DouglasGabr/mongoose-lean-defaults.git
创建时间: 2019-07-09T00:18:18Z
项目社区:https://github.com/DouglasGabr/mongoose-lean-defaults

开源协议:ISC License

下载


mongoose-lean-defaults

Attach defaults to the results of mongoose queries when using .lean().
Highly inspired by mongoose-lean-virtuals.

Run Tests
Node.js Package

Install

  1. npm install --save mongoose-lean-defaults

or

  1. yarn add mongoose-lean-defaults

Usage

  1. import mongooseLeanDefaults from 'mongoose-lean-defaults';
  2. // const mongooseLeanDefaults = require('mongoose-lean-defaults').default;
  3. const userSchema = new mongoose.Schema({
  4. name: {
  5. type: String,
  6. default: 'Bob',
  7. },
  8. });
  9. // documents will only have `name` field on database
  10. // Later
  11. const updatedUserSchema = new mongoose.Schema({
  12. name: {
  13. type: String,
  14. default: 'Bob',
  15. },
  16. country: {
  17. type: String,
  18. default: 'USA',
  19. },
  20. });
  21. // `.find().lean()` will return documents without `country` field
  22. updatedUserSchema.plugin(mongooseLeanDefaults);
  23. // You must pass `defaults: true` to `.lean()`
  24. const bob = await UserModel.findOne().lean({ defaults: true });
  25. /**
  26. * bob = {
  27. * _id: ...,
  28. * name: 'Bob',
  29. * country: 'USA'
  30. * }
  31. */