Repository navigation
fix(query-builder): prevent upsert from mutating input entities - #13029
ElwinErnst wants to merge 5 commits into
Conversation
When calling upsert() on drivers without RETURNING (e.g. mysql, sqlite), TypeORM fabricated the generated primary key from the driver insert id and merged it back into each input entity in place. On ON DUPLICATE KEY UPDATE / ON CONFLICT DO UPDATE this wrote a wrong id onto rows that were updated, silently corrupting the caller's in-memory objects. Operate on clones of the entities for upserts so the originals are left untouched. Generated values are still surfaced through insertResult.generatedMaps and insertResult.identifiers. Closes typeorm#12094
Code Review by Qodo
1.
|
Address review feedback on the upsert input-mutation fix: - after-insert listeners/subscribers were given the original (now un-mutated) value sets, so upsert callbacks no longer saw the generated id. The updator now returns the entities carrying the generated values (the clones for an upsert, the originals otherwise) and execute() broadcasts those, so listeners still receive the id while the caller's objects stay untouched. - OrmUtils.cloneObject is shallow, so embedded objects were still shared by reference and merged into in place. Clone the embedded tree as well (never following relations, so no cyclic traversal). Adds tests covering the after-insert listener and embedded cases.
|
Addressed the review in
On the two rule-violation notes: the behavior change (generated ids are read from the returned |
…of any The typeorm#12094 embedded-mutation regression declared its upsert input as `any`, bypassing compile-time checks. Use QueryDeepPartialEntity<Order>, the type upsert actually accepts, so an invalid input shape fails the build.
Document that generated values are no longer written back onto the input objects and must be read from the returned InsertResult.
… when cloning for upsert cloneEmbeddeds replaced each embedded via target[propertyName] = clone. The upsert clone keeps the entity prototype, so for an embedded exposed through a getter-only accessor the assignment threw (TypeError: Cannot set property which has only a getter) after the insert had already executed. Define an own data property on the clone instead, which shadows the inherited accessor and keeps the same result for writable embeddeds. Add a regression with a getter-only mapped embedded.
|
Thanks for the review. Pushed
On Rule 3 (functional vs per-issue test): keeping this one under |
Description of change
What:
upsert()no longer mutates the input entity objects passed by the caller.Why: On drivers without
RETURNING(e.g. mysql, sqlite),ReturningResultsEntityUpdator.insert()fabricated the generated primary key from the driver insert id and merged it back into every input entity in place. UnderON DUPLICATE KEY UPDATE/ON CONFLICT DO UPDATE, the driver still reports a non-zero insert id even when a row was updated, so updated rows had a wrong primary key written onto them — silently corrupting the caller's in-memory objects with no error or warning. See #12094.Change: When the operation is an upsert (
expressionMap.onUpdateis set), the updator now operates on clones of the entities (OrmUtils.cloneObject, which preserves the entity prototype) instead of the caller's originals. The originals are left untouched. Generated values remain available throughinsertResult.generatedMapsandinsertResult.identifiers.Behavior note: As a consequence,
upsert()no longer writes generated ids back into the passed-in objects (previously it did so — correctly for inserted rows, incorrectly for updated rows). Read generated values from the returnedInsertResultinstead. This aligns with the expectation in #12094 that input objects should not be mutated.Current vs new behavior
await repo.upsert(entity, ["email"])could overwriteentity.idwith a wrong value when the row was updated.entityis left exactly as the caller passed it; ids are exposed via the returnedInsertResult.How verified: Added
test/github-issues/12094/with three cases (no mutation on insert, no wrong-id overwrite on update, generated ids still exposed viaInsertResult). Confirmed the new tests fail onmasterand pass with this change, and that the existing upsert/returning test suites still pass. The defect reproduces beyond MySQL — it was verified against sqljs as well.Pull-Request Checklist
masterbranchCloses #12094Closes #12094