@@ -38,6 +38,10 @@ DatabaseController.prototype.collection = function(className) {
38
38
return this . rawCollection ( className ) ;
39
39
} ;
40
40
41
+ DatabaseController . prototype . adaptiveCollection = function ( className ) {
42
+ return this . adapter . adaptiveCollection ( this . collectionPrefix + className ) ;
43
+ } ;
44
+
41
45
DatabaseController . prototype . collectionExists = function ( className ) {
42
46
return this . adapter . collectionExists ( this . collectionPrefix + className ) ;
43
47
} ;
@@ -340,9 +344,8 @@ DatabaseController.prototype.create = function(className, object, options) {
340
344
// to avoid Mongo-format dependencies.
341
345
// Returns a promise that resolves to a list of items.
342
346
DatabaseController . prototype . mongoFind = function ( className , query , options = { } ) {
343
- return this . collection ( className ) . then ( ( coll ) => {
344
- return coll . find ( query , options ) . toArray ( ) ;
345
- } ) ;
347
+ return this . adaptiveCollection ( className )
348
+ . then ( collection => collection . find ( query , options ) ) ;
346
349
} ;
347
350
348
351
// Deletes everything in the database matching the current collectionPrefix
@@ -378,23 +381,17 @@ function keysForQuery(query) {
378
381
// Returns a promise for a list of related ids given an owning id.
379
382
// className here is the owning className.
380
383
DatabaseController . prototype . relatedIds = function ( className , key , owningId ) {
381
- var joinTable = '_Join:' + key + ':' + className ;
382
- return this . collection ( joinTable ) . then ( ( coll ) => {
383
- return coll . find ( { owningId : owningId } ) . toArray ( ) ;
384
- } ) . then ( ( results ) => {
385
- return results . map ( r => r . relatedId ) ;
386
- } ) ;
384
+ return this . adaptiveCollection ( joinTableName ( className , key ) )
385
+ . then ( coll => coll . find ( { owningId : owningId } ) )
386
+ . then ( results => results . map ( r => r . relatedId ) ) ;
387
387
} ;
388
388
389
389
// Returns a promise for a list of owning ids given some related ids.
390
390
// className here is the owning className.
391
391
DatabaseController . prototype . owningIds = function ( className , key , relatedIds ) {
392
- var joinTable = '_Join:' + key + ':' + className ;
393
- return this . collection ( joinTable ) . then ( ( coll ) => {
394
- return coll . find ( { relatedId : { '$in' : relatedIds } } ) . toArray ( ) ;
395
- } ) . then ( ( results ) => {
396
- return results . map ( r => r . owningId ) ;
397
- } ) ;
392
+ return this . adaptiveCollection ( joinTableName ( className , key ) )
393
+ . then ( coll => coll . find ( { relatedId : { '$in' : relatedIds } } ) )
394
+ . then ( results => results . map ( r => r . owningId ) ) ;
398
395
} ;
399
396
400
397
// Modifies query so that it no longer has $in on relation fields, or
@@ -443,38 +440,6 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
443
440
}
444
441
} ;
445
442
446
- // Does a find with "smart indexing".
447
- // Currently this just means, if it needs a geoindex and there is
448
- // none, then build the geoindex.
449
- // This could be improved a lot but it's not clear if that's a good
450
- // idea. Or even if this behavior is a good idea.
451
- DatabaseController . prototype . smartFind = function ( coll , where , options ) {
452
- return coll . find ( where , options ) . toArray ( )
453
- . then ( ( result ) => {
454
- return result ;
455
- } , ( error ) => {
456
- // Check for "no geoindex" error
457
- if ( ! error . message . match ( / u n a b l e t o f i n d i n d e x f o r .g e o N e a r / ) ||
458
- error . code != 17007 ) {
459
- throw error ;
460
- }
461
-
462
- // Figure out what key needs an index
463
- var key = error . message . match ( / f i e l d = ( [ A - Z a - z _ 0 - 9 ] + ) / ) [ 1 ] ;
464
- if ( ! key ) {
465
- throw error ;
466
- }
467
-
468
- var index = { } ;
469
- index [ key ] = '2d' ;
470
- //TODO: condiser moving index creation logic into Schema.js
471
- return coll . createIndex ( index ) . then ( ( ) => {
472
- // Retry, but just once.
473
- return coll . find ( where , options ) . toArray ( ) ;
474
- } ) ;
475
- } ) ;
476
- } ;
477
-
478
443
// Runs a query on the database.
479
444
// Returns a promise that resolves to a list of items.
480
445
// Options:
@@ -528,8 +493,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
528
493
} ) . then ( ( ) => {
529
494
return this . reduceInRelation ( className , query , schema ) ;
530
495
} ) . then ( ( ) => {
531
- return this . collection ( className ) ;
532
- } ) . then ( ( coll ) => {
496
+ return this . adaptiveCollection ( className ) ;
497
+ } ) . then ( collection => {
533
498
var mongoWhere = transform . transformWhere ( schema , className , query ) ;
534
499
if ( ! isMaster ) {
535
500
var orParts = [
@@ -542,9 +507,9 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
542
507
mongoWhere = { '$and' : [ mongoWhere , { '$or' : orParts } ] } ;
543
508
}
544
509
if ( options . count ) {
545
- return coll . count ( mongoWhere , mongoOptions ) ;
510
+ return collection . count ( mongoWhere , mongoOptions ) ;
546
511
} else {
547
- return this . smartFind ( coll , mongoWhere , mongoOptions )
512
+ return collection . find ( mongoWhere , mongoOptions )
548
513
. then ( ( mongoResults ) => {
549
514
return mongoResults . map ( ( r ) => {
550
515
return this . untransformObject (
@@ -555,4 +520,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
555
520
} ) ;
556
521
} ;
557
522
523
+ function joinTableName ( className , key ) {
524
+ return `_Join:${ key } :${ className } ` ;
525
+ }
526
+
558
527
module . exports = DatabaseController ;
0 commit comments