docs(entity-subscribers): document primary key availability in UpdateEvent (#11308)

* docs(entity-subscribers): describe UpdateEvent behavior

* Update docs/listeners-and-subscribers.md

Co-authored-by: Simon Garner <simon@equalogic.com>

---------

Co-authored-by: Simon Garner <simon@equalogic.com>
This commit is contained in:
Jovana Đurić 2025-04-05 14:45:22 +02:00 committed by GitHub
parent 7c5ea99b31
commit c15cb077a7
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 15 additions and 0 deletions

View File

@ -396,4 +396,15 @@ Excluding `listenTo`, all `EntitySubscriberInterface` methods are passed an even
See each [Event's interface](https://github.com/typeorm/typeorm/tree/master/src/subscriber/event) for additional properties.
Note that `event.entity` may not necessarily contain primary key(s) when `Repository.update()` is used. Only the values provided as the entity partial will be available. In order to make primary keys available in the subscribers, you can explicitly pass primary key value(s) in the partial entity object literal or use `Repository.save()`, which performs re-fetching.
```typescript
await postRepository.update(post.id, { description: "Bacon ipsum dolor amet cow" })
// post.subscriber.ts
afterUpdate(event: UpdateEvent<Post>) {
console.log(event.entity) // outputs { description: 'Bacon ipsum dolor amet cow' }
}
```
**Note:** All database operations in the subscribed event listeners should be performed using the event object's `queryRunner` or `manager` instance.

View File

@ -29,6 +29,8 @@ export interface UpdateEvent<Entity> {
/**
* Updating entity.
*
* Contains the same data that was passed to the updating method, be it the instance of an entity or the partial entity.
*/
entity: ObjectLiteral | undefined
@ -39,6 +41,8 @@ export interface UpdateEvent<Entity> {
/**
* Updating entity in the database.
*
* Is set only when one of the following methods are used: .save(), .remove(), .softRemove(), and .recover()
*/
databaseEntity: Entity