From 386609f1cb9b6dc11fb358c570b0694f10d757c5 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Tue, 5 Apr 2016 21:25:28 -0400 Subject: [PATCH 1/2] invalidates schema once per request --- spec/Schema.spec.js | 4 +++ src/Controllers/DatabaseController.js | 39 ++++++++++++++++----------- src/middlewares.js | 1 + 3 files changed, 29 insertions(+), 15 deletions(-) diff --git a/spec/Schema.spec.js b/spec/Schema.spec.js index 7e87a72e41..59553a37db 100644 --- a/spec/Schema.spec.js +++ b/spec/Schema.spec.js @@ -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}); diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index 0475c301ac..b7c88964a8 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -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) => { + db._schema = res; + return Promise.resolve(db._schema); + }) } + return db.schemaPromise; +} - return this.schemaPromise.then((schema) => { +DatabaseController.prototype.invalidateSchema = function() { + delete this._schema; + delete this.schemaPromise; +} + +// 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); }); }; diff --git a/src/middlewares.js b/src/middlewares.js index d56840b4b0..49adef30d6 100644 --- a/src/middlewares.js +++ b/src/middlewares.js @@ -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); From 896f62b717817556e78a1b3acd945687604e0136 Mon Sep 17 00:00:00 2001 From: Florent Vilmart Date: Thu, 7 Apr 2016 10:43:12 -0400 Subject: [PATCH 2/2] nits --- src/Controllers/DatabaseController.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Controllers/DatabaseController.js b/src/Controllers/DatabaseController.js index b7c88964a8..29885e368a 100644 --- a/src/Controllers/DatabaseController.js +++ b/src/Controllers/DatabaseController.js @@ -68,6 +68,7 @@ function _loadSchema(db) { db.schemaPromise = db.schemaCollection().then(collection => { return Schema.load(collection); }).then((res) => { + delete db.schemaPromise; db._schema = res; return Promise.resolve(db._schema); }) @@ -77,7 +78,6 @@ function _loadSchema(db) { DatabaseController.prototype.invalidateSchema = function() { delete this._schema; - delete this.schemaPromise; } // Returns a promise for a schema object.