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 support for Embedded column name #5361

Open
maphe opened this issue Mar 19, 2024 · 5 comments
Open

Add support for Embedded column name #5361

maphe opened this issue Mar 19, 2024 · 5 comments
Labels
enhancement New feature or request

Comments

@maphe
Copy link

maphe commented Mar 19, 2024

Is your feature request related to a problem? Please describe.
Currently it is possible to define the name of a column to be different from the name of the ts/js field

  @Property({ name: 'n' })
  name!: string;

It is also possible to define an embedded object

  @Embedded(() => Profile, { object: true, nullable: true })
  profile?: Profile;

but it doesn't seem to be possible to combine both

Describe the solution you'd like
Would be nice to be able to do something like

  @Embedded(() => Profile, { object: true, nullable: true, name: 'p' })
  profile?: Profile;

Describe alternatives you've considered
The only alternative is to have the attribute name match the db name.

Additional context
This might be more of a mongoDB use case where nested objects are fairly common and it's not unusual to shorten the db field name for storage optimization, but still want the application name to be verbose to keep the code understandable.

But Im sure it can also have some SQL use cases...

@maphe maphe added the enhancement New feature or request label Mar 19, 2024
@B4nan
Copy link
Member

B4nan commented Mar 20, 2024

Hmm I would say this should be already working.

@maphe
Copy link
Author

maphe commented Mar 20, 2024

Maybe I'm not looking at the right place but this is the definition for EmbeddedOptions:

export type EmbeddedOptions = {
    entity?: string | (() => AnyEntity | AnyEntity[]);
    type?: string;
    prefix?: string | boolean;
    nullable?: boolean;
    object?: boolean;
    array?: boolean;
    hidden?: boolean;
    serializer?: (value: any) => any;
    serializedName?: string;
};

in node_modules/@mikro-orm/core/decorators/Embedded.d.ts (I'm using "@mikro-orm/core": "^6.1.11").

It doesn't seem to have a name property available.

@B4nan
Copy link
Member

B4nan commented Mar 20, 2024

Ok, but that might be just a type-level issue, have you tried to put it in (and cast the options as any)?

@maphe
Copy link
Author

maphe commented Mar 20, 2024

I just gave it a try.

On insert, it skips the field, meaning the document created in mongoDB doesn't have the data, neither with the class attribute name nor the decorator config name.

On select, it throws an error TypeError: Cannot read properties of undefined (reading 'object') (at node_modules/@mikro-orm/core/utils/EntityComparator.js:266:88).

@RossComputerGuy
Copy link

@maphe I got the same error as well but with node_modules/@mikro-orm/core/metadata/MetadataValidator.js:17

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement New feature or request
Projects
None yet
Development

No branches or pull requests

3 participants