Skip to content

Commit 37fd074

Browse files
committed
Moves Controller types into a single file
1 parent ee0b6c3 commit 37fd074

File tree

6 files changed

+71
-51
lines changed

6 files changed

+71
-51
lines changed

src/Adapters/Storage/Mongo/MongoStorageAdapter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ import MongoSchemaCollection from './MongoSchemaCollection';
44
import { StorageAdapter, IndexingStorageAdapter } from '../StorageAdapter';
55
import type { SchemaType,
66
QueryType,
7-
QueryOptionsType } from '../StorageAdapter';
7+
QueryOptions } from '../StorageAdapter';
88
import {
99
parse as parseUrl,
1010
format as formatUrl,
@@ -354,7 +354,7 @@ export class MongoStorageAdapter implements StorageAdapter, IndexingStorageAdapt
354354
}
355355

356356
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
357-
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys, readPreference }: QueryOptionsType) {
357+
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys, readPreference }: QueryOptions) {
358358
schema = convertParseSchemaToMongoSchema(schema);
359359
const mongoWhere = transformWhere(className, query, schema);
360360
const mongoSort = _.mapKeys(sort, (value, fieldName) => transformKey(className, fieldName, schema));

src/Adapters/Storage/Postgres/PostgresStorageAdapter.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ const debug = function(...args: any) {
2323
import { StorageAdapter } from '../StorageAdapter';
2424
import type { SchemaType,
2525
QueryType,
26-
QueryOptionsType } from '../StorageAdapter';
26+
QueryOptions } from '../StorageAdapter';
2727

2828
const parseTypeToPostgresType = type => {
2929
switch (type.type) {
@@ -1210,7 +1210,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
12101210
});
12111211
}
12121212

1213-
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptionsType) {
1213+
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptions) {
12141214
debug('find', className, query, {skip, limit, sort, keys });
12151215
const hasLimit = limit !== undefined;
12161216
const hasSkip = skip !== undefined;

src/Adapters/Storage/StorageAdapter.js

+20-7
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,27 @@
22
export type SchemaType = any;
33
export type StorageClass = any;
44
export type QueryType = any;
5-
export type QueryOptionsType = {
6-
skip?: ?number;
7-
limit?: ?number;
8-
sort?: ?any;
9-
keys?: ?string[];
10-
readPreference?: ?string;
5+
6+
export type QueryOptions = {
7+
skip?: number,
8+
limit?: number,
9+
acl?: string[],
10+
sort?: {[string]: number},
11+
count?: boolean | number,
12+
keys?: string[],
13+
op?: string,
14+
distinct?: boolean,
15+
pipeline?: any,
16+
readPreference?: ?string,
1117
};
1218

19+
export type UpdateQueryOptions = {
20+
many?: boolean,
21+
upsert?: boolean
22+
}
23+
24+
export type FullQueryOptions = QueryOptions & UpdateQueryOptions;
25+
1326
export interface StorageAdapter {
1427
classExists(className: string): Promise<boolean>;
1528
setClassLevelPermissions(className: string, clps: any): Promise<void>;
@@ -25,7 +38,7 @@ export interface StorageAdapter {
2538
updateObjectsByQuery(className: string, schema: SchemaType, query: QueryType, update: any): Promise<[any]>;
2639
findOneAndUpdate(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any>;
2740
upsertOneObject(className: string, schema: SchemaType, query: QueryType, update: any): Promise<any>;
28-
find(className: string, schema: SchemaType, query: QueryType, options: QueryOptionsType): Promise<[any]>;
41+
find(className: string, schema: SchemaType, query: QueryType, options: QueryOptions): Promise<[any]>;
2942
ensureUniqueness(className: string, schema: SchemaType, fieldNames: Array<string>): Promise<void>;
3043
count(className: string, schema: SchemaType, query: QueryType, readPreference: ?string): Promise<number>;
3144
distinct(className: string, schema: SchemaType, query: QueryType, fieldName: string): Promise<any>;

src/Controllers/DatabaseController.js

+9-12
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,9 @@ import deepcopy from 'deepcopy';
1313
import logger from '../logger';
1414
import * as SchemaController from './SchemaController';
1515
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter';
16+
import type { QueryOptions,
17+
FullQueryOptions } from '../Adapters/Storage/StorageAdapter';
18+
1619
function addWriteACL(query, acl) {
1720
const newQuery = _.cloneDeep(query);
1821
//Can't be any existing '_wperm' query, we don't allow client queries on that, no need to $and
@@ -152,13 +155,7 @@ const filterSensitiveData = (isMaster, aclGroup, className, object) => {
152155
return object;
153156
};
154157

155-
type Options = {
156-
acl?: string[]
157-
}
158-
159-
type LoadSchemaOptions = {
160-
clearCache: boolean
161-
}
158+
import type { LoadSchemaOptions } from './types';
162159

163160
// Runs an update on the database.
164161
// Returns a promise for an object with the new values for field
@@ -349,7 +346,7 @@ class DatabaseController {
349346
// Returns a promise that resolves to the new schema.
350347
// This does not update this.schema, because in a situation like a
351348
// batch request, that could confuse other users of the schema.
352-
validateObject(className: string, object: any, query: any, { acl }: Options): Promise<boolean> {
349+
validateObject(className: string, object: any, query: any, { acl }: QueryOptions): Promise<boolean> {
353350
let schema;
354351
const isMaster = acl === undefined;
355352
var aclGroup: string[] = acl || [];
@@ -368,7 +365,7 @@ class DatabaseController {
368365
acl,
369366
many,
370367
upsert,
371-
}: any = {}, skipSanitization: boolean = false): Promise<any> {
368+
}: FullQueryOptions = {}, skipSanitization: boolean = false): Promise<any> {
372369
const originalQuery = query;
373370
const originalUpdate = update;
374371
// Make a copy of the object, so we don't mutate the incoming data.
@@ -544,7 +541,7 @@ class DatabaseController {
544541
// acl: a list of strings. If the object to be updated has an ACL,
545542
// one of the provided strings must provide the caller with
546543
// write permissions.
547-
destroy(className: string, query: any, { acl }: Options = {}): Promise<any> {
544+
destroy(className: string, query: any, { acl }: QueryOptions = {}): Promise<any> {
548545
const isMaster = acl === undefined;
549546
const aclGroup = acl || [];
550547

@@ -586,7 +583,7 @@ class DatabaseController {
586583

587584
// Inserts an object into the database.
588585
// Returns a promise that resolves successfully iff the object saved.
589-
create(className: string, object: any, { acl }: Options = {}): Promise<any> {
586+
create(className: string, object: any, { acl }: QueryOptions = {}): Promise<any> {
590587
// Make a copy of the object, so we don't mutate the incoming data.
591588
const originalObject = object;
592589
object = transformObjectACL(object);
@@ -646,7 +643,7 @@ class DatabaseController {
646643

647644
// Returns a promise for a list of related ids given an owning id.
648645
// className here is the owning className.
649-
relatedIds(className: string, key: string, owningId: string, queryOptions: any): Promise<Array<string>> {
646+
relatedIds(className: string, key: string, owningId: string, queryOptions: QueryOptions): Promise<Array<string>> {
650647
const { skip, limit, sort } = queryOptions;
651648
const findOptions = {};
652649
if (sort && sort.createdAt && this.adapter.canSortOnJoinTables) {

src/Controllers/SchemaController.js

+10-28
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,17 @@
1414
// DatabaseController. This will let us replace the schema logic for
1515
// different databases.
1616
// TODO: hide all schema logic inside the database adapter.
17-
type SchemaField = {
18-
type: string;
19-
targetClass?: ?string;
20-
}
21-
22-
type SchemaFields = { [string]: SchemaField }
23-
24-
type Schema = {
25-
className: string,
26-
fields: SchemaFields,
27-
classLevelPermissions: ClassLevelPermissions
28-
};
29-
30-
type ClassLevelPermissions = {
31-
find?: {[string]: boolean};
32-
count?: {[string]: boolean};
33-
get?: {[string]: boolean};
34-
create?: {[string]: boolean};
35-
update?: {[string]: boolean};
36-
delete?: {[string]: boolean};
37-
addField?: {[string]: boolean};
38-
readUserFields?: string[];
39-
writeUserFields?: string[];
40-
};
41-
4217
// @flow-disable-next
4318
const Parse = require('parse/node').Parse;
4419
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter';
4520
import DatabaseController from './DatabaseController';
21+
import type {
22+
Schema,
23+
SchemaFields,
24+
ClassLevelPermissions,
25+
SchemaField,
26+
LoadSchemaOptions,
27+
} from './types';
4628

4729
const defaultColumns: {[string]: SchemaFields} = Object.freeze({
4830
// Contain the default columns for every parse object type (except _Join collection)
@@ -396,7 +378,7 @@ export default class SchemaController {
396378
this.perms = {};
397379
}
398380

399-
reloadData(options: any = {clearCache: false}): Promise<any> {
381+
reloadData(options: LoadSchemaOptions = {clearCache: false}): Promise<any> {
400382
let promise = Promise.resolve();
401383
if (options.clearCache) {
402384
promise = promise.then(() => {
@@ -434,7 +416,7 @@ export default class SchemaController {
434416
return this.reloadDataPromise;
435417
}
436418

437-
getAllClasses(options: any = {clearCache: false}): Promise<Array<Schema>> {
419+
getAllClasses(options: LoadSchemaOptions = {clearCache: false}): Promise<Array<Schema>> {
438420
let promise = Promise.resolve();
439421
if (options.clearCache) {
440422
promise = this._cache.clear();
@@ -455,7 +437,7 @@ export default class SchemaController {
455437
});
456438
}
457439

458-
getOneSchema(className: string, allowVolatileClasses: boolean = false, options: any = {clearCache: false}): Promise<Schema> {
440+
getOneSchema(className: string, allowVolatileClasses: boolean = false, options: LoadSchemaOptions = {clearCache: false}): Promise<Schema> {
459441
let promise = Promise.resolve();
460442
if (options.clearCache) {
461443
promise = this._cache.clear();

src/Controllers/types.js

+28
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
export type LoadSchemaOptions = {
2+
clearCache: boolean
3+
};
4+
5+
export type SchemaField = {
6+
type: string;
7+
targetClass?: ?string;
8+
}
9+
10+
export type SchemaFields = { [string]: SchemaField }
11+
12+
export type Schema = {
13+
className: string,
14+
fields: SchemaFields,
15+
classLevelPermissions: ClassLevelPermissions
16+
};
17+
18+
export type ClassLevelPermissions = {
19+
find?: {[string]: boolean};
20+
count?: {[string]: boolean};
21+
get?: {[string]: boolean};
22+
create?: {[string]: boolean};
23+
update?: {[string]: boolean};
24+
delete?: {[string]: boolean};
25+
addField?: {[string]: boolean};
26+
readUserFields?: string[];
27+
writeUserFields?: string[];
28+
};

0 commit comments

Comments
 (0)