question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

EntityAssigner not using proper EntityManager

See original GitHub issue

I am using ApolloServer and passing a forked em into the ctx: Context. I am watching sql queries with debug mode on and notice that for a very simple case when fetching relationships there are fewer queries than expected, meaning that somehow my forked em is being polluted with global context from previous requests.

I am still familiarizing myself with the code, but I believe the error might stem from EntityAssigner L15

  static assign<T extends AnyEntity<T>>(entity: T, data: EntityData<T>, onlyProperties: AssignOptions | boolean = false): T {
    const em = wrap(entity).__em;
    ...

which is called by EntityManager L222

    // add to IM immediately - needed for self-references that can be part of `data` (and do not trigger cascade merge)
    this.getUnitOfWork().merge(entity, [entity]);
    EntityAssigner.assign(entity, data as EntityData<T>, true);
    this.getUnitOfWork().merge(entity); // add to IM again so we have correct payload saved to change set computation

EntityAssigner.assign is accessing a different instance of em that has more objects defined in its unitOfWork.identityMap than the forked instance of em that we had been using in EntityManager.merge previously in the call stack.

Here is the relevant portion of my index.ts just to show you that I am following all of your suggestions and still having this issue:

  app.use((req, res, next) => {
    RequestContext.create(orm.em, next);
  });

  const apolloServer = new ApolloServer({
    schema: await buildSchema({
      resolvers: [UserResolver]
    }),
    context: ({ req, res }) => {
      const em = orm.em.fork()
      em.clear()
      // confirming that each request starts with a clean map
      console.log(em.getUnitOfWork().getIdentityMap())
    
      return {
        req,
        res,
        em,
      }
    }
  });

Issue Analytics

  • State:closed
  • Created 4 years ago
  • Comments:11 (6 by maintainers)

github_iconTop GitHub Comments

1reaction
B4nancommented, Nov 27, 2019

Ok so the problem is that the em.merge() is actually not correcting the internal __em value as it should be (not for the user entity because it was not looked up by primary key).

Will try to publish new version soon.

1reaction
B4nancommented, Nov 27, 2019

Thanks for the repro, that will help a lot! Will try to look into this soon.

Read more comments on GitHub >

github_iconTop Results From Across the Web

entityManager.assign does not wrap Entity when ... - GitHub
Describe the bug mikro-orm/packages/core/src/entity/EntityAssigner.ts Line 81 in 757801e return EntityAssigner.assignReference (entity ...
Read more >
MikroORM 5: Stricter, Safer, Smarter
MikroORM v5 now has a new package for seeding your database with initial or testing data. It allows creating entities via the same...
Read more >
mikro-orm | Yarn - Package Manager
Supports MongoDB, MySQL, PostgreSQL and SQLite databases as well as usage with vanilla JavaScript. orm, mongo, mongodb, mysql ...
Read more >
https://raw.githubusercontent.com/mikro-orm/mikro-...
... (2022-08-01) ### Bug Fixes * **core:** do not trigger auto flush from inside flush hooks ... **core:** validate decorator parameters are used...
Read more >
@mikro-orm/migrations: Versions | Openbase
... core: ensure correct context usage in all EntityManager public methods (cc6d59b), closes #3271; core: ensure FK as PK is not marked as...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found