-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
Defer init of TypeORM import (async dynamic modules) #800
Comments
That's impossible. I'd suggest initializing Mongo before creating app instance. |
Is there a technical reason why this doesn't work or is it simply currently not implemented? |
@y0hy0h modules are initialized in definition order, so if you're going to create an MongoModule where you will have an async provider, that will do your job to bring up mongo in memory, then you will be able to use it wherever you wanna. |
So I could create a module that wraps I might have a closer look at how exactly async providers interact later. Maybe I missed something. Thanks for your reply, @cojack! |
@y0hy0h I have some boilerplate for nest with some additional stuff that Im using to start working on new project every time and Im trying to improve it if I feel that it needs some additional polishing. You can take a look at https://github.com/neoteric-eu/nestjs-auth Please consider to exclude mongo bootstrap from code and place it in the script that will bring your environment up, fe: docker-compose, also in the repo I've made, there is available docker-compose.yml file for development purpose to work with postgresql (I'm RDBMS lover ❤️) Take a look! Regards! |
Thanks, @cojack, for your answer! I appreciate your boilerplate. Still, I would prefer to reuse as much stuff, and write as little own code, as possible. That's why I'm pushing a bit to consider this in Nest. Maybe in my use case it's best to take care of this outside the app. But more generally, wouldn't async dependencies be useful? What I can do now, if I understood correctly, is write a module that depends on an async module, like e. g. I would totally understand if this is either technically impossible or would make the implementation too complicated. That was just idea I had and I think it makes sense in general, so I wanted to propose and discuss it. |
What is missing for this use case is basically a way to asynchronously create a dynamic module, i. e. call TypeOrmModule.forRoot() in an async function. For now, dynamic modules have to be created synchronously, and I wondered whether that is necessary. This feature will be available in the upcoming release. |
Wow, that is amazing! :) Thanks for your quick reply! |
Nest 5.1.0 is published already |
This feature seems like exactly what I'm looking for in my efforts to establish consistent e2e testing without the need to mock repositories or stand up a test database. I'm trying to override my TypeORM module provider to instead use a SQLite in-memory database instance to back up my repositories. @y0hy0h @kamilmysliwiec Do you perhaps have an example of how you've used this to override the TypeORM configuration for testing? I've tried every permutation I can think of (including those mentioned in this and related threads) but I have been unable to do so. |
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
I'm submitting a...
I tried asking this question on StackOverflow, but it got ignored and now I don't think I'll get any answers. I hope that me posting here is ok.
Expected behavior
I would like to defer the initialization of the TypeORM connection with
TypeOrmModul.forRoot()
until an in-memory mongodb is started using mongo-unit. I could imagine something like async module imports, but they do not work, and I can't figure out if I can use async providers or what to do otherwise.What I tried
imports: [Promise.resolve(TypeOrmModule.forRoot(...))]
AppModule
dynamic:Minimal reproduction of the problem with instructions
Create a module that imports a TypeOrmModule. Try to delay the initialization until after a promise resolves (in my case
mongoUnit.start()
).Environment
The text was updated successfully, but these errors were encountered: