Skip to content

Commit da7be35

Browse files
authored
feat(db-postgres): dependency inject pg to allow Sentry instrumentation (#11478)
### What? I changed the interface of `@payloadcms/db-postgres` to allow a user to (optionally) inject their own `pg` module. ### Why? I noticed that `@payloadcms/sentry-plugin` wasn't instrumenting Payload's database queries through the [local payload API](https://payloadcms.com/docs/local-api/overview): ![image](https://github.com/user-attachments/assets/425691f5-cf7e-4625-89e0-6d07dda9cbc0) This is because Sentry applies a patch to the `pg` driver on import. For whatever reason, it doesn't patch `pg` when imported by dependencies (e.g. `@payloadcms/db-postgres`). After applying this fix, I can see the underlying query traces! ![image](https://github.com/user-attachments/assets/fb6f9aef-13d9-41b1-b4cc-36c565d15930)
1 parent 55d00e2 commit da7be35

File tree

3 files changed

+9
-2
lines changed

3 files changed

+9
-2
lines changed

Diff for: packages/db-postgres/src/connect.ts

+1-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ import type { Connect, Migration, Payload } from 'payload'
33

44
import { pushDevSchema } from '@payloadcms/drizzle'
55
import { drizzle } from 'drizzle-orm/node-postgres'
6-
import pg from 'pg'
76

87
import type { PostgresAdapter } from './types.js'
98

@@ -61,7 +60,7 @@ export const connect: Connect = async function connect(
6160

6261
try {
6362
if (!this.pool) {
64-
this.pool = new pg.Pool(this.poolOptions)
63+
this.pool = new this.pg.Pool(this.poolOptions)
6564
await connectWithReconnect({ adapter: this, payload: this.payload })
6665
}
6766

Diff for: packages/db-postgres/src/index.ts

+2
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ import {
5454
} from '@payloadcms/drizzle/postgres'
5555
import { pgEnum, pgSchema, pgTable } from 'drizzle-orm/pg-core'
5656
import { createDatabaseAdapter, defaultBeginTransaction } from 'payload'
57+
import pgDependency from 'pg'
5758
import { fileURLToPath } from 'url'
5859

5960
import type { Args, PostgresAdapter } from './types.js'
@@ -130,6 +131,7 @@ export function postgresAdapter(args: Args): DatabaseAdapterObj<PostgresAdapter>
130131
localesSuffix: args.localesSuffix || '_locales',
131132
logger: args.logger,
132133
operators: operatorMap,
134+
pg: args.pg || pgDependency,
133135
pgSchema: adapterSchema,
134136
// @ts-expect-error - vestiges of when tsconfig was not strict. Feel free to improve
135137
pool: undefined,

Diff for: packages/db-postgres/src/types.ts

+6
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@ import type { NodePgDatabase } from 'drizzle-orm/node-postgres'
1212
import type { PgSchema, PgTableFn, PgTransactionConfig } from 'drizzle-orm/pg-core'
1313
import type { Pool, PoolConfig } from 'pg'
1414

15+
type PgDependency = typeof import('pg')
16+
1517
export type Args = {
1618
/**
1719
* Transform the schema after it's built.
@@ -45,6 +47,7 @@ export type Args = {
4547
localesSuffix?: string
4648
logger?: DrizzleConfig['logger']
4749
migrationDir?: string
50+
pg?: PgDependency
4851
pool: PoolConfig
4952
prodMigrations?: {
5053
down: (args: MigrateDownArgs) => Promise<void>
@@ -74,6 +77,7 @@ type ResolveSchemaType<T> = 'schema' extends keyof T
7477
type Drizzle = NodePgDatabase<ResolveSchemaType<GeneratedDatabaseSchema>>
7578
export type PostgresAdapter = {
7679
drizzle: Drizzle
80+
pg: PgDependency
7781
pool: Pool
7882
poolOptions: PoolConfig
7983
} & BasePostgresAdapter
@@ -98,6 +102,8 @@ declare module 'payload' {
98102
initializing: Promise<void>
99103
localesSuffix?: string
100104
logger: DrizzleConfig['logger']
105+
/** Optionally inject your own node-postgres. This is required if you wish to instrument the driver with @payloadcms/plugin-sentry. */
106+
pg?: PgDependency
101107
pgSchema?: { table: PgTableFn } | PgSchema
102108
pool: Pool
103109
poolOptions: Args['pool']

0 commit comments

Comments
 (0)