-
Notifications
You must be signed in to change notification settings - Fork 463
Expand file tree
/
Copy pathdb.server.ts
More file actions
40 lines (37 loc) · 1.21 KB
/
db.server.ts
File metadata and controls
40 lines (37 loc) · 1.21 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
import 'dotenv/config'
import { styleText } from 'node:util'
import { remember } from '@epic-web/remember'
import { PrismaClient } from '#prisma/generated/client.ts'
import { PrismaBetterSqlite3 } from '@prisma/adapter-better-sqlite3'
export const prisma = remember('prisma', () => {
// NOTE: if you change anything in this function you'll need to restart
// the dev server to see your changes.
// Feel free to change this log threshold to something that makes sense for you
const logThreshold = 20
const adapter = new PrismaBetterSqlite3({ url: process.env.DATABASE_URL })
const client = new PrismaClient({
adapter,
log: [
{ level: 'query', emit: 'event' },
{ level: 'error', emit: 'stdout' },
{ level: 'warn', emit: 'stdout' },
],
})
client.$on('query', async (e) => {
if (e.duration < logThreshold) return
const color =
e.duration < logThreshold * 1.1
? 'green'
: e.duration < logThreshold * 1.2
? 'blue'
: e.duration < logThreshold * 1.3
? 'yellow'
: e.duration < logThreshold * 1.4
? 'redBright'
: 'red'
const dur = styleText(color, `${e.duration}ms`)
console.info(`prisma:query - ${dur} - ${e.query}`)
})
void client.$connect()
return client
})