@@ -3,7 +3,7 @@ import { Schema } from '../../../schema/Schema'
33import { Element , Collection } from '../../../data/Data'
44import { Query } from '../../../query/Query'
55import { Model } from '../../Model'
6- import { Relation , Dictionary } from './Relation'
6+ import { Relation } from './Relation'
77
88export class MorphOne extends Relation {
99 /**
@@ -63,32 +63,35 @@ export class MorphOne extends Relation {
6363 * Set the constraints for an eager load of the relation.
6464 */
6565 addEagerConstraints ( query : Query , models : Collection ) : void {
66- query . where ( this . morphType , this . parent . $entity ( ) )
67- query . whereIn ( this . morphId , this . getKeys ( models , this . localKey ) )
66+ query
67+ . where ( this . morphType , this . parent . $entity ( ) )
68+ . whereIn ( this . morphId , this . getKeys ( models , this . localKey ) )
6869 }
6970
7071 /**
7172 * Match the eagerly loaded results to their parents.
7273 */
73- match ( relation : string , models : Collection , results : Collection ) : void {
74- const dictionary = this . buildDictionary ( results )
74+ match ( relation : string , models : Collection , query : Query ) : void {
75+ const dictionary = this . buildDictionary ( query . get ( ) )
7576
7677 models . forEach ( ( model ) => {
7778 const key = model [ this . localKey ]
7879
7980 dictionary [ key ]
80- ? model . $setRelation ( relation , dictionary [ key ] [ 0 ] )
81+ ? model . $setRelation ( relation , dictionary [ key ] )
8182 : model . $setRelation ( relation , null )
8283 } )
8384 }
8485
8586 /**
8687 * Build model dictionary keyed by the relation's foreign key.
8788 */
88- protected buildDictionary ( results : Collection ) : Dictionary {
89- return this . mapToDictionary ( results , ( result ) => {
90- return [ result [ this . morphId ] , result ]
91- } )
89+ protected buildDictionary ( models : Collection ) : Record < string , Model > {
90+ return models . reduce < Record < string , Model > > ( ( dictionary , model ) => {
91+ dictionary [ model [ this . morphId ] ] = model
92+
93+ return dictionary
94+ } , { } )
9295 }
9396
9497 /**
0 commit comments