Empresas
Empleos
  • Sobre nosotros
  • Soluciones
    • Publicación de vacantes
      Publica tu vacante y recibe candidatos calificados en 48h.
    • Evaluación de candidatos
      500+ pruebas técnicas y psicológicas, más anti-fraude.
    • Headhunting
      Búsqueda ejecutiva a la medida de principio a fin.
    • Nómina + EOR
      Dispersión de nómina y EOR en más de 15 países de LATAM.
  • Precios
  • Empleos

0

156
Vistas
Can I add a mongoose plugin to a model after it has been created?

I have a situation where I need to add a plugin to a mongoose model but change the options passed to that plugin potentially every time it is used.

See example below:

const PersonnelSchema = new Schema({
    _id: { type: Schema.ObjectId },
    GivenName: { type: String },
    FamilyName: { type: String }
});
module.exports = mongoose.model('Personnel', PersonnelSchema, 'Personnel');

What I'd like to be able to do is add the plugin at the time of using the model so that I can pass parameters to it.

I've tried adding the plugin to the schema object on the model when using it for example:

 objModel.schema.plugin(mongoosastic, {
                index: strIndexName,
                transform: (data) => {
                    data.TenantDB = strTenantDB;
                    return data;
                }
});

but this only adds the plugin methods to statics on the schema object, and does not initialize the plugin properly on the model.

Is there some way of achieving this?

over 4 years ago · Santiago Trujillo
1 Respuestas
Responde la pregunta

0

Shortly after posting I found that I can achieve this by calling compile on my model after attaching the plugin to the schema, for example:

objModel.schema.plugin(mongoosastic, objOptions);
return objModel.compile(objModel.modelName, objModel.schema, objModel.collection.name, objModel.db, mongoose);
over 4 years ago · Santiago Trujillo Denunciar
Responde la pregunta
Encuentra empleos remotos

¡Descubre la nueva forma de encontrar empleo!

Top de empleos
Top categorías de empleo
Empresas
Publicar vacante Precios Comercial
Legal
Términos y condiciones Política de privacidad
© 2026 PeakU Inc. All Rights Reserved.
Andres GPT
Recomiéndame algunas ofertas
Necesito ayuda