Skip to content
12 changes: 7 additions & 5 deletions src/query-builder/ReturningResultsEntityUpdator.ts
Original file line number Diff line number Diff line change
Expand Up @@ -205,11 +205,13 @@ export class ReturningResultsEntityUpdator {
)
}

this.queryRunner.manager.merge(
metadata.target as any,
entity,
generatedMap,
)
if (!this.expressionMap.onUpdate) {
this.queryRunner.manager.merge(
metadata.target as any,
entity,
generatedMap,
)
}

return generatedMap
})
Expand Down
13 changes: 13 additions & 0 deletions test/github-issues/12094/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class User {
@PrimaryGeneratedColumn()
id: number

@Column()
name: string

@Column({ unique: true })
email: string
}
84 changes: 84 additions & 0 deletions test/github-issues/12094/issue-12094.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
import { expect } from "chai"
import { DataSource } from "../../../src"
import {
closeTestingConnections,
reloadTestingDatabases,
setupSingleTestingConnection,
} from "../../utils/test-utils"
import type { MysqlDataSourceOptions } from "../../../src/driver/mysql/MysqlDataSourceOptions"
import { User } from "./entity/User"

describe("github issues > #12094", () => {
let dataSource: DataSource

before(async () => {
const options = setupSingleTestingConnection("mysql", {
entities: [User],
}) as MysqlDataSourceOptions

if (!options) return

dataSource = new DataSource({
...options,
replication: undefined,
})

await dataSource.initialize()
})

beforeEach(async () => {
if (!dataSource) return
await reloadTestingDatabases([dataSource])
})

after(() => closeTestingConnections([dataSource]))

it("should not mutate input entity when upsert performs insert", async () => {
const repo = dataSource.getRepository(User)

const input = {
email: "[email protected]",
name: "Test",
}

await repo.upsert(input, ["email"])

expect(input).to.deep.equal({
email: "[email protected]",
name: "Test",
})
})

it("should not mutate input entity when upsert performs update", async () => {
const repo = dataSource.getRepository(User)

await repo.insert({
email: "[email protected]",
name: "Before",
})

const input = {
email: "[email protected]",
name: "After",
}

await repo.upsert(input, ["email"])

expect(input).to.deep.equal({
email: "[email protected]",
name: "After",
})
})

it("should still mutate entity on normal insert", async () => {
const repo = dataSource.getRepository(User)

const input = {
name: "Test",
email: "[email protected]",
}

await repo.insert(input)
expect(input).to.have.property("id")
})
})