Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
50 changes: 43 additions & 7 deletions lib/relations.js
Original file line number Diff line number Diff line change
Expand Up @@ -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) {

Expand All @@ -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);
Expand Down Expand Up @@ -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);
};
Expand Down Expand Up @@ -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.
Expand Down
88 changes: 88 additions & 0 deletions test/zlink.js
Original file line number Diff line number Diff line change
@@ -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!")
})
})
})
})
})