@@ -5,17 +5,23 @@ import { EJSON } from 'bson'
5
5
import { getCollection } from './helpers'
6
6
7
7
export 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
11
16
}
12
17
13
- if ( ObjectId . isValid ( str ) ) {
14
- return new ObjectId ( str )
18
+ if ( isValidObjectIdString ( string ) ) {
19
+ return new ObjectId ( string )
15
20
}
16
21
17
- return str
22
+ return string
18
23
}
24
+
19
25
const fieldToDocField = key => ( key === 'id' ? '_id' : key )
20
26
21
27
// https://github.com/graphql/dataloader#batch-function
@@ -110,13 +116,15 @@ export const createCachingMethods = ({ collection, model, cache }) => {
110
116
findByFields : async ( fields , { ttl } = { } ) => {
111
117
const cleanedFields = { }
112
118
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
+ } )
120
128
121
129
const loaderJSON = JSON . stringify ( cleanedFields )
122
130
0 commit comments