Skip to content

Commit c603bb2

Browse files
feat: Refactor SchemaControler to make it stateless - fixed curcular dependencies
1 parent 8691c01 commit c603bb2

File tree

10 files changed

+300
-271
lines changed

10 files changed

+300
-271
lines changed

src/Adapters/Schema/InMemorySchemaCache.js

+3-11
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,18 @@
1-
import type { Schema } from '../../Controllers/types';
2-
import { SchemaData } from '../../Controllers/SchemaController';
1+
import { SchemaAndData } from './types';
32

43
export default class InMemorySchemaCache {
5-
dataProvider: () => Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }>;
64
fetchingSchemaPromise: any;
75
cache = {};
86

9-
setDataProvider(
10-
dataProvider: () => Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }>
11-
) {
12-
this.dataProvider = dataProvider;
13-
}
14-
15-
async fetchSchema(): Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }> {
7+
async fetchSchema(getDataFromDb: () => Promise<SchemaAndData>): Promise<SchemaAndData> {
168
if (this.cache.isCached) {
179
return {
1810
allClasses: this.cache.allClasses,
1911
schemaData: this.cache.schemaData,
2012
};
2113
}
2214
if (!this.fetchingSchemaPromise) {
23-
this.fetchingSchemaPromise = this.dataProvider();
15+
this.fetchingSchemaPromise = await getDataFromDb();
2416
}
2517
const result = await this.fetchingSchemaPromise;
2618
this.cache.isCached = true;

src/Adapters/Schema/SchemaCacheAccess.js

+24-10
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,39 @@
33
* @module Adapters
44
*/
55
import type { Schema } from '../../Controllers/types';
6-
import { SchemaData } from '../../Controllers/SchemaController';
76
import SchemaCacheAdapter from './SchemaCacheAdapter';
7+
import { injectDefaultSchema, SchemaData } from '../../Schema/SchemaData';
8+
import { StorageAdapter } from '../Storage/StorageAdapter';
9+
import type { ParseServerOptions } from '../../Options';
10+
import { SchemaAndData } from './types';
811

912
/**
1013
* @interface SchemaCacheAccess
1114
*/
1215
export class SchemaCacheAccess {
13-
constructor(schemaCacheAdapter: SchemaCacheAdapter) {
16+
schemaCacheAdapter: SchemaCacheAdapter;
17+
dbAdapter: StorageAdapter;
18+
protectedFields: any;
19+
20+
constructor(schemaCacheAdapter: SchemaCacheAdapter, dbAdapter, options: ParseServerOptions) {
1421
this.schemaCacheAdapter = schemaCacheAdapter;
22+
this.dbAdapter = dbAdapter;
23+
this.protectedFields = options ? options.protectedFields : undefined;
1524
}
1625

17-
setDataProvider(
18-
dataProvider: () => Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }>
19-
) {
20-
this.schemaCacheAdapter.setDataProvider(dataProvider);
21-
}
26+
async getSchemaAndData(): Promise<SchemaAndData> {
27+
const that = this;
28+
return this.schemaCacheAdapter.fetchSchema(async () => {
29+
const rawAllSchemas = await that.dbAdapter.getAllClasses();
30+
const allSchemas = rawAllSchemas.map(injectDefaultSchema);
31+
32+
const schemaData = new SchemaData(allSchemas, that.protectedFields);
2233

23-
async getSchemaAndData(): Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }> {
24-
return this.schemaCacheAdapter.fetchSchema();
34+
return {
35+
schemaData,
36+
allClasses: allSchemas,
37+
};
38+
});
2539
}
2640

2741
async all(): Promise<Array<Schema>> {
@@ -37,7 +51,7 @@ export class SchemaCacheAccess {
3751
}
3852

3953
clear(): Promise<void> {
40-
this.schemaCacheAdapter.clear();
54+
return this.schemaCacheAdapter.clear();
4155
}
4256

4357
async getSchemaData(): Promise<SchemaData> {

src/Adapters/Schema/SchemaCacheAdapter.js

+2-10
Original file line numberDiff line numberDiff line change
@@ -2,24 +2,16 @@
22
/**
33
* @module Adapters
44
*/
5-
import type { Schema } from '../../Controllers/types';
6-
import { SchemaData } from '../../Controllers/SchemaController';
5+
import { SchemaAndData } from './types';
76

87
/**
98
* @interface SchemaCacheAdapter
109
*/
1110
export default class SchemaCacheAdapter {
12-
/**
13-
* Used by controller to provide data from database
14-
*/
15-
setDataProvider(
16-
dataProvider: () => Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }>
17-
) {}
18-
1911
/**
2012
* Get all schema entries and its corresponding intermediate format
2113
*/
22-
async fetchSchema(): Promise<{ allClasses: Array<Schema>, schemaData: SchemaData }> {}
14+
async fetchSchema(getDataFromDb: () => Promise<SchemaAndData>): Promise<SchemaAndData> {}
2315

2416
/**
2517
* Clear cache

src/Adapters/Schema/types.js

+7
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import type { Schema } from '../../Controllers/types';
2+
import { SchemaData } from '../../Schema/SchemaData';
3+
4+
export type SchemaAndData = {
5+
allClasses: Array<Schema>,
6+
schemaData: SchemaData,
7+
};

src/Controllers/DatabaseController.js

+5-6
Original file line numberDiff line numberDiff line change
@@ -364,7 +364,7 @@ const relationSchema = {
364364
class DatabaseController {
365365
adapter: StorageAdapter;
366366
schemaCache: SchemaCacheAccess;
367-
schemaController: SchemaController;
367+
schemaController: any;
368368
_transactionalSession: ?any;
369369
options: ParseServerOptions;
370370
idempotencyOptions: any;
@@ -375,7 +375,8 @@ class DatabaseController {
375375
options: ParseServerOptions
376376
) {
377377
this.adapter = adapter;
378-
this.schemaCache = schemaCache || new InMemorySchemaCache();
378+
this.schemaCache =
379+
schemaCache || new SchemaCacheAccess(new InMemorySchemaCache(), adapter, options);
379380
this.options = options || {};
380381
this.idempotencyOptions = this.options.idempotencyOptions || {};
381382
// Prevent mutable this.schema, otherwise one request could use
@@ -386,7 +387,6 @@ class DatabaseController {
386387
this.schemaCache,
387388
this.options
388389
);
389-
this.schemaCache.setDataProvider(() => this.schemaController.getSchemaObjects());
390390
}
391391

392392
collectionExists(className: string): Promise<boolean> {
@@ -891,9 +891,8 @@ class DatabaseController {
891891
* @param {boolean} fast set to true if it's ok to just delete rows and not indexes
892892
* @returns {Promise<void>} when the deletions completes
893893
*/
894-
deleteEverything(fast: boolean = false): Promise<any> {
895-
this.schemaPromise = null;
896-
this.schemaCache.clear();
894+
async deleteEverything(fast: boolean = false): Promise<any> {
895+
await this.schemaCache.clear();
897896
return this.adapter.deleteAllClasses(fast);
898897
}
899898

0 commit comments

Comments
 (0)