Search code examples
node.jsangularmongoosemean-stackmongoose-populate

Mongoose | Keep the same Population to all functions


I Have this schema for my Crop Model

var CropSchema = new mongoose.Schema({
    name: String,
    zones: [{
        type: Schema.Types.ObjectId,
        ref: 'zone'
    }],
    ...
});

This schema for my Zone Model

var ZoneSchema = new mongoose.Schema({
    name: String,
    poor: [{
        type: Schema.Types.ObjectId,
        ref: 'condition'
    }],
    ...
});

This schema for my Condition Model

var ConditionSchema = new mongoose.Schema({
    name: String,
    action_on_controls: [{
        type: Schema.Types.ObjectId,
        ref: 'control'
    }],
    ...
});

And this schema for my Control Model

var ControlSchema = new mongoose.Schema({
    name: String,
    ...
});

The way I am getting all the Crops in Node is this:

  public index(req: Request, res: Response) {
    return Crop.find().populate('zones').populate({
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    }).exec()
      .then(respondWithResult(res, 200))
      .catch(handleError(res, 500));
  }

The way I am getting an individual Crop is this:

  public show(req: Request, res: Response) {
    return Crop.findById(req.params.id).populate({
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    }).exec()
      .then(handleEntityNotFound(res))
      .then(respondWithResult(res, 200))
      .catch(handleError(res, 500));
  }

As you can see, the part:

.populate({..})

is repeated twice.

How can I keep the same populate configurations so I don't have to write/update the same thing all the time?


Solution

  • You can save the populate object as a variable and share it:

    const zonePopulateObj = {
      path: 'zones',
      populate: [
        {
          path: 'poor', populate: [
            { path: 'action_on_controls' }]
        }
      ]
    };
    

    And then in your queries

    return Crop.find().populate(zonePopulateObj).exec();
    
    return Crop.findById(req.params.id).populate(zonePopulateObj).exec();
    

    Or you could pull the query logic into a new function and share that

    public index(req: Request, res: Response) {
        return findCrop()
          .then(respondWithResult(res, 200))
          .catch(handleError(res, 500));
      }
    
    
    public show(req: Request, res: Response) {
        return findCrop(req.params.id)
          .then((array)=>array.length ? array[0] : {})
          .then(handleEntityNotFound(res)) // may need to update this function not sure how it checks for not found.
          .then(respondWithResult(res, 200))
          .catch(handleError(res, 500));
      }
    
    
    
      const findCrop = (id)=>{
          let queryObj = {};
          if(id){
              queryObj._id=id
          }
          return Crop.find(queryObj).populate({
            path: 'zones',
            populate: [
              {
                path: 'poor', populate: [
                  { path: 'action_on_controls' }]
              }
            ]
          }).exec()
      }
    

    Personally I prefer the first method.