@@ -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 ,
@@ -89,6 +90,10 @@ const mongoSchemaFromFieldsAndClassNameAndCLP = (fields, className, classLevelPe
89
90
mongoObject . _metadata . indexes = indexes ;
90
91
}
91
92
93
+ if ( ! mongoObject . _metadata ) { // cleanup the unused _metadata
94
+ delete mongoObject . _metadata ;
95
+ }
96
+
92
97
return mongoObject ;
93
98
}
94
99
@@ -168,7 +173,7 @@ export class MongoStorageAdapter implements StorageAdapter {
168
173
. then ( rawCollection => new MongoCollection ( rawCollection ) ) ;
169
174
}
170
175
171
- _schemaCollection ( ) {
176
+ _schemaCollection ( ) : Promise < MongoSchemaCollection > {
172
177
return this . connect ( )
173
178
. then ( ( ) => this . _adaptiveCollection ( MongoSchemaCollectionName ) )
174
179
. then ( collection => new MongoSchemaCollection ( collection ) ) ;
@@ -182,7 +187,7 @@ export class MongoStorageAdapter implements StorageAdapter {
182
187
} ) ;
183
188
}
184
189
185
- setClassLevelPermissions ( className : string , CLPs : any ) {
190
+ setClassLevelPermissions ( className : string , CLPs : any ) : Promise < void > {
186
191
return this . _schemaCollection ( )
187
192
. then ( schemaCollection => schemaCollection . updateSchema ( className , {
188
193
$set : { '_metadata.class_permissions' : CLPs }
@@ -258,24 +263,16 @@ export class MongoStorageAdapter implements StorageAdapter {
258
263
} ) ;
259
264
}
260
265
261
- createClass ( className : string , schema : SchemaType ) {
266
+ createClass ( className : string , schema : SchemaType ) : Promise < void > {
262
267
schema = convertParseSchemaToMongoSchema ( schema ) ;
263
268
const mongoObject = mongoSchemaFromFieldsAndClassNameAndCLP ( schema . fields , className , schema . classLevelPermissions , schema . indexes ) ;
264
269
mongoObject . _id = className ;
265
270
return this . setIndexesWithSchemaFormat ( className , schema . indexes , { } , schema . fields )
266
271
. then ( ( ) => this . _schemaCollection ( ) )
267
- . then ( schemaCollection => schemaCollection . _collection . insertOne ( mongoObject ) )
268
- . then ( result => MongoSchemaCollection . _TESTmongoSchemaToParseSchema ( result . ops [ 0 ] ) )
269
- . catch ( error => {
270
- if ( error . code === 11000 ) { //Mongo's duplicate key error
271
- throw new Parse . Error ( Parse . Error . DUPLICATE_VALUE , 'Class already exists.' ) ;
272
- } else {
273
- throw error ;
274
- }
275
- } )
272
+ . then ( schemaCollection => schemaCollection . insertSchema ( mongoObject ) ) ;
276
273
}
277
274
278
- addFieldIfNotExists ( className : string , fieldName : string , type : any ) {
275
+ addFieldIfNotExists ( className : string , fieldName : string , type : any ) : Promise < void > {
279
276
return this . _schemaCollection ( )
280
277
. then ( schemaCollection => schemaCollection . addFieldIfNotExists ( className , fieldName , type ) )
281
278
. then ( ( ) => this . createIndexesIfNeeded ( className , fieldName , type ) ) ;
@@ -351,14 +348,14 @@ export class MongoStorageAdapter implements StorageAdapter {
351
348
// Return a promise for all schemas known to this adapter, in Parse format. In case the
352
349
// schemas cannot be retrieved, returns a promise that rejects. Requirements for the
353
350
// rejection reason are TBD.
354
- getAllClasses ( ) {
351
+ getAllClasses ( ) : Promise < StorageClass [ ] > {
355
352
return this . _schemaCollection ( ) . then ( schemasCollection => schemasCollection . _fetchAllSchemasFrom_SCHEMA ( ) ) ;
356
353
}
357
354
358
355
// Return a promise for the schema with the given name, in Parse format. If
359
356
// this adapter doesn't know about the schema, return a promise that rejects with
360
357
// undefined as the reason.
361
- getClass ( className : string ) {
358
+ getClass ( className : string ) : Promise < StorageClass > {
362
359
return this . _schemaCollection ( )
363
360
. then ( schemasCollection => schemasCollection . _fetchOneSchemaFrom_SCHEMA ( className ) )
364
361
}
@@ -628,7 +625,7 @@ export class MongoStorageAdapter implements StorageAdapter {
628
625
. then ( collection => collection . _mongoCollection . dropIndexes ( ) ) ;
629
626
}
630
627
631
- updateSchemaWithIndexes ( ) {
628
+ updateSchemaWithIndexes ( ) : Promise < any > {
632
629
return this . getAllClasses ( )
633
630
. then ( ( classes ) => {
634
631
const promises = classes . map ( ( schema ) => {
0 commit comments