Skip to content

Skip constructor when entity is loaded from database - #3845

Merged
pleerock merged 1 commit into
typeorm:nextfrom
XanderEmu:skip-constructor-when-entity-is-loaded-from-the-database
Mar 24, 2019
Merged

pleerock merged 1 commit into
typeorm:nextfrom
XanderEmu:skip-constructor-when-entity-is-loaded-from-the-database

Conversation

@XanderEmu

@XanderEmu XanderEmu commented Mar 19, 2019 •

Copy link
Copy Markdown

I basically copied the code submitted by @pepakriz into a new branch created against next, only I used JS's Reflect instead of ObjectInstantiator).

Original PR: #1773

@pleerock

Copy link
Copy Markdown
Member

Thank you!

@pepakriz

Copy link
Copy Markdown
Contributor

Wow! Thank you too

@vlapo

vlapo commented Mar 27, 2019

Copy link
Copy Markdown
Contributor

What about documentation? We have a note in docs about optionality of constructor parameter (https://typeorm.io/#/entities). It should be updated in next branch too.

@XanderEmu
XanderEmu deleted the skip-constructor-when-entity-is-loaded-from-the-database branch March 28, 2019 15:12
@pleerock

Copy link
Copy Markdown
Member

I just found one issue with this change. Right now property initializations won't work with this change:

export class Post {
  @Column()
  isLoaded = false
}

we don't get isLoaded initialized. I'm not yet sure about this behaviour, but it looks right too. But I would like to get your thoughts about it. Is it something expected now? Adding @nicomouss too.

@pepakriz

Copy link
Copy Markdown
Contributor

@pleerock It's expected in my opinion

@nicomouss

Copy link
Copy Markdown

Same here, it's expected as constructor is skipped when entities are built from the ORM. Thus properties initialisations are also skipped.

@Kononnable

Kononnable commented Apr 7, 2019 •

Copy link
Copy Markdown
Contributor

Right now 3 tests in @next are broken because of this change:
/functional/entity-metadata-validator/initialized-relations
/functional/repository/basic-methods
/github-issues/3256
We also have to change Avoid relation property initializers in relations-faq since it's outdated after merging this change. If we don't initialize properties on creation we don't need InitializedRelationError anymore.
But support for old method was partially introduced in #3910 so maybe we should just update those error/tests/docs(otherwise delete all/parts of it).

@pkuczynski

Copy link
Copy Markdown
Member

Any prospects when this would be released?

@pleerock

Copy link
Copy Markdown
Member

@pkuczynski its already released

@pkuczynski

pkuczynski commented May 15, 2019 •

Copy link
Copy Markdown
Member

Hmm, this is strange and does not seem to work for ActiveRecord. My example:

export class Car extends BaseEntity {
  @PrimaryGeneratedColumn()
  id: number

  registration: string

  constructor() {
    console.log('Constructor has been called')
  }
}

And when I call Car.findOne(1) I can see in the console:

Constructor has been called

I thought this PR would prevent constructor from being called? I am using version 0.2.17

@Kononnable

Kononnable commented May 15, 2019 •

Copy link
Copy Markdown
Contributor

This change is only available for 0.3.x version. It's released under @next tag.

@pkuczynski

Copy link
Copy Markdown
Member

0.3 is still in alpha, so a bit risky to use in production, right?

@Kononnable

Copy link
Copy Markdown
Contributor

I believe some folks use @next in production environments. I think is preety stable right now, but it would be best to ask people who actually use it daily to tell if they think it's usable in production.
We(as typeorm) will move to 0.3 soon, but there is still some work which need to be done and we don't have much free time to do it, so no even approximate release date yet.

@pkuczynski

Copy link
Copy Markdown
Member

Got it! Thanks for the detailed explanation. Do you have any high-level description of changes introduced by 3.0?

@Kononnable

Copy link
Copy Markdown
Contributor

0.3.0 wasn't released, so there is no migration guide or similiar documents. You can check changelog - some of the changes are pointed out there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

7 participants