Skip to content
Browse files

Bump version to v0.1.4

Added partialSave which only saves properties that have been changed. (fails if any of them are invalid)
  • Loading branch information...
1 parent c5eb270 commit 5c855778594fd1c3056e16b8fd84360f16467848 @maritz committed Sep 30, 2010
Showing with 80 additions and 7 deletions.
  1. +2 −0 HISTORY.md
  2. +37 −5 lib/nohm.js
  3. +5 −2 package.json
  4. +36 −0 test/features.js
View
2 HISTORY.md
@@ -1,3 +1,5 @@
+### v0.1.4
+ - three small changes
### v0.1.3
- added numLinks()
- lots of bugfixes and some semi-internal changes
View
42 lib/nohm.js
@@ -319,6 +319,40 @@ var ModelClass = {
}
});
},
+
+ /**
+ * Only operates on properties that have been changed
+ */
+ partialSave: function partialSave(callback) {
+ if (!this.id) {
+ var err = 'Trying to do a partialSave on an object without id.'
+ this.logError(err);
+ callback(err);
+ }
+ var props = this.properties,
+ num_updated = 0,
+ success = true,
+ self = this,
+ validCallback = function (valid) {
+ if (!valid) {
+ success = false;
+ }
+ num_updated = num_updated - 1;
+ if (num_updated <= 0) {
+ if (! success) {
+ callback('invalid');
+ } else {
+ self.__update(false, callback);
+ }
+ }
+ };
+ for (p in props) {
+ if (props[p].__updated) {
+ num_updated = num_updated + 1;
+ self.valid(p, true, validCallback);
+ }
+ }
+ },
/**
* Creates a new empty (!) dataset in the database and calls __update to populate it.
@@ -397,9 +431,6 @@ var ModelClass = {
}
}
}
- // call the provided callback.
- // we do this here instead of nested in the dels/srems/sadds above to speed things a little up.
- // this means there might be problems that you only see if you look at the logger. :/
self.__inDB = true;
if (typeof callback !== 'function' && err) {
sys.debug('Nohm: Updating an object resulted in a redis error: ' + sys.inspect(err));
@@ -932,7 +963,8 @@ var ModelClass = {
*/
valid: function valid(key, setDirectly, callback) {
var validbool = true,
- p;
+ p,
+ self = this;
for (p in this.properties) {
if (!key || key === p) {
if (!this.__validateProperty(p)) {
@@ -952,7 +984,7 @@ var ModelClass = {
if (!key && !success) {
validbool = false;
} else if (key && !success) {
- validbool = !!this.errors[key];
+ validbool = !!self.errors[key];
}
callback(validbool);
});
View
7 package.json
@@ -1,6 +1,6 @@
{
"name": "nohm",
- "version": "0.1.3",
+ "version": "0.1.4",
"engines": {
"node" : ">=0.2.0"
},
@@ -9,5 +9,8 @@
"lib": "./lib"
},
"description": "redis ORM (Object relational mapper)",
- "main": "./lib/nohm"
+ "main": "./lib/nohm",
+ "dependencies": {
+ "conductor": ">=0.1.0"
+ }
}
View
36 test/features.js
@@ -350,6 +350,42 @@ exports.update = function (t) {
});
};
+exports.partialUpdate = function (t) {
+ var user = new UserMockup(),
+ user2 = new UserMockup();
+ t.expect(6);
+
+ user.p('name', 'updateTest1');
+ user.p('email', 'updateTest1@email.de');
+ user.save(function (err) {
+ t.ok(!err, 'There was a redis error in the update test. (creation part)');
+ if (err) {
+ t.done();
+ }
+ user2.id = user.id;
+ user2.p({
+ name: 'updateTest2',
+ visits: 10
+ });
+ user2.partialSave(function (err) {
+ t.ok(!err, 'There was a redis error in the update test.');
+ if (err) {
+ t.done();
+ }
+ redis.hgetall(prefix + ':hash:UserMockup:' + user2.id, function (err, value) {
+ t.ok(!err, 'There was a redis error in the update test check.');
+ if (err) {
+ t.done();
+ }
+ t.ok(value.name.toString() === 'updateTest2', 'The user name was not updated properly');
+ t.ok(value.email.toString() === 'updateTest1@email.de', 'The user email was not left correctly.');
+ t.ok(value.visits.toString() === '10', 'The visits were not set correctly.');
+ t.done();
+ });
+ });
+ });
+};
+
exports.unique = function (t) {
var user1 = new UserMockup(),
user2 = new UserMockup();

0 comments on commit 5c85577

Please sign in to comment.
Something went wrong with that request. Please try again.