Skip to content

Commit af6ba86

Browse files
authored
feat(db-mongodb): add afterCreateConnection and afterOpenConnection hooks (#14649)
This PR exposes `afterCreateConnection` and `afterOpenConnection` hooks through the db-mongodb adapter args. ## Use-case These lifecycle hooks allow developers to perform additional setup logic after a MongoDB connection or pool has been created or opened - for example, to enable [connection pooling in Vercel Functions](https://vercel.com/guides/connection-pooling-with-functions): ```ts export const databaseAdapter = mongooseAdapter({ // ... afterCreateConnection: async (adapter) => { const client = adapter.connection.getClient() attachDatabasePool(client); }, }) ```
1 parent fd7c94c commit af6ba86

File tree

3 files changed

+21
-4
lines changed

3 files changed

+21
-4
lines changed

packages/db-mongodb/src/connect.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,17 @@ export const connect: Connect = async function connect(
3737
try {
3838
if (!this.connection) {
3939
this.connection = await mongoose.createConnection(urlToConnect, connectionOptions).asPromise()
40+
if (this.afterCreateConnection) {
41+
await this.afterCreateConnection(this)
42+
}
4043
}
4144

4245
await this.connection.openUri(urlToConnect, connectionOptions)
4346

47+
if (this.afterOpenConnection) {
48+
await this.afterOpenConnection(this)
49+
}
50+
4451
if (this.useAlternativeDropDatabase) {
4552
if (this.connection.db) {
4653
// Firestore doesn't support dropDatabase, so we monkey patch
@@ -70,9 +77,7 @@ export const connect: Connect = async function connect(
7077
await new Promise((resolve) => setTimeout(resolve, 2000))
7178
}
7279

73-
const client = this.connection.getClient()
74-
75-
if (!client.options.replicaSet) {
80+
if (!this.connection.getClient().options.replicaSet) {
7681
this.transactionOptions = false
7782
this.beginTransaction = defaultBeginTransaction()
7883
}

packages/db-mongodb/src/index.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -67,6 +67,8 @@ import { upsert } from './upsert.js'
6767
export type { MigrateDownArgs, MigrateUpArgs } from './types.js'
6868

6969
export interface Args {
70+
afterCreateConnection?: (adapter: MongooseAdapter) => Promise<void> | void
71+
afterOpenConnection?: (adapter: MongooseAdapter) => Promise<void> | void
7072
/**
7173
* By default, Payload strips all additional keys from MongoDB data that don't exist
7274
* in the Payload schema. If you have some data that you want to include to the result
@@ -173,6 +175,8 @@ export interface Args {
173175
}
174176

175177
export type MongooseAdapter = {
178+
afterCreateConnection?: (adapter: MongooseAdapter) => Promise<void> | void
179+
afterOpenConnection?: (adapter: MongooseAdapter) => Promise<void> | void
176180
collections: {
177181
[slug: string]: CollectionModel
178182
}
@@ -236,6 +240,8 @@ declare module 'payload' {
236240
}
237241

238242
export function mongooseAdapter({
243+
afterCreateConnection,
244+
afterOpenConnection,
239245
allowAdditionalKeys = false,
240246
allowIDOnCreate = false,
241247
autoPluralization = true,
@@ -262,6 +268,8 @@ export function mongooseAdapter({
262268
name: 'mongoose',
263269

264270
// Mongoose-specific
271+
afterCreateConnection,
272+
afterOpenConnection,
265273
autoPluralization,
266274
collections: {},
267275
// @ts-expect-error initialize without a connection

packages/db-mongodb/src/init.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,11 +18,15 @@ import { buildSchema } from './models/buildSchema.js'
1818
import { getBuildQueryPlugin } from './queries/getBuildQueryPlugin.js'
1919
import { getDBName } from './utilities/getDBName.js'
2020

21-
export const init: Init = function init(this: MongooseAdapter) {
21+
export const init: Init = async function init(this: MongooseAdapter) {
2222
// Always create a scoped, **unopened** connection object
2323
// (no URI here; models compile per-connection and do not require an open socket)
2424
this.connection ??= mongoose.createConnection()
2525

26+
if (this.afterCreateConnection) {
27+
await this.afterCreateConnection(this)
28+
}
29+
2630
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
2731
const schemaOptions = this.collectionsSchemaOptions?.[collection.slug]
2832

0 commit comments

Comments
 (0)