@@ -5,17 +5,23 @@ import { EJSON } from 'bson'
55import { getCollection } from './helpers'
66
77export const idToString = id => ( id instanceof ObjectId ? id . toHexString ( ) : id )
8- const stringToId = str => {
9- if ( str instanceof ObjectId ) {
10- return str
8+
9+ // https://www.geeksforgeeks.org/how-to-check-if-a-string-is-valid-mongodb-objectid-in-nodejs/
10+ export const isValidObjectIdString = string =>
11+ ObjectId . isValid ( string ) && String ( new ObjectId ( string ) ) === string
12+
13+ export const stringToId = string => {
14+ if ( string instanceof ObjectId ) {
15+ return string
1116 }
1217
13- if ( ObjectId . isValid ( str ) ) {
14- return new ObjectId ( str )
18+ if ( isValidObjectIdString ( string ) ) {
19+ return new ObjectId ( string )
1520 }
1621
17- return str
22+ return string
1823}
24+
1925const fieldToDocField = key => ( key === 'id' ? '_id' : key )
2026
2127// https://github.com/graphql/dataloader#batch-function
@@ -110,13 +116,15 @@ export const createCachingMethods = ({ collection, model, cache }) => {
110116 findByFields : async ( fields , { ttl } = { } ) => {
111117 const cleanedFields = { }
112118
113- Object . keys ( fields ) . sort ( ) . forEach ( key => {
114- if ( typeof key !== 'undefined' ) {
115- cleanedFields [ key ] = Array . isArray ( fields [ key ] )
116- ? fields [ key ]
117- : [ fields [ key ] ]
118- }
119- } )
119+ Object . keys ( fields )
120+ . sort ( )
121+ . forEach ( key => {
122+ if ( typeof key !== 'undefined' ) {
123+ cleanedFields [ key ] = Array . isArray ( fields [ key ] )
124+ ? fields [ key ]
125+ : [ fields [ key ] ]
126+ }
127+ } )
120128
121129 const loaderJSON = JSON . stringify ( cleanedFields )
122130
0 commit comments