prisma-sql 1.55.0 → 1.56.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -56,7 +56,7 @@ var require_package = __commonJS({
56
56
  "package.json"(exports$1, module) {
57
57
  module.exports = {
58
58
  name: "prisma-sql",
59
- version: "1.55.0",
59
+ version: "1.56.0",
60
60
  description: "Convert Prisma queries to optimized SQL with type safety. 2-7x faster than Prisma Client.",
61
61
  main: "dist/index.cjs",
62
62
  module: "dist/index.js",
@@ -5066,6 +5066,10 @@ function normalizeValue(value: unknown, seen = new WeakSet<object>(), depth = 0)
5066
5066
  return value
5067
5067
  }
5068
5068
 
5069
+ function normalizeParams(params: unknown[]): unknown[] {
5070
+ return params.map(p => normalizeValue(p))
5071
+ }
5072
+
5069
5073
  function getByPath(obj: any, path: string): unknown {
5070
5074
  if (!obj || !path) return undefined
5071
5075
  const keys = path.split('.')
@@ -5077,11 +5081,6 @@ function getByPath(obj: any, path: string): unknown {
5077
5081
  return result
5078
5082
  }
5079
5083
 
5080
- function normalizeParams(params: unknown[]): unknown[] {
5081
- return params.map(p => normalizeValue(p))
5082
- }
5083
-
5084
-
5085
5084
  export const MODELS: Model[] = ${JSON.stringify(cleanModels, null, 2)}
5086
5085
 
5087
5086
  const ENUM_MAPPINGS: Record<string, Record<string, string>> = ${JSON.stringify(mappings, null, 2)}
@@ -5099,8 +5098,11 @@ const DIALECT = ${JSON.stringify(dialect)}
5099
5098
 
5100
5099
  const MODEL_MAP = new Map(MODELS.map(m => [m.name, m]))
5101
5100
 
5102
- function isDynamicParam(key: string): boolean {
5103
- return key === 'skip' || key === 'take' || key === 'cursor'
5101
+ function isDynamicKeyForQueryKey(path: string[], key: string): boolean {
5102
+ if (key !== 'skip' && key !== 'take' && key !== 'cursor') return false
5103
+ if (path.includes('where')) return false
5104
+ if (path.includes('data')) return false
5105
+ return true
5104
5106
  }
5105
5107
 
5106
5108
  function transformEnumInValue(value: unknown, enumType: string | undefined): unknown {
@@ -5129,28 +5131,43 @@ function transformEnumInValue(value: unknown, enumType: string | undefined): unk
5129
5131
  return value
5130
5132
  }
5131
5133
 
5132
- function transformEnumValues(modelName: string, obj: any, currentPath: string[] = []): any {
5134
+ function getRelatedModelName(currentModelName: string, relationFieldName: string): string | null {
5135
+ const m = MODEL_MAP.get(currentModelName)
5136
+ if (!m) return null
5137
+ const f: any = (m as any).fields?.find((x: any) => x?.name === relationFieldName)
5138
+ if (!f || !f.isRelation) return null
5139
+ return f.relatedModel || null
5140
+ }
5141
+
5142
+ function transformEnumValuesByModel(modelName: string, obj: any, path: string[] = []): any {
5133
5143
  if (obj === null || obj === undefined) {
5134
5144
  return obj
5135
5145
  }
5136
-
5146
+
5137
5147
  if (Array.isArray(obj)) {
5138
- return obj.map(item => transformEnumValues(modelName, item, currentPath))
5148
+ return obj.map(item => transformEnumValuesByModel(modelName, item, path))
5139
5149
  }
5140
-
5150
+
5141
5151
  if (obj instanceof Date) {
5142
5152
  return obj
5143
5153
  }
5144
-
5154
+
5145
5155
  if (typeof obj === 'object') {
5146
5156
  const transformed: any = {}
5147
5157
  const modelFields = ENUM_FIELDS[modelName] || {}
5148
-
5158
+
5149
5159
  for (const [key, value] of Object.entries(obj)) {
5150
- const newPath = [...currentPath, key]
5151
-
5160
+ const nextPath = [...path, key]
5161
+
5162
+ const relModel = getRelatedModelName(modelName, key)
5163
+
5164
+ if (relModel && value && typeof value === 'object') {
5165
+ transformed[key] = transformEnumValuesByModel(relModel, value, nextPath)
5166
+ continue
5167
+ }
5168
+
5152
5169
  const enumType = modelFields[key]
5153
-
5170
+
5154
5171
  if (enumType) {
5155
5172
  if (value && typeof value === 'object' && !Array.isArray(value) && !(value instanceof Date)) {
5156
5173
  const transformedOperators: any = {}
@@ -5164,46 +5181,46 @@ function transformEnumValues(modelName: string, obj: any, currentPath: string[]
5164
5181
  } else if (value instanceof Date) {
5165
5182
  transformed[key] = value
5166
5183
  } else if (typeof value === 'object' && value !== null) {
5167
- transformed[key] = transformEnumValues(modelName, value, newPath)
5184
+ transformed[key] = transformEnumValuesByModel(modelName, value, nextPath)
5168
5185
  } else {
5169
5186
  transformed[key] = value
5170
5187
  }
5171
5188
  }
5189
+
5172
5190
  return transformed
5173
5191
  }
5174
-
5192
+
5175
5193
  return obj
5176
5194
  }
5177
5195
 
5178
-
5179
5196
  function normalizeQuery(args: any): string {
5180
5197
  if (!args) return '{}'
5181
-
5198
+
5182
5199
  const normalized = JSON.parse(JSON.stringify(args))
5183
-
5184
- function replaceDynamicParams(obj: any): any {
5200
+
5201
+ function replaceDynamicParams(obj: any, path: string[] = []): any {
5185
5202
  if (!obj || typeof obj !== 'object') return obj
5186
-
5203
+
5187
5204
  if (Array.isArray(obj)) {
5188
- return obj.map(replaceDynamicParams)
5205
+ return obj.map((v) => replaceDynamicParams(v, path))
5189
5206
  }
5190
-
5207
+
5191
5208
  const result: any = {}
5192
5209
  for (const [key, value] of Object.entries(obj)) {
5193
- if (isDynamicParam(key)) {
5210
+ if (isDynamicKeyForQueryKey(path, key)) {
5194
5211
  result[key] = \`__DYNAMIC_\${key}__\`
5195
5212
  } else {
5196
- result[key] = replaceDynamicParams(value)
5213
+ result[key] = replaceDynamicParams(value, [...path, key])
5197
5214
  }
5198
5215
  }
5199
5216
  return result
5200
5217
  }
5201
-
5218
+
5202
5219
  const withMarkers = replaceDynamicParams(normalized)
5203
-
5220
+
5204
5221
  function removeEmptyObjects(obj: any): any {
5205
5222
  if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return obj
5206
-
5223
+
5207
5224
  const result: any = {}
5208
5225
  for (const [key, value] of Object.entries(obj)) {
5209
5226
  if (value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 0) {
@@ -5213,14 +5230,14 @@ function normalizeQuery(args: any): string {
5213
5230
  }
5214
5231
  return result
5215
5232
  }
5216
-
5233
+
5217
5234
  const cleaned = removeEmptyObjects(withMarkers)
5218
-
5235
+
5219
5236
  return JSON.stringify(cleaned, (key, value) => {
5220
5237
  if (value && typeof value === 'object' && !Array.isArray(value)) {
5221
5238
  const sorted: Record<string, unknown> = {}
5222
5239
  for (const k of Object.keys(value).sort()) {
5223
- sorted[k] = value[k]
5240
+ sorted[k] = (value as any)[k]
5224
5241
  }
5225
5242
  return sorted
5226
5243
  }
@@ -5248,7 +5265,6 @@ function extractDynamicParams(args: any, dynamicKeys: string[]): unknown[] {
5248
5265
  return params
5249
5266
  }
5250
5267
 
5251
-
5252
5268
  async function executeQuery(client: any, sql: string, params: unknown[]): Promise<unknown[]> {
5253
5269
  const normalizedParams = normalizeParams(params)
5254
5270
 
@@ -5311,7 +5327,7 @@ export function speedExtension(config: {
5311
5327
  const modelName = this?.name || this?.$name
5312
5328
  const startTime = Date.now()
5313
5329
 
5314
- const transformedArgs = transformEnumValues(modelName, args || {})
5330
+ const transformedArgs = transformEnumValuesByModel(modelName, args || {})
5315
5331
 
5316
5332
  const queryKey = normalizeQuery(transformedArgs)
5317
5333
  const prebakedQuery = QUERIES[modelName]?.[method]?.[queryKey]
@@ -5464,7 +5480,7 @@ export function speedExtension(config: {
5464
5480
 
5465
5481
  onQuery?.({
5466
5482
  model: '_transaction',
5467
- method: 'count',
5483
+ method: 'transaction',
5468
5484
  sql: \`TRANSACTION(\${queries.length})\`,
5469
5485
  params: [],
5470
5486
  duration,
@@ -5476,7 +5492,7 @@ export function speedExtension(config: {
5476
5492
 
5477
5493
  return prisma.$extends({
5478
5494
  name: 'prisma-sql-generated',
5479
-
5495
+
5480
5496
  client: {
5481
5497
  $original: prisma,
5482
5498
  $batch: batch as <T extends Record<string, DeferredQuery>>(