Skip to content

WIP: Reduces number of calls to _SCHEMA #1387

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

Closed
wants to merge 2 commits into from
Closed
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
4 changes: 4 additions & 0 deletions spec/Schema.spec.js
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@ var hasAllPODobject = () => {
};

describe('Schema', () => {
beforeEach(() => {
config.database.invalidateSchema();
});

it('can validate one object', (done) => {
config.database.loadSchema().then((schema) => {
return schema.validateObject('TestObject', {a: 1, b: 'yo', c: false});
Expand Down
39 changes: 24 additions & 15 deletions src/Controllers/DatabaseController.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,28 +59,37 @@ DatabaseController.prototype.validateClassName = function(className) {
return Promise.resolve();
};

// Returns a promise for a schema object.
// If we are provided a acceptor, then we run it on the schema.
// If the schema isn't accepted, we reload it at most once.
DatabaseController.prototype.loadSchema = function(acceptor = () => true) {
function _loadSchema(db) {
if (db._schema) {
return Promise.resolve(db._schema);
}

if (!this.schemaPromise) {
this.schemaPromise = this.schemaCollection().then(collection => {
delete this.schemaPromise;
if (!db.schemaPromise) {
db.schemaPromise = db.schemaCollection().then(collection => {
return Schema.load(collection);
});
return this.schemaPromise;
}).then((res) => {
delete db.schemaPromise;
db._schema = res;
return Promise.resolve(db._schema);
})
}
return db.schemaPromise;
}

return this.schemaPromise.then((schema) => {
DatabaseController.prototype.invalidateSchema = function() {
delete this._schema;
}

// Returns a promise for a schema object.
// If we are provided a acceptor, then we run it on the schema.
// If the schema isn't accepted, we reload it at most once.
DatabaseController.prototype.loadSchema = function(acceptor = returnsTrue) {
return _loadSchema(this).then((schema) => {
if (acceptor(schema)) {
return schema;
}
this.schemaPromise = this.schemaCollection().then(collection => {
delete this.schemaPromise;
return Schema.load(collection);
});
return this.schemaPromise;
this.invalidateSchema();
return _loadSchema(this);
});
};

Expand Down
1 change: 1 addition & 0 deletions src/middlewares.js
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@ function handleParseHeaders(req, res, next) {

info.app = cache.apps.get(info.appId);
req.config = new Config(info.appId, mount);
req.config.database.invalidateSchema();
req.info = info;

var isMaster = (info.masterKey === req.config.masterKey);
Expand Down