@@ -323,74 +323,99 @@ export default class SchemaController {
323
323
}
324
324
325
325
reloadData ( options = { clearCache : false } ) {
326
- if ( options . clearCache ) {
327
- this . _cache . clear ( ) ;
328
- }
329
326
if ( this . reloadDataPromise && ! options . clearCache ) {
330
327
return this . reloadDataPromise ;
331
328
}
332
- this . data = { } ;
333
- this . perms = { } ;
334
- this . reloadDataPromise = this . getAllClasses ( options )
335
- . then ( allSchemas => {
336
- allSchemas . forEach ( schema => {
337
- this . data [ schema . className ] = injectDefaultSchema ( schema ) . fields ;
338
- this . perms [ schema . className ] = schema . classLevelPermissions ;
339
- } ) ;
340
-
341
- // Inject the in-memory classes
342
- volatileClasses . forEach ( className => {
343
- this . data [ className ] = injectDefaultSchema ( {
344
- className,
345
- fields : { } ,
346
- classLevelPermissions : { }
347
- } ) ;
329
+ this . reloadDataPromise = Promise . resolve ( )
330
+ . then ( ( ) => {
331
+ if ( options . clearCache ) {
332
+ return this . _cache . clear ( ) . then ( ( ) => { } ) ;
333
+ }
334
+ return this . _cache . getSchemaInfo ( )
335
+ } )
336
+ . then ( schemaInfo => {
337
+ if ( schemaInfo ) {
338
+ return schemaInfo ;
339
+ }
340
+ return this . getAllClasses ( options )
341
+ . then ( allSchemas => {
342
+ const data = { } ;
343
+ const perms = { }
344
+ allSchemas . forEach ( schema => {
345
+ data [ schema . className ] = injectDefaultSchema ( schema ) . fields ;
346
+ perms [ schema . className ] = schema . classLevelPermissions ;
347
+ } ) ;
348
+
349
+ // Inject the in-memory classes
350
+ volatileClasses . forEach ( className => {
351
+ data [ className ] = injectDefaultSchema ( {
352
+ className,
353
+ fields : { } ,
354
+ classLevelPermissions : { }
355
+ } ) ;
356
+ } ) ;
357
+ const schemaInfo = { data : Object . freeze ( data ) , perms : Object . freeze ( perms ) } ;
358
+ this . _cache . setSchemaInfo ( schemaInfo ) ;
359
+ return schemaInfo ;
360
+ } ) ;
361
+ } )
362
+ . then ( schemaInfo => {
363
+ this . data = schemaInfo . data ;
364
+ this . perms = schemaInfo . perms ;
365
+ delete this . reloadDataPromise ;
366
+ } , ( err ) => {
367
+ delete this . reloadDataPromise ;
368
+ throw err ;
348
369
} ) ;
349
- delete this . reloadDataPromise ;
350
- } , ( err ) => {
351
- delete this . reloadDataPromise ;
352
- throw err ;
353
- } ) ;
354
370
return this . reloadDataPromise ;
355
371
}
356
372
357
373
getAllClasses ( options = { clearCache : false } ) {
358
- if ( options . clearCache ) {
359
- this . _cache . clear ( ) ;
360
- }
361
- return this . _cache . getAllClasses ( ) . then ( ( allClasses ) => {
362
- if ( allClasses && allClasses . length && ! options . clearCache ) {
363
- return Promise . resolve ( allClasses ) ;
364
- }
365
- return this . _dbAdapter . getAllClasses ( )
366
- . then ( allSchemas => allSchemas . map ( injectDefaultSchema ) )
367
- . then ( allSchemas => {
368
- return this . _cache . setAllClasses ( allSchemas ) . then ( ( ) => {
369
- return allSchemas ;
370
- } ) ;
371
- } )
372
- } ) ;
374
+ return Promise . resolve ( )
375
+ . then ( ( ) => {
376
+ if ( options . clearCache ) {
377
+ return this . _cache . clear ( ) ;
378
+ }
379
+ } )
380
+ . then ( ( ) => this . _cache . getAllClasses ( ) )
381
+ . then ( ( allClasses ) => {
382
+ if ( allClasses && allClasses . length && ! options . clearCache ) {
383
+ return Promise . resolve ( allClasses ) ;
384
+ }
385
+ return this . _dbAdapter . getAllClasses ( )
386
+ . then ( allSchemas => allSchemas . map ( injectDefaultSchema ) )
387
+ . then ( allSchemas => {
388
+ return this . _cache . setAllClasses ( Object . freeze ( allSchemas ) ) . then ( ( ) => {
389
+ return allSchemas ;
390
+ } ) ;
391
+ } )
392
+ } ) ;
373
393
}
374
394
375
395
getOneSchema ( className , allowVolatileClasses = false , options = { clearCache : false } ) {
376
- if ( options . clearCache ) {
377
- this . _cache . clear ( ) ;
378
- }
379
- if ( allowVolatileClasses && volatileClasses . indexOf ( className ) > - 1 ) {
380
- return Promise . resolve ( this . data [ className ] ) ;
381
- }
382
- return this . _cache . getOneSchema ( className ) . then ( ( cached ) => {
383
- if ( cached && ! options . clearCache ) {
384
- return Promise . resolve ( cached ) ;
385
- }
386
- return this . _dbAdapter . getClass ( className )
387
- . then ( injectDefaultSchema )
388
- . then ( ( result ) => {
389
- return this . _cache . setOneSchema ( className , result ) . then ( ( ) => {
390
- return result ;
391
- } )
396
+ return Promise . resolve ( )
397
+ . then ( ( ) => {
398
+ if ( options . clearCache ) {
399
+ return this . _cache . clear ( ) ;
400
+ }
401
+ } )
402
+ . then ( ( ) => {
403
+ if ( allowVolatileClasses && volatileClasses . indexOf ( className ) > - 1 ) {
404
+ return Promise . resolve ( this . data [ className ] ) ;
405
+ }
406
+ return this . _cache . getOneSchema ( className ) . then ( ( cached ) => {
407
+ if ( cached && ! options . clearCache ) {
408
+ return Promise . resolve ( cached ) ;
409
+ }
410
+ return this . _dbAdapter . getClass ( className )
411
+ . then ( injectDefaultSchema )
412
+ . then ( ( result ) => {
413
+ return this . _cache . setOneSchema ( className , result ) . then ( ( ) => {
414
+ return result ;
415
+ } )
416
+ } ) ;
417
+ } ) ;
392
418
} ) ;
393
- } ) ;
394
419
}
395
420
396
421
// Create a new class that includes the three default fields.
@@ -409,8 +434,7 @@ export default class SchemaController {
409
434
return this . _dbAdapter . createClass ( className , convertSchemaToAdapterSchema ( { fields, classLevelPermissions, className } ) )
410
435
. then ( convertAdapterSchemaToParseSchema )
411
436
. then ( ( res ) => {
412
- this . _cache . clear ( ) ;
413
- return res ;
437
+ return this . _cache . clear ( ) . then ( ( ) => res ) ;
414
438
} )
415
439
. catch ( error => {
416
440
if ( error && error . code === Parse . Error . DUPLICATE_VALUE ) {
@@ -621,9 +645,8 @@ export default class SchemaController {
621
645
throw new Parse . Error ( Parse . Error . INVALID_JSON , `Could not add field ${ fieldName } ` ) ;
622
646
}
623
647
// Remove the cached schema
624
- this . _cache . clear ( ) ;
625
- return this ;
626
- } ) ;
648
+ return this . _cache . clear ( ) ;
649
+ } ) . then ( ( ) => this )
627
650
} ) ;
628
651
}
629
652
@@ -664,9 +687,8 @@ export default class SchemaController {
664
687
. then ( ( ) => database . adapter . deleteClass ( `_Join:${ fieldName } :${ className } ` ) ) ;
665
688
}
666
689
return database . adapter . deleteFields ( className , schema , [ fieldName ] ) ;
667
- } ) . then ( ( ) => {
668
- this . _cache . clear ( ) ;
669
- } ) ;
690
+ } )
691
+ . then ( ( ) => this . _cache . clear ( ) ) ;
670
692
}
671
693
672
694
// Validates an object provided in REST format.
0 commit comments