Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Mongoose - ability to use mongoose plugins in module imports declaration #22

Closed
PsyGik opened this issue Oct 11, 2018 · 4 comments
Closed

Comments

@PsyGik
Copy link

PsyGik commented Oct 11, 2018

I'm submitting a...


[ ] Regression 
[ ] Bug report
[ x ] Feature request
[ x ] Documentation issue or request
[ ] Support request => Please do not submit support request here, instead post your question on Stack Overflow.

Current behavior

We want to usemongoosastic in our project to store certain collections in ElasticSearch.

Our code structure is a bit different than the traditional examples given in Nest docs.

Our models are created independent of the service code. We create models and push to them to a private npm repository so that it can be reused across various other modules.

Here is a sample model example:

import * as mongoose from 'mongoose';
import { AddressInfoSchema } from '@private-package/address-model';

export const StopSchema = new mongoose.Schema({
    _id: String,
    stopName: String,
    addressInfo: AddressInfoSchema,
    stopType: { type: String, enum: ['PARKING', 'WAYPOINT', 'STOP'] }
}, {
        versionKey: false,
        timestamps: { createdAt: 'createTime', updatedAt: 'updateTime' }
    });

And we are importing it into our services like so:

// imports
@Module({
  imports: [
    MongooseModule.forFeature([
      { name: 'Stop', schema: StopSchema },
    ]),
  ],

// rest of the code

As per the mongoosastic docs, To have a model indexed into Elasticsearch simply add the plugin.

So We are adding the plugin like so,

// imports
@Module({
  imports: [
    MongooseModule.forFeature([
      { name: 'Stop', schema: StopSchema.plugin(mongoosastic, { hosts: ['localhost:9200','anotherhost:9200']}) },
    ]),

// rest of the code

When injecting this model in the controller, We are not able to access the mongoosastic functions. Also when using save() methods, it is not indexing to ElasticSearch.

Expected behavior

We should be able to use the plugins in @Module declarations.

Minimal reproduction of the problem with instructions

Mentioned in Current behavior section

What is the motivation / use case for changing the behavior?

To be able to use plugins for mongoose models, independent of whether the models are included in NestJS projects.

Environment


Nest version: X.Y.Z
````
    "@nestjs/common": "^5.0.0-beta.6",
    "@nestjs/core": "^5.0.0-beta.6",
    "@nestjs/elasticsearch": "^0.1.2",
    "@nestjs/microservices": "^5.0.0-beta.6",
    "@nestjs/mongoose": "^5.0.0",
````

 
For Tooling issues:
- Node version: XX  
````
> node -v
v10.10.0
````
- Platform:   All

Others:

@shekohex
Copy link

Hi 😃
Why not just use it at the declaration of the scheme

export const StopSchema = new Schema({...});

StopSchema.plugin(plg, opts);

I'm using mongoose-timestamp Plugin like so , and it works 😃

@PsyGik
Copy link
Author

PsyGik commented Oct 12, 2018

@shekohex because our models cannot contain any configs or implementations. Elastic Search is something which is required in just a couple of service and the model is being used in various other places.

@kamilmysliwiec
Copy link
Member

Actually, all schemas are being used to create models in the same way as in the pure Node+Mongoose app (see useFactory):

provide: getModelToken(model.name),
useFactory: connection => connection.model(model.name, model.schema),

Nest doesn't perform any heavy lifting underneath.

@cobrimusic
Copy link

@PsyGik ¿Did you can solved it?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

4 participants