prisma-sql 1.12.0 → 1.14.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.12.0",
59
+ version: "1.14.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",
@@ -4390,15 +4390,20 @@ function generateClient(options) {
4390
4390
  const queries = /* @__PURE__ */ new Map();
4391
4391
  for (const [modelName, result] of directiveResults) {
4392
4392
  if (result.directives.length === 0) continue;
4393
- const modelQueries = /* @__PURE__ */ new Map();
4393
+ if (!queries.has(modelName)) {
4394
+ queries.set(modelName, /* @__PURE__ */ new Map());
4395
+ }
4396
+ const modelQueries = queries.get(modelName);
4394
4397
  for (const directive of result.directives) {
4395
4398
  try {
4399
+ const method = directive.header;
4396
4400
  const sqlDirective = generateSQL2(directive);
4397
- const queryKey = JSON.stringify(
4398
- directive.query.original,
4399
- Object.keys(directive.query.original).sort()
4400
- );
4401
- modelQueries.set(queryKey, {
4401
+ if (!modelQueries.has(method)) {
4402
+ modelQueries.set(method, /* @__PURE__ */ new Map());
4403
+ }
4404
+ const methodQueries = modelQueries.get(method);
4405
+ const queryKey = createQueryKey(directive.query.processed);
4406
+ methodQueries.set(queryKey, {
4402
4407
  sql: sqlDirective.sql,
4403
4408
  params: sqlDirective.staticParams,
4404
4409
  dynamicKeys: sqlDirective.dynamicKeys,
@@ -4408,9 +4413,6 @@ function generateClient(options) {
4408
4413
  if (!config.skipInvalid) throw error;
4409
4414
  }
4410
4415
  }
4411
- if (modelQueries.size > 0) {
4412
- queries.set(modelName, modelQueries);
4413
- }
4414
4416
  }
4415
4417
  const absoluteOutputDir = path.resolve(process.cwd(), outputDir);
4416
4418
  yield promises.mkdir(absoluteOutputDir, { recursive: true });
@@ -4418,13 +4420,28 @@ function generateClient(options) {
4418
4420
  const outputPath = path.join(absoluteOutputDir, "index.ts");
4419
4421
  yield promises.writeFile(outputPath, code);
4420
4422
  const totalQueries = Array.from(queries.values()).reduce(
4421
- (sum, m) => sum + m.size,
4423
+ (sum, methodMap) => sum + Array.from(methodMap.values()).reduce(
4424
+ (s, queryMap) => s + queryMap.size,
4425
+ 0
4426
+ ),
4422
4427
  0
4423
4428
  );
4424
4429
  console.log(`\u2713 Generated ${queries.size} models, ${totalQueries} queries`);
4425
4430
  console.log(`\u2713 Output: ${outputPath}`);
4426
4431
  });
4427
4432
  }
4433
+ function createQueryKey(processedQuery) {
4434
+ return JSON.stringify(processedQuery, (key, value) => {
4435
+ if (value && typeof value === "object" && !Array.isArray(value)) {
4436
+ const sorted = {};
4437
+ for (const k of Object.keys(value).sort()) {
4438
+ sorted[k] = value[k];
4439
+ }
4440
+ return sorted;
4441
+ }
4442
+ return value;
4443
+ });
4444
+ }
4428
4445
  function generateCode(models, queries, dialect) {
4429
4446
  const cleanModels = models.map((model) => __spreadProps(__spreadValues({}, model), {
4430
4447
  fields: model.fields.filter((f) => f !== void 0 && f !== null)
@@ -4434,28 +4451,87 @@ import { buildSQL, transformQueryResults, type PrismaMethod } from 'prisma-sql'
4434
4451
 
4435
4452
  const MODELS = ${JSON.stringify(cleanModels, null, 2)}
4436
4453
 
4437
- const QUERIES: Record<string, Record<string, {
4454
+ const QUERIES: Record<string, Record<string, Record<string, {
4438
4455
  sql: string
4439
4456
  params: unknown[]
4440
4457
  dynamicKeys: string[]
4441
4458
  paramMappings: any[]
4442
- }>> = ${formatQueries(queries)}
4459
+ }>>> = ${formatQueries(queries)}
4443
4460
 
4444
4461
  const DIALECT = ${JSON.stringify(dialect)}
4445
4462
 
4463
+ function isDynamicParam(key: string): boolean {
4464
+ // Common dynamic parameters that should be replaced with markers
4465
+ return key === 'skip' || key === 'take' || key === 'cursor'
4466
+ }
4467
+
4446
4468
  function normalizeQuery(args: any): string {
4447
4469
  if (!args) return '{}'
4448
- return JSON.stringify(args, Object.keys(args).sort())
4470
+
4471
+ // Clone and normalize the args
4472
+ const normalized = JSON.parse(JSON.stringify(args))
4473
+
4474
+ // Replace dynamic params with markers to match prebaked keys
4475
+ function replaceDynamicParams(obj: any): any {
4476
+ if (!obj || typeof obj !== 'object') return obj
4477
+
4478
+ if (Array.isArray(obj)) {
4479
+ return obj.map(replaceDynamicParams)
4480
+ }
4481
+
4482
+ const result: any = {}
4483
+ for (const [key, value] of Object.entries(obj)) {
4484
+ // Replace top-level dynamic params with markers
4485
+ if (isDynamicParam(key)) {
4486
+ result[key] = \`__DYNAMIC_\${key}__\`
4487
+ } else {
4488
+ result[key] = replaceDynamicParams(value)
4489
+ }
4490
+ }
4491
+ return result
4492
+ }
4493
+
4494
+ const withMarkers = replaceDynamicParams(normalized)
4495
+
4496
+ // Remove empty objects to match prebaked keys
4497
+ function removeEmptyObjects(obj: any): any {
4498
+ if (!obj || typeof obj !== 'object' || Array.isArray(obj)) return obj
4499
+
4500
+ const result: any = {}
4501
+ for (const [key, value] of Object.entries(obj)) {
4502
+ // Skip empty objects (but keep empty arrays)
4503
+ if (value && typeof value === 'object' && !Array.isArray(value) && Object.keys(value).length === 0) {
4504
+ continue
4505
+ }
4506
+ result[key] = removeEmptyObjects(value)
4507
+ }
4508
+ return result
4509
+ }
4510
+
4511
+ const cleaned = removeEmptyObjects(withMarkers)
4512
+
4513
+ // Sort keys recursively for deterministic matching
4514
+ return JSON.stringify(cleaned, (key, value) => {
4515
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
4516
+ const sorted: Record<string, unknown> = {}
4517
+ for (const k of Object.keys(value).sort()) {
4518
+ sorted[k] = value[k]
4519
+ }
4520
+ return sorted
4521
+ }
4522
+ return value
4523
+ })
4449
4524
  }
4450
4525
 
4451
4526
  function extractDynamicParams(args: any, dynamicKeys: string[]): unknown[] {
4452
4527
  const params: unknown[] = []
4453
4528
 
4454
4529
  for (const key of dynamicKeys) {
4455
- const parts = key.split('.')
4530
+ const parts = key.split(':')
4531
+ const path = parts[0].split('.')
4456
4532
  let value: any = args
4457
4533
 
4458
- for (const part of parts) {
4534
+ for (const part of path) {
4459
4535
  if (value === null || value === undefined) break
4460
4536
  value = value[part]
4461
4537
  }
@@ -4516,7 +4592,7 @@ export function createExtension(config: {
4516
4592
  const startTime = Date.now()
4517
4593
 
4518
4594
  const queryKey = normalizeQuery(args)
4519
- const prebakedQuery = QUERIES[modelName]?.[queryKey]
4595
+ const prebakedQuery = QUERIES[modelName]?.[method]?.[queryKey]
4520
4596
 
4521
4597
  let sql: string
4522
4598
  let params: unknown[]
@@ -4597,23 +4673,29 @@ function formatQueries(queries) {
4597
4673
  if (queries.size === 0) {
4598
4674
  return "{}";
4599
4675
  }
4600
- const entries = [];
4601
- for (const [modelName, modelQueries] of queries) {
4602
- const queryEntries = [];
4603
- for (const [queryKey, query] of modelQueries) {
4604
- queryEntries.push(` ${JSON.stringify(queryKey)}: {
4605
- sql: ${JSON.stringify(query.sql)},
4606
- params: ${JSON.stringify(query.params)},
4607
- dynamicKeys: ${JSON.stringify(query.dynamicKeys)},
4608
- paramMappings: ${JSON.stringify(query.paramMappings)},
4609
- }`);
4610
- }
4611
- entries.push(` ${JSON.stringify(modelName)}: {
4676
+ const modelEntries = [];
4677
+ for (const [modelName, methodMap] of queries) {
4678
+ const methodEntries = [];
4679
+ for (const [method, queryMap] of methodMap) {
4680
+ const queryEntries = [];
4681
+ for (const [queryKey, query] of queryMap) {
4682
+ queryEntries.push(` ${JSON.stringify(queryKey)}: {
4683
+ sql: ${JSON.stringify(query.sql)},
4684
+ params: ${JSON.stringify(query.params)},
4685
+ dynamicKeys: ${JSON.stringify(query.dynamicKeys)},
4686
+ paramMappings: ${JSON.stringify(query.paramMappings)},
4687
+ }`);
4688
+ }
4689
+ methodEntries.push(` ${JSON.stringify(method)}: {
4612
4690
  ${queryEntries.join(",\n")}
4691
+ }`);
4692
+ }
4693
+ modelEntries.push(` ${JSON.stringify(modelName)}: {
4694
+ ${methodEntries.join(",\n")}
4613
4695
  }`);
4614
4696
  }
4615
4697
  return `{
4616
- ${entries.join(",\n")}
4698
+ ${modelEntries.join(",\n")}
4617
4699
  }`;
4618
4700
  }
4619
4701
  var { version } = require_package();