@@ -7,6 +7,7 @@ let mongodb = require('mongodb');
77let MongoClient = mongodb . MongoClient ;
88
99const MongoSchemaCollectionName = '_SCHEMA' ;
10+ const DefaultMongoURI = 'mongodb://localhost:27017/parse' ;
1011
1112export class MongoStorageAdapter {
1213 // Private
@@ -18,7 +19,7 @@ export class MongoStorageAdapter {
1819 database ;
1920
2021 constructor ( {
21- uri,
22+ uri = DefaultMongoURI ,
2223 collectionPrefix = '' ,
2324 mongoOptions = { } ,
2425 } ) {
@@ -50,37 +51,38 @@ export class MongoStorageAdapter {
5051
5152 adaptiveCollection ( name : string ) {
5253 return this . connect ( )
53- . then ( ( ) => this . database . collection ( name ) )
54+ . then ( ( ) => this . database . collection ( this . _collectionPrefix + name ) )
5455 . then ( rawCollection => new MongoCollection ( rawCollection ) ) ;
5556 }
5657
57- schemaCollection ( collectionPrefix : string ) {
58+ schemaCollection ( ) {
5859 return this . connect ( )
59- . then ( ( ) => this . adaptiveCollection ( collectionPrefix + MongoSchemaCollectionName ) )
60+ . then ( ( ) => this . adaptiveCollection ( this . _collectionPrefix + MongoSchemaCollectionName ) )
6061 . then ( collection => new MongoSchemaCollection ( collection ) ) ;
6162 }
6263
6364 collectionExists ( name : string ) {
6465 return this . connect ( ) . then ( ( ) => {
65- return this . database . listCollections ( { name : name } ) . toArray ( ) ;
66+ return this . database . listCollections ( { name : this . _collectionPrefix + name } ) . toArray ( ) ;
6667 } ) . then ( collections => {
6768 return collections . length > 0 ;
6869 } ) ;
6970 }
7071
7172 dropCollection ( name : string ) {
72- return this . collection ( name ) . then ( collection => collection . drop ( ) ) ;
73+ return this . collection ( this . _collectionPrefix + name ) . then ( collection => collection . drop ( ) ) ;
7374 }
75+
7476 // Used for testing only right now.
75- collectionsContaining ( match : string ) {
77+ allCollections ( ) {
7678 return this . connect ( ) . then ( ( ) => {
7779 return this . database . collections ( ) ;
7880 } ) . then ( collections => {
7981 return collections . filter ( collection => {
8082 if ( collection . namespace . match ( / \. s y s t e m \. / ) ) {
8183 return false ;
8284 }
83- return ( collection . collectionName . indexOf ( match ) == 0 ) ;
85+ return ( collection . collectionName . indexOf ( this . _collectionPrefix ) == 0 ) ;
8486 } ) ;
8587 } ) ;
8688 }
@@ -105,13 +107,7 @@ export class MongoStorageAdapter {
105107 // may do so.
106108
107109 // Returns a Promise.
108-
109- // This function currently accepts the collectionPrefix and adaptive collection as a paramater because it isn't
110- // actually capable of determining the location of it's own _SCHEMA collection without having
111- // the collectionPrefix. Also, Schemas.js, the caller of this function, only stores the collection
112- // itself, and not the prefix. Eventually Parse Server won't care what a SchemaCollection is and
113- // will just tell the DB adapter to do things and it will do them.
114- deleteFields ( className : string , fieldNames , pointerFieldNames , collectionPrefix , adaptiveCollection ) {
110+ deleteFields ( className : string , fieldNames , pointerFieldNames ) {
115111 const nonPointerFieldNames = _ . difference ( fieldNames , pointerFieldNames ) ;
116112 const mongoFormatNames = nonPointerFieldNames . concat ( pointerFieldNames . map ( name => `_p_${ name } ` ) ) ;
117113 const collectionUpdate = { '$unset' : { } } ;
@@ -124,10 +120,9 @@ export class MongoStorageAdapter {
124120 schemaUpdate [ '$unset' ] [ name ] = null ;
125121 } ) ;
126122
127- return adaptiveCollection . updateMany ( { } , collectionUpdate )
128- . then ( updateResult => {
129- return this . schemaCollection ( collectionPrefix )
130- } )
123+ return this . adaptiveCollection ( className )
124+ . then ( collection => collection . updateMany ( { } , collectionUpdate ) )
125+ . then ( updateResult => this . schemaCollection ( ) )
131126 . then ( schemaCollection => schemaCollection . updateSchema ( className , schemaUpdate ) ) ;
132127 }
133128}
0 commit comments