Skip to content
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

Provide an identity-map friendly update/remove method #2397

Open
parisholley opened this issue Nov 10, 2021 · 3 comments
Open

Provide an identity-map friendly update/remove method #2397

parisholley opened this issue Nov 10, 2021 · 3 comments
Labels
enhancement New feature or request

Comments

@parisholley
Copy link
Contributor

Is your feature request related to a problem? Please describe.

Sometimes you have the id of an entity available and want to remove the relationships associated with them, or otherwise update in batch. There are times where some of the items which would match that query, are already loaded in the identify map. Today, the only way to blend the two, would be to run a query to get all of the entities, then update it your self, example:

const newCard = new PaymentMethod();

// some logic to populate and persist entity

const customer = await em.findOne(Customer, customerId, {populate: {paymentMethods: true}});

for(const method of customer.paymentMethods){
	method.primary = false;
}

newCard.primary = true;

Describe the solution you'd like

Due to some of the advanced capabilities of the current QB implementation, it isn't practical to use that interface (eg: you can have a where clause on expr(), so we can provide a simpler API, which only supports an EntityData like object graph.

const customer = await em.update(PaymentMethod, {customer}, {primary: false});

The above would effectively do two things:

  • Compare the simplified where clause with entities in-memory, and apply the update (em.assign())
  • Queue an update query to be executed at flush
@parisholley parisholley added the enhancement New feature or request label Nov 10, 2021
@B4nan
Copy link
Member

B4nan commented Nov 10, 2021

Interesting idea, we might wanna update the entity snapshots (originalEntityData) when assigning too, so we do not issue two update queries if there is a match.

I was actually thinking about having em.insert() too, that would basically partially flush the entities from parameter (cascading their relations, but not fully sure if it should handle more than inserts).

@parisholley
Copy link
Contributor Author

@B4nan is this still a viable idea with v5?

@B4nan
Copy link
Member

B4nan commented Nov 15, 2022

Yes, more like v6 I guess, I already have too many things in the pipeline for 5.x and for v6 I'd like to revisit the EM API a bit, introducing em.insert/update/delete that work similarly to the new em.upsert - works with entities as well as POJOs, at the same time respects the identity map (if things are already in context, it delegates writing to the flush call). Those would probably replace the native* methods.

Speaking of em.upsert(), your suggestion is quite aligned with what that method already does.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

2 participants