Skip to content

Commit c4261e4

Browse files
authored
refactor: move normalizeJsonProtocolValues to main repo (#5524)
1 parent 897c49f commit c4261e4

2 files changed

Lines changed: 2 additions & 103 deletions

File tree

libs/driver-adapters/executor/src/engines/JsonProtocol.ts

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import type { JsonTaggedValue } from '@prisma/client-engine-runtime'
12
import * as Transaction from './Transaction.js'
23

34
export type JsonQuery = {
@@ -52,27 +53,3 @@ export type JsonArgumentValue =
5253
| JsonTaggedValue
5354
| JsonArgumentValue[]
5455
| { [key: string]: JsonArgumentValue }
55-
56-
export type DateTaggedValue = { $type: 'DateTime'; value: string }
57-
export type DecimalTaggedValue = { $type: 'Decimal'; value: string }
58-
export type BytesTaggedValue = { $type: 'Bytes'; value: string }
59-
export type BigIntTaggedValue = { $type: 'BigInt'; value: string }
60-
export type FieldRefTaggedValue = { $type: 'FieldRef'; value: { _ref: string } }
61-
export type EnumTaggedValue = { $type: 'Enum'; value: string }
62-
export type JsonTaggedValue = { $type: 'Json'; value: string }
63-
64-
export type JsonInputTaggedValue =
65-
| DateTaggedValue
66-
| DecimalTaggedValue
67-
| BytesTaggedValue
68-
| BigIntTaggedValue
69-
| FieldRefTaggedValue
70-
| JsonTaggedValue
71-
| EnumTaggedValue
72-
73-
export type JsonOutputTaggedValue =
74-
| DateTaggedValue
75-
| DecimalTaggedValue
76-
| BytesTaggedValue
77-
| BigIntTaggedValue
78-
| JsonTaggedValue

libs/driver-adapters/executor/src/qc-test-worker/worker-query.ts

Lines changed: 1 addition & 79 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import * as util from 'node:util'
22

33
import {
44
noopTracingHelper,
5+
normalizeJsonProtocolValues,
56
QueryEvent,
67
QueryInterpreter,
78
type QueryInterpreterTransactionManager,
@@ -11,9 +12,7 @@ import {
1112
UserFacingError,
1213
} from '@prisma/client-engine-runtime'
1314
import { IsolationLevel, SqlQueryable } from '@prisma/driver-adapter-utils'
14-
import Decimal from 'decimal.js'
1515

16-
import { JsonOutputTaggedValue } from '../engines/JsonProtocol.js'
1716
import { withLocalPanicHandler } from '../panic.js'
1817
import { QueryCompiler } from '../query-compiler.js'
1918
import { 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

Comments
 (0)