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

Question regarding EntitySchema and Entity functions #512

Closed
njeirath opened this issue Apr 24, 2020 · 3 comments
Closed

Question regarding EntitySchema and Entity functions #512

njeirath opened this issue Apr 24, 2020 · 3 comments

Comments

@njeirath
Copy link

I had a question regarding the use of EntitySchema and how to properly handle functions:

import {EntitySchema} from 'mikro-orm'

export class User {
    id: string
    email: string
    agreedToTerms: Date

    constructor(id: string, email: string) {
        this.id = id
        this.email = email
    }

    agreeToTerms(): void {
        this.agreedToTerms = new Date()
    }

export const schema = new EntitySchema<User>({
    path: __filename,
    name: 'User',
    class: User,
    tableName: 'person',
    properties: {
        id: {type: 'string', primary: true, name: 'cognito_id'},
        email: {type: 'string'},
        agreedToTerms: {type: 'datetime', nullable: true},
        agreeToTerms: {persist: false, hidden: true, entity: ''},
    },
})

Through some trial and error I finally found that adding the agreeToTerms: {persist: false, hidden: true, entity: ''}, line to properties got the compiler to stop complaining but I was having trouble finding documentation covering what exactly that line does, especially the entity field. What I was curious about is if I was doing this correctly or if there is a different way to tell the ORM to ignore my function?

Additionally, I had another function on my User class that looks something like:

    updateFromUserData(userData: UserData): void {
        //Code here to update the user based on UserData class
    }

Where UserData is not an Entity but a POJO provided from some external service.

Again I had added updateFromUserData: {persist: false, hidden: true, entity: ''} to the EntitySchema properties and that all worked as expected however when I attempted to generate a migration I get a Cannot find name 'UserData'. (2304) so I suspect I'm missing something to tell the migration:create to ignore that function/parameter.

@B4nan
Copy link
Member

B4nan commented Apr 24, 2020

Hmm might be a bug in typings. You should not define methods there at all.

You could use the second type argument to ignore them, that is used for base entities, and properties from there are ignored.

@njeirath
Copy link
Author

Interesting, I figured the methods were included as a way to support virtual properties but it'd make more sense to me for them to not be required by default.

You could use the second type argument to ignore them, that is used for base entities, and properties from there are ignored.

I'm not 100% sure what you mean here.

@B4nan
Copy link
Member

B4nan commented Apr 25, 2020

This is what I mean:

export const schema = new EntitySchema<User, { agreeToTerms: any }>({
  class: User,
  tableName: 'person',
  properties: {
    id: { type: 'string', primary: true, name: 'cognito_id' },
    email: { type: 'string' },
    agreedToTerms: { type: 'datetime', nullable: true },
  },
});

Note I also removed name as you don't need that if you provide class. path is also not needed as it is used only for caching and caching is disabled when you use EntitySchema.

@B4nan B4nan closed this as completed in 58c9643 Apr 25, 2020
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

No branches or pull requests

2 participants