Skip to content

Commit

Permalink
f
Browse files Browse the repository at this point in the history
  • Loading branch information
LucianBuzzo committed Oct 31, 2024
1 parent 57811c0 commit c5a74b8
Showing 1 changed file with 42 additions and 0 deletions.
42 changes: 42 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,38 @@ export const createRoleName = (name: string) => {
return sanitizeSlug(hashWithPrefix("yates_role_", `${name}`));
};

// @ts-ignore
export function getBatchId(query: any): string | undefined {
if (query.action !== "findUnique" && query.action !== "findUniqueOrThrow") {
return undefined;
}
const parts: string[] = [];
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: object): string {
const keysArray = Object.keys(obj)
.sort()
.map((key) => {
// @ts-ignore
const value = obj[key];
if (typeof value === "object" && value !== null) {
return `(${key} ${buildKeysString(value)})`;
}
return key;
});

return `(${keysArray.join(" ")})`;
}

// This uses client extensions to set the role and context for the current user so that RLS can be applied
export const createClient = (
prisma: PrismaClient,
Expand All @@ -283,6 +315,16 @@ export const createClient = (
// Set default options
const { txMaxWait = 30000, txTimeout = 30000 } = options;

(prisma as any)._requestHandler.batchBy = (n) => {

Check failure on line 318 in src/index.ts

View workflow job for this annotation

GitHub Actions / release

Parameter 'n' implicitly has an 'any' type.

Check failure on line 318 in src/index.ts

View workflow job for this annotation

GitHub Actions / npm-pack-check

Parameter 'n' implicitly has an 'any' type.
console.log("batch by yates id?", n.transaction?.yates_id);
console.log("pq", getBatchId(n.protocolQuery));
return n.transaction?.yates_id
? n.transaction.yates_id + (getBatchId(n.protocolQuery) || "")
: n.transaction?.id
? `transaction-${n.transaction.id}`
: getBatchId(n.protocolQuery);
};

// biome-ignore lint/suspicious/noExplicitAny: TODO fix this
(prisma as any)._transactionWithArray = async function ({
promises,
Expand Down

0 comments on commit c5a74b8

Please sign in to comment.