Skip to content

Commit 09c41d5

Browse files
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: #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>
1 parent def595e commit 09c41d5

File tree

1 file changed

+14
-12
lines changed

1 file changed

+14
-12
lines changed

packages/db-mongodb/src/init.ts

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,25 +45,28 @@ export const init: Init = function init(this: MongooseAdapter) {
4545
versionSchema.plugin(mongooseAggregatePaginate)
4646
}
4747

48-
const model = mongoose.model(
48+
const versionCollectionName =
49+
this.autoPluralization === true && !collection.dbName ? undefined : versionModelName
50+
51+
this.versions[collection.slug] = mongoose.model(
4952
versionModelName,
5053
versionSchema,
51-
this.autoPluralization === true ? undefined : versionModelName,
54+
versionCollectionName,
5255
) as CollectionModel
53-
54-
this.versions[collection.slug] = model
5556
}
5657

57-
const model = mongoose.model(
58-
getDBName({ config: collection }),
58+
const modelName = getDBName({ config: collection })
59+
const collectionName =
60+
this.autoPluralization === true && !collection.dbName ? undefined : modelName
61+
62+
this.collections[collection.slug] = mongoose.model(
63+
modelName,
5964
schema,
60-
this.autoPluralization === true ? undefined : collection.slug,
65+
collectionName,
6166
) as CollectionModel
62-
this.collections[collection.slug] = model
6367
})
6468

65-
const model = buildGlobalModel(this.payload.config)
66-
this.globals = model
69+
this.globals = buildGlobalModel(this.payload.config)
6770

6871
this.payload.config.globals.forEach((global) => {
6972
if (global.versions) {
@@ -85,12 +88,11 @@ export const init: Init = function init(this: MongooseAdapter) {
8588
.plugin<any, PaginateOptions>(paginate, { useEstimatedCount: true })
8689
.plugin(getBuildQueryPlugin({ versionsFields: versionGlobalFields }))
8790

88-
const versionsModel = mongoose.model(
91+
this.versions[global.slug] = mongoose.model(
8992
versionModelName,
9093
versionSchema,
9194
versionModelName,
9295
) as CollectionModel
93-
this.versions[global.slug] = versionsModel
9496
}
9597
})
9698
}

0 commit comments

Comments
 (0)