@@ -11,7 +11,7 @@ import intersect from 'intersect';
11
11
// @flow -disable-next
12
12
import deepcopy from 'deepcopy' ;
13
13
import logger from '../logger' ;
14
- import * as SchemaController from './SchemaController' ;
14
+ import * as SchemaController from './SchemaController' ;
15
15
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter' ;
16
16
function addWriteACL ( query , acl ) {
17
17
const newQuery = _ . cloneDeep ( query ) ;
@@ -152,6 +152,14 @@ const filterSensitiveData = (isMaster, aclGroup, className, object) => {
152
152
return object ;
153
153
} ;
154
154
155
+ type Options = {
156
+ acl ?: string [ ]
157
+ }
158
+
159
+ type LoadSchemaOptions = {
160
+ clearCache : boolean
161
+ }
162
+
155
163
// Runs an update on the database.
156
164
// Returns a promise for an object with the new values for field
157
165
// modifications that don't know their results ahead of time, like
@@ -285,7 +293,7 @@ const relationSchema = { fields: { relatedId: { type: 'String' }, owningId: { ty
285
293
class DatabaseController {
286
294
adapter : StorageAdapter ;
287
295
schemaCache : any ;
288
- schemaPromise : ?Promise < any > ;
296
+ schemaPromise : ?Promise < SchemaController . SchemaController > ;
289
297
290
298
constructor ( adapter : StorageAdapter , schemaCache : any ) {
291
299
this . adapter = adapter ;
@@ -314,7 +322,7 @@ class DatabaseController {
314
322
}
315
323
316
324
// Returns a promise for a schemaController.
317
- loadSchema ( options : any = { clearCache : false } ) : Promise < any > {
325
+ loadSchema ( options : LoadSchemaOptions = { clearCache : false } ) : Promise < SchemaController . SchemaController > {
318
326
if ( this . schemaPromise != null ) {
319
327
return this . schemaPromise ;
320
328
}
@@ -327,25 +335,24 @@ class DatabaseController {
327
335
// Returns a promise for the classname that is related to the given
328
336
// classname through the key.
329
337
// TODO: make this not in the DatabaseController interface
330
- redirectClassNameForKey ( className : string , key : string ) : Promise < string > {
338
+ redirectClassNameForKey ( className : string , key : string ) : Promise < ? string > {
331
339
return this . loadSchema ( ) . then ( ( schema ) => {
332
- var t = schema . getExpectedType ( className , key ) ;
333
- if ( t && t . type == 'Relation' ) {
340
+ var t = schema . getExpectedType ( className , key ) ;
341
+ if ( t != null && typeof t !== 'string' && t . type = == 'Relation' ) {
334
342
return t . targetClass ;
335
- } else {
336
- return className ;
337
343
}
344
+ return className ;
338
345
} ) ;
339
346
}
340
347
341
348
// Uses the schema to validate the object (REST API format).
342
349
// Returns a promise that resolves to the new schema.
343
350
// This does not update this.schema, because in a situation like a
344
351
// batch request, that could confuse other users of the schema.
345
- validateObject ( className : string , object : any , query : any , { acl } : any ) : Promise < boolean > {
352
+ validateObject ( className : string , object : any , query : any , { acl } : Options ) : Promise < boolean > {
346
353
let schema ;
347
354
const isMaster = acl === undefined ;
348
- var aclGroup = acl || [ ] ;
355
+ var aclGroup : string [ ] = acl || [ ] ;
349
356
return this . loadSchema ( ) . then ( s => {
350
357
schema = s ;
351
358
if ( isMaster ) {
@@ -537,7 +544,7 @@ class DatabaseController {
537
544
// acl: a list of strings. If the object to be updated has an ACL,
538
545
// one of the provided strings must provide the caller with
539
546
// write permissions.
540
- destroy ( className : string , query : any , { acl } : any = { } ) : Promise < any > {
547
+ destroy ( className : string , query : any , { acl } : Options = { } ) : Promise < any > {
541
548
const isMaster = acl === undefined ;
542
549
const aclGroup = acl || [ ] ;
543
550
@@ -579,7 +586,7 @@ class DatabaseController {
579
586
580
587
// Inserts an object into the database.
581
588
// Returns a promise that resolves successfully iff the object saved.
582
- create ( className : string , object : any , { acl } : any = { } ) : Promise < any > {
589
+ create ( className : string , object : any , { acl } : Options = { } ) : Promise < any > {
583
590
// Make a copy of the object, so we don't mutate the incoming data.
584
591
const originalObject = object ;
585
592
object = transformObjectACL ( object ) ;
@@ -610,7 +617,7 @@ class DatabaseController {
610
617
} )
611
618
}
612
619
613
- canAddField ( schema : any , className : string , object : any , aclGroup : string [ ] ) : Promise < void > {
620
+ canAddField ( schema : SchemaController . SchemaController , className : string , object : any , aclGroup : string [ ] ) : Promise < void > {
614
621
const classSchema = schema . data [ className ] ;
615
622
if ( ! classSchema ) {
616
623
return Promise . resolve ( ) ;
@@ -843,7 +850,7 @@ class DatabaseController {
843
850
distinct,
844
851
pipeline,
845
852
readPreference
846
- } : any = { } ) : Promise < [ any ] > {
853
+ } : any = { } ) : Promise < any > {
847
854
const isMaster = acl === undefined ;
848
855
const aclGroup = acl || [ ] ;
849
856
op = op || ( typeof query . objectId == 'string' && Object . keys ( query ) . length === 1 ? 'get' : 'find' ) ;
@@ -938,7 +945,7 @@ class DatabaseController {
938
945
}
939
946
940
947
deleteSchema ( className : string ) : Promise < void > {
941
- return this . loadSchema ( true )
948
+ return this . loadSchema ( { clearCache : true } )
942
949
. then ( schemaController => schemaController . getOneSchema ( className , true ) )
943
950
. catch ( error => {
944
951
if ( error === undefined ) {
0 commit comments