You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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 =newAdvertEntity();
100
+
advert.owner=user;
101
+
102
+
awaitrepository.save(advert);
103
+
```
104
+
105
+
#### Parent
106
+
107
+
```ts
108
+
const repository =connection.getRepository(MerchantRepository); // That extends AbstractPolymorphicRepository
109
+
110
+
const advert =newAdvertEntity();
111
+
112
+
const parent =newMerchantEntity();
113
+
merchant.adverts= [advert];
114
+
115
+
awaitrepository.save(merchant);
116
+
```
117
+
118
+
### find
119
+
```ts
120
+
const repository =connection.getRepository(MerchantRepository); // That extends AbstractPolymorphicRepository
121
+
122
+
const results =awaitrepository.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
The owner property object's type property will now either be string value of `UserEntity` or `MerchantEntity`
163
+
164
+
## Notes
165
+
94
166
I think [Perf](https://github.com/Perf) might have some suggestions on how to improve things (sorry I have replied been mega busy!)
95
167
96
168
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