Skip to content

Commit

Permalink
feat: Allow maxWait and timeout ITX options to be configurable
Browse files Browse the repository at this point in the history
The maximum time the client will wait to acquie a transaction from the
database, and the maximum time the transaction can run before being
rolled back are now configurable. The both default to 30s

Signed-off-by: Lucian Buzzo <lucian.buzzo@gmail.com>
  • Loading branch information
LucianBuzzo committed May 29, 2023
1 parent 5be412c commit 8e0bd6c
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 4 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,12 @@ const client = await setup({
},
};
},
options: {
// The maximum amount of time Yates will wait to acquire a transaction from the database. The default value is 30 seconds.
txMaxWait: 5000,
// The maximum amount of time the Yates query transaction can run before being canceled and rolled back. The default value is 30 seconds.
txTimeout: 10000,
}
});
```

Expand Down
19 changes: 15 additions & 4 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ const VALID_OPERATIONS = ["SELECT", "UPDATE", "INSERT", "DELETE"] as const;

type Operation = typeof VALID_OPERATIONS[number];
export type Models = Prisma.ModelName;

interface ClientOptions {
/** The maximum amount of time Yates will wait to acquire a transaction from the database. The default value is 30 seconds. */
txMaxWait?: number;
/** The maximum amount of time the Yates query transaction can run before being canceled and rolled back. The default value is 30 seconds. */
txTimeout?: number;
}

export interface Ability<ContextKeys extends string = string> {
description?: string;
expression?: Expression<ContextKeys>;
Expand Down Expand Up @@ -72,7 +80,9 @@ export const createRoleName = (name: string) => {
};

// 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, getContext: GetContextFn) => {
export const createClient = (prisma: PrismaClient, getContext: GetContextFn, options: ClientOptions = {}) => {
// Set default options
const { txMaxWait = 30000, txTimeout = 30000 } = options;
const client = prisma.$extends({
name: "Yates client",
query: {
Expand Down Expand Up @@ -143,8 +153,8 @@ export const createClient = (prisma: PrismaClient, getContext: GetContextFn) =>
return result;
},
{
maxWait: 30000,
timeout: 30000,
maxWait: txMaxWait,
timeout: txTimeout,
},
);

Expand Down Expand Up @@ -403,6 +413,7 @@ export interface SetupParams<
* Returning `null` will result in the permissions being skipped entirely.
*/
getContext: GetContextFn<ContextKeys>;
options?: ClientOptions;
}

/**
Expand All @@ -416,7 +427,7 @@ export const setup = async <
) => {
const { prisma, customAbilities, getRoles, getContext } = params;
await createRoles<K>({ prisma, customAbilities, getRoles });
const client = createClient(prisma, getContext);
const client = createClient(prisma, getContext, params.options);

return client;
};

0 comments on commit 8e0bd6c

Please sign in to comment.