docs: add in an example for 4526 (#9538)

* docs: add in an example for 4526

* small fixes

Co-authored-by: Umed Khudoiberdiev <pleerock.me@gmail.com>
This commit is contained in:
Charlton Austin 2022-12-03 06:31:32 -06:00 committed by GitHub
parent 658604d0ae
commit d71e9c4394
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 43 additions and 2 deletions

View File

@ -57,8 +57,8 @@ Install all TypeORM dependencies by running this command:
npm install
```
During installation you may have some problems with some dependencies.
For example to proper install oracle driver you need to follow all instructions from
During installation, you may have some problems with some dependencies.
For example to properly install oracle driver you need to follow all instructions from
[node-oracle documentation](https://github.com/oracle/node-oracledb).
## ORM config

View File

@ -5,6 +5,7 @@
- [How to load relations in entities](#how-to-load-relations-in-entities)
- [Avoid relation property initializers](#avoid-relation-property-initializers)
- [Avoid foreign key constraint creation](#avoid-foreign-key-constraint-creation)
- [Avoid circular import errors](#avoid-circular-import-errors)
## How to create self referencing relation
@ -262,3 +263,43 @@ export class ActionLog {
person: Person
}
```
## Avoid circular import errors
Here is an example if you want to define your entities, and you don't want those to cause errors in some environments.
In this situation we have Action.ts and Person.ts importing each other for a many-to-many relationship.
We use import type so that we can use the type information without any JavaScript code being generated.
```typescript
import { Entity, PrimaryColumn, Column, ManytoMany } from "typeorm"
import type { Person } from "./Person"
@Entity()
export class ActionLog {
@PrimaryColumn()
id: number
@Column()
date: Date
@Column()
action: string
@ManyToMany("Person", "id")
person: Person
}
```
```typescript
import { Entity, PrimaryColumn, ManytoMany } from "typeorm"
import type { ActionLog } from "./Action"
@Entity()
export class Person {
@PrimaryColumn()
id: number
@ManyToMany("ActionLog", "id")
log: ActionLog
}
```