Inheritance with Typeorm

Asked

Viewed 263 times

-1

There are common problems that I see a lot of difficulty to deal with Orms, and ignoring a native way I decided to ask what is the best way to deal with these using Typeorm in project. Today I have a project that deals with generalization and person specialization, basically I have a table Pessoa where I store the id or enrollment, I create relationships with phone numbers, emails and addresses, and I have two other tables that inherit the primary key of the person who is the pessoa_juridica and pessoa_fisica, bearing in mind that my application works with these two types of people. The problem is how I should implement this using an ORM like Typeorm?

To make the model more explicit:

DER representando exemplo citado

in the image we have:

  1. A relationship 1-N table pessoa for table telefone
  2. A relationship 1-N table pessoa for table email
  3. A relationship 1-1 table pessoa for table pessoa_fisica
  4. A relationship 1-1 table pessoa for table pessoa_juridica

The question there is also in, a natural person can not be a legal person at the same time and vice versa.

  • 1

    although it has an objective point in your question, part of it can be based on opinions, and honestly an entity with 1 attribute does not make sense, in my humble opinion ok? could take the attribute "name" there, and remove the "name" and "name_fantasi", that would make sense. Now "a natural person cannot be a legal person at the same time" this is solved by the right model itself? a PF has no CNPJ or Registration, just as a PJ has no CPF or RG. But already researched on the @TableInheritance and @ChildEntity? They should solve the implementation problem of this model

  • This Ricardo, I’m actually setting up an api for an existing bank and it’s modeled on this vein, so I gave the example, basically the entity person only keeps the ids, and the entities of physical person and person Urica inherit these ids as pk, other entities such as telephone and email for example related person, so daughter entities inherit these relationships

1 answer

0

Very simple example, I’ll send 1-N Phone and 1-1 PJ and just replicate add the other relationships. I hope it’s simple to understand

@Entity()
export class Pessoa {
 @PrimaryGeneratedColumn()
 id: number

 @OneToMany(() => Telefone, telefone => telefone.pessoa)
 telefones: Telefone[]
}


@Entity()
export class Telefone {
 @PrimaryGeneratedColumn()
 id: number

 @ManyToOne(() => Pessoa, pessoa => pessoa.telefones)
 pessoa: Pessoa
}


@Entity()
export class PJ {
 @PrimaryGeneratedColumn()
 id: number

 @OneToOne(() => Pessoa)
 @JoinColumn()
 pessoa: Pessoa
}

PS: Import the decorators from Typeorm and also every Entity you use. If you use Pessoa on PJ, import Entity Pessoa into the PJ file.

Doc: https://typeorm.io/#/Relations

  • My question, William, is that using common relationships, we interpret that Person has a Physical Person and not that Physical Person is a person, you understand? The whole question is based there, so I wanted to know how Typeorm relates to generalization and specialization in the database.

  • Just to complement, the child entities Physical Person and Legal Person have need to inherit the Person id

Browser other questions tagged

You are not signed in. Login or sign up in order to post.