Unexpected Load event triggered on subscriber when inserting versioned entity
See original GitHub issueIf we implement a simple subscriber that track down entities lifecycle by logging out events on the console:
@EventSubscriber()
export class EntitySubscriber implements EntitySubscriberInterface {
public afterLoad(entity: any) {
console.log(`AFTER LOAD -> ${JSON.stringify(entity)}`);
}
beforeInsert(event: InsertEvent<any>) {
console.log(`BEFORE INSERT -> ${JSON.stringify(event.entity)}`);
}
afterInsert(event: InsertEvent<any>) {
console.log(`AFTER INSERT -> ${JSON.stringify(event.entity)}`);
}
}
Now let’s say we have a Post
entity:
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
}
We create a Post
and save it to the database:
let post = new Post();
post.title = "A first post";
await connection.manager.save(post);
The output is, as we expect, the following:
BEGIN TRANSACTION
BEFORE INSERT -> {"title":"A first post"}
INSERT INTO "post"("title") VALUES (?) -- PARAMETERS: ["A first post"]
AFTER INSERT -> {"title":"A first post","id":1}
COMMIT
Now let’s add a Version column to our Post entity:
@Entity()
export class Post {
@PrimaryGeneratedColumn()
id: number;
@Column()
title: string;
@VersionColumn()
version: number;
}
Again we create a Post
and save it to the database:
let post = new Post();
post.title = "A second post";
await connection.manager.save(post);
The output is, as we don’t expect, the following:
BEGIN TRANSACTION
BEFORE INSERT -> {"title":"A second post"}
INSERT INTO "post"("title", "version") VALUES (?, 1) -- PARAMETERS: ["A second post"]
SELECT "Post"."id" AS "Post_id", "Post"."version" AS "Post_version" FROM "post" "Post" WHERE "Post"."id" = ? -- PARAMETERS: [1]
AFTER LOAD -> {"id":1,"version":1}
AFTER INSERT -> {"title":"A second post","id":1,"version":1}
COMMIT
The afterLoad
event is triggered, but should not be, as the SELECT
here is an internal behaviour of TypeOrm part of the overall Save
operation (To further underline the wrong behaviour here, the afterLoad
event receives as a parameter an object which is not an entity, but an anonymous structure {"id":1,"version":1}
).
Correct behaviour should be that the afterLoad
event be triggered only as a result of a find()
query or a SELECT
query from a QueryBuilder
.
Issue Analytics
- State:
- Created 4 years ago
- Reactions:1
- Comments:6 (2 by maintainers)
Top GitHub Comments
Has there been any update on this?
I am experiencing the same issue where my
@AfterLoad
receives relation entities as just{id: 1234}
without any other properties.I was using the last stable version published on npm registry: 0.2.16 Now version 0.2.17 has been published, which I upgraded to, but the same behaviour remains.
Please find below the code I use to reproduce the issue:
Which code outputs the following:
Also if we don’t use any version number, still there is an afterLoad event triggered when we save an entity (see the first afterLoad event triggered in the second save() in the example above - knowing that internally typeorm is doing a SELECT before its UPDATE, but that should be transparent here). The only afterLoad event triggered should be the one after the findOne() call.