Skip to content

Commit

Permalink
docs: added a documentation section on debugging query builder (#9846)
Browse files Browse the repository at this point in the history
* Added a section on debugging query builder

It covers using getQuery() and getQueryAndParameters() which are both useful when trying to figure out why your query isn't doing what you might expect it should do.

* forgot await

* fix typo
  • Loading branch information
rizen committed Apr 5, 2023
1 parent e0e7d2c commit 98f2205
Showing 1 changed file with 41 additions and 0 deletions.
41 changes: 41 additions & 0 deletions docs/select-query-builder.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@
- [Using subqueries](#using-subqueries)
- [Hidden Columns](#hidden-columns)
- [Querying Deleted rows](#querying-deleted-rows)
- [Debugging](#debugging)

## What is `QueryBuilder`

Expand Down Expand Up @@ -1292,3 +1293,43 @@ console.log(account)
By default `timeTravelQuery()` uses `follower_read_timestamp()` function if no arguments passed.
For another supported timestamp arguments and additional information please refer to
[CockroachDB](https://www.cockroachlabs.com/docs/stable/as-of-system-time.html) docs.

## Debugging

You can get the generated SQL from the query builder by calling `getQuery()` or `getQueryAndParameters()`.

If you just want the query you can use `getQuery()`

```typescript
const sql = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.where("user.id = :id", { id: 1 })
.getQuery()
```

Which results in:

```sql
SELECT `user`.`id` as `userId`, `user`.`firstName` as `userFirstName`, `user`.`lastName` as `userLastName` FROM `users` `user` WHERE `user`.`id` = ?
```

Or if you want the query and the parameters you can get an array back using `getQueryAndParameters()`

```typescript
const queryAndParams = await dataSource
.getRepository(User)
.createQueryBuilder("user")
.where("user.id = :id", { id: 1 })
.getQueryAndParameters()
```

Which results in:

```typescript
[
"SELECT `user`.`id` as `userId`, `user`.`firstName` as `userFirstName`, `user`.`lastName` as `userLastName` FROM `users` `user` WHERE `user`.`id` = ?",
[ 1 ]
]
```

0 comments on commit 98f2205

Please sign in to comment.