forked from typeorm/typeorm
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: resolve duplicate subscriber updateed columns
Closes: typeorm#9948
- Loading branch information
1 parent
b064049
commit 5890f61
Showing
4 changed files
with
88 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
name: string | ||
|
||
@Column({ default: 0 }) | ||
updatedNameColumnsCount: number | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
import "reflect-metadata" | ||
import { | ||
createTestingConnections, | ||
closeTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { DataSource } from "../../../src/data-source/DataSource" | ||
import { expect } from "chai" | ||
import { Post } from "./entity/Post" | ||
|
||
describe("github issues > #9948 Subscribers with both 'beforeUpdate' and 'afterUpdate' methods defined cause duplicate 'updatedColumn' entries", () => { | ||
let dataSources: DataSource[] | ||
before( | ||
async () => | ||
(dataSources = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should not duplicate update column", () => | ||
Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const manager = dataSource.manager | ||
|
||
const initialPost = new Post() | ||
initialPost.name = "post-init" | ||
await manager.save(initialPost) | ||
|
||
initialPost.name = "post-update" | ||
const updatedPost = await manager.save(initialPost) | ||
|
||
expect(updatedPost.updatedNameColumnsCount).to.be.eq(1) | ||
}), | ||
)) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
import { | ||
EntitySubscriberInterface, | ||
EventSubscriber, | ||
UpdateEvent, | ||
} from "../../../../src" | ||
import { Post } from "../entity/Post" | ||
|
||
@EventSubscriber() | ||
export class PostSubscriber implements EntitySubscriberInterface { | ||
listenTo(): string | Function { | ||
return Post | ||
} | ||
|
||
beforeUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce( | ||
(p, c) => { | ||
return p + (c.propertyName === "name" ? 1 : 0) | ||
}, | ||
0, | ||
) | ||
} | ||
|
||
afterUpdate(event: UpdateEvent<Post>): void | Promise<Post> { | ||
event.entity!.updatedNameColumnsCount = event.updatedColumns.reduce( | ||
(p, c) => { | ||
return p + (c.propertyName === "name" ? 1 : 0) | ||
}, | ||
0, | ||
) | ||
} | ||
} |