1- import _ from 'lodash' ;
2-
31/** Thrown when mapOne does not find an object in the resultSet and "isRequired" is passed in as true */
42function NotFoundError ( message = 'Not Found' ) {
53 this . name = 'NotFoundError' ;
@@ -23,7 +21,7 @@ function map(resultSet, maps, mapId, columnPrefix) {
2321
2422 let mappedCollection = [ ] ;
2523
26- _ . each ( resultSet , ( result ) => {
24+ resultSet . forEach ( result => {
2725 injectResultInCollection ( result , mappedCollection , maps , mapId , columnPrefix ) ;
2826 } ) ;
2927
@@ -72,15 +70,24 @@ function mapOne(resultSet, maps, mapId, columnPrefix, isRequired = true) {
7270function injectResultInCollection ( result , mappedCollection , maps , mapId , columnPrefix = '' ) {
7371
7472 // Check if the object is already in mappedCollection
75- let resultMap = _ . find ( maps , [ ' mapId' , mapId ] ) ;
73+ let resultMap = maps . find ( map => map . mapId === mapId ) ;
7674 let idProperty = getIdProperty ( resultMap ) ;
77- let predicate = _ . transform ( idProperty , ( accumulator , field ) => {
75+ let predicate = idProperty . reduce ( ( accumulator , field ) => {
7876 accumulator [ field . name ] = result [ columnPrefix + field . column ] ;
77+ return accumulator ;
7978 } , { } ) ;
80- let mappedObject = _ . find ( mappedCollection , predicate ) ;
79+
80+ let mappedObject = mappedCollection . find ( item => {
81+ for ( let k in predicate ) {
82+ if ( item [ k ] !== predicate [ k ] ) {
83+ return false ;
84+ }
85+ }
86+ return true ;
87+ } ) ;
8188
8289 // Inject only if the value of idProperty is not null (ignore joins to null records)
83- let isIdPropertyNotNull = _ . every ( idProperty , field => ! _ . isNull ( result [ columnPrefix + field . column ] ) ) ;
90+ let isIdPropertyNotNull = idProperty . every ( field => result [ columnPrefix + field . column ] !== null ) ;
8491
8592 if ( isIdPropertyNotNull ) {
8693 // Create mappedObject if it does not exist in mappedCollection
@@ -106,20 +113,21 @@ function injectResultInCollection(result, mappedCollection, maps, mapId, columnP
106113function injectResultInObject ( result , mappedObject , maps , mapId , columnPrefix = '' ) {
107114
108115 // Get the resultMap for this object
109- let resultMap = _ . find ( maps , [ ' mapId' , mapId ] ) ;
116+ let resultMap = maps . find ( map => map . mapId === mapId ) ;
110117
111118 // Copy id property
112119 let idProperty = getIdProperty ( resultMap ) ;
113120
114- _ . each ( idProperty , field => {
121+ idProperty . forEach ( field => {
115122 if ( ! mappedObject [ field . name ] ) {
116123 mappedObject [ field . name ] = result [ columnPrefix + field . column ] ;
117124 }
118125 } ) ;
119126
127+ const { properties, associations, collections} = resultMap ;
120128
121129 // Copy other properties
122- _ . each ( resultMap . properties , ( property ) => {
130+ properties && properties . forEach ( property => {
123131 // If property is a string, convert it to an object
124132 if ( typeof property === 'string' ) {
125133 // eslint-disable-next-line
@@ -137,20 +145,19 @@ function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix =
137145 } ) ;
138146
139147 // Copy associations
140- _ . each ( resultMap . associations , ( association ) => {
148+ associations && associations . forEach ( association => {
141149
142150 let associatedObject = mappedObject [ association . name ] ;
143151
144152 if ( ! associatedObject ) {
145- let associatedResultMap = _ . find ( maps , [ ' mapId' , association . mapId ] ) ;
153+ let associatedResultMap = maps . find ( map => map . mapId === association . mapId ) ;
146154 let associatedObjectIdProperty = getIdProperty ( associatedResultMap ) ;
147155
148156 mappedObject [ association . name ] = null ;
149157
150158 // Don't create associated object if it's key value is null
151- let isAssociatedObjectIdPropertyNotNull = _ . every (
152- associatedObjectIdProperty ,
153- field => ! _ . isNull ( result [ association . columnPrefix + field . column ] )
159+ let isAssociatedObjectIdPropertyNotNull = associatedObjectIdProperty . every ( field =>
160+ result [ association . columnPrefix + field . column ] !== null
154161 ) ;
155162
156163 if ( isAssociatedObjectIdPropertyNotNull ) {
@@ -165,7 +172,7 @@ function injectResultInObject(result, mappedObject, maps, mapId, columnPrefix =
165172 } ) ;
166173
167174 // Copy collections
168- _ . each ( resultMap . collections , ( collection ) => {
175+ collections && collections . forEach ( collection => {
169176
170177 let mappedCollection = mappedObject [ collection . name ] ;
171178
@@ -190,14 +197,14 @@ function getIdProperty(resultMap) {
190197
191198 let idProperties = resultMap . idProperty ;
192199
193- if ( ! _ . isArray ( idProperties ) ) {
200+ if ( ! Array . isArray ( idProperties ) ) {
194201 idProperties = [ idProperties ] ;
195202 }
196203
197- return _ . map ( idProperties , idProperty => {
204+ return idProperties . map ( idProperty => {
198205
199206 // If property is a string, convert it to an object
200- if ( _ . isString ( idProperty ) ) {
207+ if ( typeof idProperty === 'string' ) {
201208 return { name : idProperty , column : idProperty } ;
202209 }
203210
0 commit comments