Skip to content

Commit e1b51d8

Browse files
committed
do not modify original entity if extra upsert is needed
1 parent a6753ad commit e1b51d8

File tree

1 file changed

+12
-10
lines changed

1 file changed

+12
-10
lines changed

typeorm/typeorm-store/src/utils/stateManager.ts

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -198,9 +198,8 @@ export class StateManager {
198198
case ChangeType.Insert: {
199199
assert(cached?.value != null, `unable to insert entity ${metadata.name} ${id}`)
200200

201-
inserts.push(cached.value)
202-
203-
const extraUpsert = this.extractExtraUpsert(metadata, cached.value)
201+
const {entity, extraUpsert} = this.extractExtraUpsert(metadata, cached.value)
202+
inserts.push(entity)
204203
if (extraUpsert != null) {
205204
extraUpserts.push(extraUpsert)
206205
}
@@ -210,9 +209,9 @@ export class StateManager {
210209
case ChangeType.Upsert: {
211210
assert(cached?.value != null, `unable to upsert entity ${metadata.name} ${id}`)
212211

213-
upserts.push(cached.value)
214-
215-
const extraUpsert = this.extractExtraUpsert(metadata, cached.value)
212+
213+
const {entity, extraUpsert} = this.extractExtraUpsert(metadata, cached.value)
214+
upserts.push(entity)
216215
if (extraUpsert != null) {
217216
extraUpserts.push(extraUpsert)
218217
}
@@ -268,15 +267,18 @@ export class StateManager {
268267
if (!isInverseInserted) continue
269268

270269
if (extraUpsert == null) {
271-
extraUpsert = metadata.create() as E
272-
extraUpsert.id = entity.id
273-
Object.assign(extraUpsert, entity)
270+
extraUpsert = entity
271+
entity = metadata.create() as E
272+
Object.assign(entity, extraUpsert)
274273
}
275274

276275
relation.setEntityValue(entity, undefined)
277276
}
278277

279-
return extraUpsert
278+
return {
279+
entity,
280+
extraUpsert
281+
}
280282
}
281283

282284
private setState(metadata: EntityMetadata, id: string, type: ChangeType): this {

0 commit comments

Comments
 (0)