Files
speckle-server/packages/server/modules/core/patches/knex.ts
T
Kristaps Fabians Geikins 347fa4b462 feat(server): improving knex query stack traces (#3855)
* feat(server): improving knex query stack traces

* minor cleanup

* more improvements
2025-01-21 10:59:09 +01:00

28 lines
943 B
TypeScript

/* eslint-disable @typescript-eslint/no-explicit-any */
import { enableImprovedKnexTelemetryStackTraces } from '@/modules/shared/helpers/envHelper'
import { collectLongTrace } from '@speckle/shared'
import knexQueryCompiler from 'knex/lib/query/querycompiler'
/**
* ⚠️ HERE BE DARKNESS! ⚠️
* We're monkey patching knex which is pretty cursed, but i'm not aware of any other way
* to get a proper stack trace in query event handlers
*/
export const patchKnex = () => {
if (enableImprovedKnexTelemetryStackTraces()) {
// Preserve stack trace on query compilation
const originalToSQL = knexQueryCompiler.prototype.toSQL
const newToSQL: typeof originalToSQL = function (
this: typeof knexQueryCompiler.prototype,
...args: any
) {
const ret = originalToSQL.apply(this, args)
ret.__stackTrace = collectLongTrace()
return ret
}
knexQueryCompiler.prototype.toSQL = newToSQL
}
}