Do not store null values

This commit is contained in:
Anatoliy Chakkaev 2012-01-14 00:06:57 +04:00
parent c3835d089b
commit a8db2ad081
3 changed files with 15 additions and 6 deletions

View File

@ -39,6 +39,7 @@ BridgeToRedis.prototype.defineForeignKey = function (model, key, cb) {
};
BridgeToRedis.prototype.save = function (model, data, callback) {
deleteNulls(data);
var log = this.logger('HMSET ' + model + ':' + data.id + ' ...');
this.client.hmset(model + ':' + data.id, data, function (err) {
log();
@ -214,12 +215,19 @@ BridgeToRedis.prototype.count = function count(model, callback) {
BridgeToRedis.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
var t1 = Date.now();
deleteNulls(data);
this.client.hmset(model + ':' + id, data, function () {
this.log('HMSET ' + model + ':' + id, t1);
this.updateIndexes(model, id, data, cb);
}.bind(this));
};
function deleteNulls(data) {
Object.keys(data).forEach(function (key) {
if (data[key] === null) delete data[key];
});
}
BridgeToRedis.prototype.disconnect = function disconnect() {
this.log('QUIT', Date.now());
this.client.quit();

View File

@ -309,7 +309,7 @@ function testOrm(schema) {
res.forEach(function (r) {
if (r.title != 'New title') pass = false;
});
test.ok(res.length > 0);
test.ok(res.length > 0, 'Exact match with string returns dataset');
test.ok(pass, 'Exact match with string');
done();
});
@ -321,7 +321,7 @@ function testOrm(schema) {
res.forEach(function (r) {
if (r.title != null) pass = false;
});
test.ok(res.length > 0);
test.ok(res.length > 0, 'Matching null returns dataset');
test.ok(pass, 'Matching null');
done();
});
@ -332,7 +332,7 @@ function testOrm(schema) {
res.forEach(function (r) {
if (!r.title || !r.title.match(/hello/i)) pass = false;
});
test.ok(res.length > 0);
test.ok(res.length > 0, 'Matching regexp returns dataset');
test.ok(pass, 'Matching regexp');
done();
});

View File

@ -2,13 +2,14 @@ juggling = require('../index')
Schema = juggling.Schema
Text = Schema.Text
DBNAME = 'migrationtest'
DBUSER = 'root'
DBNAME = process.env.DBNAME || 'migrationtest'
DBUSER = process.env.DBUSER || 'root'
DBPASS = ''
DBENGINE = process.env.DBENGINE || 'mysql'
require('./spec_helper').init module.exports
schema = new Schema 'mysql', database: '', username: DBUSER, password: DBPASS
schema = new Schema DBENGINE, database: '', username: DBUSER, password: DBPASS
schema.log = (q) -> console.log q
query = (sql, cb) ->