I am working on a NestJS app using TypeORM, and have a Person
entity and a Business
entity. Each of those entities can have 0 or more Address
es. The address fields are the same whether it's for a Business
or Person
. Because of that, I'd like to put all addresses in one table, instead of managing a PersonAddress
and a BusinessAddress
table (provided this is the "proper" way to design my database).
Currently, here is part of my Person
and Address
entities:
// person.entity.ts
export class Person {
@OneToMany(() => Address, (address) => address.person, { cascade: true})
addresses: PersonAddress[]
}
// address.entity.ts
export class Address {
@ManyToOne(() => Person, (person) => person.addresses)
person: Person
}
I believe this is the proper way to do this, for TypeORM, but I'd like to be able to remove the reference in Address
to Person
so that I can use that same entity for Business
.
Now, my question is two parts:
- Is my desired design the proper way to design a database? I have experience using databases and querying them, but not designing them. If what I'm trying to do is poor database design, then I'll continue with having a separate entity for the address for
Business
andPerson
. - If my desired design is correct, how can I define the entities in TypeORM to achieve this outcome?