@@ -38,6 +38,10 @@ DatabaseController.prototype.collection = function(className) {
3838 return this . rawCollection ( className ) ;
3939} ;
4040
41+ DatabaseController . prototype . adaptiveCollection = function ( className ) {
42+ return this . adapter . adaptiveCollection ( this . collectionPrefix + className ) ;
43+ } ;
44+
4145DatabaseController . prototype . collectionExists = function ( className ) {
4246 return this . adapter . collectionExists ( this . collectionPrefix + className ) ;
4347} ;
@@ -340,9 +344,8 @@ DatabaseController.prototype.create = function(className, object, options) {
340344// to avoid Mongo-format dependencies.
341345// Returns a promise that resolves to a list of items.
342346DatabaseController . 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 ) ) ;
346349} ;
347350
348351// Deletes everything in the database matching the current collectionPrefix
@@ -378,23 +381,17 @@ function keysForQuery(query) {
378381// Returns a promise for a list of related ids given an owning id.
379382// className here is the owning className.
380383DatabaseController . 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 ) ) ;
387387} ;
388388
389389// Returns a promise for a list of owning ids given some related ids.
390390// className here is the owning className.
391391DatabaseController . 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 ) ) ;
398395} ;
399396
400397// Modifies query so that it no longer has $in on relation fields, or
@@ -443,38 +440,6 @@ DatabaseController.prototype.reduceRelationKeys = function(className, query) {
443440 }
444441} ;
445442
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-
478443// Runs a query on the database.
479444// Returns a promise that resolves to a list of items.
480445// Options:
@@ -528,8 +493,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
528493 } ) . then ( ( ) => {
529494 return this . reduceInRelation ( className , query , schema ) ;
530495 } ) . then ( ( ) => {
531- return this . collection ( className ) ;
532- } ) . then ( ( coll ) => {
496+ return this . adaptiveCollection ( className ) ;
497+ } ) . then ( collection => {
533498 var mongoWhere = transform . transformWhere ( schema , className , query ) ;
534499 if ( ! isMaster ) {
535500 var orParts = [
@@ -542,9 +507,9 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
542507 mongoWhere = { '$and' : [ mongoWhere , { '$or' : orParts } ] } ;
543508 }
544509 if ( options . count ) {
545- return coll . count ( mongoWhere , mongoOptions ) ;
510+ return collection . count ( mongoWhere , mongoOptions ) ;
546511 } else {
547- return this . smartFind ( coll , mongoWhere , mongoOptions )
512+ return collection . find ( mongoWhere , mongoOptions )
548513 . then ( ( mongoResults ) => {
549514 return mongoResults . map ( ( r ) => {
550515 return this . untransformObject (
@@ -555,4 +520,8 @@ DatabaseController.prototype.find = function(className, query, options = {}) {
555520 } ) ;
556521} ;
557522
523+ function joinTableName ( className , key ) {
524+ return `_Join:${ key } :${ className } ` ;
525+ }
526+
558527module . exports = DatabaseController ;
0 commit comments