Skip to content

Commit 6bb4067

Browse files
authored
feat: adds option to mongoose to ensure indexes (#9155)
Adds option `ensureIndexes` to Mongoose adapter, which will ensure indexes are ready prior to completing connection.
1 parent a3ebf51 commit 6bb4067

File tree

3 files changed

+26
-0
lines changed

3 files changed

+26
-0
lines changed

packages/db-mongodb/src/connect.ts

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,21 @@ export const connect: Connect = async function connect(
5757
}
5858
}
5959

60+
if (this.ensureIndexes) {
61+
await Promise.all(
62+
this.payload.config.collections.map(async (coll) => {
63+
await new Promise((resolve, reject) => {
64+
this.collections[coll.slug]?.ensureIndexes(function (err) {
65+
if (err) {
66+
reject(err)
67+
}
68+
resolve(true)
69+
})
70+
})
71+
}),
72+
)
73+
}
74+
6075
if (process.env.NODE_ENV === 'production' && this.prodMigrations) {
6176
await this.migrate({ migrations: this.prodMigrations })
6277
}

packages/db-mongodb/src/index.ts

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,8 +74,14 @@ export interface Args {
7474
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */
7575
useFacet?: boolean
7676
} & ConnectOptions
77+
7778
/** Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false */
7879
disableIndexHints?: boolean
80+
/**
81+
* Set to `true` to ensure that indexes are ready before completing connection.
82+
* NOTE: not recommended for production. This can slow down the initialization of Payload.
83+
*/
84+
ensureIndexes?: boolean
7985
migrationDir?: string
8086
/**
8187
* typed as any to avoid dependency
@@ -96,6 +102,7 @@ export type MongooseAdapter = {
96102
[slug: string]: CollectionModel
97103
}
98104
connection: Connection
105+
ensureIndexes: boolean
99106
globals: GlobalModel
100107
mongoMemoryServer: MongoMemoryReplSet
101108
prodMigrations?: {
@@ -118,6 +125,7 @@ declare module 'payload' {
118125
[slug: string]: CollectionModel
119126
}
120127
connection: Connection
128+
ensureIndexes: boolean
121129
globals: GlobalModel
122130
mongoMemoryServer: MongoMemoryReplSet
123131
prodMigrations?: {
@@ -138,6 +146,7 @@ export function mongooseAdapter({
138146
autoPluralization = true,
139147
connectOptions,
140148
disableIndexHints = false,
149+
ensureIndexes,
141150
migrationDir: migrationDirArg,
142151
mongoMemoryServer,
143152
prodMigrations,
@@ -157,6 +166,7 @@ export function mongooseAdapter({
157166
connection: undefined,
158167
connectOptions: connectOptions || {},
159168
disableIndexHints,
169+
ensureIndexes,
160170
globals: undefined,
161171
mongoMemoryServer,
162172
sessions: {},

test/generateDatabaseAdapter.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@ export const allDatabaseAdapters = {
1010
import { mongooseAdapter } from '@payloadcms/db-mongodb'
1111
1212
export const databaseAdapter = mongooseAdapter({
13+
ensureIndexes: true,
1314
url:
1415
process.env.MONGODB_MEMORY_SERVER_URI ||
1516
process.env.DATABASE_URI ||

0 commit comments

Comments
 (0)