diff --git a/query-engine/driver-adapters/executor/src/recording.ts b/query-engine/driver-adapters/executor/src/recording.ts index c7cd67ffe200..a4152488e985 100644 --- a/query-engine/driver-adapters/executor/src/recording.ts +++ b/query-engine/driver-adapters/executor/src/recording.ts @@ -4,7 +4,8 @@ import { type Result, type ResultSet, } from "@prisma/driver-adapter-utils"; -import { RetryHandler } from "undici"; + +type Recordings = ReturnType; export function recording(adapter: DriverAdapter) { const recordings = createInMemoryRecordings(); @@ -15,7 +16,7 @@ export function recording(adapter: DriverAdapter) { }; } -function recorder(adapter: DriverAdapter, recordings) { +function recorder(adapter: DriverAdapter, recordings: Recordings) { return { provider: adapter.provider, startTransaction: () => { @@ -25,20 +26,19 @@ function recorder(adapter: DriverAdapter, recordings) { return adapter.getConnectionInfo!(); }, queryRaw: async (params) => { - const result = adapter.queryRaw(params); + const result = await adapter.queryRaw(params); recordings.addQueryResults(params, result); return result; }, - executeRaw: async (params) => { - const result = adapter.executeRaw(params); + const result = await adapter.executeRaw(params); recordings.addCommandResults(params, result); return result; }, }; } -function replayer(adapter: DriverAdapter, recordings) { +function replayer(adapter: DriverAdapter, recordings: Recordings) { return { provider: adapter.provider, recordings: recordings, @@ -58,42 +58,49 @@ function replayer(adapter: DriverAdapter, recordings) { } function createInMemoryRecordings() { - const queryResults = {}; - const commandResults = {}; + const queryResults: Map> = new Map(); + const commandResults: Map> = new Map(); // Recording is currently only used in benchmarks. Before we used to serialize the whole query + // (sql + args) but since bigints are not serialized by JSON.stringify, and we didn’t really need // (sql + args) but since bigints are not serialized by JSON.stringify, and we didn't really need // args for benchmarks, we just serialize the sql part. // // If this ever changes (we reuse query recording in tests) we need to make sure to serialize the // args as well. - const queryToKey = (query) => JSON.stringify(query.sql); + const queryToKey = (params: Query) => { + return JSON.stringify(params.sql); + }; return { - addQueryResults: (params, result) => { + addQueryResults: (params: Query, result: Result) => { const key = queryToKey(params); - queryResults[key] = result; + queryResults.set(key, result); }, - getQueryResults: (params) => { + getQueryResults: (params: Query) => { const key = queryToKey(params); - if (!(key in queryResults)) { + + if (!queryResults.has(key)) { throw new Error(`Query not recorded: ${key}`); } - return queryResults[key]; + + return queryResults.get(key)!; }, - addCommandResults: (params, result) => { + addCommandResults: (params: Query, result: Result) => { const key = queryToKey(params); - commandResults[key] = result; + commandResults.set(key, result); }, - getCommandResults: (params) => { + getCommandResults: (params: Query) => { const key = queryToKey(params); - if (!(key in commandResults)) { + + if (!commandResults.has(key)) { throw new Error(`Command not recorded: ${key}`); } - return commandResults[key]; + + return commandResults.get(key)!; }, }; }