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