fix(db-mongodb)!: use dbName for mongodb model (#9107)

### What?
Uses the `collection.dbName` property for the Mongoose model, if
defined.

### Why?
Currently, `collection.dbName` is used for the version name but not for
the actual collection name. Additionally, `autoPluralization` modifies
the `dbName` regardless. This behavior is inconsistent and contradicts
the documentation.

### How?
- Utilize `collection.dbName` instead of `collection.slug`.
- Disable `autoPluralization` for collections with a defined `dbName`.

Related: https://github.com/payloadcms/payload/discussions/9058

**BREAKING CHANGES**
If a `dbName` was previously provided, it will now be used as the
MongoDB collection name instead of the collection `slug`.
`autoPluralization` will not be applied to `dbName`.

---------

Co-authored-by: Dan Ribbens <dan.ribbens@gmail.com>
This commit is contained in:
Tobias Odendahl
2024-11-12 19:31:23 +01:00
committed by GitHub
parent def595e645
commit 09c41d5c86

View File

@@ -45,25 +45,28 @@ export const init: Init = function init(this: MongooseAdapter) {
versionSchema.plugin(mongooseAggregatePaginate)
}
const model = mongoose.model(
const versionCollectionName =
this.autoPluralization === true && !collection.dbName ? undefined : versionModelName
this.versions[collection.slug] = mongoose.model(
versionModelName,
versionSchema,
this.autoPluralization === true ? undefined : versionModelName,
versionCollectionName,
) as CollectionModel
this.versions[collection.slug] = model
}
const model = mongoose.model(
getDBName({ config: collection }),
const modelName = getDBName({ config: collection })
const collectionName =
this.autoPluralization === true && !collection.dbName ? undefined : modelName
this.collections[collection.slug] = mongoose.model(
modelName,
schema,
this.autoPluralization === true ? undefined : collection.slug,
collectionName,
) as CollectionModel
this.collections[collection.slug] = model
})
const model = buildGlobalModel(this.payload.config)
this.globals = model
this.globals = buildGlobalModel(this.payload.config)
this.payload.config.globals.forEach((global) => {
if (global.versions) {
@@ -85,12 +88,11 @@ export const init: Init = function init(this: MongooseAdapter) {
.plugin<any, PaginateOptions>(paginate, { useEstimatedCount: true })
.plugin(getBuildQueryPlugin({ versionsFields: versionGlobalFields }))
const versionsModel = mongoose.model(
this.versions[global.slug] = mongoose.model(
versionModelName,
versionSchema,
versionModelName,
) as CollectionModel
this.versions[global.slug] = versionsModel
}
})
}