From a8db2ad08144d38ffb368dbdc8d2499c532675c9 Mon Sep 17 00:00:00 2001 From: Anatoliy Chakkaev Date: Sat, 14 Jan 2012 00:06:57 +0400 Subject: [PATCH] Do not store null values --- lib/adapters/redis.js | 8 ++++++++ test/common_test.js | 6 +++--- test/migration_test.coffee | 7 ++++--- 3 files changed, 15 insertions(+), 6 deletions(-) diff --git a/lib/adapters/redis.js b/lib/adapters/redis.js index 08aa05b7..896867eb 100644 --- a/lib/adapters/redis.js +++ b/lib/adapters/redis.js @@ -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(); diff --git a/test/common_test.js b/test/common_test.js index 62f361c5..34c180b2 100644 --- a/test/common_test.js +++ b/test/common_test.js @@ -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(); }); diff --git a/test/migration_test.coffee b/test/migration_test.coffee index a3343454..9aeb89f4 100644 --- a/test/migration_test.coffee +++ b/test/migration_test.coffee @@ -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) ->