File tree Expand file tree Collapse file tree 1 file changed +19
-10
lines changed Expand file tree Collapse file tree 1 file changed +19
-10
lines changed Original file line number Diff line number Diff line change 1
1
import DataLoader from 'dataloader'
2
2
3
+ const remapDocs = ( docs , ids ) => {
4
+ const idMap = { }
5
+ docs . forEach ( doc => {
6
+ idMap [ doc . _id ] = doc
7
+ } )
8
+ return ids . map ( id => idMap [ id ] )
9
+ }
10
+
3
11
export const createCachingMethods = ( { collection, cache } ) => {
12
+ const isMongoose = typeof collection === 'function'
13
+
4
14
const loader = new DataLoader ( ids =>
5
- collection
6
- . find ( { _id : { $in : ids } } )
7
- . toArray ( )
8
- . then ( docs => {
9
- const idMap = { }
10
- docs . forEach ( doc => {
11
- idMap [ doc . _id ] = doc
12
- } )
13
- return ids . map ( id => idMap [ id ] )
14
- } )
15
+ isMongoose
16
+ ? collection
17
+ . find ( { _id : { $in : ids } } )
18
+ . lean ( )
19
+ . then ( docs => remapDocs ( docs , ids ) )
20
+ : collection
21
+ . find ( { _id : { $in : ids } } )
22
+ . toArray ( )
23
+ . then ( docs => remapDocs ( docs , ids ) )
15
24
)
16
25
17
26
const cachePrefix = `mongo-${ collection . collectionName } -`
You can’t perform that action at this time.
0 commit comments