-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
d8200c8
commit c392a63
Showing
28 changed files
with
910 additions
and
3 deletions.
There are no files selected for viewing
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file added
BIN
+5.85 KB
...s-version-npm-5.9.0-32.23fdc5965b1e05fc54e5f26ed3de66776b93de64-c7d3f36404-aae5655675.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file removed
BIN
-1.18 MB
.yarn/cache/@rollup-rollup-linux-x64-gnu-npm-4.1.4-f310dd9305-8.zip
Binary file not shown.
Binary file removed
BIN
-1.17 MB
.yarn/cache/@rollup-rollup-win32-x64-msvc-npm-4.1.4-a9b7b64292-8.zip
Binary file not shown.
Binary file not shown.
Binary file not shown.
Binary file not shown.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
import assert from "node:assert"; | ||
|
||
import { ESTree } from "meriyah"; | ||
import type prisma from "@prisma/client"; | ||
|
||
import type AppMap from "../AppMap"; | ||
import { getTime } from "../util/getTime"; | ||
import { fixReturnEventIfPromiseResult, recording } from "../recorder"; | ||
import { FunctionInfo } from "../registry"; | ||
|
||
export default function prismaHook(mod: typeof prisma) { | ||
assert("PrismaClient" in mod); | ||
assert(mod.PrismaClient != null); | ||
const PC = mod.PrismaClient as { prototype: unknown }; | ||
const proto = PC.prototype; | ||
assert(proto != null && typeof proto === "object"); | ||
assert("_request" in proto); | ||
proto._request = createProxy(proto._request as (...args: unknown[]) => unknown); | ||
return mod; | ||
} | ||
|
||
prismaHook.applicable = function (id: string) { | ||
return ["@prisma/client"].includes(id); | ||
}; | ||
|
||
// https://github.com/prisma/prisma/blob/095cba1a1b79d0d950246b07c9fb48d22fd7f229/packages/client/src/runtime/getPrismaClient.ts#L181 | ||
interface QueryEvent { | ||
timestamp: Date; | ||
query: string; | ||
params: string; | ||
duration: number; | ||
target: string; | ||
} | ||
|
||
interface PrismaRequestParamsArgs { | ||
data?: unknown; | ||
include?: unknown; | ||
where?: unknown; | ||
} | ||
|
||
interface PrismaRequestParams { | ||
action?: string; | ||
model?: string; | ||
args?: PrismaRequestParamsArgs; | ||
} | ||
|
||
let hookAttached = false; | ||
|
||
function createProxy<T extends (...args: unknown[]) => unknown>(prismaClientMethod: T) { | ||
return new Proxy(prismaClientMethod, { | ||
apply(target, thisArg: unknown, argArray: Parameters<T>) { | ||
if (!hookAttached) { | ||
hookAttached = true; | ||
assert( | ||
thisArg != null && | ||
typeof thisArg === "object" && | ||
"_engine" in thisArg && | ||
thisArg._engine != null && | ||
typeof thisArg._engine === "object" && | ||
"config" in thisArg._engine && | ||
thisArg._engine.config != null && | ||
typeof thisArg._engine.config === "object" && | ||
"logLevel" in thisArg._engine.config && | ||
"logQueries" in thisArg._engine.config && | ||
"activeProvider" in thisArg._engine.config && | ||
typeof thisArg._engine.config.activeProvider == "string", | ||
); | ||
|
||
const dbType = thisArg._engine.config.activeProvider; | ||
thisArg._engine.config.logLevel = "query"; | ||
thisArg._engine.config.logQueries = true; | ||
assert("$on" in thisArg && typeof thisArg.$on === "function"); | ||
thisArg.$on("query", (queryEvent: QueryEvent) => { | ||
const call = recording.sqlQuery(dbType, queryEvent.query); | ||
recording.functionReturn(call.id, undefined, queryEvent.duration); | ||
}); | ||
} | ||
|
||
// Report Prisma query as a function call, if suitable | ||
let prismaCall: AppMap.FunctionCallEvent | undefined; | ||
if (argArray?.length > 0) { | ||
const requestParams = argArray[0] as PrismaRequestParams; | ||
|
||
const params = | ||
requestParams.args != null | ||
? Object.keys(requestParams.args).map((k) => { | ||
return { type: "Identifier", name: k } as ESTree.Identifier; | ||
}) | ||
: []; | ||
|
||
if (requestParams.action && requestParams.model) { | ||
const info: FunctionInfo = { | ||
async: true, | ||
generator: false, | ||
id: requestParams.action, | ||
params: params, | ||
location: { path: "unknown", lineno: 0 }, | ||
klassOrFile: requestParams.model, | ||
static: true, | ||
}; | ||
prismaCall = recording.functionCall( | ||
info, | ||
undefined, | ||
params.map((p) => requestParams.args?.[p.name as keyof PrismaRequestParamsArgs]), | ||
); | ||
} | ||
} | ||
|
||
if (prismaCall) { | ||
const start = getTime(); | ||
try { | ||
const result = target.apply(thisArg, argArray); | ||
const ret = recording.functionReturn(prismaCall.id, result, getTime() - start); | ||
return fixReturnEventIfPromiseResult(result, ret, prismaCall, start); | ||
} catch (exn: unknown) { | ||
recording.functionException(prismaCall.id, exn, getTime() - start); | ||
throw exn; | ||
} | ||
} | ||
|
||
return target.apply(thisArg, argArray); | ||
}, | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.