-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix: resolve issue delete column null on after update event subscriber #8227
Closed
Closed
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
73f1341
fix: resolve issue delete column null on after update event subscriber
spotykatch cbe5d58
Merge branch 'typeorm:master' into spotykatch-1
spotykatch e99369c
Merge branch 'typeorm:master' into spotykatch-1
spotykatch 43b746f
fix: resolve issue delete column null on after update event subscriber
spotykatch 7c736f0
fix: resolve issue delete column null on after update event subscriber
spotykatch 7b63d6b
Merge branch 'spotykatch-1' of https://github.com/spotykatch/typeorm …
spotykatch b8241a0
fix: resolve issue delete column null on after update event subscriber
spotykatch b64376b
fix: resolve issue delete column null on after update event subscriber
spotykatch File filter
Filter by extension
Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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
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 { DeleteDateColumn, Entity, PrimaryGeneratedColumn, UpdateDateColumn } from "../../../../src"; | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@UpdateDateColumn() | ||
updatedAt: Date; | ||
|
||
@DeleteDateColumn() | ||
deletedAt: Date; | ||
} |
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 "reflect-metadata"; | ||
import { createTestingConnections, closeTestingConnections, reloadTestingDatabases } from "../../utils/test-utils"; | ||
import { Connection } from "../../../src/connection/Connection"; | ||
import { Post } from "./entity/Post"; | ||
|
||
describe("github issues > #6327 softRemove DeleteDateColumn is null at Susbscriber's AfterUpdate method", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
subscribers: [__dirname + "/subscriber/*{.js,.ts}"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should send correct update and delete date columns to after update subscriber", () => Promise.all(connections.map(async connection => { | ||
|
||
const manager = connection.manager; | ||
|
||
const entity = new Post(); | ||
await manager.save(entity); | ||
|
||
const deletedEntity = await manager.softRemove(entity, { data: { action: "soft-delete" } }); | ||
|
||
await manager.recover(deletedEntity, { data: { action: "restore" } }); | ||
|
||
}))); | ||
|
||
}); |
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,24 @@ | ||
import { expect } from "chai"; | ||
import { EntitySubscriberInterface, EventSubscriber, UpdateEvent } from "../../../../src"; | ||
import { Post } from "../entity/Post"; | ||
|
||
@EventSubscriber() | ||
export class PostSubscriber implements EntitySubscriberInterface<Post> { | ||
listenTo() { | ||
return Post; | ||
} | ||
|
||
afterUpdate(event: UpdateEvent<Post>): void { | ||
const { entity, queryRunner: { data } } = event; | ||
|
||
expect(["soft-delete", "restore"]).to.include(data!.action); | ||
|
||
if (data!.action === "soft-delete") { | ||
expect(Object.prototype.toString.call(entity!.deletedAt)).to.be.eq("[object Date]"); | ||
} | ||
|
||
if (data!.action === "restore") { | ||
expect(entity!.deletedAt).to.be.null; | ||
} | ||
} | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is this line of code the only difference with
update
method? If so, I would like to re-useupdate
method to prevent another peace of complex code to be added into the repo... We can makeupdate
method to accept returning columns if needed.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
oh another diff I see is
withDeleted
. Maybe we can add a third parameter toupdate
, liketype: "default" | "soft-delete"
. Dunno, better to came up with better names, but having singleupdate
method is better for sure.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Got it.
I think
withDeleted
should be used with updates too. So if the entity is changed when soft deleted, it will fireafterUpdate
event. It will not if there is nowithDeleted
.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok. Let me think in writing ))) Just helps me to think))) Those are only my thoughts. You can skip it.
To get rid of
softDelete
method we do this:As
this.expressionMap
passed by reference inSoftDeleteQueryBulder.execute()
andUpdateQueryBuilder.execute()
methods andthis.expressionMap.extraReturningColumns
are the columns we need to updateand
expressionMap
is required in theReturningResultsEntityUpdator
constructorand
extraReturningColumns
are never null or undefinedwe don't need to get
updationColumns
like soinsead we can use
So that
updationColumns
are unique for update and soft-delete and we don't need to call the same method multiple times.Also, we can use
withDeleted()
for both update and soft delete as select is done by id. Guess it won't slow down a lot even with lots of soft-deleted rows. I already mentioned, that if the entity is changed while soft-deleted it won't make changes to the entity fired toafterUpdate
event. And we can lose the blame in history if the event is used for that case.