* persistAllProperties() and persistProperties() for User obje

This commit is contained in:
Bryan Ashby 2015-08-16 21:47:33 -06:00
parent d491bd9582
commit d71cae5c37
1 changed files with 30 additions and 2 deletions

View File

@ -280,7 +280,7 @@ User.prototype.persist = function(useTransaction, cb) {
} }
}, },
function saveProps(callback) { function saveProps(callback) {
self.persistProperties(function persisted(err) { self.persistAllProperties(function persisted(err) {
callback(err); callback(err);
}); });
}, },
@ -324,9 +324,36 @@ User.prototype.persistProperty = function(propName, propValue, cb) {
); );
} }
User.prototype.persistProperties = function(cb) { User.prototype.persistProperties = function(properties, cb) {
var self = this;
// :TODO: should probably add/update live properties
var stmt = userDb.prepare(
'REPLACE INTO user_property (user_id, prop_name, prop_value) ' +
'VALUES (?, ?, ?);');
async.each(Object.keys(properties), function property(propName, callback) {
stmt.run(self.userId, propName, properties[propName], function onRun(err) {
callback(err);
});
}, function complete(err) {
if(err) {
cb(err);
} else {
stmt.finalize(function finalized() {
cb(null);
});
}
});
};
User.prototype.persistAllProperties = function(cb) {
assert(this.userId > 0); assert(this.userId > 0);
this.persistProperties(this.properties, cb);
/*
var self = this; var self = this;
var stmt = userDb.prepare( var stmt = userDb.prepare(
@ -346,6 +373,7 @@ User.prototype.persistProperties = function(cb) {
}); });
} }
}); });
*/
}; };
User.prototype.getAge = function() { User.prototype.getAge = function() {