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..29885e368a 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) => { + 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); }); }; 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);