@@ -31,6 +31,22 @@ const MongoClient = mongodb.MongoClient;
3131const ReadPreference = mongodb . ReadPreference ;
3232
3333const MongoSchemaCollectionName = '_SCHEMA' ;
34+
35+ const storageAdapterAllCollections = mongoAdapter => {
36+ return mongoAdapter . connect ( )
37+ . then ( ( ) => mongoAdapter . database . collections ( ) )
38+ . then ( collections => {
39+ return collections . filter ( collection => {
40+ if ( collection . namespace . match ( / \. s y s t e m \. / ) ) {
41+ return false ;
42+ }
43+ // TODO: If you have one app with a collection prefix that happens to be a prefix of another
44+ // apps prefix, this will go very very badly. We should fix that somehow.
45+ return ( collection . collectionName . indexOf ( mongoAdapter . _collectionPrefix ) == 0 ) ;
46+ } ) ;
47+ } ) ;
48+ }
49+
3450const convertParseSchemaToMongoSchema = ( { ...schema } ) => {
3551 delete schema . fields . _rperm ;
3652 delete schema . fields . _wperm ;
@@ -94,6 +110,7 @@ export class MongoStorageAdapter implements StorageAdapter {
94110 client : MongoClient ;
95111 _maxTimeMS : ?number ;
96112 canSortOnJoinTables : boolean ;
113+ collections : any ;
97114
98115 constructor ( {
99116 uri = defaults . DefaultMongoURI ,
@@ -108,6 +125,7 @@ export class MongoStorageAdapter implements StorageAdapter {
108125 this . _maxTimeMS = mongoOptions . maxTimeMS ;
109126 this . canSortOnJoinTables = true ;
110127 delete mongoOptions . maxTimeMS ;
128+ this . collections = { } ;
111129 }
112130
113131 connect ( ) {
@@ -163,10 +181,16 @@ export class MongoStorageAdapter implements StorageAdapter {
163181 }
164182
165183 _adaptiveCollection ( name : string ) {
166- return this . connect ( )
167- . then ( ( ) => this . database . collection ( this . _collectionPrefix + name ) )
168- . then ( rawCollection => new MongoCollection ( rawCollection ) )
169- . catch ( err => this . handleError ( err ) ) ;
184+ if ( ! this . collections [ name ] ) {
185+ this . collections [ name ] = this . connect ( )
186+ . then ( ( ) => this . database . collection ( this . _collectionPrefix + name ) )
187+ . then ( rawCollection => new MongoCollection ( rawCollection ) )
188+ . catch ( err => {
189+ delete this . collections [ name ] ;
190+ this . handleError ( err ) ;
191+ } ) ;
192+ }
193+ return this . collections [ name ] ;
170194 }
171195
172196 _schemaCollection ( ) : Promise < MongoSchemaCollection > {
@@ -297,9 +321,10 @@ export class MongoStorageAdapter implements StorageAdapter {
297321 . catch ( err => this . handleError ( err ) ) ;
298322 }
299323
300- dropDatabase ( ) {
301- if ( ! this . database ) { return Promise . resolve ( ) ; }
302- return this . database . dropDatabase ( ) ;
324+ deleteAllClasses ( fast : boolean ) {
325+ return storageAdapterAllCollections ( this )
326+ . then ( collections => Promise . all ( collections . map ( collection => fast ? collection . remove ( { } ) : collection . drop ( ) ) ) )
327+ . catch ( err => this . handleError ( err ) ) ;
303328 }
304329
305330 // Remove the column and all the data. For Relations, the _Join collection is handled
0 commit comments