Skip to content

Commit

Permalink
feat: entityId in InsertEvent (#10540)
Browse files Browse the repository at this point in the history
Co-authored-by: Paul Potsides <paul@koble.ai>
  • Loading branch information
strongpauly and Paul Potsides committed Dec 29, 2023
1 parent 122c897 commit ae006af
Show file tree
Hide file tree
Showing 8 changed files with 112 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/persistence/SubjectExecutor.ts
Original file line number Diff line number Diff line change
Expand Up @@ -300,6 +300,7 @@ export class SubjectExecutor {
result,
subject.metadata,
subject.entity!,
subject.identifier,
),
)
if (this.updateSubjects.length)
Expand Down
2 changes: 2 additions & 0 deletions src/subscriber/Broadcaster.ts
Original file line number Diff line number Diff line change
Expand Up @@ -366,6 +366,7 @@ export class Broadcaster {
result: BroadcasterResult,
metadata: EntityMetadata,
entity?: ObjectLiteral,
identifier?: ObjectLiteral,
): void {
if (entity && metadata.afterInsertListeners.length) {
metadata.afterInsertListeners.forEach((listener) => {
Expand All @@ -390,6 +391,7 @@ export class Broadcaster {
manager: this.queryRunner.manager,
entity: entity,
metadata: metadata,
entityId: metadata.getEntityIdMixedMap(identifier),
})
if (executionResult instanceof Promise)
result.promises.push(executionResult)
Expand Down
6 changes: 6 additions & 0 deletions src/subscriber/event/InsertEvent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { EntityManager } from "../../entity-manager/EntityManager"
import { DataSource } from "../../data-source/DataSource"
import { QueryRunner } from "../../query-runner/QueryRunner"
import { EntityMetadata } from "../../metadata/EntityMetadata"
import { ObjectLiteral } from "../../common/ObjectLiteral"

/**
* InsertEvent is an object that broadcaster sends to the entity subscriber when entity is inserted to the database.
Expand Down Expand Up @@ -29,6 +30,11 @@ export interface InsertEvent<Entity> {
*/
entity: Entity

/**
* Id or ids of the entity being inserted.
*/
entityId?: ObjectLiteral

/**
* Metadata of the entity.
*/
Expand Down
3 changes: 2 additions & 1 deletion test/github-issues/8221/entity/SettingSubscriber.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import {
EntitySubscriberInterface,
EventSubscriber,
InsertEvent,
LoadEvent,
UpdateEvent,
} from "../../../../src"
Expand All @@ -27,7 +28,7 @@ export class SettingSubscriber implements EntitySubscriberInterface {
this.counter.updates++
}

beforeInsert(event: UpdateEvent<any>): void {
beforeInsert(event: InsertEvent<any>): void {
this.counter.inserts++
}

Expand Down
10 changes: 10 additions & 0 deletions test/github-issues/9965/entity/Book.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src"

@Entity()
export class Book {
@PrimaryGeneratedColumn("uuid")
id: string

@Column()
name: string
}
23 changes: 23 additions & 0 deletions test/github-issues/9965/entity/User.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
import {
Column,
Entity,
JoinTable,
ManyToMany,
PrimaryGeneratedColumn,
} from "../../../../src"
import { Book } from "./Book"

export const BORROWED = "borrowed"

@Entity()
export class User {
@PrimaryGeneratedColumn("uuid")
id: string

@Column()
name: string

@ManyToMany((_type) => Book, {})
@JoinTable({ name: BORROWED })
public borrowed: Book[]
}
57 changes: 57 additions & 0 deletions test/github-issues/9965/issue-9965.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
import Sinon, { spy } from "sinon"
import { DataSource } from "../../../src"
import {
closeTestingConnections,
createTestingConnections,
reloadTestingDatabases,
} from "../../utils/test-utils"
import { Book } from "./entity/Book"
import { User } from "./entity/User"
import { BorrowedSubscriber } from "./subscriber/BorrrowedSubscriber"
import { expect } from "chai"

describe("github issues > #9965", () => {
let dataSources: DataSource[]

before(async () => {
dataSources = await createTestingConnections({
entities: [Book, User],
subscribers: [BorrowedSubscriber],
enabledDrivers: ["postgres"],
schemaCreate: true,
dropSchema: true,
})
})

beforeEach(() => reloadTestingDatabases(dataSources))
after(() => closeTestingConnections(dataSources))

it("should pass entityId to afterInsert method", async () => {
const [dataSource] = dataSources

const testBook = dataSource.manager.create(Book, { name: "TestPost" })
await dataSource.manager.save(testBook)

const testUser = dataSource.manager.create(User, { name: "TestUser" })
await dataSource.manager.save(testUser)

testUser.borrowed = [testBook]

const [subscriber] = dataSource.subscribers
const beforeInsert = spy(subscriber, "afterInsert")

await dataSource.manager.save(testUser)

expect(beforeInsert.called).to.be.true
expect(
beforeInsert.calledWith(
Sinon.match((event) => {
return (
typeof event.entityId?.userId === "string" &&
typeof event.entityId?.bookId === "string"
)
}),
),
).to.be.ok
})
})
11 changes: 11 additions & 0 deletions test/github-issues/9965/subscriber/BorrrowedSubscriber.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
import { EntitySubscriberInterface, EventSubscriber } from "../../../../src"
import { BORROWED } from "../entity/User"

@EventSubscriber()
export class BorrowedSubscriber implements EntitySubscriberInterface {
listenTo(): string | Function {
return BORROWED
}

afterInsert(): void | Promise<any> {}
}

0 comments on commit ae006af

Please sign in to comment.