Skip to content

[Question] Recommend Configuration & Usage EntityManager with Nest.js #69

Answered by B4nan
jays1204 asked this question in Q&A
Discussion options

You must be logged in to vote

Both ways are doing the same, and actually the first one should be preferred. The point of using RequestContext helper is to allow the 1. way. We still work with the fork internally, as all methods on the EM that are using the context somehow will first get the fork via em.getContext(), which is by default doing RequestContext.getEntityManager(). You can check the em.id, it should show you different values for each request, even if you use the instance from DI. Those instances in DI are always global, but when they are executed inside context handler (so inside the middleware handler), they will see the fork and use that instead. With v5 we even validate that you are not touching the glob…

Replies: 1 comment 1 reply

Comment options

You must be logged in to vote
1 reply
@jays1204
Comment options

Answer selected by jays1204
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
2 participants