prisma-sql 1.12.0 → 1.13.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.cjs +63 -30
- package/dist/generator.cjs.map +1 -1
- package/dist/generator.js +63 -30
- package/dist/generator.js.map +1 -1
- package/package.json +1 -1
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.
|
|
57
|
+
version: "1.13.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
|
-
|
|
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
|
-
|
|
4396
|
-
|
|
4397
|
-
|
|
4398
|
-
);
|
|
4399
|
-
|
|
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,
|
|
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,38 @@ 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
|
-
}
|
|
4457
|
+
}>>> = ${formatQueries(queries)}
|
|
4441
4458
|
|
|
4442
4459
|
const DIALECT = ${JSON.stringify(dialect)}
|
|
4443
4460
|
|
|
4444
4461
|
function normalizeQuery(args: any): string {
|
|
4445
4462
|
if (!args) return '{}'
|
|
4446
|
-
return JSON.stringify(args,
|
|
4463
|
+
return JSON.stringify(args, (key, value) => {
|
|
4464
|
+
if (value && typeof value === 'object' && !Array.isArray(value)) {
|
|
4465
|
+
const sorted: Record<string, unknown> = {}
|
|
4466
|
+
for (const k of Object.keys(value).sort()) {
|
|
4467
|
+
sorted[k] = value[k]
|
|
4468
|
+
}
|
|
4469
|
+
return sorted
|
|
4470
|
+
}
|
|
4471
|
+
return value
|
|
4472
|
+
})
|
|
4447
4473
|
}
|
|
4448
4474
|
|
|
4449
4475
|
function extractDynamicParams(args: any, dynamicKeys: string[]): unknown[] {
|
|
4450
4476
|
const params: unknown[] = []
|
|
4451
4477
|
|
|
4452
4478
|
for (const key of dynamicKeys) {
|
|
4453
|
-
const parts = key.split('
|
|
4479
|
+
const parts = key.split(':')
|
|
4480
|
+
const path = parts[0].split('.')
|
|
4454
4481
|
let value: any = args
|
|
4455
4482
|
|
|
4456
|
-
for (const part of
|
|
4483
|
+
for (const part of path) {
|
|
4457
4484
|
if (value === null || value === undefined) break
|
|
4458
4485
|
value = value[part]
|
|
4459
4486
|
}
|
|
@@ -4514,7 +4541,7 @@ export function createExtension(config: {
|
|
|
4514
4541
|
const startTime = Date.now()
|
|
4515
4542
|
|
|
4516
4543
|
const queryKey = normalizeQuery(args)
|
|
4517
|
-
const prebakedQuery = QUERIES[modelName]?.[queryKey]
|
|
4544
|
+
const prebakedQuery = QUERIES[modelName]?.[method]?.[queryKey]
|
|
4518
4545
|
|
|
4519
4546
|
let sql: string
|
|
4520
4547
|
let params: unknown[]
|
|
@@ -4595,23 +4622,29 @@ function formatQueries(queries) {
|
|
|
4595
4622
|
if (queries.size === 0) {
|
|
4596
4623
|
return "{}";
|
|
4597
4624
|
}
|
|
4598
|
-
const
|
|
4599
|
-
for (const [modelName,
|
|
4600
|
-
const
|
|
4601
|
-
for (const [
|
|
4602
|
-
queryEntries
|
|
4603
|
-
|
|
4604
|
-
|
|
4605
|
-
|
|
4606
|
-
|
|
4607
|
-
|
|
4608
|
-
|
|
4609
|
-
|
|
4625
|
+
const modelEntries = [];
|
|
4626
|
+
for (const [modelName, methodMap] of queries) {
|
|
4627
|
+
const methodEntries = [];
|
|
4628
|
+
for (const [method, queryMap] of methodMap) {
|
|
4629
|
+
const queryEntries = [];
|
|
4630
|
+
for (const [queryKey, query] of queryMap) {
|
|
4631
|
+
queryEntries.push(` ${JSON.stringify(queryKey)}: {
|
|
4632
|
+
sql: ${JSON.stringify(query.sql)},
|
|
4633
|
+
params: ${JSON.stringify(query.params)},
|
|
4634
|
+
dynamicKeys: ${JSON.stringify(query.dynamicKeys)},
|
|
4635
|
+
paramMappings: ${JSON.stringify(query.paramMappings)},
|
|
4636
|
+
}`);
|
|
4637
|
+
}
|
|
4638
|
+
methodEntries.push(` ${JSON.stringify(method)}: {
|
|
4610
4639
|
${queryEntries.join(",\n")}
|
|
4640
|
+
}`);
|
|
4641
|
+
}
|
|
4642
|
+
modelEntries.push(` ${JSON.stringify(modelName)}: {
|
|
4643
|
+
${methodEntries.join(",\n")}
|
|
4611
4644
|
}`);
|
|
4612
4645
|
}
|
|
4613
4646
|
return `{
|
|
4614
|
-
${
|
|
4647
|
+
${modelEntries.join(",\n")}
|
|
4615
4648
|
}`;
|
|
4616
4649
|
}
|
|
4617
4650
|
var { version } = require_package();
|