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

Add Schema#alias() function that defines a new alias #12368

Closed
2 tasks done
IslandRhythms opened this issue Aug 31, 2022 · 2 comments
Closed
2 tasks done

Add Schema#alias() function that defines a new alias #12368

IslandRhythms opened this issue Aug 31, 2022 · 2 comments
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Milestone

Comments

@IslandRhythms
Copy link
Collaborator

IslandRhythms commented Aug 31, 2022

Prerequisites

  • I have written a descriptive issue title
  • I have searched existing issues to ensure the feature has not already been requested

🚀 Feature Proposal

When creating documents from api data, users will sometimes have their schemas be identical to the keys coming in. Therefore, I propose we create a function that does the mapping and returns the resultant object.

Motivation

It is a bit tedious to have to manually map the result of the api call to your documents when the keys are identical. We could possibly take this one step further and implement an option that converts the keys of the apiResult to match the ones defined in mongoose. So if an api is in snake case but your models are in camel case, the function would convert the keys to camel, then map.

Example

function apiMap(apiResult, schema) {
  const mongooseDoc = {};
  schema.keyName === apiResult.keyName
  mongooseDoc[schema.keyName] = apiResult.keyValue

  return mongooseDoc
}

const guts = apiMap(apiResult, schema)
await Model.create(guts);
@IslandRhythms IslandRhythms added new feature This change adds new functionality, like a new method or class enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature labels Aug 31, 2022
@IslandRhythms IslandRhythms changed the title Function that given the result of an api call and those keys are identical to the keys in mongoose, maps the api properties to the mongoose properties Function that given the result of an api call and those keys are identical to the keys in the mongoose model, maps the api properties to the mongoose properties Sep 1, 2022
@akshay-nm
Copy link

I'd like to see an example of where this could be used just out of curiosity.

@vkarpov15
Copy link
Collaborator

One approach is schema aliases. For example, here's how we convert GitHub API's avatar_url field into avatar from our schema definition: https://github.com/mongoosejs/backend/blob/7a40a525291dc2e284b78829b453b1ac59926385/src/db/subscriber.js#L8 .

We don't have good support for multiple aliases, but you can always write your own virtuals that support multiple aliases.

@vkarpov15 vkarpov15 changed the title Function that given the result of an api call and those keys are identical to the keys in the mongoose model, maps the api properties to the mongoose properties Add Schema#alias() function that defines a new alias Sep 26, 2022
@vkarpov15 vkarpov15 modified the milestones: 6.x Unprioritized, 6.7 Sep 26, 2022
vkarpov15 added a commit that referenced this issue Sep 30, 2022
vkarpov15 added a commit that referenced this issue Oct 5, 2022
feat(schema): add `alias()` method that makes it easier to define multiple aliases for a given path
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement This issue is a user-facing general improvement that doesn't fix a bug or add a new feature new feature This change adds new functionality, like a new method or class
Projects
None yet
Development

No branches or pull requests

3 participants