Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(schema): add alias() method that makes it easier to define multiple aliases for a given path #12491

Merged
merged 8 commits into from Oct 5, 2022

Conversation

vkarpov15
Copy link
Collaborator

Fix #12368

Summary

One approach we recommend for renaming fields from API requests is using aliases. Here's an example of using an alias to convert the GitHub API's avatar_url response to avatar in Mongoose. But right now there's no way to define multiple aliases for a single field. This PR adds a new schema method, so you can do something like schema.alias('avatar', ['avatar_url', 'photo']).

Examples

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, with some minor details

changes to the types still need to be done

lib/schema.js Outdated Show resolved Hide resolved
lib/schema.js Outdated Show resolved Hide resolved
Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice!

Would also be great if we make the API support arrays of strings as follows:

const schema = new Schema({
  avatar: { type: String, alias: ['avatarURL', 'avatarImageURL'] }
});

Also, would be nice if we could have TS types and tests for those changes.

lib/schema.js Outdated Show resolved Hide resolved
@hasezoey
Copy link
Collaborator

hasezoey commented Oct 4, 2022

also just to be sure, where is checked that a alias cannot be a special name like __proto__/prototype?

Copy link
Collaborator

@AbdelrahmanHafez AbdelrahmanHafez left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM, thanks 👍

Copy link
Collaborator

@hasezoey hasezoey left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM

@vkarpov15 vkarpov15 merged commit b8f4cb4 into 6.7 Oct 5, 2022
@vkarpov15 vkarpov15 deleted the vkarpov15/gh-12368 branch October 5, 2022 21:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants