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.

Working with Entities (In Subscribers?)

See original GitHub issue

Hello,

I’d like to be able to work with an Entity that is sent to my server, whether the full object is sent, or only part of it.

i.e. Say we have two tables, Meal, and MealHistory. If someone ordered a meal, it is in the Meal table, when it is complete it gets moved to the MealHistory table.

The full meal object is { id: int, name: string, quantity: int, complete: boolean } Which belongs to the Meal table, the MealHistory table has the exact same structure.

When a meal is completed, it is moved to the MealHistory table. I want to be able to move the Meal object by only passing the PK, and complete field like: { id: 1, complete: true }

However, to do this, we need to pass the whole object to add the complete record into MealHistory, additionally, I am not able to remove the original record from the Meal table.

My questions are

  1. How can I get the whole object at this point?
  2. How can I remove the original object after it has been saved in history?

Right now I am trying to get around this with subscribers. I am on alpha 31.

Thanks

Issue Analytics

  • State:closed
  • Created 6 years ago
  • Comments:7 (3 by maintainers)

github_iconTop GitHub Comments

4reactions
pleerockcommented, Jul 22, 2017

event.manager is EntityManager which has all method to access any table, or even get a repository for entity you need, e.g. event.manager.getRepository(Post)

4reactions
pleerockcommented, Jul 21, 2017

again you forgot to await remove in your code. Please don’t forget to await your promises or you’ll end up creating lot of issues in this repo, I don’t have a time to answer them each time.

But your problem here is different. If you want to perform database operations use event.manager and only it to do that. Don’t use getEntityManager() or getRepository() or any other global function.

This is due to a transaction. Save is running in a transaction and your data is saved in a transaction which is not committed yet. But global functions you use are running out of transaction.

Read more comments on GitHub >

github_iconTop Results From Across the Web

Entity Listeners and Subscribers - typeorm - GitBook
Any of your entities can have methods with custom logic that listen to specific entity events. You must mark those methods with special...
Read more >
Defining database subscribers | Vendure docs
With a subscriber, we can listen to specific entity events and take actions based on inserts, updates, deletions and more. If you need...
Read more >
TypeORM | Entity Listeners and Subscribers - YouTube
TypeORM: Entity Listeners and Subscribers.➥ Repo: https://github.com/pragmatic-reviews/typeormexample/tree/listenersubscriber➥ Buy Me a ...
Read more >
How can an event subscriber have access to the entity type?
The event should have the entities its acting on. Have you tried the following? $entities = $event->getEntities();. To get the bundle:
Read more >
Using Entitiy Repository in TypeORM Subscriber
The reason for this is I would like to automatically generate a 'Promo' entity and assign it to the 'User' Entity on creation...
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