TriPSs / nestjs-query

Easy CRUD for GraphQL.
https://tripss.github.io/nestjs-query/
MIT License
152 stars 43 forks source link

How to use ConnectionName with Mongoose ? #102

Closed drmspatil closed 4 months ago

drmspatil commented 1 year ago

In documentation multiple databases section with TypeOrm is provided . However MongooseModule.forRootAsync also supports multiple connections by default . So is it not supported in nestjs-query ? or is there any solution for this ?

smolinari commented 1 year ago

https://docs.nestjs.com/techniques/mongodb#multiple-databases

Scott

drmspatil commented 1 year ago

Thanks for reply @smolinari but I tried to use it with nestjs-query but it throws error.

Generally Model in service required to pass connection name as well which throws following error

Error: Nest can't resolve dependencies of the TodoItemEntityQueryService (?). Please make sure that the argument TodoItemEntityModel at index [0] is available in the NestjsQueryMongooseModule context.
If TodoItemEntityModel is a provider, is it part of the current NestjsQueryMongooseModule?

Thus created custom service and provided Connection as well

  constructor(
    @InjectModel(TodoItemEntity.name, 'todo')
    model: Model<TodoItemEntity>,
  ) {
    super(model);
  }

But it still throws same error .

In TypeOrm example of multiple databases , entities are provided at rootAsync moduleoptions . But we can't pass entities in MongooseModuleOptions.

useFactory: (configService: ConfigService): TypeOrmModuleOptions => ({ ...configService.get('dbConnections.secret'), entities: secretEntities, }),

smolinari commented 1 year ago

It's available.

image

https://github.com/TriPSs/nestjs-query/blob/40eaa17e8570e91aae749c408289c87a60e48afa/packages/query-mongoose/src/module.ts#L8

Scott

drmspatil commented 1 year ago

Yes . Its available at forFeature . But when we use it in service we have to provide connection name

@InjectmModel(modelName, connectionName)

Which throws error of model not provided as I mentioned above .

Even after creating custom service class and providing this connection name manually also don't work .

However without nestjs-query all multiple connections working fine.

smolinari commented 1 year ago

Um, where does it say to add the connectionName in the service injection for mongoose models? I think you are confusing how the TypeORM module and repository injection works with how the Mongoose module and models work.

All you need to do is create the model with the particular connection. You inject only that model. So, each model is a direct "connection" to the database it was defined with. In other words, you do something with a particular model, it is "tied" to the database/ server connection it was defined with and will do its work with that database.

Scott

drmspatil commented 1 year ago

Nope Scott . There is no confusion.We are using mongoose connection in @InjectModel in service since 2 yrs + in production server .

We are moving to graphql and exploring nestjs-query.

I have mentioned specific error in my first question itself. Seems like no one used it in production with multiple databases . Also nestjs-query doesn't explicitly provide any documentation for same .

So for now I consider it as it's not supported.

smolinari commented 1 year ago

Ok, I see the option of the connection in the Mongoose @InjectModel. But, you are trying to inject a query service. Right?

Scott

drmspatil commented 1 year ago

In general also , if we don't pass connection name to @InjectModel it throws specific error of Model is provider and can't resolve dependencies of service.

By default the error was there .

Then I tried to inject query service in feature module as well as mentioned with custom service. Still had same error even I passed connection name to @InjectModel

TriPSs commented 4 months ago

Closing this, if still an issue you can re-open.