11// @flow
2+ // @flow -disable-next Cannot resolve module `parse/node`.
23const Parse = require ( 'parse/node' ) ;
34import { logger } from '../logger' ;
45import Config from '../Config' ;
@@ -13,6 +14,7 @@ export class DefinedSchemas {
1314 localSchemas: Migrations . JSONSchema [ ] ;
1415 retries: number ;
1516 maxRetries: number ;
17+ allCloudSchemas: Parse . Schema [ ] ;
1618
1719 constructor ( schemaOptions : Migrations . SchemaOptions , config : ParseServerOptions ) {
1820 this . localSchemas = [ ] ;
@@ -63,13 +65,13 @@ export class DefinedSchemas {
6365 async execute ( ) {
6466 try {
6567 logger . info ( 'Running Migrations' ) ;
66- if ( this . schemaOptions ? .beforeMigration ) {
68+ if ( this . schemaOptions && this . schemaOptions . beforeMigration ) {
6769 await Promise . resolve ( this . schemaOptions . beforeMigration ( ) ) ;
6870 }
6971
7072 await this . executeMigrations ( ) ;
7173
72- if ( this . schemaOptions ? .afterMigration ) {
74+ if ( this . schemaOptions && this . schemaOptions . afterMigration ) {
7375 await Promise . resolve ( this . schemaOptions . afterMigration ( ) ) ;
7476 }
7577
@@ -148,11 +150,11 @@ export class DefinedSchemas {
148150 }
149151
150152 // Required for testing purpose
151- wait ( time ) {
152- return new Promise ( resolve => setTimeout ( resolve , time ) ) ;
153+ wait ( time : number ) {
154+ return new Promise < void > ( resolve => setTimeout ( resolve , time ) ) ;
153155 }
154156
155- async enforceCLPForNonProvidedClass ( ) : void {
157+ async enforceCLPForNonProvidedClass ( ) : Promise < void > {
156158 const nonProvidedClasses = this . allCloudSchemas . filter (
157159 cloudSchema =>
158160 ! this . localSchemas . some ( localSchema => localSchema . className === cloudSchema . className )
@@ -198,14 +200,16 @@ export class DefinedSchemas {
198200 Object . keys ( localSchema . fields )
199201 . filter ( fieldName => ! this . isProtectedFields ( localSchema . className , fieldName ) )
200202 . forEach ( fieldName => {
201- const field = localSchema . fields [ fieldName ] ;
202- this . handleFields ( newLocalSchema , fieldName , field ) ;
203+ if ( localSchema . fields ) {
204+ const field = localSchema . fields [ fieldName ] ;
205+ this . handleFields ( newLocalSchema , fieldName , field ) ;
206+ }
203207 } ) ;
204208 }
205209 // Handle indexes
206210 if ( localSchema . indexes ) {
207211 Object . keys ( localSchema . indexes ) . forEach ( indexName => {
208- if ( ! this . isProtectedIndex ( localSchema . className , indexName ) ) {
212+ if ( localSchema . indexes && ! this . isProtectedIndex ( localSchema . className , indexName ) ) {
209213 newLocalSchema . addIndex ( indexName , localSchema . indexes [ indexName ] ) ;
210214 }
211215 } ) ;
@@ -225,16 +229,19 @@ export class DefinedSchemas {
225229 Object . keys ( localSchema . fields )
226230 . filter ( fieldName => ! this . isProtectedFields ( localSchema . className , fieldName ) )
227231 . forEach ( fieldName => {
232+ // @flow -disable-next
228233 const field = localSchema . fields [ fieldName ] ;
229- if ( ! cloudSchema . fields [ fieldName ] ) this . handleFields ( newLocalSchema , fieldName , field ) ;
234+ if ( ! cloudSchema . fields [ fieldName ] ) {
235+ this . handleFields ( newLocalSchema , fieldName , field ) ;
236+ }
230237 } ) ;
231238 }
232239
233240 const fieldsToDelete : string [ ] = [ ] ;
234241 const fieldsToRecreate : {
235242 fieldName : string ,
236- from : { type : string , targetClass : string } ,
237- to : { type : string , targetClass : string } ,
243+ from : { type : string , targetClass ? : string } ,
244+ to : { type : string , targetClass ? : string } ,
238245 } [ ] = [ ] ;
239246 const fieldsWithChangedParams : string [ ] = [ ] ;
240247
@@ -294,8 +301,10 @@ export class DefinedSchemas {
294301 await this . updateSchemaToDB ( newLocalSchema ) ;
295302
296303 fieldsToRecreate . forEach ( fieldInfo => {
297- const field = localSchema . fields [ fieldInfo . fieldName ] ;
298- this . handleFields ( newLocalSchema , fieldInfo . fieldName , field ) ;
304+ if ( localSchema . fields ) {
305+ const field = localSchema . fields [ fieldInfo . fieldName ] ;
306+ this . handleFields ( newLocalSchema , fieldInfo . fieldName , field ) ;
307+ }
299308 } ) ;
300309 } else if ( this . schemaOptions . strict === true && fieldsToRecreate . length ) {
301310 fieldsToRecreate . forEach ( field => {
@@ -310,8 +319,10 @@ export class DefinedSchemas {
310319 }
311320
312321 fieldsWithChangedParams . forEach ( fieldName => {
313- const field = localSchema . fields [ fieldName ] ;
314- this . handleFields ( newLocalSchema , fieldName , field ) ;
322+ if ( localSchema . fields ) {
323+ const field = localSchema . fields [ fieldName ] ;
324+ this . handleFields ( newLocalSchema , fieldName , field ) ;
325+ }
315326 } ) ;
316327
317328 // Handle Indexes
@@ -321,8 +332,11 @@ export class DefinedSchemas {
321332 if (
322333 ( ! cloudSchema . indexes || ! cloudSchema . indexes [ indexName ] ) &&
323334 ! this . isProtectedIndex ( localSchema . className , indexName )
324- )
325- newLocalSchema . addIndex ( indexName , localSchema . indexes [ indexName ] ) ;
335+ ) {
336+ if ( localSchema . indexes ) {
337+ newLocalSchema . addIndex ( indexName , localSchema . indexes [ indexName ] ) ;
338+ }
339+ }
326340 } ) ;
327341 }
328342
@@ -338,10 +352,12 @@ export class DefinedSchemas {
338352 ! this . paramsAreEquals ( localSchema . indexes [ indexName ] , cloudSchema . indexes [ indexName ] )
339353 ) {
340354 newLocalSchema . deleteIndex ( indexName ) ;
341- indexesToAdd . push ( {
342- indexName,
343- index : localSchema . indexes [ indexName ] ,
344- } ) ;
355+ if ( localSchema . indexes ) {
356+ indexesToAdd . push ( {
357+ indexName,
358+ index : localSchema . indexes [ indexName ] ,
359+ } ) ;
360+ }
345361 }
346362 }
347363 } ) ;
@@ -360,25 +376,29 @@ export class DefinedSchemas {
360376 }
361377 }
362378
363- handleCLP ( localSchema : Migrations . JSONSchema , newLocalSchema : Parse . Schema , cloudSchema ) {
379+ handleCLP (
380+ localSchema : Migrations . JSONSchema ,
381+ newLocalSchema : Parse . Schema ,
382+ cloudSchema : Parse . Schema
383+ ) {
364384 if ( ! localSchema . classLevelPermissions && ! cloudSchema ) {
365385 logger . warn ( `classLevelPermissions not provided for ${ localSchema . className } .` ) ;
366386 }
367387 // Use spread to avoid read only issue (encountered by Moumouls using directAccess)
368- const clp = { ...localSchema . classLevelPermissions } || { } ;
388+ const clp = ( { ...localSchema . classLevelPermissions } || { } : Parse . CLP . PermissionsMap ) ;
369389 // To avoid inconsistency we need to remove all rights on addField
370390 clp . addField = { } ;
371391 newLocalSchema . setCLP ( clp ) ;
372392 }
373393
374- isProtectedFields ( className , fieldName ) {
394+ isProtectedFields ( className : string , fieldName : string ) {
375395 return (
376396 ! ! defaultColumns . _Default [ fieldName ] ||
377397 ! ! ( defaultColumns [ className ] && defaultColumns [ className ] [ fieldName ] )
378398 ) ;
379399 }
380400
381- isProtectedIndex ( className , indexName ) {
401+ isProtectedIndex ( className : string , indexName : string ) {
382402 let indexes = [ '_id_' ] ;
383403 if ( className === '_User' ) {
384404 indexes = [
@@ -393,9 +413,9 @@ export class DefinedSchemas {
393413 return indexes . indexOf ( indexName ) !== - 1 ;
394414 }
395415
396- paramsAreEquals < T > ( objA : T , objB : T ) {
397- const keysA = Object . keys ( objA ) ;
398- const keysB = Object . keys ( objB ) ;
416+ paramsAreEquals < T : { [ key : string ] : any } > ( objA : T , objB : T ) {
417+ const keysA : string [ ] = Object . keys ( objA ) ;
418+ const keysB : string [ ] = Object . keys ( objB ) ;
399419
400420 // Check key name
401421 if ( keysA . length !== keysB . length ) return false ;
0 commit comments