From 5234086cf465997bbc9bcd9fac4de9a0e091d59b Mon Sep 17 00:00:00 2001 From: dennemark Date: Wed, 4 Dec 2024 17:21:22 +0100 Subject: [PATCH] Release 1.1.4 --- CHANGELOG.md | 6 ++++++ dist/index.d.mts | 3 ++- dist/index.d.ts | 3 ++- dist/index.js | 33 +++++++++++++++++++++++++++++++++ dist/index.mjs | 32 ++++++++++++++++++++++++++++++++ package.json | 2 +- 6 files changed, 76 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 85c6c7b..56b9907 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,11 @@ +## [1.1.4](https://github.com/dennemark/prisma-extension-casl/compare/1.1.3...1.1.4) (2024-12-04) + +### Bug Fixes + +* :bug: try fixing batching ([56cde5f](https://github.com/dennemark/prisma-extension-casl/commit/56cde5fba2b61c9968ca1f4ea8d9b27c093bffec)) + ## [1.1.3](https://github.com/dennemark/prisma-extension-casl/compare/1.1.2...1.1.3) (2024-11-29) ### Bug Fixes diff --git a/dist/index.d.mts b/dist/index.d.mts index 4f5084f..c2a450c 100644 --- a/dist/index.d.mts +++ b/dist/index.d.mts @@ -109,5 +109,6 @@ declare function useCaslAbilities(getAbilityFactory: () => AbilityBuilder AbilityBuilder __copyProps(__defProp({}, "__esModule", { value: tru var src_exports = {}; __export(src_exports, { applyCaslToQuery: () => applyCaslToQuery, + getBatchId: () => getBatchId, useCaslAbilities: () => useCaslAbilities }); module.exports = __toCommonJS(src_exports); @@ -1585,6 +1586,7 @@ function useCaslAbilities(getAbilityFactory, opts) { return transactionQuery(client._createItxClient(transaction)); } else { return client.$transaction(async (tx) => { + tx[Symbol.for("prisma.client.transaction.id")] = "casl-extension-" + tx[Symbol.for("prisma.client.transaction.id")]; return transactionQuery(tx); }, { //https://github.com/prisma/prisma/issues/20015 @@ -1594,6 +1596,12 @@ function useCaslAbilities(getAbilityFactory, opts) { } } }); + client._requestHandler.dataloader.options.batchBy = (request) => { + if (request.transaction?.id && !request.transaction?.id?.toString().startsWith("casl-extension-")) { + return `transaction-${request.transaction.id}`; + } + return getBatchId(request.protocolQuery); + }; return client.$extends({ name: "prisma-extension-casl", client: { @@ -1621,8 +1629,33 @@ function useCaslAbilities(getAbilityFactory, opts) { }); }); } +function getBatchId(query) { + if (query.action !== "findUnique" && query.action !== "findUniqueOrThrow") { + return void 0; + } + const parts = []; + if (query.modelName) { + parts.push(query.modelName); + } + if (query.query.arguments) { + parts.push(buildKeysString(query.query.arguments)); + } + parts.push(buildKeysString(query.query.selection)); + return parts.join(""); +} +function buildKeysString(obj) { + const keysArray = Object.keys(obj).sort().map((key) => { + const value = obj[key]; + if (typeof value === "object" && value !== null) { + return `(${key} ${buildKeysString(value)})`; + } + return key; + }); + return `(${keysArray.join(" ")})`; +} // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { applyCaslToQuery, + getBatchId, useCaslAbilities }); diff --git a/dist/index.mjs b/dist/index.mjs index bafd401..d0aee7c 100644 --- a/dist/index.mjs +++ b/dist/index.mjs @@ -1560,6 +1560,7 @@ function useCaslAbilities(getAbilityFactory, opts) { return transactionQuery(client._createItxClient(transaction)); } else { return client.$transaction(async (tx) => { + tx[Symbol.for("prisma.client.transaction.id")] = "casl-extension-" + tx[Symbol.for("prisma.client.transaction.id")]; return transactionQuery(tx); }, { //https://github.com/prisma/prisma/issues/20015 @@ -1569,6 +1570,12 @@ function useCaslAbilities(getAbilityFactory, opts) { } } }); + client._requestHandler.dataloader.options.batchBy = (request) => { + if (request.transaction?.id && !request.transaction?.id?.toString().startsWith("casl-extension-")) { + return `transaction-${request.transaction.id}`; + } + return getBatchId(request.protocolQuery); + }; return client.$extends({ name: "prisma-extension-casl", client: { @@ -1596,7 +1603,32 @@ function useCaslAbilities(getAbilityFactory, opts) { }); }); } +function getBatchId(query) { + if (query.action !== "findUnique" && query.action !== "findUniqueOrThrow") { + return void 0; + } + const parts = []; + if (query.modelName) { + parts.push(query.modelName); + } + if (query.query.arguments) { + parts.push(buildKeysString(query.query.arguments)); + } + parts.push(buildKeysString(query.query.selection)); + return parts.join(""); +} +function buildKeysString(obj) { + const keysArray = Object.keys(obj).sort().map((key) => { + const value = obj[key]; + if (typeof value === "object" && value !== null) { + return `(${key} ${buildKeysString(value)})`; + } + return key; + }); + return `(${keysArray.join(" ")})`; +} export { applyCaslToQuery, + getBatchId, useCaslAbilities }; diff --git a/package.json b/package.json index 5332ec4..4783c97 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "prisma-extension-casl", - "version": "1.1.3", + "version": "1.1.4", "description": "Enforce casl abilities on prisma client ", "main": "dist/index.js", "types": "dist/index.d.ts",