@@ -44,14 +44,31 @@ export function prepFields(fields) {
4444 return { loaderKey : EJSON . stringify ( cleanedFields ) , cleanedFields }
4545}
4646
47+ // getNestedValue({ nested: { foo: 'bar' } }, 'nested.foo')
48+ // => 'bar'
49+ function getNestedValue ( object , string ) {
50+ string = string . replace ( / \[ ( \w + ) \] / g, '.$1' ) // convert indexes to properties
51+ string = string . replace ( / ^ \. / , '' ) // strip a leading dot
52+ var a = string . split ( '.' )
53+ for ( var i = 0 , n = a . length ; i < n ; ++ i ) {
54+ var k = a [ i ]
55+ if ( k in object ) {
56+ object = object [ k ]
57+ } else {
58+ return
59+ }
60+ }
61+ return object
62+ }
63+
4764// https://github.com/graphql/dataloader#batch-function
4865// "The Array of values must be the same length as the Array of keys."
4966// "Each index in the Array of values must correspond to the same index in the Array of keys."
50- const orderDocs = fieldsArray => docs =>
67+ const orderDocs = ( fieldsArray , docs ) =>
5168 fieldsArray . map ( fields =>
5269 docs . filter ( doc => {
5370 for ( let fieldName of Object . keys ( fields ) ) {
54- const fieldValue = fields [ fieldName ]
71+ const fieldValue = getNestedValue ( fields , fieldName )
5572
5673 if ( typeof fieldValue === 'undefined' ) continue
5774
@@ -107,21 +124,26 @@ export const createCachingMethods = ({ collection, model, cache }) => {
107124 if ( existingFieldsFilter ) return filterArray
108125 return [ ...filterArray , filter ]
109126 } , [ ] )
127+ log ( 'filterArray: ' , filterArray )
110128
111129 const filter =
112130 filterArray . length === 1
113131 ? filterArray [ 0 ]
114132 : {
115133 $or : filterArray
116134 }
135+ log ( 'filter: ' , filter )
117136
118137 const findPromise = model
119138 ? model . find ( filter ) . exec ( )
120139 : collection . find ( filter ) . toArray ( )
121140
122141 const results = await findPromise
123- const orderedDocs = orderDocs ( fieldsArray ) ( results )
142+ log ( 'results: ' , results )
143+
144+ const orderedDocs = orderDocs ( fieldsArray , results )
124145 log ( 'orderedDocs: ' , orderedDocs )
146+
125147 return orderedDocs
126148 } )
127149
0 commit comments