#Register a service file from another Service File

10 messages · Page 1 of 1 (latest)

solid ravine
#

I want to register a service file into the providers section of module from another service file after some action performed. How could i do that ? is that even possible ?

wide saddle
#

No it's not possible. Registered services need to be declared at the start of the server. The only "different" thing you could do is have a provider that is always created but has a default noop implementation and the changed implementation based on other services' return values in a factory or possibly using ModuleRef#create, but I'd go with the former if possible

What's your use case?

solid ravine
#

i was working on analytics with nest + mongo, i was extracting all the DB's, Collections and Schema Properties inside the collections.
Before Extracting schema properties i want to register the collections into providers section so that entire module can directly import the collections service instead of importing from another service file

wide saddle
#

I feel like that should be doable with a factory. The factory injects the connection, runs a few queries, gets the collections and schemas, possibly passes them to a service that you new or have a way to set these things and viola, profit

solid ravine
#

is there any other procedure to implement the same thing in a more scable way instead using factory functions

wide saddle
#

Maybe onModuleInit

#

What's wrong with using a factory?

solid ravine
#

Nothing i need to give a try

solid ravine
#

Actually the problem is there is a servcie file-1 and inside that there is function-1 which gives all collections output upon hitting the endpoint and these collections output need to be registered as providers in module

#

so if i use the factory function all the service files would be initialized during the setup and during the intialization the collections output is null and i cant ge the collections output.

The collections data need to resgistred under providers array after hitting a Endpoint