Skip to content

Commit c15cb07

Browse files
docs(entity-subscribers): document primary key availability in UpdateEvent (typeorm#11308)
* docs(entity-subscribers): describe UpdateEvent behavior * Update docs/listeners-and-subscribers.md Co-authored-by: Simon Garner <[email protected]> --------- Co-authored-by: Simon Garner <[email protected]>
1 parent 7c5ea99 commit c15cb07

File tree

2 files changed

+15
-0
lines changed

2 files changed

+15
-0
lines changed

docs/listeners-and-subscribers.md

+11
Original file line numberDiff line numberDiff line change
@@ -396,4 +396,15 @@ Excluding `listenTo`, all `EntitySubscriberInterface` methods are passed an even
396396

397397
See each [Event's interface](https://github.com/typeorm/typeorm/tree/master/src/subscriber/event) for additional properties.
398398

399+
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.
400+
401+
```typescript
402+
await postRepository.update(post.id, { description: "Bacon ipsum dolor amet cow" })
403+
404+
// post.subscriber.ts
405+
afterUpdate(event: UpdateEvent<Post>) {
406+
console.log(event.entity) // outputs { description: 'Bacon ipsum dolor amet cow' }
407+
}
408+
```
409+
399410
**Note:** All database operations in the subscribed event listeners should be performed using the event object's `queryRunner` or `manager` instance.

src/subscriber/event/UpdateEvent.ts

+4
Original file line numberDiff line numberDiff line change
@@ -29,6 +29,8 @@ export interface UpdateEvent<Entity> {
2929

3030
/**
3131
* Updating entity.
32+
*
33+
* Contains the same data that was passed to the updating method, be it the instance of an entity or the partial entity.
3234
*/
3335
entity: ObjectLiteral | undefined
3436

@@ -39,6 +41,8 @@ export interface UpdateEvent<Entity> {
3941

4042
/**
4143
* Updating entity in the database.
44+
*
45+
* Is set only when one of the following methods are used: .save(), .remove(), .softRemove(), and .recover()
4246
*/
4347
databaseEntity: Entity
4448

0 commit comments

Comments
 (0)