@@ -226,13 +226,9 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
226226 options ?: SaveOptions & { reload : false } ,
227227 ) : Promise < ( T & E ) | Array < T & E > | T | Array < T > > {
228228 if ( ! this . isPolymorph ( ) ) {
229- return Array . isArray ( entityOrEntities ) && options
230- ? await super . save ( entityOrEntities , options )
231- : Array . isArray ( entityOrEntities )
232- ? await super . save ( entityOrEntities )
233- : options
234- ? await super . save ( entityOrEntities , options )
235- : await super . save ( entityOrEntities ) ;
229+ return Array . isArray ( entityOrEntities )
230+ ? super . save ( entityOrEntities , options )
231+ : super . save ( entityOrEntities , options ) ;
236232 }
237233
238234 const metadata = this . getPolymorphicMetadata ( ) ;
@@ -248,53 +244,51 @@ export abstract class AbstractPolymorphicRepository<E> extends Repository<E> {
248244 if ( ! parent || entity [ entityIdColumn ( options ) ] !== undefined ) {
249245 return entity ;
250246 }
247+
248+ /**
249+ * Add parent's id and type to child's id and type field
250+ */
251251 entity [ entityIdColumn ( options ) ] = parent [ PrimaryColumn ( options ) ] ;
252252 entity [ entityTypeColumn ( options ) ] = parent . constructor . name ;
253253 return entity ;
254254 } ) ;
255255 }
256256 } ) ;
257257
258- const savedEntities =
259- Array . isArray ( entityOrEntities ) && options
260- ? await super . save ( entityOrEntities , options )
261- : Array . isArray ( entityOrEntities )
262- ? await super . save ( entityOrEntities )
263- : options
264- ? await super . save ( entityOrEntities , options )
265- : await super . save ( entityOrEntities ) ;
266-
267- return savedEntities ;
268-
269- // return Promise.all(
270- // (Array.isArray(savedEntities) ? savedEntities : [savedEntities]).map(
271- // entity =>
272- // new Promise(async resolve => {
273- // // @ts -ignore
274- // await this.deletePolymorphs(entity as E, metadata);
275- // // @ts -ignore
276- // resolve(await this.savePolymorphs(entity as E, metadata));
277- // }),
278- // ),
279- // );
258+ /**
259+ * Check deleteBeforeUpdate
260+ */
261+ Array . isArray ( entityOrEntities )
262+ ? await Promise . all (
263+ ( entityOrEntities as Array < T > ) . map ( ( entity ) =>
264+ this . deletePolymorphs ( entity , metadata ) ,
265+ ) ,
266+ )
267+ : await this . deletePolymorphs ( entityOrEntities as T , metadata ) ;
268+
269+ return Array . isArray ( entityOrEntities )
270+ ? super . save ( entityOrEntities , options )
271+ : super . save ( entityOrEntities , options ) ;
280272 }
281273
282274 private async deletePolymorphs (
283- entity : E ,
275+ entity : DeepPartial < E > ,
284276 options : PolymorphicMetadataInterface [ ] ,
285277 ) : Promise < void | never > {
286278 await Promise . all (
287279 options . map (
288280 ( option : PolymorphicMetadataInterface ) =>
289281 new Promise ( ( resolve ) => {
282+ console . log ( 'delete' , option . deleteBeforeUpdate ) ;
290283 if ( ! option . deleteBeforeUpdate ) {
291- return Promise . resolve ( ) ;
284+ resolve ( Promise . resolve ( ) ) ;
292285 }
293286
294287 const entityTypes = Array . isArray ( option . classType )
295288 ? option . classType
296289 : [ option . classType ] ;
297290
291+ // resolve to singular query?
298292 resolve (
299293 Promise . all (
300294 entityTypes . map ( ( type : ( ) => Function | Function [ ] ) => {
0 commit comments