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