Git Product home page Git Product logo

Comments (4)

vkarpov15 avatar vkarpov15 commented on May 24, 2024

For setting translateAliases application-wide, you should be able to just do mongoose.set('translateAliases', true). The following script shows that's enough to turn on translateAliases for all queries:

'use strict';

const mongoose = require('mongoose');

mongoose.set('translateAliases', true);

run().catch(error => {
  console.error(error);
  process.exit(-1);
});

async function run() {
  await mongoose.connect('mongodb://127.0.0.1:27017/mongoose_test');

  const schema = new mongoose.Schema({
    name: { type: String, alias: 'fullName' }
  });
  const Test = mongoose.model('Test', schema);

  await Test.deleteMany({});
  await Test.create({ name: 'foo' });
  console.log(await Test.findOne({ fullName: 'foo' })); // Finds the doc
  process.exit(0);
}

For setting defaults on lean queries, try the mongoose-lean-defaults plugin, that's our officially supported plugin for applying defaults on lean documents. Another approach you can try is Model.applyDefaults() in a post hook, Model.applyDefaults() is a one-liner for applying a model's defaults to a given POJO.

from mongoose.

babalugats76 avatar babalugats76 commented on May 24, 2024

Thank you so much for the response. I will use mongoose.set('translateAliases', true); as suggested to apply that globally.

However, I still have some questions about how to apply the appropriate lean options to each and every lean query. I am already using the defaults plugin to set default values for properties when a lean query is involved. I think I am asking something different.

In this case, I am asking if there is a good way to set options for each and every lean query; for example:

MY OPTIONS

const LEAN_OPTS = Object.freeze({
  autopopulate: true,
  defaults: true,
  getters: true,
  virtuals: true,
  versionKey: false,
});

IN MY pre hook

    this._mongooseOptions = {
      ...this.mongooseOptions(),
      ...(this.mongooseOptions()?.lean && {
        lean: {
          ...LEAN_OPTS,
          ...this.mongooseOptions().lean,
        },
      }),
    };

Hope that makes more sense. I probably didn't describe it well. My apologies.

from mongoose.

vkarpov15 avatar vkarpov15 commented on May 24, 2024

I'd recommend doing something more like the following:

if (this.mongooseOptions()?.lean) {
  this.setOptions({
    lean: { ...LEAN_OPTS, this.mongooseOptions().lean }
  });
}

But otherwise you're right, currently the only good way to set default lean options is via plugin, and your approach works

from mongoose.

babalugats76 avatar babalugats76 commented on May 24, 2024

Thank you so much. I will give that a shot and see how that goes. Hopefully, that will cascade to the populate queries too. 👍

from mongoose.

Related Issues (20)

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.