Skip to content

Instantly share code, notes, and snippets.

@dvv
Created June 28, 2011 06:58
Show Gist options
  • Save dvv/1050641 to your computer and use it in GitHub Desktop.
Save dvv/1050641 to your computer and use it in GitHub Desktop.
'use strict';
/*!
*
* redis-backed Spine Model
*
* Copyright(c) 2011 Vladimir Dronnikov <[email protected]>
* MIT Licensed
*
*/
var db = require('redis').createClient();
var Model = require('spine').Model;
var PModel = Model.extend({
fetch: function(criteria, options) {
if (!options) options = {};
var attrs = options.attributes || this.attributes;
console.log('FETCH', arguments);
var model = this;
var args = [model.name];
var alen = attrs.length;
args.push('get');
args.push('#');
for (var i = 0; i < alen; ++i) {
args.push('get');
args.push(model.name + '/*:' + attrs[i]);
}
console.log('CMDS', args);
db.sort(args, function(err, props) {
if (!props) return;
//var records = {};
var records = [];
for (var i = 0, j = 0; i < props.length; ++i, ++j) {
if (!j) {
var o = {id: props[i]};
continue;
}
if (props[i] != null) {
o[attrs[j-1]] = props[i];
}
if (j === alen) {
j = -1;
//records[o.id] = o;
records.push(o);
}
}
console.log('FETCHED', props, records);
model.refresh(records);
console.log('REFRESHED', model.all());
});
}
}).include({
rem: function(next) {
console.log('REM', arguments);
var record = this;
var model = this.parent;
var id = record.id;
//var commands = model.attributes.concat(['id']).map(function(attr) {
var commands = model.attributes.map(function(attr) {
return ['del', model.name + '/' + id + ':' + attr];
});
if (model.numerics) {
commands.push.apply(commands, model.numerics.map(function(attr) {
return ['zrem', model.name + ':' + attr, id];
}));
}
commands.push(['zrem', model.name + ':time', id]);
commands.push(['srem', model.name, id]);
console.log('REMCMDS', commands);
db.multi(commands).exec(next);
},
sohr: function(next) {
console.log('PUT', arguments);
var record = this;
var model = this.parent;
var id = record.id;
//var commands = model.attributes.concat(['id']).map(function(attr) {
var commands = model.attributes.map(function(attr) {
return record[attr] != null ?
['set', model.name + '/' + id + ':' + attr, record[attr]] :
['del', model.name + '/' + id + ':' + attr];
});
if (model.numerics) {
commands.push.apply(commands, model.numerics.map(function(attr) {
return record[attr] != null ?
['zadd', model.name + ':' + attr, record[attr], id] :
['zrem', model.name + ':' + attr, id];
}));
}
commands.push(['zadd', model.name + ':time', Date.now(), id]);
commands.push(['sadd', model.name, id]);
console.log('SAVE', commands);
db.multi(commands).exec(next);
}
});
var Foo = PModel.setup('Foo', ['foo', 'bar', 'baz']);
var A = {
extended: function() {
this.sync(function(record, method) {
console.log('SYNC:' + record.id, arguments);
if (method === 'destroy') {
record.rem();
} else {
record.sohr();
}
});
}
};
Foo.extend(A);
//Foo.uniques = ['foo', 'baz'];
Foo.numerics = ['foo'];
Foo.fetch(1,2,3);
var x = Foo.init({foo: Math.random(), bar: Math.random()>=0.5});
x.save();
x.updateAttributes({baz: Math.random()});
//x.destroy();
//console.log(x.attributes());
//Foo.query();
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment