@@ -381,6 +381,48 @@ const convertAdapterSchemaToParseSchema = ({ ...schema }) => {
381
381
return schema ;
382
382
} ;
383
383
384
+ class SchemaData {
385
+ __data : any ;
386
+ constructor ( allSchemas = [ ] ) {
387
+ this . __data = { } ;
388
+ allSchemas . forEach ( schema => {
389
+ Object . defineProperty ( this , schema . className , {
390
+ get : ( ) => {
391
+ if ( ! this . __data [ schema . className ] ) {
392
+ const data = { } ;
393
+ data . fields = injectDefaultSchema ( schema ) . fields ;
394
+ data . classLevelPermissions = schema . classLevelPermissions ;
395
+ data . indexes = schema . indexes ;
396
+ this . __data [ schema . className ] = data ;
397
+ }
398
+ return this . __data [ schema . className ] ;
399
+ } ,
400
+ } ) ;
401
+ } ) ;
402
+
403
+ // Inject the in-memory classes
404
+ volatileClasses . forEach ( className => {
405
+ Object . defineProperty ( this , className , {
406
+ get : ( ) => {
407
+ if ( ! this . __data [ className ] ) {
408
+ const schema = injectDefaultSchema ( {
409
+ className,
410
+ fields : { } ,
411
+ classLevelPermissions : { } ,
412
+ } ) ;
413
+ const data = { } ;
414
+ data . fields = schema . fields ;
415
+ data . classLevelPermissions = schema . classLevelPermissions ;
416
+ data . indexes = schema . indexes ;
417
+ this . __data [ className ] = data ;
418
+ }
419
+ return this . __data [ className ] ;
420
+ } ,
421
+ } ) ;
422
+ } ) ;
423
+ }
424
+ }
425
+
384
426
const injectDefaultSchema = ( {
385
427
className,
386
428
fields,
@@ -469,21 +511,14 @@ const typeToString = (type: SchemaField | string): string => {
469
511
// the mongo format and the Parse format. Soon, this will all be Parse format.
470
512
export default class SchemaController {
471
513
_dbAdapter : StorageAdapter ;
472
- data : any ;
473
- perms : any ;
474
- indexes : any ;
514
+ schemaData : { [ string ] : Schema } ;
475
515
_cache: any ;
476
516
reloadDataPromise: Promise < any > ;
477
517
478
518
constructor ( databaseAdapter : StorageAdapter , schemaCache : any ) {
479
519
this . _dbAdapter = databaseAdapter ;
480
520
this . _cache = schemaCache ;
481
- // this.data[className][fieldName] tells you the type of that field, in mongo format
482
- this . data = { } ;
483
- // this.perms[className][operation] tells you the acl-style permissions
484
- this . perms = { } ;
485
- // this.indexes[className][operation] tells you the indexes
486
- this . indexes = { } ;
521
+ this . schemaData = new SchemaData ( ) ;
487
522
}
488
523
489
524
reloadData ( options : LoadSchemaOptions = { clearCache : false } ) : Promise < any > {
@@ -500,35 +535,11 @@ export default class SchemaController {
500
535
. then ( ( ) => {
501
536
return this . getAllClasses ( options ) . then (
502
537
allSchemas => {
503
- const data = { } ;
504
- const perms = { } ;
505
- const indexes = { } ;
506
- allSchemas . forEach ( schema => {
507
- data [ schema . className ] = injectDefaultSchema ( schema ) . fields ;
508
- perms [ schema . className ] = schema . classLevelPermissions ;
509
- indexes [ schema . className ] = schema . indexes ;
510
- } ) ;
511
-
512
- // Inject the in-memory classes
513
- volatileClasses . forEach ( className => {
514
- const schema = injectDefaultSchema ( {
515
- className,
516
- fields : { } ,
517
- classLevelPermissions : { } ,
518
- } ) ;
519
- data [ className ] = schema . fields ;
520
- perms [ className ] = schema . classLevelPermissions ;
521
- indexes [ className ] = schema . indexes ;
522
- } ) ;
523
- this . data = data ;
524
- this . perms = perms ;
525
- this . indexes = indexes ;
538
+ this . schemaData = new SchemaData ( allSchemas ) ;
526
539
delete this . reloadDataPromise ;
527
540
} ,
528
541
err => {
529
- this . data = { } ;
530
- this . perms = { } ;
531
- this . indexes = { } ;
542
+ this . schemaData = new SchemaData ( ) ;
532
543
delete this . reloadDataPromise ;
533
544
throw err ;
534
545
}
@@ -575,11 +586,12 @@ export default class SchemaController {
575
586
}
576
587
return promise . then ( ( ) => {
577
588
if ( allowVolatileClasses && volatileClasses . indexOf ( className ) > - 1 ) {
589
+ const data = this . schemaData [ className ] ;
578
590
return Promise . resolve ( {
579
591
className,
580
- fields : this . data [ className ] ,
581
- classLevelPermissions : this . perms [ className ] ,
582
- indexes : this . indexes [ className ] ,
592
+ fields : data . fields ,
593
+ classLevelPermissions : data . classLevelPermissions ,
594
+ indexes : data . indexes ,
583
595
} ) ;
584
596
}
585
597
return this . _cache . getOneSchema ( className ) . then ( cached => {
@@ -730,16 +742,14 @@ export default class SchemaController {
730
742
. then ( ( ) => this . reloadData ( { clearCache : true } ) )
731
743
//TODO: Move this logic into the database adapter
732
744
. then ( ( ) => {
745
+ const schema = this . schemaData [ className ] ;
733
746
const reloadedSchema : Schema = {
734
747
className : className ,
735
- fields : this . data [ className ] ,
736
- classLevelPermissions : this . perms [ className ] ,
748
+ fields : schema . fields ,
749
+ classLevelPermissions : schema . classLevelPermissions ,
737
750
} ;
738
- if (
739
- this . indexes [ className ] &&
740
- Object . keys ( this . indexes [ className ] ) . length !== 0
741
- ) {
742
- reloadedSchema . indexes = this . indexes [ className ] ;
751
+ if ( schema . indexes && Object . keys ( schema . indexes ) . length !== 0 ) {
752
+ reloadedSchema . indexes = schema . indexes ;
743
753
}
744
754
return reloadedSchema ;
745
755
} )
@@ -760,7 +770,7 @@ export default class SchemaController {
760
770
// Returns a promise that resolves successfully to the new schema
761
771
// object or fails with a reason.
762
772
enforceClassExists ( className : string ) : Promise < SchemaController > {
763
- if ( this . data [ className ] ) {
773
+ if ( this . schemaData [ className ] ) {
764
774
return Promise . resolve ( this ) ;
765
775
}
766
776
// We don't have this class. Update the schema
@@ -777,7 +787,7 @@ export default class SchemaController {
777
787
} )
778
788
. then ( ( ) => {
779
789
// Ensure that the schema now validates
780
- if ( this . data [ className ] ) {
790
+ if ( this . schemaData [ className ] ) {
781
791
return this ;
782
792
} else {
783
793
throw new Parse . Error (
@@ -801,7 +811,7 @@ export default class SchemaController {
801
811
fields : SchemaFields = { } ,
802
812
classLevelPermissions : any
803
813
) : any {
804
- if ( this . data [ className ] ) {
814
+ if ( this . schemaData [ className ] ) {
805
815
throw new Parse . Error (
806
816
Parse . Error . INVALID_CLASS_NAME ,
807
817
`Class ${ className } already exists.`
@@ -1114,11 +1124,15 @@ export default class SchemaController {
1114
1124
1115
1125
// Validates the base CLP for an operation
1116
1126
testBaseCLP ( className : string , aclGroup : string [ ] , operation : string ) {
1117
- if ( ! this . perms [ className ] || ! this . perms [ className ] [ operation ] ) {
1127
+ const classSchema = this . schemaData [ className ] ;
1128
+ if (
1129
+ ! classSchema ||
1130
+ ! classSchema . classLevelPermissions ||
1131
+ ! classSchema . classLevelPermissions [ operation ]
1132
+ ) {
1118
1133
return true ;
1119
1134
}
1120
- const classPerms = this . perms [ className ] ;
1121
- const perms = classPerms [ operation ] ;
1135
+ const perms = classSchema . classLevelPermissions [ operation ] ;
1122
1136
// Handle the public scenario quickly
1123
1137
if ( perms [ '*' ] ) {
1124
1138
return true ;
@@ -1139,12 +1153,16 @@ export default class SchemaController {
1139
1153
if ( this . testBaseCLP ( className , aclGroup , operation ) ) {
1140
1154
return Promise . resolve ( ) ;
1141
1155
}
1142
-
1143
- if ( ! this . perms [ className ] || ! this . perms [ className ] [ operation ] ) {
1156
+ const classSchema = this . schemaData [ className ] ;
1157
+ if (
1158
+ ! classSchema ||
1159
+ ! classSchema . classLevelPermissions ||
1160
+ ! classSchema . classLevelPermissions [ operation ]
1161
+ ) {
1144
1162
return true ;
1145
1163
}
1146
- const classPerms = this . perms [ className ] ;
1147
- const perms = classPerms [ operation ] ;
1164
+ const classPerms = classSchema . classLevelPermissions ;
1165
+ const perms = classSchema . classLevelPermissions [ operation ] ;
1148
1166
1149
1167
// If only for authenticated users
1150
1168
// make sure we have an aclGroup
@@ -1200,16 +1218,16 @@ export default class SchemaController {
1200
1218
className : string ,
1201
1219
fieldName : string
1202
1220
) : ?( SchemaField | string ) {
1203
- if ( this . data && this . data [ className ] ) {
1204
- const expectedType = this . data [ className ] [ fieldName ] ;
1221
+ if ( this . schemaData [ className ] ) {
1222
+ const expectedType = this . schemaData [ className ] . fields [ fieldName ] ;
1205
1223
return expectedType === 'map' ? 'Object' : expectedType ;
1206
1224
}
1207
1225
return undefined ;
1208
1226
}
1209
1227
1210
1228
// Checks if a given class is in the schema.
1211
1229
hasClass ( className : string ) {
1212
- return this . reloadData ( ) . then ( ( ) => ! ! this . data [ className ] ) ;
1230
+ return this . reloadData ( ) . then ( ( ) => ! ! this . schemaData [ className ] ) ;
1213
1231
}
1214
1232
}
1215
1233
0 commit comments