-
-
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.
fix: allow where IsNull for ManyToOne relations
Closes: #8890
- Loading branch information
Showing
5 changed files
with
137 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
import { Column, Entity, OneToMany, PrimaryColumn } from "../../../../src" | ||
import { Post } from "./Post" | ||
|
||
@Entity() | ||
export class Author { | ||
@PrimaryColumn() | ||
id: number | ||
|
||
@Column() | ||
firstName: string | ||
|
||
@Column() | ||
lastName: string | ||
|
||
@Column() | ||
age: number | ||
|
||
@OneToMany(() => Post, (post) => post.author) | ||
posts: Post[] | ||
} |
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,24 @@ | ||
import { | ||
Column, | ||
Entity, | ||
ManyToOne, | ||
PrimaryGeneratedColumn, | ||
} from "../../../../src" | ||
import { Author } from "./Author" | ||
|
||
@Entity() | ||
export class Post { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
title: string | ||
|
||
@Column() | ||
text: string | ||
|
||
@ManyToOne(() => Author, (author) => author.posts, { | ||
nullable: true, | ||
}) | ||
author: Author | ||
} |
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,32 @@ | ||
import "reflect-metadata" | ||
import { EntityManager } from "../../../src" | ||
import { Post } from "./entity/Post" | ||
import { Author } from "./entity/Author" | ||
|
||
export async function prepareData(manager: EntityManager) { | ||
const post1 = new Post() | ||
post1.id = 1 | ||
post1.title = "Post #1" | ||
post1.text = "About post #1" | ||
await manager.save(post1) | ||
|
||
const post2 = new Post() | ||
post2.id = 2 | ||
post2.title = "Post #2" | ||
post2.text = "About post #2" | ||
await manager.save(post2) | ||
|
||
const post3 = new Post() | ||
post3.id = 3 | ||
post3.title = "Post #3" | ||
post3.text = "About post #3" | ||
await manager.save(post3) | ||
|
||
const author1 = new Author() | ||
author1.id = 1 | ||
author1.firstName = "Timber" | ||
author1.lastName = "Saw" | ||
author1.age = 25 | ||
author1.posts = [post1] | ||
await manager.save(author1) | ||
} |
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,51 @@ | ||
import "reflect-metadata" | ||
import "../../utils/test-setup" | ||
import { DataSource, IsNull } from "../../../src" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { Post } from "./entity/Post" | ||
import { prepareData } from "./issue-8890-utils" | ||
|
||
describe("github issues > #8890 it should be possible to query IS NULL on ManyToOne relations", () => { | ||
let connections: DataSource[] | ||
before( | ||
async () => | ||
(connections = await createTestingConnections({ | ||
__dirname, | ||
})), | ||
) | ||
beforeEach(() => reloadTestingDatabases(connections)) | ||
after(() => closeTestingConnections(connections)) | ||
|
||
it("where IsNull", () => | ||
Promise.all( | ||
connections.map(async (connection) => { | ||
await prepareData(connection.manager) | ||
|
||
const posts = await connection | ||
.createQueryBuilder(Post, "post") | ||
.setFindOptions({ | ||
where: { | ||
author: IsNull(), | ||
}, | ||
}) | ||
.orderBy("id", "ASC") | ||
.getMany() | ||
posts.should.be.eql([ | ||
{ | ||
id: 2, | ||
title: "Post #2", | ||
text: "About post #2", | ||
}, | ||
{ | ||
id: 3, | ||
title: "Post #3", | ||
text: "About post #3", | ||
}, | ||
]) | ||
}), | ||
)) | ||
}) |