How to use the Brackets function from typeorm

Find comprehensive JavaScript typeorm.Brackets code examples handpicked from public code repositorys.

typeorm.Brackets is a class that represents a group of conditions in a SQL WHERE clause.

35
36
37
38
39
40
41
42
43
44
if (currentTeam) {
    users = await User_1.User.createQueryBuilder('user')
        .where('countryId = :countryId', { countryId: user.country.id })
        .andWhere('id != :userId', { userId: user.id })
        .andWhere('id IN (:ids)', { ids: input.invitations.map(i => i.id) })
        .andWhere(new typeorm_1.Brackets(qb => {
        qb.where('teamId IS NULL')
            .orWhere('teamId = :teamId', { teamId: currentTeam.id });
    }))
        .getMany();
fork icon1
star icon0
watch icon0

93
94
95
96
97
98
99
100
101
102
    query.andWhere(`${table}.${property} IN (:${table}${property})`, {
        [`${table}${property}`]: value,
    });
    break;
case PageParameter_1.Operator.INS:
    query.andWhere(new typeorm_1.Brackets((qb) => {
        const values = value.split(",");
        values.forEach((val) => {
            qb.orWhere(`${table}.${property} IN ('${val}')`);
        });
fork icon0
star icon0
watch icon1

+ 120 other calls in file

How does typeorm.Brackets work?

typeorm.Brackets is a helper class in the TypeORM library that allows grouping of multiple conditions in a WHERE clause using parentheses to create more complex queries. Internally, Brackets creates an array of conditions and stores them until they are needed by the query builder to build the SQL query. When Brackets is used in a query, the conditions are concatenated using the specified operator (default is 'AND') and enclosed in parentheses. This allows for more complex queries to be built and executed in a structured and organized manner.

6
7
8
9
10
11
12
13
14
15
class SearchBuilder extends Builder_1.Builder {
    build({ search }) {
        var _a;
        const { searchableColumns } = this.configuration;
        if ((_a = search === null || search === void 0 ? void 0 : search.fields) === null || _a === void 0 ? void 0 : _a.length) {
            this.queryBuilder.andWhere(new typeorm_1.Brackets((qb) => {
                for (const field of search.fields) {
                    if (searchableColumns && !searchableColumns.includes(field)) {
                        continue;
                    }
fork icon0
star icon0
watch icon0

6
7
8
9
10
11
12
13
14
15
class WhereBuilder extends Builder_1.Builder {
    build({ filters: filters }) {
        this.buildConditionsFromFilter(filters, this.queryBuilder);
    }
    buildConditionsFromFilter(filters, queryBuilder, { filterWith } = { filterWith: null }) {
        queryBuilder.andWhere(new typeorm_1.Brackets((qb) => {
            for (const filter of filters) {
                qb[!filterWith || filterWith === "AND" ? "andWhere" : "orWhere"](new typeorm_1.Brackets((qb2) => {
                    for (const filterKey in filter) {
                        if (["AND", "OR"].includes(filterKey) && Array.isArray(filter[filterKey])) {
fork icon0
star icon0
watch icon0

+ 13 other calls in file

Ai Example

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
import { createConnection } from "typeorm";
import { Brackets } from "typeorm";

createConnection().then(async (connection) => {
  const userRepository = connection.getRepository(User);

  const users = await userRepository
    .createQueryBuilder("user")
    .where(
      new Brackets((qb) => {
        qb.where("user.firstName = :firstName", {
          firstName: "Timber",
        }).orWhere("user.lastName = :lastName", { lastName: "Saw" });
      })
    )
    .getMany();
});

In this example, we create a query builder for the User entity, and add a where clause that uses the Brackets class to group two conditions together using orWhere. The resulting query will find all users whose first name is "Timber" or last name is "Saw".