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

Issue with "createFunction" when using embeddable property with hyphens #1958

Closed
ryankauk opened this issue Jun 22, 2021 · 7 comments
Closed
Assignees
Labels
bug Something isn't working
Milestone

Comments

@ryankauk
Copy link

ryankauk commented Jun 22, 2021

Describe the bug

This issues seems to be an issue with the create function. Maybe it could use bracket notation instead of dot notation? Please let me know if there's any work arounds that you can think of. I'm unable to change the name of the property in my situation. Not sure if this is specific to embeddable or entities as a whole.

Stack trace

SyntaxError: Unexpected token 'with'
        at new Function (<anonymous>)

      at Function.createFunction (node_modules/@mikro-orm/core/utils/Utils.js:611:52)

  console.error
    return function(entity) {
      const ret = {};
      if ('id' in entity && entity.id != null) {
        ret.id = entity.id;
      }
    
      if ('name' in entity) {
        ret.name = entity.name;
      }
    
      if (entity.sub != null) {
        ret.sub_some-prop-with-hyphens = clone(entity.sub.some-prop-with-hyphens);
      }
    
      return ret;
    }

      at Function.createFunction (node_modules/@mikro-orm/core/utils/Utils.js:615:21)

To Reproduce
https://github.com/ryankauk/mikro-jest-global-setup/blob/master/src/index.spec.ts

test case "test embedded model with property and hyphens"

Expected behavior
I expect that you should be able to use hyphens in property name.

Additional context

Versions

Dependency Version
node v12.16.3
typescript ^4.1.3
mikro-orm 4.5.6
your-driver mongo
@B4nan
Copy link
Member

B4nan commented Jun 23, 2021

To Reproduce ryankauk/mikro-jest-global-setup@e2f63b2/src/index.spec.ts

That link is invalid, looks like the repo is not even there?

I was fixing pretty much the same problem some time ago, it might as well be already fixed in master (v5) or in #1948.

Or it is just a wrong entity definition. I'd say the property names itself cannot even contain hyphens (as that is not allowed at JS/TS level). What you can do is to have a fieldName with hyphen, but not a property name.

@ryankauk
Copy link
Author

ryankauk commented Jun 23, 2021

Sorry updated the link and made my repo public. I tried the fieldName to be using hyphens but it was the same issue.

@B4nan
Copy link
Member

B4nan commented Jun 23, 2021

Wow, never thought this is actually valid property definition. So you have tried this, right?

@Embeddable()
export class SubModel {

  @Property({ name: 'some-prop-with-hyphens', nullable: true })
  somePropWithHyphens?: string;

}

@ryankauk
Copy link
Author

Yea tried that and it ended up with the same error

@ryankauk
Copy link
Author

So I did a bit more testing, the above you mentioned does work, I realized my real use case is that the property I'm trying to use hyphens with is using @Embedded , which does not have a fieldName option, I found a workaround in the meantime that I just introduced a function on my model that does the mapping

@B4nan
Copy link
Member

B4nan commented Jun 28, 2021

So you have object embeddables, right? It should actually work to pass the fieldName there too, you will just need to type cast the options.

@B4nan B4nan added the bug Something isn't working label Aug 26, 2021
@B4nan B4nan added this to the 5.0 milestone Aug 26, 2021
@secato
Copy link

secato commented Aug 26, 2021

So I did a bit more testing, the above you mentioned does work, I realized my real use case is that the property I'm trying to use hyphens with is using @Embedded , which does not have a fieldName option, I found a workaround in the meantime that I just introduced a function on my model that does the mapping

Hey, how did you solved your issue?

@B4nan B4nan closed this as completed in cc68230 Aug 28, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

3 participants