Skip to content

feat: make schema controller and schema cache stateless #7951

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 3 commits into
base: alpha
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
42 changes: 21 additions & 21 deletions spec/DatabaseController.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -69,16 +69,16 @@ describe('DatabaseController', function () {
'getExpectedType',
]);

it('should not decorate query if no pointer CLPs are present', done => {
it('should not decorate query if no pointer CLPs are present', async done => {
const clp = buildCLP();
const query = { a: 'b' };

schemaController.testPermissionsForClassName
.withArgs(CLASS_NAME, ACL_GROUP, OPERATION)
.and.returnValue(true);
schemaController.getClassLevelPermissions.withArgs(CLASS_NAME).and.returnValue(clp);
await schemaController.getClassLevelPermissions.withArgs(CLASS_NAME).and.returnValue(clp);

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -91,7 +91,7 @@ describe('DatabaseController', function () {
done();
});

it('should decorate query if a pointer CLP entry is present', done => {
it('should decorate query if a pointer CLP entry is present', async done => {
const clp = buildCLP(['user']);
const query = { a: 'b' };

Expand All @@ -103,7 +103,7 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'user')
.and.returnValue({ type: 'Pointer' });

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -116,7 +116,7 @@ describe('DatabaseController', function () {
done();
});

it('should decorate query if an array CLP entry is present', done => {
it('should decorate query if an array CLP entry is present', async done => {
const clp = buildCLP(['users']);
const query = { a: 'b' };

Expand All @@ -128,7 +128,7 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'users')
.and.returnValue({ type: 'Array' });

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -144,7 +144,7 @@ describe('DatabaseController', function () {
done();
});

it('should decorate query if an object CLP entry is present', done => {
it('should decorate query if an object CLP entry is present', async done => {
const clp = buildCLP(['user']);
const query = { a: 'b' };

Expand All @@ -156,7 +156,7 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'user')
.and.returnValue({ type: 'Object' });

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -172,7 +172,7 @@ describe('DatabaseController', function () {
done();
});

it('should decorate query if a pointer CLP is present and the same field is part of the query', done => {
it('should decorate query if a pointer CLP is present and the same field is part of the query', async done => {
const clp = buildCLP(['user']);
const query = { a: 'b', user: 'a' };

Expand All @@ -184,7 +184,7 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'user')
.and.returnValue({ type: 'Pointer' });

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -199,7 +199,7 @@ describe('DatabaseController', function () {
done();
});

it('should transform the query to an $or query if multiple array/pointer CLPs are present', done => {
it('should transform the query to an $or query if multiple array/pointer CLPs are present', async done => {
const clp = buildCLP(['user', 'users', 'userObject']);
const query = { a: 'b' };

Expand All @@ -217,7 +217,7 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'userObject')
.and.returnValue({ type: 'Object' });

const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -236,7 +236,7 @@ describe('DatabaseController', function () {
done();
});

it('should not return a $or operation if the query involves one of the two fields also used as array/pointer permissions', done => {
it('should not return a $or operation if the query involves one of the two fields also used as array/pointer permissions', async done => {
const clp = buildCLP(['users', 'user']);
const query = { a: 'b', user: createUserPointer(USER_ID) };
schemaController.testPermissionsForClassName
Expand All @@ -249,7 +249,7 @@ describe('DatabaseController', function () {
schemaController.getExpectedType
.withArgs(CLASS_NAME, 'users')
.and.returnValue({ type: 'Array' });
const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -260,7 +260,7 @@ describe('DatabaseController', function () {
done();
});

it('should not return a $or operation if the query involves one of the fields also used as array/pointer permissions', done => {
it('should not return a $or operation if the query involves one of the fields also used as array/pointer permissions', async done => {
const clp = buildCLP(['user', 'users', 'userObject']);
const query = { a: 'b', user: createUserPointer(USER_ID) };
schemaController.testPermissionsForClassName
Expand All @@ -276,7 +276,7 @@ describe('DatabaseController', function () {
schemaController.getExpectedType
.withArgs(CLASS_NAME, 'userObject')
.and.returnValue({ type: 'Object' });
const output = databaseController.addPointerPermissions(
const output = await databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
Expand All @@ -287,7 +287,7 @@ describe('DatabaseController', function () {
done();
});

it('should throw an error if for some unexpected reason the property specified in the CLP is neither a pointer nor an array', done => {
it('should throw an error if for some unexpected reason the property specified in the CLP is neither a pointer nor an array', async done => {
const clp = buildCLP(['user']);
const query = { a: 'b' };

Expand All @@ -299,15 +299,15 @@ describe('DatabaseController', function () {
.withArgs(CLASS_NAME, 'user')
.and.returnValue({ type: 'Number' });

expect(() => {
await expectAsync(
databaseController.addPointerPermissions(
schemaController,
CLASS_NAME,
OPERATION,
query,
ACL_GROUP
);
}).toThrow(
)
).toBeRejectedWith(
Error(
`An unexpected condition occurred when resolving pointer permissions: ${CLASS_NAME} user`
)
Expand Down
25 changes: 18 additions & 7 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -875,6 +875,14 @@ describe('SchemaController', () => {
addField: { '*': true },
protectedFields: { '*': [] },
},
indexes: {
_id_: {
_id: 1,
},
name_1: {
name: 1,
},
},
};
expect(dd(actualSchema, expectedSchema)).toEqual(undefined);
done();
Expand Down Expand Up @@ -1097,14 +1105,15 @@ describe('SchemaController', () => {
})
.then(() => schema.deleteField('relationField', 'NewClass', config.database))
.then(() => schema.reloadData())
.then(() => {
.then(async () => {
const expectedSchema = {
objectId: { type: 'String' },
updatedAt: { type: 'Date' },
createdAt: { type: 'Date' },
ACL: { type: 'ACL' },
};
expect(dd(schema.schemaData.NewClass.fields, expectedSchema)).toEqual(undefined);
const schemaData = await schema.getSchemaData();
expect(dd(schemaData.NewClass.fields, expectedSchema)).toEqual(undefined);
})
.then(done)
.catch(done.fail);
Expand Down Expand Up @@ -1331,14 +1340,16 @@ describe('SchemaController', () => {
schema = s;
return schema.getOneSchema('_User', false);
})
.then(userSchema => {
.then(async userSchema => {
validateSchemaStructure(userSchema);
validateSchemaDataStructure(schema.schemaData);
const schemaData = await schema.getSchemaData();
validateSchemaDataStructure(schemaData);
return schema.getOneSchema('_PushStatus', true);
})
.then(pushStatusSchema => {
.then(async pushStatusSchema => {
const schemaData = await schema.getSchemaData();
validateSchemaStructure(pushStatusSchema);
validateSchemaDataStructure(schema.schemaData);
validateSchemaDataStructure(schemaData);
})
.then(done)
.catch(done.fail);
Expand All @@ -1353,7 +1364,7 @@ describe('SchemaController', () => {
it('ensureFields should throw when schema is not set', async () => {
const schema = await config.database.loadSchema();
try {
schema.ensureFields([
await schema.ensureFields([
{
className: 'NewClass',
fieldName: 'fieldName',
Expand Down
2 changes: 1 addition & 1 deletion spec/SchemaPerformance.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,6 @@ describe('Schema Performance', function () {
{},
config.database
);
expect(getAllSpy.calls.count()).toBe(2);
expect(getAllSpy.calls.count()).toBe(4);
});
});
2 changes: 1 addition & 1 deletion spec/schemas.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -1556,7 +1556,7 @@ describe('schemas', () => {

it('ensure refresh cache after deleting a class', async done => {
config = Config.get('test');
spyOn(config.schemaCache, 'del').and.callFake(() => {});
spyOn(config.schemaCache, 'clear').and.callFake(() => {});
spyOn(SchemaController.prototype, 'reloadData').and.callFake(() => Promise.resolve());
await request({
url: 'http://localhost:8378/1/schemas',
Expand Down
23 changes: 0 additions & 23 deletions src/Adapters/Cache/SchemaCache.js

This file was deleted.

34 changes: 34 additions & 0 deletions src/Adapters/Schema/InMemorySchemaCache.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
import { SchemaAndData } from './types';

export default class InMemorySchemaCache {
fetchingSchemaPromise: any;
cache = {};

async fetchSchema(getDataFromDb: () => Promise<SchemaAndData>): Promise<SchemaAndData> {
if (this.cache.isCached) {
return {
allClasses: this.cache.allClasses,
schemaData: this.cache.schemaData,
};
}
if (!this.fetchingSchemaPromise) {
this.fetchingSchemaPromise = await getDataFromDb();
}
const result = await this.fetchingSchemaPromise;
this.cache.isCached = true;
this.cache.allClasses = result ? result.allClasses : undefined;
this.cache.schemaData = result ? result.schemaData : undefined;

return {
allClasses: this.cache.allClasses,
schemaData: this.cache.schemaData,
};
}

clear(): Promise<void> {
this.cache.isCached = false;
this.cache.allClasses = undefined;
this.cache.schemaData = undefined;
this.fetchingSchemaPromise = undefined;
}
}
62 changes: 62 additions & 0 deletions src/Adapters/Schema/SchemaCacheAccess.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
/*eslint no-unused-vars: "off"*/
/**
* @module Adapters
*/
import type { Schema } from '../../Controllers/types';
import SchemaCacheAdapter from './SchemaCacheAdapter';
import { injectDefaultSchema, SchemaData } from '../../Schema/SchemaData';
import { StorageAdapter } from '../Storage/StorageAdapter';
import type { ParseServerOptions } from '../../Options';
import { SchemaAndData } from './types';

/**
* @interface SchemaCacheAccess
*/
export class SchemaCacheAccess {
schemaCacheAdapter: SchemaCacheAdapter;
dbAdapter: StorageAdapter;
protectedFields: any;

constructor(schemaCacheAdapter: SchemaCacheAdapter, dbAdapter, options: ParseServerOptions) {
this.schemaCacheAdapter = schemaCacheAdapter;
this.dbAdapter = dbAdapter;
this.protectedFields = options ? options.protectedFields : undefined;
}

async getSchemaAndData(): Promise<SchemaAndData> {
const that = this;
return this.schemaCacheAdapter.fetchSchema(async () => {
const rawAllSchemas = await that.dbAdapter.getAllClasses();
const allSchemas = rawAllSchemas.map(injectDefaultSchema);

const schemaData = new SchemaData(allSchemas, that.protectedFields);

return {
schemaData,
allClasses: allSchemas,
};
});
}

async all(): Promise<Array<Schema>> {
const data = await this.getSchemaAndData();

return data.allClasses;
}

async get(className): Promise<Schema> {
const allSchemas = await this.all();

return allSchemas.find(cached => cached.className === className);
}

clear(): Promise<void> {
return this.schemaCacheAdapter.clear();
}

async getSchemaData(): Promise<SchemaData> {
const data = await this.getSchemaAndData();

return data.schemaData;
}
}
20 changes: 20 additions & 0 deletions src/Adapters/Schema/SchemaCacheAdapter.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
/*eslint no-unused-vars: "off"*/
/**
* @module Adapters
*/
import { SchemaAndData } from './types';

/**
* @interface SchemaCacheAdapter
*/
export default class SchemaCacheAdapter {
/**
* Get all schema entries and its corresponding intermediate format
*/
async fetchSchema(getDataFromDb: () => Promise<SchemaAndData>): Promise<SchemaAndData> {}

/**
* Clear cache
*/
clear(): Promise<void> {}
}
Loading