-
-
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: "And" operator in FindOptions (#9489)
Closes #3113
- Loading branch information
Showing
7 changed files
with
100 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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,3 +18,4 @@ export type FindOperatorType = | |
| "arrayContains" | ||
| "arrayContainedBy" | ||
| "arrayOverlap" | ||
| "and" |
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,5 @@ | ||
import { FindOperator } from "../FindOperator" | ||
|
||
export function And<T>(...values: FindOperator<T>[]): FindOperator<T> { | ||
return new FindOperator("and", values as any, true, true) | ||
} |
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,13 @@ | ||
import { Column, Entity, PrimaryGeneratedColumn } from "../../../../src" | ||
|
||
@Entity() | ||
export class User { | ||
@PrimaryGeneratedColumn() | ||
id: number | ||
|
||
@Column() | ||
name: string | ||
|
||
@Column({ type: "int", nullable: true }) | ||
money: number | null | ||
} |
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,63 @@ | ||
import { And, DataSource, IsNull, LessThan, MoreThan, Not } from "../../../src" | ||
import { | ||
closeTestingConnections, | ||
createTestingConnections, | ||
reloadTestingDatabases, | ||
} from "../../utils/test-utils" | ||
import { User } from "./entities/User" | ||
import { expect } from "chai" | ||
|
||
describe("github issues > #9316 specify how should interpret null and undefined values in conditions ", () => { | ||
let dataSources: DataSource[] | ||
|
||
before(async () => { | ||
dataSources = await createTestingConnections({ | ||
entities: [User], | ||
enabledDrivers: ["postgres", "mysql"], | ||
schemaCreate: true, | ||
dropSchema: true, | ||
}) | ||
}) | ||
|
||
beforeEach(() => reloadTestingDatabases(dataSources)) | ||
after(() => closeTestingConnections(dataSources)) | ||
|
||
it("should find users what money is not null and money is more than 10 and money is less than 100", async () => { | ||
await Promise.all( | ||
dataSources.map(async (dataSource) => { | ||
const foo = new User() | ||
foo.name = "Foo" | ||
foo.money = null | ||
|
||
const john = new User() | ||
john.name = "John" | ||
john.money = 11 | ||
|
||
const jane = new User() | ||
jane.name = "Jane" | ||
jane.money = 90 | ||
|
||
const bar = new User() | ||
bar.name = "Bar" | ||
bar.money = 101 | ||
|
||
await dataSource.manager.save([foo, john, jane, bar]) | ||
|
||
const users = await dataSource.manager.find(User, { | ||
where: { | ||
money: And(Not(IsNull()), MoreThan(10), LessThan(100)), | ||
}, | ||
}) | ||
|
||
// assert users | ||
expect(users).to.have.length(2) | ||
|
||
expect(users.find((user) => user.name === "John")).to.be.not | ||
.undefined | ||
|
||
expect(users.find((user) => user.name === "Jane")).to.be.not | ||
.undefined | ||
}), | ||
) | ||
}) | ||
}) |