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