kosiakMD
kosiakMD

Reputation: 1062

Why TypeOrm advises to create empty object via `new EntityClass()` THEN only pass fields value?

I'm using NestJS (not Next) NodeJS framework

When I'm creating new objects I used to use new OjbectClass({...fieldsValues});

It's great especially when you use transform pipes from class-transformer;

Besides this approach is used for entity creating: https://docs.nestjs.com/techniques/database#separating-entity-definition

But as far I see in different guides of TypeOrm usage here: https://typeorm.io/#/ , and here: https://orkhan.gitbook.io/typeorm/docs/entities . They show first to create an empty object, then only set fields with values:

const object = new EntityObject();
object.field = 'value';

Why? Does it make sense? Does NodeJS create a redundant hidden class of properties passed via object into Entity Class constructor? If yes - then we can pass coma-separated arguments

Upvotes: 1

Views: 1415

Answers (1)

Jay McDoniel
Jay McDoniel

Reputation: 70141

I believe it's just cause that's how the docs are. Looking at the code for BaseEntity it does not look like having a constructor to assign the fields would be a problem

Upvotes: 1

Related Questions