Skip to content

Commit 0c8180f

Browse files
committed
Moves Controller types into a single file
1 parent b257017 commit 0c8180f

File tree

6 files changed

+72
-52
lines changed

6 files changed

+72
-52
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 } 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,
@@ -429,7 +429,7 @@ export class MongoStorageAdapter implements StorageAdapter {
429429
}
430430

431431
// Executes a find. Accepts: className, query in Parse format, and { skip, limit, sort }.
432-
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys, readPreference }: QueryOptionsType): Promise<any> {
432+
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys, readPreference }: QueryOptions): Promise<any> {
433433
schema = convertParseSchemaToMongoSchema(schema);
434434
const mongoWhere = transformWhere(className, query, schema);
435435
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) {
@@ -1267,7 +1267,7 @@ export class PostgresStorageAdapter implements StorageAdapter {
12671267
});
12681268
}
12691269

1270-
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptionsType) {
1270+
find(className: string, schema: SchemaType, query: QueryType, { skip, limit, sort, keys }: QueryOptions) {
12711271
debug('find', className, query, {skip, limit, sort, keys });
12721272
const hasLimit = limit !== undefined;
12731273
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-29
Original file line numberDiff line numberDiff line change
@@ -14,36 +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-
indexes?: ?any
29-
};
30-
31-
type ClassLevelPermissions = {
32-
find?: {[string]: boolean};
33-
count?: {[string]: boolean};
34-
get?: {[string]: boolean};
35-
create?: {[string]: boolean};
36-
update?: {[string]: boolean};
37-
delete?: {[string]: boolean};
38-
addField?: {[string]: boolean};
39-
readUserFields?: string[];
40-
writeUserFields?: string[];
41-
};
42-
4317
// @flow-disable-next
4418
const Parse = require('parse/node').Parse;
4519
import { StorageAdapter } from '../Adapters/Storage/StorageAdapter';
4620
import DatabaseController from './DatabaseController';
21+
import type {
22+
Schema,
23+
SchemaFields,
24+
ClassLevelPermissions,
25+
SchemaField,
26+
LoadSchemaOptions,
27+
} from './types';
4728

4829
const defaultColumns: {[string]: SchemaFields} = Object.freeze({
4930
// Contain the default columns for every parse object type (except _Join collection)
@@ -410,7 +391,7 @@ export default class SchemaController {
410391
this.indexes = {};
411392
}
412393

413-
reloadData(options: any = {clearCache: false}): Promise<any> {
394+
reloadData(options: LoadSchemaOptions = {clearCache: false}): Promise<any> {
414395
let promise = Promise.resolve();
415396
if (options.clearCache) {
416397
promise = promise.then(() => {
@@ -453,7 +434,7 @@ export default class SchemaController {
453434
return this.reloadDataPromise;
454435
}
455436

456-
getAllClasses(options: any = {clearCache: false}): Promise<Array<Schema>> {
437+
getAllClasses(options: LoadSchemaOptions = {clearCache: false}): Promise<Array<Schema>> {
457438
let promise = Promise.resolve();
458439
if (options.clearCache) {
459440
promise = this._cache.clear();
@@ -474,7 +455,7 @@ export default class SchemaController {
474455
});
475456
}
476457

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

src/Controllers/types.js

+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
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+
indexes?: ?any
17+
};
18+
19+
export type ClassLevelPermissions = {
20+
find?: {[string]: boolean};
21+
count?: {[string]: boolean};
22+
get?: {[string]: boolean};
23+
create?: {[string]: boolean};
24+
update?: {[string]: boolean};
25+
delete?: {[string]: boolean};
26+
addField?: {[string]: boolean};
27+
readUserFields?: string[];
28+
writeUserFields?: string[];
29+
};

0 commit comments

Comments
 (0)