Skip to content

Commit 58a4521

Browse files
committed
Switched pg-native.Pool detection method to avoid errors on runtimes with forbidden require()
1 parent 0f52822 commit 58a4521

1 file changed

Lines changed: 10 additions & 7 deletions

File tree

drizzle-orm/src/node-postgres/session.ts

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -16,8 +16,6 @@ import { tracer } from '~/tracing.ts';
1616
import { type Assume, mapResultRow } from '~/utils.ts';
1717

1818
const { Pool, types } = pg;
19-
const NativePool = (<any> pg).native ? (<{ Pool: typeof Pool }> (<any> pg).native).Pool : undefined;
20-
2119
export type NodePgClient = pg.Pool | PoolClient | Client;
2220

2321
export class NodePgPreparedQuery<T extends PreparedQueryConfig, TIsRqbV2 extends boolean = false>
@@ -305,8 +303,15 @@ export class NodePgSession<
305303
transaction: (tx: NodePgTransaction<TFullSchema, TRelations, TSchema>) => Promise<T>,
306304
config?: PgTransactionConfig | undefined,
307305
): Promise<T> {
308-
const session = (this.client instanceof Pool || (NativePool && this.client instanceof NativePool)) // oxlint-disable-line drizzle-internal/no-instanceof
309-
? new NodePgSession(await this.client.connect(), this.dialect, this.relations, this.schema, this.options)
306+
const isPool = this.client instanceof Pool || Object.getPrototypeOf(this.client).constructor.name.includes('Pool'); // oxlint-disable-line drizzle-internal/no-instanceof
307+
const session = isPool
308+
? new NodePgSession(
309+
await (<pg.Pool> this.client).connect(),
310+
this.dialect,
311+
this.relations,
312+
this.schema,
313+
this.options,
314+
)
310315
: this;
311316
const tx = new NodePgTransaction<TFullSchema, TRelations, TSchema>(
312317
this.dialect,
@@ -323,9 +328,7 @@ export class NodePgSession<
323328
await tx.execute(sql`rollback`);
324329
throw error;
325330
} finally {
326-
if (this.client instanceof Pool || (NativePool && this.client instanceof NativePool)) { // oxlint-disable-line drizzle-internal/no-instanceof
327-
(session.client as PoolClient).release();
328-
}
331+
if (isPool) (session.client as PoolClient).release();
329332
}
330333
}
331334

0 commit comments

Comments
 (0)