From 9fab7cc5ae846a5e0016354a70cb40fa867e2b21 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20Bajto=C5=A1?= Date: Wed, 14 May 2014 15:16:41 +0200 Subject: [PATCH] Project: toConfig() must not use stale relations Modify Project.toConfig() to always fetch the related models like model and datasource definitions from the datastore, bypassing any possibly stale cached values. Related: strongloop/loopback#256 --- models/project.js | 3 ++- test/project.test.js | 37 +++++++++++++++++++++++++++++++++++++ 2 files changed, 39 insertions(+), 1 deletion(-) diff --git a/models/project.js b/models/project.js index 4765aeb8..8d803b7e 100644 --- a/models/project.js +++ b/models/project.js @@ -63,7 +63,8 @@ Project.prototype.toConfig = function(cb) { function findAndReduce(type) { return function(cb) { - project[type](function(err, objects) { + var FORCE_RELOAD = true; + project[type](FORCE_RELOAD, function(err, objects) { if(err) return cb(err); config[type] = objects.reduce(reduce, {}); cb(); diff --git a/test/project.test.js b/test/project.test.js index 7e26263c..aa2dd7d3 100644 --- a/test/project.test.js +++ b/test/project.test.js @@ -223,6 +223,43 @@ describe('Project', function () { }); }); }); + + it('should ignore cached relations', function(done) { + var dir = SANDBOX; + + async.waterfall([ + function load(next) { + loadProject(next) + }, + function fillProjectModelsCache(project, next) { + project.models(function(err) { + next(err, project); + }); + }, + function updateModelOptions(project, next) { + project.models( + { where: { name: 'foo-bar'}, limit: 1}, + function(err, res) { + if (err) return done(err); + var model = res[0]; + model.options.newOption = true; + model.save(function(err) { + next(err, project); + }); + }); + }, + function save(project, next) { + project.saveToFiles(dir, function(err) { + next(err, project); + }); + }, + function verify(project, next) { + var modelsJson = path.join(dir, 'models.json'); + assertJSONFileHas(modelsJson, 'foo-bar.options.newOption', true); + next(); + }, + ], done); + }); }); describe('project.getDataSourceByName(name, cb)', function () {