#2397·mikro-orm

Provide an identity-map friendly update/remove method

Author: parisholleyCreated Nov 10, 2021Updated Nov 15, 2022
Labelsenhancement

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:

typescript
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.

typescript
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