mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
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:
parent
7c5ea99b31
commit
c15cb077a7
@ -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.
|
||||
|
||||
@ -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
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user