Skip to content

Commit d03fc8a

Browse files
committed
feat: added addition readme info
1 parent 8b80bff commit d03fc8a

File tree

1 file changed

+81
-9
lines changed

1 file changed

+81
-9
lines changed

README.md

+81-9
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,8 @@
33

44
A package for polymorphic relationships for typeorm
55

6+
> Experiemental repository
7+
68
## Install
79

810
```bash
@@ -59,15 +61,6 @@ export class MerchantEntity {
5961
@Entity('adverts')
6062
export class AdvertEntity implements PolymorphicChildInterface {
6163
@PolymorphicParent(() => [UserEntity, MerchantEntity])
62-
@Transform(
63-
(value: UserEntity | MerchantEntity) => ({
64-
...value,
65-
type: value.constructor.name,
66-
}),
67-
{
68-
toPlainOnly: true,
69-
},
70-
)
7164
owner: UserEntity | MerchantEntity;
7265

7366
@Column()
@@ -91,6 +84,85 @@ id | entityId | entityType
9184
3 | 2 | 'UserEntity'
9285
```
9386

87+
88+
## Methods
89+
90+
The majority of these methods overwrite the typeorm's `Repository` class methods to ensure polymorph relationships are handled before/after the parent's method.
91+
92+
### save
93+
94+
#### Child
95+
96+
```ts
97+
const repository = connection.getRepository(AdvertRepository); // That extends AbstractPolymorphicRepository
98+
99+
const advert = new AdvertEntity();
100+
advert.owner = user;
101+
102+
await repository.save(advert);
103+
```
104+
105+
#### Parent
106+
107+
```ts
108+
const repository = connection.getRepository(MerchantRepository); // That extends AbstractPolymorphicRepository
109+
110+
const advert = new AdvertEntity();
111+
112+
const parent = new MerchantEntity();
113+
merchant.adverts= [advert];
114+
115+
await repository.save(merchant);
116+
```
117+
118+
### find
119+
```ts
120+
const repository = connection.getRepository(MerchantRepository); // That extends AbstractPolymorphicRepository
121+
122+
const results = await repository.find();
123+
124+
// results[0].adverts === AdvertEntity[]
125+
```
126+
### findOne
127+
128+
### hydrateMany
129+
130+
### hydrateOne
131+
132+
### create
133+
134+
135+
## Class-transformer
136+
137+
We recommend if you're working with polymorphic relationships that you use `class-transformers` `Transform` decorator to distinguish the different types on the frontend when returning your entities from a http call
138+
139+
```ts
140+
@Entity('adverts')
141+
export class AdvertEntity implements PolymorphicChildInterface {
142+
@PolymorphicParent(() => [UserEntity, MerchantEntity])
143+
@Transform(
144+
(value: UserEntity | MerchantEntity) => ({
145+
...value,
146+
type: value.constructor.name,
147+
}),
148+
{
149+
toPlainOnly: true,
150+
},
151+
)
152+
owner: UserEntity | MerchantEntity;
153+
154+
@Column()
155+
entityId: number;
156+
157+
@Column()
158+
entityType: string;
159+
}
160+
```
161+
162+
The owner property object's type property will now either be string value of `UserEntity` or `MerchantEntity`
163+
164+
## Notes
165+
94166
I think [Perf](https://github.com/Perf) might have some suggestions on how to improve things (sorry I have replied been mega busy!)
95167

96168
I've also used the class-transformer package so that my response objects have a different type value depending on the entityType. Could use the field tbh

0 commit comments

Comments
 (0)