@@ -2,6 +2,7 @@ import * as util from 'node:util'
22
33import {
44 noopTracingHelper ,
5+ normalizeJsonProtocolValues ,
56 QueryEvent ,
67 QueryInterpreter ,
78 type QueryInterpreterTransactionManager ,
@@ -11,9 +12,7 @@ import {
1112 UserFacingError ,
1213} from '@prisma/client-engine-runtime'
1314import { IsolationLevel , SqlQueryable } from '@prisma/driver-adapter-utils'
14- import Decimal from 'decimal.js'
1515
16- import { JsonOutputTaggedValue } from '../engines/JsonProtocol.js'
1716import { withLocalPanicHandler } from '../panic.js'
1817import { QueryCompiler } from '../query-compiler.js'
1918import { JsonProtocolQuery , QueryParams } from '../types/jsonRpc.js'
@@ -213,80 +212,3 @@ function getFullOperationName(query: JsonProtocolQuery): string {
213212 }
214213 }
215214}
216-
217- function normalizeJsonProtocolValues ( result : unknown ) : unknown {
218- if ( result === null ) {
219- return result
220- }
221-
222- if ( Array . isArray ( result ) ) {
223- return result . map ( normalizeJsonProtocolValues )
224- }
225-
226- if ( typeof result === 'object' ) {
227- if ( isTaggedValue ( result ) ) {
228- return normalizeTaggedValue ( result )
229- }
230-
231- // avoid mapping class instances
232- if ( result . constructor !== null && result . constructor . name !== 'Object' ) {
233- return result
234- }
235-
236- return mapObjectValues ( result , normalizeJsonProtocolValues )
237- }
238-
239- return result
240- }
241-
242- function isTaggedValue ( value : unknown ) : value is JsonOutputTaggedValue {
243- return (
244- value !== null &&
245- typeof value == 'object' &&
246- typeof value [ '$type' ] === 'string'
247- )
248- }
249-
250- /**
251- * Normalizes the value inside a tagged value to match the snapshots in tests.
252- * Sometimes there are multiple equally valid representations of the same value
253- * (e.g. a decimal string may contain an arbitrary number of trailing zeros,
254- * datetime strings may specify the UTC offset as either '+00:00' or 'Z', etc).
255- * Since these differences have no effect on the actual values received from the
256- * Prisma Client once the response is deserialized to JavaScript values, we don't
257- * spend extra CPU cycles on normalizing them in the data mapper. Instead, we
258- * patch and normalize them here to ensure they are consistent with the snapshots
259- * in the query engine tests.
260- */
261- function normalizeTaggedValue ( {
262- $type,
263- value,
264- } : JsonOutputTaggedValue ) : JsonOutputTaggedValue {
265- switch ( $type ) {
266- case 'BigInt' :
267- return { $type, value : String ( value ) }
268- case 'Bytes' :
269- return { $type, value }
270- case 'DateTime' :
271- return { $type, value : new Date ( value ) . toISOString ( ) }
272- case 'Decimal' :
273- return { $type, value : String ( new Decimal ( value ) ) }
274- case 'Json' :
275- return { $type, value : JSON . stringify ( JSON . parse ( value ) ) }
276- default :
277- assertNever ( value , 'Unknown tagged value' )
278- }
279- }
280-
281- function mapObjectValues < K extends PropertyKey , T , U > (
282- object : Record < K , T > ,
283- mapper : ( value : T , key : K ) => U ,
284- ) : Record < K , U > {
285- const result = { } as Record < K , U >
286-
287- for ( const key of Object . keys ( object ) ) {
288- result [ key ] = mapper ( object [ key ] as T , key as K )
289- }
290-
291- return result
292- }
0 commit comments