Skip to content

Commit f601172

Browse files
committed
feat: expose Prisma driver timeout options
1 parent 055aa3f commit f601172

4 files changed

Lines changed: 58 additions & 7 deletions

File tree

packages/challenge-prisma-client/README.md

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,19 @@ const activeCount = await client.challenge.count({
1616
await client.$disconnect();
1717
```
1818

19+
Callers that need bounded database work can pass PostgreSQL pool settings
20+
without constructing the Prisma 7 adapter themselves:
21+
22+
```ts
23+
const client = createChallengePrismaClient(databaseUrl, {
24+
driverOptions: {
25+
connectionTimeoutMillis: 5000,
26+
query_timeout: 5000,
27+
statement_timeout: 5000,
28+
},
29+
});
30+
```
31+
1932
The client connects lazily. Applications own its lifecycle and must disconnect
2033
it during shutdown. An empty or non-string connection URL raises `TypeError`;
2134
Prisma reports its normal configuration and database errors during creation or

packages/challenge-prisma-client/factory.test.js

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,3 +37,32 @@ test('rejects an empty Challenge database connection string', () => {
3737
/Challenge database connection string is required/,
3838
);
3939
});
40+
41+
/**
42+
* Verifies driver timeout options reach the factory-owned PostgreSQL adapter
43+
* without being forwarded as invalid generated PrismaClient options.
44+
*
45+
* @returns {Promise<void>} Resolves after the client is disconnected.
46+
* @throws AssertionError when the adapter does not retain the driver options.
47+
*/
48+
test('passes bounded driver options to the PostgreSQL adapter', async () => {
49+
const client = createChallengePrismaClient(
50+
'postgresql://user:password@localhost:5432/challenges?schema=challenge',
51+
{
52+
driverOptions: {
53+
connectionTimeoutMillis: 2500,
54+
query_timeout: 4000,
55+
statement_timeout: 4000,
56+
},
57+
},
58+
);
59+
60+
assert.deepEqual(client._engineConfig.adapter.config, {
61+
connectionString:
62+
'postgresql://user:password@localhost:5432/challenges?schema=challenge',
63+
connectionTimeoutMillis: 2500,
64+
query_timeout: 4000,
65+
statement_timeout: 4000,
66+
});
67+
await client.$disconnect();
68+
});

packages/challenge-prisma-client/index.d.ts

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,23 @@
11
export * from './generated';
22

33
import { Prisma, PrismaClient } from './generated';
4+
import type { PoolConfig } from 'pg';
45

56
/** Options accepted by the Challenge API external-client factory. */
67
export type ChallengePrismaClientOptions = Omit<
78
Prisma.PrismaClientOptions,
89
'adapter' | 'accelerateUrl'
9-
>;
10+
> & {
11+
/** PostgreSQL driver pool settings such as connection/query timeouts. */
12+
driverOptions?: Omit<PoolConfig, 'connectionString'>;
13+
};
1014

1115
/**
1216
* Creates a lazily connected Prisma client for the Challenge API database.
1317
*
1418
* @param connectionString PostgreSQL URL, including an optional Prisma
1519
* `schema` query parameter.
16-
* @param options Optional Prisma logging, transaction, and omit settings.
20+
* @param options Optional Prisma settings and PostgreSQL driver pool settings.
1721
* @returns A challenge-schema Prisma client. The caller owns its lifecycle and
1822
* must call `$disconnect()` during shutdown.
1923
* @throws TypeError when `connectionString` is not a non-empty string. Prisma

packages/challenge-prisma-client/index.js

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -29,9 +29,11 @@ function getPostgresSchema(connectionString) {
2929
* setup and connection-string schema handling inside this package.
3030
*
3131
* @param {string} connectionString PostgreSQL URL for the challenge database.
32-
* @param {import('./generated').Prisma.PrismaClientOptions} [options] Optional
33-
* Prisma logging, transaction, and omit settings. The factory owns `adapter`
34-
* and `accelerateUrl`, so any JavaScript values supplied for them are ignored.
32+
* @param {import('./generated').Prisma.PrismaClientOptions &
33+
* {driverOptions?: import('pg').PoolConfig}} [options] Optional Prisma logging,
34+
* transaction, and omit settings plus PostgreSQL pool/timeout settings. The
35+
* factory owns `adapter`, `accelerateUrl`, and the driver connection string, so
36+
* JavaScript values supplied for them are ignored.
3537
* @returns {import('./generated').PrismaClient} A lazily connected challenge
3638
* Prisma client. The caller must invoke `$disconnect()` during shutdown.
3739
* @throws {TypeError} When `connectionString` is not a non-empty string.
@@ -43,13 +45,16 @@ function createChallengePrismaClient(connectionString, options = {}) {
4345
throw new TypeError('Challenge database connection string is required');
4446
}
4547

46-
const clientOptions = { ...options };
48+
const { driverOptions: suppliedDriverOptions = {}, ...clientOptions } =
49+
options;
50+
const driverOptions = { ...suppliedDriverOptions };
4751
delete clientOptions.adapter;
4852
delete clientOptions.accelerateUrl;
53+
delete driverOptions.connectionString;
4954

5055
const schema = getPostgresSchema(connectionString);
5156
const adapter = new PrismaPg(
52-
{ connectionString },
57+
{ ...driverOptions, connectionString },
5358
schema ? { schema } : undefined,
5459
);
5560

0 commit comments

Comments
 (0)