mirror of
https://github.com/jbranchaud/til
synced 2026-01-03 23:28:02 +00:00
Add Configure Client To Log SQL Queries as a Prisma TIL
This commit is contained in:
30
prisma/configure-client-to-log-sql-queries.md
Normal file
30
prisma/configure-client-to-log-sql-queries.md
Normal file
@@ -0,0 +1,30 @@
|
||||
# Configure Client To Log SQL Queries
|
||||
|
||||
During development, especially while debugging, it can be helpful to see the
|
||||
actual SQL queries generated by Prisma queries we are formulating. Because an
|
||||
ORM is an abstraction over SQL, it isn't always obvious what the resulting SQL
|
||||
will turn out to be.
|
||||
|
||||
By adding the `log` configuration to where we initialize our Prisma client, we
|
||||
can tell it to log things like errors and the SQL of the queries it executes.
|
||||
|
||||
```javascript
|
||||
export const prisma =
|
||||
new PrismaClient({
|
||||
log: ['error', 'query']
|
||||
})
|
||||
```
|
||||
|
||||
If we only want the SQL logged in development, we could do something like this:
|
||||
|
||||
```javascript
|
||||
export const prisma =
|
||||
new PrismaClient({
|
||||
log:
|
||||
process.env.NODE_ENV === 'development'
|
||||
? ['query', 'error']
|
||||
: ['error'],
|
||||
})
|
||||
```
|
||||
|
||||
[source](https://github.com/prisma/prisma/discussions/3967)
|
||||
Reference in New Issue
Block a user