Giter Club home page Giter Club logo

mongoose-plugin-deep-populate's Introduction

mongoose-plugin-deep-populate

DeepPopulate plugin helps you do some auto population without thinking about depth, inspired from mongoose-autopopulate.

Installation

Just run

npm install mongoose-plugin-deep-populate

or

yarn add mongoose-plugin-deep-populate

And just call it from a model created JavaScript file

const mongoose = require('mongoose');

const schema = new mongoose.Schema({
  ...
});

schema.plugin(require('./mongoose-plugin-deep-populate'));

Or install it for all schemas

const mongoose = require('mongoose');

mongoose.plugin(require('./mongoose-plugin-deep-populate'));

Usage

Let's create a School model

const SchoolSchema = new mongoose.Schema(
  {
    name: {
      type: String
    },
  },
);

const School = mongoose.model('school', SchoolSchema);

And Student model

const StudentSchema = new mongoose.Schema(
  {
    firstName: { type: String },
    lastName: { type: String },
    school: {
      type: Mongoose.SchemaTypes.ObjectId,
      ref: 'school',
    },
  },
);

const Student = mongoose.model('student', StudentSchema);

And ExamMark model

const ExamMarkSchema = new mongoose.Schema(
  {
    student: {
      type: Mongoose.SchemaTypes.ObjectId,
      ref: 'student',
    },
    mark: {
      type: Number,
    }
  },
);

const ExamMark = mongoose.model('exammark', ExamMarkSchema);

And the only think we have to do to use the deep population plugin is add the deepPopulate option field

const ExamMarkSchema = new mongoose.Schema(
  {
    ...
  },
  {
    deepPopulate: {
      student: {
        school: true,
      },
    },
  },
);

Or you want to populate only one deep level, you can also do

const ExamMarkSchema = new mongoose.Schema(
  {
    ...
  },
  {
    deepPopulate: ['student'],
  },
);

And just call

const examMark = await ExamMark.findOne({ _id: '...' });

And with doing that every time a query runs on ExamMark model, student and school of student fields automatically populated as nested. Now, deepPopulation only works on the queries on ExamMark model, since we add options to the only ExamMark model. Any query runs on Student model does not populate school field since we have not add the deepPopulation field to the student model.


To skip the deep population step, just use skipPopulation function

const examMark = await ExamMark.findOne({ _id: '...' }).skipPopulation(true);

Default value of the skipPopulation function parameters is true, so you can also do

const examMark = await ExamMark.findOne({ _id: '...' }).skipPopulation();

mongoose-plugin-deep-populate's People

Contributors

oznakn avatar

Watchers

 avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.