-
-
Notifications
You must be signed in to change notification settings - Fork 6.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: entityId in InsertEvent (#10540)
Co-authored-by: Paul Potsides <paul@koble.ai>
- Loading branch information
1 parent
122c897
commit ae006af
Showing
8 changed files
with
112 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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[] | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}) | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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> {} | ||
} |