Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: query builder negating with "NotBrackets" for complex expressio…
…ns (#8476) Co-authored-by: Christian Forgács <christian@wunderbit.de>
- Loading branch information
1 parent
546b3ed
commit fe7f328
Showing
6 changed files
with
169 additions
and
2 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
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,9 @@ | ||
import {Brackets} from "./Brackets"; | ||
|
||
/** | ||
* Syntax sugar. | ||
* Allows to use negate brackets in WHERE expressions for better syntax. | ||
*/ | ||
export class NotBrackets extends Brackets { | ||
|
||
} |
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 {Entity} from "../../../../../src/decorator/entity/Entity"; | ||
import {PrimaryGeneratedColumn} from "../../../../../src/decorator/columns/PrimaryGeneratedColumn"; | ||
import {Column} from "../../../../../src/decorator/columns/Column"; | ||
|
||
@Entity() | ||
export class User { | ||
|
||
@PrimaryGeneratedColumn() | ||
id: number; | ||
|
||
@Column() | ||
firstName: string; | ||
|
||
@Column() | ||
lastName: string; | ||
|
||
@Column() | ||
isAdmin: boolean; | ||
|
||
} |
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,118 @@ | ||
import "reflect-metadata"; | ||
import {expect} from "chai"; | ||
import {closeTestingConnections, createTestingConnections, reloadTestingDatabases} from "../../../utils/test-utils"; | ||
import {Connection} from "../../../../src/connection/Connection"; | ||
import {User} from "./entity/User"; | ||
import {NotBrackets} from "../../../../src/query-builder/NotBrackets"; | ||
|
||
describe("query builder > not", () => { | ||
|
||
let connections: Connection[]; | ||
before(async () => connections = await createTestingConnections({ | ||
entities: [__dirname + "/entity/*{.js,.ts}"], | ||
enabledDrivers: [ "sqlite" ], | ||
})); | ||
beforeEach(() => reloadTestingDatabases(connections)); | ||
after(() => closeTestingConnections(connections)); | ||
|
||
it("should put negation in the SQL with one condition", () => Promise.all(connections.map(async connection => { | ||
const sql = await connection.createQueryBuilder(User, "user") | ||
.where("user.isAdmin = :isAdmin", { isAdmin: true }) | ||
.andWhere(new NotBrackets(qb => { | ||
qb.where("user.firstName = :firstName1", { firstName1: "Hello" }) | ||
})) | ||
.disableEscaping() | ||
.getSql() | ||
|
||
expect(sql).to.be.equal( | ||
"SELECT user.id AS user_id, user.firstName AS user_firstName, " + | ||
"user.lastName AS user_lastName, user.isAdmin AS user_isAdmin " + | ||
"FROM user user " + | ||
"WHERE user.isAdmin = ? " + | ||
"AND NOT(user.firstName = ?)" | ||
) | ||
}))); | ||
|
||
it("should put negation in the SQL with two condition", () => Promise.all(connections.map(async connection => { | ||
const sql = await connection.createQueryBuilder(User, "user") | ||
.where("user.isAdmin = :isAdmin", { isAdmin: true }) | ||
.andWhere(new NotBrackets(qb => { | ||
qb.where("user.firstName = :firstName1", { firstName1: "Hello" }) | ||
.andWhere("user.lastName = :lastName1", { lastName1: "Mars" }); | ||
})) | ||
.disableEscaping() | ||
.getSql() | ||
|
||
expect(sql).to.be.equal( | ||
"SELECT user.id AS user_id, user.firstName AS user_firstName, " + | ||
"user.lastName AS user_lastName, user.isAdmin AS user_isAdmin " + | ||
"FROM user user " + | ||
"WHERE user.isAdmin = ? " + | ||
"AND NOT((user.firstName = ? AND user.lastName = ?))" | ||
) | ||
}))); | ||
|
||
it("should put negation correctly into WHERE expression with one condition", () => Promise.all(connections.map(async connection => { | ||
|
||
const user1 = new User(); | ||
user1.firstName = "Timber"; | ||
user1.lastName = "Saw"; | ||
user1.isAdmin = false; | ||
await connection.manager.save(user1); | ||
|
||
const user2 = new User(); | ||
user2.firstName = "Alex"; | ||
user2.lastName = "Messer"; | ||
user2.isAdmin = false; | ||
await connection.manager.save(user2); | ||
|
||
const user3 = new User(); | ||
user3.firstName = "Umed"; | ||
user3.lastName = "Pleerock"; | ||
user3.isAdmin = true; | ||
await connection.manager.save(user3); | ||
|
||
const users = await connection.createQueryBuilder(User, "user") | ||
.where("user.isAdmin = :isAdmin", { isAdmin: true }) | ||
.andWhere(new NotBrackets(qb => { | ||
qb.where("user.firstName = :firstName1", { firstName1: "Timber" }) | ||
})) | ||
.getMany(); | ||
|
||
expect(users.length).to.be.equal(1); | ||
|
||
}))); | ||
|
||
it("should put negation correctly into WHERE expression with two conditions", () => Promise.all(connections.map(async connection => { | ||
|
||
const user1 = new User(); | ||
user1.firstName = "Timber"; | ||
user1.lastName = "Saw"; | ||
user1.isAdmin = false; | ||
await connection.manager.save(user1); | ||
|
||
const user2 = new User(); | ||
user2.firstName = "Alex"; | ||
user2.lastName = "Messer"; | ||
user2.isAdmin = false; | ||
await connection.manager.save(user2); | ||
|
||
const user3 = new User(); | ||
user3.firstName = "Umed"; | ||
user3.lastName = "Pleerock"; | ||
user3.isAdmin = true; | ||
await connection.manager.save(user3); | ||
|
||
const users = await connection.createQueryBuilder(User, "user") | ||
.where("user.isAdmin = :isAdmin", { isAdmin: true }) | ||
.andWhere(new NotBrackets(qb => { | ||
qb.where("user.firstName = :firstName1", { firstName1: "Timber" }) | ||
.andWhere("user.lastName = :lastName1", { lastName1: "Saw" }); | ||
})) | ||
.getMany(); | ||
|
||
expect(users.length).to.be.equal(1); | ||
|
||
}))); | ||
|
||
}); |