Skip to content

Commit 198763a

Browse files
javierlinkedr1tsuu
andauthored
feat(db-mongodb): allow to customize mongoose schema options with collectionsSchemaOptions (#9885)
Adds the ability to pass additional schema options for collections with: ```ts mongooseAdapter({ collectionsSchemaOptions: { posts: { strict: false, }, }, }) ``` This changes relates to these: - #4533 - #4534 It is a proposal to set custom schema options for mongoose driver. I understand this got introduced into `main` v2 after `beta` branch was created so this feature got lost. - [x] I have read and understand the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository. <!-- Thank you for the PR! Please go through the checklist below and make sure you've completed all the steps. Please review the [CONTRIBUTING.md](https://github.com/payloadcms/payload/blob/main/CONTRIBUTING.md) document in this repository if you haven't already. The following items will ensure that your PR is handled as smoothly as possible: - PR Title must follow conventional commits format. For example, `feat: my new feature`, `fix(plugin-seo): my fix`. - Minimal description explained as if explained to someone not immediately familiar with the code. - Provide before/after screenshots or code diffs if applicable. - Link any related issues/discussions from GitHub or Discord. - Add review comments if necessary to explain to the reviewer the logic behind a change ### What? ### Why? ### How? Fixes # --> --------- Co-authored-by: Sasha <64744993+r1tsuu@users.noreply.github.com>
1 parent b8de401 commit 198763a

File tree

3 files changed

+26
-11
lines changed

3 files changed

+26
-11
lines changed

docs/database/mongodb.mdx

Lines changed: 9 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,14 +30,15 @@ export default buildConfig({
3030

3131
## Options
3232

33-
| Option | Description |
34-
| -------------------- | ----------- |
35-
| `autoPluralization` | Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection `slug`s. |
36-
| `connectOptions` | Customize MongoDB connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the [options](https://mongoosejs.com/docs/connections.html#options) available to mongoose. |
37-
| `disableIndexHints` | Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination, as it increases the speed of the count function used in that query. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false |
38-
| `migrationDir` | Customize the directory that migrations are stored. |
39-
| `transactionOptions` | An object with configuration properties used in [transactions](https://www.mongodb.com/docs/manual/core/transactions/) or `false` which will disable the use of transactions. |
40-
| `collation` | Enable language-specific string comparison with customizable options. Available on MongoDB 3.4+. Defaults locale to "en". Example: `{ strength: 3 }`. For a full list of collation options and their definitions, see the [MongoDB documentation](https://www.mongodb.com/docs/manual/reference/collation/). |
33+
| Option | Description |
34+
| -------------------------- | ------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------ |
35+
| `autoPluralization` | Tell Mongoose to auto-pluralize any collection names if it encounters any singular words used as collection `slug`s. |
36+
| `connectOptions` | Customize MongoDB connection options. Payload will connect to your MongoDB database using default options which you can override and extend to include all the [options](https://mongoosejs.com/docs/connections.html#options) available to mongoose. |
37+
| `collectionsSchemaOptions` | Customize Mongoose schema options for collections. |
38+
| `disableIndexHints` | Set to true to disable hinting to MongoDB to use 'id' as index. This is currently done when counting documents for pagination, as it increases the speed of the count function used in that query. Disabling this optimization might fix some problems with AWS DocumentDB. Defaults to false |
39+
| `migrationDir` | Customize the directory that migrations are stored. |
40+
| `transactionOptions` | An object with configuration properties used in [transactions](https://www.mongodb.com/docs/manual/core/transactions/) or `false` which will disable the use of transactions. |
41+
| `collation` | Enable language-specific string comparison with customizable options. Available on MongoDB 3.4+. Defaults locale to "en". Example: `{ strength: 3 }`. For a full list of collation options and their definitions, see the [MongoDB documentation](https://www.mongodb.com/docs/manual/reference/collation/). |
4142

4243
## Access to Mongoose models
4344

packages/db-mongodb/src/index.ts

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,15 @@
11
import type { CollationOptions, TransactionOptions } from 'mongodb'
22
import type { MongoMemoryReplSet } from 'mongodb-memory-server'
3-
import type { ClientSession, Connection, ConnectOptions, QueryOptions } from 'mongoose'
3+
import type {
4+
ClientSession,
5+
Connection,
6+
ConnectOptions,
7+
QueryOptions,
8+
SchemaOptions,
9+
} from 'mongoose'
410
import type {
511
BaseDatabaseAdapter,
12+
CollectionSlug,
613
DatabaseAdapterObj,
714
Payload,
815
TypeWithID,
@@ -79,12 +86,13 @@ export interface Args {
7986
* Defaults to disabled.
8087
*/
8188
collation?: Omit<CollationOptions, 'locale'>
89+
collectionsSchemaOptions?: Partial<Record<CollectionSlug, SchemaOptions>>
90+
8291
/** Extra configuration options */
8392
connectOptions?: {
8493
/** Set false to disable $facet aggregation in non-supporting databases, Defaults to true */
8594
useFacet?: boolean
8695
} & ConnectOptions
87-
8896
/** 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 */
8997
disableIndexHints?: boolean
9098
/**
@@ -103,6 +111,7 @@ export interface Args {
103111
up: (args: MigrateUpArgs) => Promise<void>
104112
}[]
105113
transactionOptions?: false | TransactionOptions
114+
106115
/** The URL to connect to MongoDB or false to start payload and prevent connecting */
107116
url: false | string
108117
}
@@ -163,6 +172,7 @@ declare module 'payload' {
163172

164173
export function mongooseAdapter({
165174
autoPluralization = true,
175+
collectionsSchemaOptions = {},
166176
connectOptions,
167177
disableIndexHints = false,
168178
ensureIndexes,
@@ -194,6 +204,7 @@ export function mongooseAdapter({
194204
versions: {},
195205
// DatabaseAdapter
196206
beginTransaction: transactionOptions === false ? defaultBeginTransaction() : beginTransaction,
207+
collectionsSchemaOptions,
197208
commitTransaction,
198209
connect,
199210
count,

packages/db-mongodb/src/init.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,9 @@ import { getDBName } from './utilities/getDBName.js'
1717

1818
export const init: Init = function init(this: MongooseAdapter) {
1919
this.payload.config.collections.forEach((collection: SanitizedCollectionConfig) => {
20-
const schema = buildCollectionSchema(collection, this.payload)
20+
const schemaOptions = this.collectionsSchemaOptions[collection.slug]
21+
22+
const schema = buildCollectionSchema(collection, this.payload, schemaOptions)
2123

2224
if (collection.versions) {
2325
const versionModelName = getDBName({ config: collection, versions: true })
@@ -32,6 +34,7 @@ export const init: Init = function init(this: MongooseAdapter) {
3234
minimize: false,
3335
timestamps: false,
3436
},
37+
...schemaOptions,
3538
})
3639

3740
versionSchema.plugin<any, PaginateOptions>(paginate, { useEstimatedCount: true }).plugin(

0 commit comments

Comments
 (0)