mirror of
https://github.com/typeorm/typeorm.git
synced 2025-12-08 21:26:23 +00:00
docs: added a documentation section on debugging query builder (#9846)
* 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
This commit is contained in:
parent
e0e7d2c125
commit
98f22052be
@ -29,6 +29,7 @@
|
||||
- [Using subqueries](#using-subqueries)
|
||||
- [Hidden Columns](#hidden-columns)
|
||||
- [Querying Deleted rows](#querying-deleted-rows)
|
||||
- [Debugging](#debugging)
|
||||
|
||||
## What is `QueryBuilder`
|
||||
|
||||
@ -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 ]
|
||||
]
|
||||
```
|
||||
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user