From 95b507c45cd5cd7fa95063905ad93c7cebc63616 Mon Sep 17 00:00:00 2001 From: Sam2nx Date: Tue, 24 Feb 2015 21:29:53 +0100 Subject: [PATCH 1/2] zlink --- test/zlink.js | 88 +++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 88 insertions(+) create mode 100644 test/zlink.js diff --git a/test/zlink.js b/test/zlink.js new file mode 100644 index 0000000..cb284d2 --- /dev/null +++ b/test/zlink.js @@ -0,0 +1,88 @@ +var nohm = require(__dirname+'/../lib/nohm').Nohm; +var redisC = require('redis').createClient() + +nohm.setClient(redisC) + +var User = nohm.model('User', { + properties: { + name: { + type: 'string', + defaultValue: 'testName', + index: true, + unique: true, + }, + }, + idGenerator: 'increment', +}); + +var Location = nohm.model('Location', { + properties: { + city: { + type: 'string', + defaultValue: 'Unknown', + index: true, + unique: true, + }, + }, + idGenerator: 'increment', +}); + +function defaultvalue(cb){ + user = new User() + user.p('name', 'Samir') + user.save( + function(err){ + if (err) + cb('error saving user model: ' + err) + else + { + loc = new Location() + loc.p('city', 'Paris') + loc.save(function(err){ + if (err) + cb('error saving location model: ' + err) + else + cb(null) + }) + } + }) // save +} + +redisC.on("ready", function (err) { + console.log("connection ready") + + defaultvalue(function(err){ + if (err) + console.log("[-] Error during populate\n", err) + + // loading user + nohm.factory('User', 1, function(err){ + + userSelf = this + + // loading location + nohm.factory('Location', 1, function(err){ + locationSelf = this + + console.log("zlinking ", userSelf.p('name'), " model to ", locationSelf.p('city')) + + // linking + userSelf.link(locationSelf, { name:'link', + score: (new Date().getTime()) + }) + + // linking + userSelf.zlink(locationSelf, { name:'zlink', + score: (new Date().getTime()) + }) + + userSelf.save(function(err, islink, link_error){ + if (err) + console.log('error linking :', err, islink, link_error, this.errors) + else + console.log("Yeah zlink done!") + }) + }) + }) + }) +}) \ No newline at end of file From 46a448843c96bac38ae474c8ebe77306a7a20aa3 Mon Sep 17 00:00:00 2001 From: sam2nx Date: Tue, 24 Feb 2015 21:38:49 +0100 Subject: [PATCH 2/2] zlink --- lib/relations.js | 50 +++++++++++++++++++++++++++++++++++++++++------- 1 file changed, 43 insertions(+), 7 deletions(-) diff --git a/lib/relations.js b/lib/relations.js index 097269c..b090970 100644 --- a/lib/relations.js +++ b/lib/relations.js @@ -87,7 +87,7 @@ exports.numLinks = function numLinks(obj_name, relation_name, callback) { }); }; -var allowedLinkTypes = ['sadd', 'srem']; +var allowedLinkTypes = ['sadd', 'srem', 'zadd', 'zrem']; exports.__linkProxied = function __linkProxied(type, obj, options, callback) { @@ -102,24 +102,30 @@ exports.__linkProxied = function __linkProxied(type, obj, options, callback) { var dbVals = [{ key: self.relationKey(obj.modelName, options.name), keyStore: self.modelName+':'+self.id, - id: obj.id + id: obj.id, + score: options.score, }, { key: obj.relationKey(self.modelName, foreignName), keyStore: obj.modelName+':'+obj.id, - id: self.id + id: self.id, + score: options.score, } ]; - async.forEach(dbVals, function (val, next) { var multi = client.multi(); - multi[type](Nohm.prefix.relationKeys+val.keyStore, val.key); - multi[type](val.key, val.id); + console.log("type, Nohm.prefix.relationKeys+val.keyStoren val.key, val.id score", + type, + Nohm.prefix.relationKeys+val.keyStore, + val.key, val.id, val.score) + + multi[type](Nohm.prefix.relationKeys+val.keyStore, val.score, val.key); + multi[type](val.key, val.score, val.id); multi.exec(next); }, function (err) { if (!silent && !err) { - self.fireEvent( type === 'sadd' ? 'link' : 'unlink', obj, options.name ); + self.fireEvent( (type === 'sadd'||type === 'zadd') ? 'link' : 'unlink', obj, options.name ); } if (err && typeof(options.error) === 'function') { options.error(err, 'Linking failed.', obj); @@ -149,6 +155,13 @@ exports.__linkProxied = function __linkProxied(type, obj, options, callback) { } }; +exports.__zlink = function __link(obj, options, cb) { + this.__linkProxied('zadd', obj, options, cb); +} +exports.__zunlink = function __link(obj, options, cb) { + this.__linkProxied('zrem', obj, options, cb); +} + exports.__link = function __link(obj, options, cb) { this.__linkProxied('sadd', obj, options, cb); }; @@ -179,6 +192,29 @@ exports.link = function link(obj, options, callback) { }); }; + +/** + * Adds a sorted-set reference to another object. + */ +exports.zlink = function zlink(obj, options, callback) { + + if (typeof(options) === 'string') { + options = {name: options}; + } + var opts = h.$extend({ + name: 'default' + }, options); + + callback = h.getCallback(arguments); + + this.relationChanges.push({ + action: 'zlink', + object: obj, + options: opts, + callback: callback + }); +}; + /** * Removes the reference in the current object to * the object given in the first argument.