@@ -16,6 +16,7 @@ import {
16
16
transformKey ,
17
17
transformWhere ,
18
18
transformUpdate ,
19
+ transformPointerString ,
19
20
} from './MongoTransform' ;
20
21
// @flow -disable-next
21
22
import Parse from 'parse/node' ;
@@ -132,7 +133,12 @@ export class MongoStorageAdapter implements StorageAdapter {
132
133
// encoded
133
134
const encodedUri = formatUrl ( parseUrl ( this . _uri ) ) ;
134
135
135
- this . connectionPromise = MongoClient . connect ( encodedUri , this . _mongoOptions ) . then ( database => {
136
+ this . connectionPromise = MongoClient . connect ( encodedUri , this . _mongoOptions ) . then ( client => {
137
+ // Starting mongoDB 3.0, the MongoClient.connect don't return a DB anymore but a client
138
+ // Fortunately, we can get back the options and use them to select the proper DB.
139
+ // https://github.com/mongodb/node-mongodb-native/blob/2c35d76f08574225b8db02d7bef687123e6bb018/lib/mongo_client.js#L885
140
+ const options = client . s . options ;
141
+ const database = client . db ( options . dbName ) ;
136
142
if ( ! database ) {
137
143
delete this . connectionPromise ;
138
144
return ;
@@ -143,6 +149,7 @@ export class MongoStorageAdapter implements StorageAdapter {
143
149
database . on ( 'close' , ( ) => {
144
150
delete this . connectionPromise ;
145
151
} ) ;
152
+ this . client = client ;
146
153
this . database = database ;
147
154
} ) . catch ( ( err ) => {
148
155
delete this . connectionPromise ;
@@ -153,10 +160,10 @@ export class MongoStorageAdapter implements StorageAdapter {
153
160
}
154
161
155
162
handleShutdown ( ) {
156
- if ( ! this . database ) {
163
+ if ( ! this . client ) {
157
164
return ;
158
165
}
159
- this . database . close ( false ) ;
166
+ this . client . close ( false ) ;
160
167
}
161
168
162
169
_adaptiveCollection ( name : string ) {
@@ -491,9 +498,19 @@ export class MongoStorageAdapter implements StorageAdapter {
491
498
492
499
distinct ( className : string , schema : SchemaType , query : QueryType , fieldName : string ) {
493
500
schema = convertParseSchemaToMongoSchema ( schema ) ;
501
+ const isPointerField = schema . fields [ fieldName ] && schema . fields [ fieldName ] . type === 'Pointer' ;
502
+ if ( isPointerField ) {
503
+ fieldName = `_p_${ fieldName } `
504
+ }
494
505
return this . _adaptiveCollection ( className )
495
506
. then ( collection => collection . distinct ( fieldName , transformWhere ( className , query , schema ) ) )
496
- . then ( objects => objects . map ( object => mongoObjectToParseObject ( className , object , schema ) ) ) ;
507
+ . then ( objects => objects . map ( object => {
508
+ if ( isPointerField ) {
509
+ const field = fieldName . substring ( 3 ) ;
510
+ return transformPointerString ( schema , field , object ) ;
511
+ }
512
+ return mongoObjectToParseObject ( className , object , schema ) ;
513
+ } ) ) ;
497
514
}
498
515
499
516
aggregate ( className : string , schema : any , pipeline : any , readPreference : ?string ) {
@@ -513,26 +530,28 @@ export class MongoStorageAdapter implements StorageAdapter {
513
530
}
514
531
515
532
_parseReadPreference ( readPreference : ?string ) : ?string {
516
- if ( readPreference != null ) {
517
- switch ( readPreference ) {
518
- case 'PRIMARY' :
519
- readPreference = ReadPreference . PRIMARY ;
520
- break ;
521
- case 'PRIMARY_PREFERRED' :
522
- readPreference = ReadPreference . PRIMARY_PREFERRED ;
523
- break ;
524
- case 'SECONDARY' :
525
- readPreference = ReadPreference . SECONDARY ;
526
- break ;
527
- case 'SECONDARY_PREFERRED' :
528
- readPreference = ReadPreference . SECONDARY_PREFERRED ;
529
- break ;
530
- case 'NEAREST' :
531
- readPreference = ReadPreference . NEAREST ;
532
- break ;
533
- default :
534
- throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Not supported read preference.' ) ;
535
- }
533
+ switch ( readPreference ) {
534
+ case 'PRIMARY' :
535
+ readPreference = ReadPreference . PRIMARY ;
536
+ break ;
537
+ case 'PRIMARY_PREFERRED' :
538
+ readPreference = ReadPreference . PRIMARY_PREFERRED ;
539
+ break ;
540
+ case 'SECONDARY' :
541
+ readPreference = ReadPreference . SECONDARY ;
542
+ break ;
543
+ case 'SECONDARY_PREFERRED' :
544
+ readPreference = ReadPreference . SECONDARY_PREFERRED ;
545
+ break ;
546
+ case 'NEAREST' :
547
+ readPreference = ReadPreference . NEAREST ;
548
+ break ;
549
+ case undefined :
550
+ // this is to match existing tests, which were failing as [email protected] don't report readPreference anymore
551
+ readPreference = ReadPreference . PRIMARY ;
552
+ break ;
553
+ default :
554
+ throw new Parse . Error ( Parse . Error . INVALID_QUERY , 'Not supported read preference.' ) ;
536
555
}
537
556
return readPreference ;
538
557
}
0 commit comments