Permalink
Browse files

Change meta version checking to only be called once for every model d…

…efinition. (There were no actionable consequences on version mismatching anyways, might have to be included in the future though)
  • Loading branch information...
1 parent a6b66b1 commit 4fa6f630bc2e6156684eed735d94fe30d00c9abc @maritz committed Jul 5, 2013
Showing with 62 additions and 3 deletions.
  1. +14 −3 lib/nohm.js
  2. +48 −0 test/metaTests.js
View
@@ -243,7 +243,7 @@ Nohm.prototype.init = function (options) {
}
if ( ! this.meta.inDb) {
- __updateMeta.call(this);
+ __updateMeta.call(this, options.metaCallback);
}
if (typeof(options.client) !== 'undefined') {
@@ -292,18 +292,23 @@ Nohm.prototype.init = function (options) {
-var __updateMeta = function () {
+var __updateMeta = function (callback) {
if ( ! Nohm.meta) {
return false;
}
+
+ if (typeof(callback) !== 'function') {
+ callback = function () {};
+ }
+
var self = this;
var version_key = Nohm.prefix.meta.version + this.modelName;
var idGenerator_key = Nohm.prefix.meta.idGenerator + this.modelName;
var properties_key = Nohm.prefix.meta.properties + this.modelName;
var properties = traverse(self.meta.properties).map(function (x) {
if (typeof x === 'function') {
- return String(x)
+ return String(x);
} else {
return x;
}
@@ -312,6 +317,7 @@ var __updateMeta = function () {
this.getClient().get(version_key, function (err, db_version) {
if (err) {
Nohm.logError(err);
+ callback(err);
} else if (self.meta.version !== db_version) {
async.parallel({
version: function (next) {
@@ -326,10 +332,15 @@ var __updateMeta = function () {
}, function (err) {
if (err) {
Nohm.logError(err);
+ callback(err, self.meta.version);
} else {
self.meta.inDb = true;
+ callback(null, self.meta.version);
}
});
+ } else {
+ self.meta.inDb = true;
+ callback(null, self.meta.version);
}
});
};
View
@@ -172,6 +172,54 @@ exports.meta = {
});
},
+ "meta callback and setting meta.inDb": function(t) {
+ t.expect(4);
+ var test, testInstance;
+
+
+ test = nohm.model('TestVersionMetaMockup', {
+ properties: {
+ name: {
+ type: 'string',
+ defaultValue: 'testPropertyy',
+ }
+ },
+ metaCallback: function (err, version) {
+ t.same(err, null, "Meta version callback had an error.");
+ t.same(test.prototype.meta.inDb, true, "Meta version inDb was not false.");
+ t.ok(version, "No version in meta.inDb callback");
+ t.done();
+ }
+ });
+
+ testInstance = new test();
+ t.same(test.prototype.meta.inDb, false, "Meta version inDb was not false.");
+ },
+
+ "meta set inDb when version is correct": function(t) {
+ t.expect(4);
+ var test, testInstance;
+
+
+ test = nohm.model('TestVersionMetaMockup', {
+ properties: {
+ name: {
+ type: 'string',
+ defaultValue: 'testPropertyy',
+ }
+ },
+ metaCallback: function (err, version) {
+ t.same(err, null, "Meta version callback had an error.");
+ t.same(test.prototype.meta.inDb, true, "Meta version inDb was not false.");
+ t.ok(version, "No version in meta.inDb callback");
+ t.done();
+ }
+ });
+
+ testInstance = new test();
+ t.same(test.prototype.meta.inDb, false, "Meta version inDb was not false.");
+ },
+
idGenerator: function(t) {
var user = nohm.factory('UserMetaMockup');
var comment = nohm.factory('CommentMetaMockup');

0 comments on commit 4fa6f63

Please sign in to comment.