1
1
// @flow
2
+ // @flow -disable-next Cannot resolve module `parse/node`.
2
3
const Parse = require ( 'parse/node' ) ;
3
4
import { logger } from '../logger' ;
4
5
import Config from '../Config' ;
@@ -13,6 +14,7 @@ export class DefinedSchemas {
13
14
localSchemas: Migrations . JSONSchema [ ] ;
14
15
retries: number ;
15
16
maxRetries: number ;
17
+ allCloudSchemas: Parse . Schema [ ] ;
16
18
17
19
constructor ( schemaOptions : Migrations . SchemaOptions , config : ParseServerOptions ) {
18
20
this . localSchemas = [ ] ;
@@ -63,13 +65,13 @@ export class DefinedSchemas {
63
65
async execute ( ) {
64
66
try {
65
67
logger . info ( 'Running Migrations' ) ;
66
- if ( this . schemaOptions ? .beforeMigration ) {
68
+ if ( this . schemaOptions && this . schemaOptions . beforeMigration ) {
67
69
await Promise . resolve ( this . schemaOptions . beforeMigration ( ) ) ;
68
70
}
69
71
70
72
await this . executeMigrations ( ) ;
71
73
72
- if ( this . schemaOptions ? .afterMigration ) {
74
+ if ( this . schemaOptions && this . schemaOptions . afterMigration ) {
73
75
await Promise . resolve ( this . schemaOptions . afterMigration ( ) ) ;
74
76
}
75
77
@@ -148,11 +150,11 @@ export class DefinedSchemas {
148
150
}
149
151
150
152
// 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 ) ) ;
153
155
}
154
156
155
- async enforceCLPForNonProvidedClass ( ) : void {
157
+ async enforceCLPForNonProvidedClass ( ) : Promise < void > {
156
158
const nonProvidedClasses = this . allCloudSchemas . filter (
157
159
cloudSchema =>
158
160
! this . localSchemas . some ( localSchema => localSchema . className === cloudSchema . className )
@@ -198,14 +200,16 @@ export class DefinedSchemas {
198
200
Object . keys ( localSchema . fields )
199
201
. filter ( fieldName => ! this . isProtectedFields ( localSchema . className , fieldName ) )
200
202
. 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
+ }
203
207
} ) ;
204
208
}
205
209
// Handle indexes
206
210
if ( localSchema . indexes ) {
207
211
Object . keys ( localSchema . indexes ) . forEach ( indexName => {
208
- if ( ! this . isProtectedIndex ( localSchema . className , indexName ) ) {
212
+ if ( localSchema . indexes && ! this . isProtectedIndex ( localSchema . className , indexName ) ) {
209
213
newLocalSchema . addIndex ( indexName , localSchema . indexes [ indexName ] ) ;
210
214
}
211
215
} ) ;
@@ -225,16 +229,19 @@ export class DefinedSchemas {
225
229
Object . keys ( localSchema . fields )
226
230
. filter ( fieldName => ! this . isProtectedFields ( localSchema . className , fieldName ) )
227
231
. forEach ( fieldName => {
232
+ // @flow -disable-next
228
233
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
+ }
230
237
} ) ;
231
238
}
232
239
233
240
const fieldsToDelete : string [ ] = [ ] ;
234
241
const fieldsToRecreate : {
235
242
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 } ,
238
245
} [ ] = [ ] ;
239
246
const fieldsWithChangedParams : string [ ] = [ ] ;
240
247
@@ -294,8 +301,10 @@ export class DefinedSchemas {
294
301
await this . updateSchemaToDB ( newLocalSchema ) ;
295
302
296
303
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
+ }
299
308
} ) ;
300
309
} else if ( this . schemaOptions . strict === true && fieldsToRecreate . length ) {
301
310
fieldsToRecreate . forEach ( field => {
@@ -310,8 +319,10 @@ export class DefinedSchemas {
310
319
}
311
320
312
321
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
+ }
315
326
} ) ;
316
327
317
328
// Handle Indexes
@@ -321,8 +332,11 @@ export class DefinedSchemas {
321
332
if (
322
333
( ! cloudSchema . indexes || ! cloudSchema . indexes [ indexName ] ) &&
323
334
! 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
+ }
326
340
} ) ;
327
341
}
328
342
@@ -338,10 +352,12 @@ export class DefinedSchemas {
338
352
! this . paramsAreEquals ( localSchema . indexes [ indexName ] , cloudSchema . indexes [ indexName ] )
339
353
) {
340
354
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
+ }
345
361
}
346
362
}
347
363
} ) ;
@@ -360,25 +376,29 @@ export class DefinedSchemas {
360
376
}
361
377
}
362
378
363
- handleCLP ( localSchema : Migrations . JSONSchema , newLocalSchema : Parse . Schema , cloudSchema ) {
379
+ handleCLP (
380
+ localSchema : Migrations . JSONSchema ,
381
+ newLocalSchema : Parse . Schema ,
382
+ cloudSchema : Parse . Schema
383
+ ) {
364
384
if ( ! localSchema . classLevelPermissions && ! cloudSchema ) {
365
385
logger . warn ( `classLevelPermissions not provided for ${ localSchema . className } .` ) ;
366
386
}
367
387
// 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 ) ;
369
389
// To avoid inconsistency we need to remove all rights on addField
370
390
clp . addField = { } ;
371
391
newLocalSchema . setCLP ( clp ) ;
372
392
}
373
393
374
- isProtectedFields ( className , fieldName ) {
394
+ isProtectedFields ( className : string , fieldName : string ) {
375
395
return (
376
396
! ! defaultColumns . _Default [ fieldName ] ||
377
397
! ! ( defaultColumns [ className ] && defaultColumns [ className ] [ fieldName ] )
378
398
) ;
379
399
}
380
400
381
- isProtectedIndex ( className , indexName ) {
401
+ isProtectedIndex ( className : string , indexName : string ) {
382
402
let indexes = [ '_id_' ] ;
383
403
if ( className === '_User' ) {
384
404
indexes = [
@@ -393,9 +413,9 @@ export class DefinedSchemas {
393
413
return indexes . indexOf ( indexName ) !== - 1 ;
394
414
}
395
415
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 ) ;
399
419
400
420
// Check key name
401
421
if ( keysA . length !== keysB . length ) return false ;
0 commit comments