@@ -4,6 +4,7 @@ import MongoSchemaCollection from './MongoSchemaCollection';
4
4
import { StorageAdapter } from '../StorageAdapter' ;
5
5
import type { SchemaType ,
6
6
QueryType ,
7
+ StorageClass ,
7
8
QueryOptions } from '../StorageAdapter' ;
8
9
import {
9
10
parse as parseUrl ,
@@ -88,6 +89,10 @@ const mongoSchemaFromFieldsAndClassNameAndCLP = (fields, className, classLevelPe
88
89
mongoObject . _metadata . indexes = indexes ;
89
90
}
90
91
92
+ if ( ! mongoObject . _metadata ) { // cleanup the unused _metadata
93
+ delete mongoObject . _metadata ;
94
+ }
95
+
91
96
return mongoObject ;
92
97
}
93
98
@@ -160,7 +165,7 @@ export class MongoStorageAdapter implements StorageAdapter {
160
165
. then ( rawCollection => new MongoCollection ( rawCollection ) ) ;
161
166
}
162
167
163
- _schemaCollection ( ) {
168
+ _schemaCollection ( ) : Promise < MongoSchemaCollection > {
164
169
return this . connect ( )
165
170
. then ( ( ) => this . _adaptiveCollection ( MongoSchemaCollectionName ) )
166
171
. then ( collection => new MongoSchemaCollection ( collection ) ) ;
@@ -174,7 +179,7 @@ export class MongoStorageAdapter implements StorageAdapter {
174
179
} ) ;
175
180
}
176
181
177
- setClassLevelPermissions ( className : string , CLPs : any ) {
182
+ setClassLevelPermissions ( className : string , CLPs : any ) : Promise < void > {
178
183
return this . _schemaCollection ( )
179
184
. then ( schemaCollection => schemaCollection . updateSchema ( className , {
180
185
$set : { '_metadata.class_permissions' : CLPs }
@@ -250,24 +255,16 @@ export class MongoStorageAdapter implements StorageAdapter {
250
255
} ) ;
251
256
}
252
257
253
- createClass ( className : string , schema : SchemaType ) {
258
+ createClass ( className : string , schema : SchemaType ) : Promise < void > {
254
259
schema = convertParseSchemaToMongoSchema ( schema ) ;
255
260
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP ( schema . fields , className , schema . classLevelPermissions , schema . indexes ) ;
256
261
mongoObject . _id = className ;
257
262
return this . setIndexesWithSchemaFormat ( className , schema . indexes , { } , schema . fields )
258
263
. then ( ( ) => this . _schemaCollection ( ) )
259
- . then ( schemaCollection => schemaCollection . _collection . insertOne ( mongoObject ) )
260
- . then ( result => MongoSchemaCollection . _TESTmongoSchemaToParseSchema ( result . ops [ 0 ] ) )
261
- . catch ( error => {
262
- if ( error . code === 11000 ) { //Mongo's duplicate key error
263
- throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'Class already exists.' ) ;
264
- } else {
265
- throw error ;
266
- }
267
- } )
264
+ . then ( schemaCollection => schemaCollection . insertSchema ( mongoObject ) ) ;
268
265
}
269
266
270
- addFieldIfNotExists ( className : string , fieldName : string , type : any ) {
267
+ addFieldIfNotExists ( className : string , fieldName : string , type : any ) : Promise < void > {
271
268
return this . _schemaCollection ( )
272
269
. then ( schemaCollection => schemaCollection . addFieldIfNotExists ( className , fieldName , type ) )
273
270
. then ( ( ) => this . createIndexesIfNeeded ( className , fieldName , type ) ) ;
@@ -343,14 +340,14 @@ export class MongoStorageAdapter implements StorageAdapter {
343
340
// Return a promise for all schemas known to this adapter, in Parse format. In case the
344
341
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
345
342
// rejection reason are TBD.
346
- getAllClasses ( ) {
343
+ getAllClasses ( ) : Promise < StorageClass [ ] > {
347
344
return this . _schemaCollection ( ) . then ( schemasCollection => schemasCollection . _fetchAllSchemasFrom_SCHEMA ( ) ) ;
348
345
}
349
346
350
347
// Return a promise for the schema with the given name, in Parse format. If
351
348
// this adapter doesn't know about the schema, return a promise that rejects with
352
349
// undefined as the reason.
353
- getClass ( className : string ) {
350
+ getClass ( className : string ) : Promise < StorageClass > {
354
351
return this . _schemaCollection ( )
355
352
. then ( schemasCollection => schemasCollection . _fetchOneSchemaFrom_SCHEMA ( className ) )
356
353
}
@@ -606,7 +603,7 @@ export class MongoStorageAdapter implements StorageAdapter {
606
603
. then ( collection => collection . _mongoCollection . dropIndexes ( ) ) ;
607
604
}
608
605
609
- updateSchemaWithIndexes ( ) {
606
+ updateSchemaWithIndexes ( ) : Promise < any > {
610
607
return this . getAllClasses ( )
611
608
. then ( ( classes ) => {
612
609
const promises = classes . map ( ( schema ) => {
0 commit comments