mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
fix: correctly keep query.data from ormOption for commit / rollback subscribers (#10151)
This commit is contained in:
parent
e67d704138
commit
73ee70b331
@ -53,10 +53,10 @@ export class EntityPersistExecutor {
|
||||
|
||||
// save data in the query runner - this is useful functionality to share data from outside of the world
|
||||
// with third classes - like subscribers and listener methods
|
||||
let oldQueryRunnerData = queryRunner.data
|
||||
if (this.options && this.options.data) {
|
||||
queryRunner.data = this.options.data
|
||||
}
|
||||
let oldQueryRunnerData = queryRunner.data
|
||||
|
||||
try {
|
||||
// collect all operate subjects
|
||||
|
||||
@ -7,12 +7,14 @@ import {
|
||||
closeTestingConnections,
|
||||
createTestingConnections,
|
||||
} from "../../../utils/test-utils"
|
||||
import { Example } from "../query-data/entity/Example"
|
||||
import sinon from "sinon"
|
||||
import { expect } from "chai"
|
||||
|
||||
describe("entity subscriber > transaction flow", () => {
|
||||
let beforeTransactionStart = sinon.spy()
|
||||
let afterTransactionStart = sinon.spy()
|
||||
let afterInsert = sinon.spy()
|
||||
let beforeTransactionCommit = sinon.spy()
|
||||
let afterTransactionCommit = sinon.spy()
|
||||
let beforeTransactionRollback = sinon.spy()
|
||||
@ -21,27 +23,31 @@ describe("entity subscriber > transaction flow", () => {
|
||||
@EventSubscriber()
|
||||
class PostSubscriber implements EntitySubscriberInterface {
|
||||
beforeTransactionStart() {
|
||||
if (beforeTransactionStart) beforeTransactionStart()
|
||||
if (beforeTransactionStart) beforeTransactionStart(arguments)
|
||||
}
|
||||
|
||||
afterTransactionStart() {
|
||||
if (afterTransactionStart) afterTransactionStart()
|
||||
if (afterTransactionStart) afterTransactionStart(arguments)
|
||||
}
|
||||
|
||||
afterInsert() {
|
||||
if (afterInsert) afterInsert(arguments)
|
||||
}
|
||||
|
||||
beforeTransactionCommit() {
|
||||
if (beforeTransactionCommit) beforeTransactionCommit()
|
||||
if (beforeTransactionCommit) beforeTransactionCommit(arguments)
|
||||
}
|
||||
|
||||
afterTransactionCommit() {
|
||||
if (afterTransactionCommit) afterTransactionCommit()
|
||||
if (afterTransactionCommit) afterTransactionCommit(arguments)
|
||||
}
|
||||
|
||||
beforeTransactionRollback() {
|
||||
if (beforeTransactionRollback) beforeTransactionRollback()
|
||||
if (beforeTransactionRollback) beforeTransactionRollback(arguments)
|
||||
}
|
||||
|
||||
afterTransactionRollback() {
|
||||
if (afterTransactionRollback) afterTransactionRollback()
|
||||
if (afterTransactionRollback) afterTransactionRollback(arguments)
|
||||
}
|
||||
}
|
||||
|
||||
@ -49,6 +55,7 @@ describe("entity subscriber > transaction flow", () => {
|
||||
before(
|
||||
async () =>
|
||||
(connections = await createTestingConnections({
|
||||
entities: [Example],
|
||||
subscribers: [PostSubscriber],
|
||||
dropSchema: true,
|
||||
schemaCreate: true,
|
||||
@ -62,7 +69,7 @@ describe("entity subscriber > transaction flow", () => {
|
||||
connection.driver.options.type === "mssql" ||
|
||||
connection.driver.options.type === "spanner"
|
||||
)
|
||||
return
|
||||
continue
|
||||
|
||||
beforeTransactionStart.resetHistory()
|
||||
afterTransactionStart.resetHistory()
|
||||
@ -148,7 +155,7 @@ describe("entity subscriber > transaction flow", () => {
|
||||
connection.driver.options.type === "mssql" ||
|
||||
connection.driver.options.type === "spanner"
|
||||
)
|
||||
return
|
||||
continue
|
||||
|
||||
beforeTransactionCommit.resetHistory()
|
||||
afterTransactionCommit.resetHistory()
|
||||
@ -216,7 +223,7 @@ describe("entity subscriber > transaction flow", () => {
|
||||
connection.driver.options.type === "mssql" ||
|
||||
connection.driver.options.type === "spanner"
|
||||
)
|
||||
return
|
||||
continue
|
||||
|
||||
beforeTransactionRollback.resetHistory()
|
||||
afterTransactionRollback.resetHistory()
|
||||
@ -280,4 +287,40 @@ describe("entity subscriber > transaction flow", () => {
|
||||
await queryRunner.release()
|
||||
}
|
||||
})
|
||||
|
||||
it("query data in subscribers", async () => {
|
||||
const example = new Example()
|
||||
const data = { hello: ["world"] }
|
||||
|
||||
for (let connection of connections) {
|
||||
if (
|
||||
connection.driver.options.type === "mssql" ||
|
||||
connection.driver.options.type === "spanner"
|
||||
)
|
||||
return
|
||||
|
||||
beforeTransactionCommit.resetHistory()
|
||||
afterTransactionCommit.resetHistory()
|
||||
afterInsert.resetHistory()
|
||||
|
||||
const queryRunner = await connection.createQueryRunner()
|
||||
await queryRunner.startTransaction()
|
||||
|
||||
await connection.manager.save(example, { data })
|
||||
|
||||
await queryRunner.commitTransaction()
|
||||
|
||||
expect(afterInsert.getCall(0).args[0][0].queryRunner.data).to.eql(
|
||||
data,
|
||||
)
|
||||
expect(
|
||||
beforeTransactionCommit.getCall(0).args[0][0].queryRunner.data,
|
||||
).to.eql(data)
|
||||
expect(
|
||||
afterTransactionCommit.getCall(0).args[0][0].queryRunner.data,
|
||||
).to.eql(data)
|
||||
|
||||
await queryRunner.release()
|
||||
}
|
||||
})
|
||||
})
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user