forked from hackclub/toriel
-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
db.js
49 lines (45 loc) · 1.37 KB
/
db.js
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
41
42
43
44
45
46
47
48
49
const { PrismaClient } = require('@prisma/client')
const { metrics } = require('./util/metrics')
const operationToType = {
findUnique: 'find',
findUniqueOrThrow: 'find',
findFirst: 'find',
findFirstOrThrow: 'find',
findMany: 'find',
create: 'create',
createMany: 'create',
update: 'update',
updateMany: 'update',
upsert: 'update',
delete: 'delete',
deleteMany: 'delete',
aggregate: 'find',
groupBy: 'find',
count: 'find',
findRaw: 'find',
}
const prisma = new PrismaClient().$extends({
// extend prisma client
// to send query metrics such as latency & failures
query: {
async $allOperations({ operation, model, args, query }) {
const metricKey = `${operation}_${model}`
console.log(metricKey, operationToType[operation])
try {
const start = performance.now()
const queryResult = await query(args)
const time = performance.now() - start
metrics.timing(`prisma.latency.${metricKey}`, time)
metrics.increment(`prisma.success.${operationToType[operation]}`, 1)
metrics.increment(`prisma.success.model.${model}`, 1)
return queryResult
} catch (err) {
console.log(err)
metrics.increment(`prisma.errors.${operationToType[operation]}`, 1)
metrics.increment(`prisma.errors.model.${model}`, 1)
}
return
},
},
})
module.exports = { prisma }