feat(db-mongodb): allow custom id field types#15522
Open
MurzNN wants to merge 1 commit intopayloadcms:mainfrom
Open
feat(db-mongodb): allow custom id field types#15522MurzNN wants to merge 1 commit intopayloadcms:mainfrom
MurzNN wants to merge 1 commit intopayloadcms:mainfrom
Conversation
6efd586 to
da12084
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Allows setting the custom ID field type in the Mongoose adapter by adding a new argument
idType: mongooseIDTypeand passing it to the functionbuildSchema().No test coverage yet, cuz I'm pretty new with the Node.js tests and Payload contributions.
Why?
By default, Payload CMS creates docs using the MongoDB ObjectID as the ID value. But pretty often, the Payload's document IDs are reused in other database types, where the MongoDB's ObjectID value is not native and unclear.
The pretty popular ID value type in most modern databases is UUID, so for some projects, it would be much more convenient to use UUID in all Payload Collections by default.
Yes, we already can customize the ID field, according to the docs https://payloadcms.com/docs/fields/overview#custom-id-fields, but it should be done manually in each collection, so much better to allow choosing the default ID field type for the whole project.
And one more significant limitation of the current Payload Custom ID Field API is that even if we generate the UUID value in JS, it will be stored as a string, wasting 32-36 bytes per id and decreasing the database performance, instead of using the 16-byte binary storage.
This resolves the missing UUID type for MongoDB issue discussed in #13054
How?
idType: mongooseIDTypeto the functionmongooseAdapter().buildSchema()._idfield schema in the collections.