New Case Study:See how Anthropic automated 95% of dependency reviews with Socket.Learn More
Socket
Sign inDemoInstall
Socket

backbone-db-redis

Package Overview
Dependencies
Maintainers
2
Versions
48
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-db-redis - npm Package Compare versions

Comparing version 0.0.15 to 0.0.16

.jscs.json

100

index.js

@@ -1,2 +0,2 @@

var _ = require('underscore'),
var _ = require('lodash'),
Backbone = require('backbone'),

@@ -10,4 +10,4 @@ Db = require('backbone-db'),

Backbone.RedisDb = function(name, client) {
this.name = name || "";
var RedisDb = Backbone.RedisDb = function(name, client) {
this.name = name || '';
this.redis = client;

@@ -20,3 +20,3 @@ if (!this.redis) {

Backbone.RedisDb.prototype.key = function(key) {
if (this.name === "") {
if (this.name === '') {
return key;

@@ -28,7 +28,14 @@ } else {

Backbone.RedisDb.sync = Db.sync;
var loadFnMap = {
'string': 'get',
'hash': 'hgetall'
};
_.extend(Backbone.RedisDb.prototype, Db.prototype, {
var saveFnMap = {
'string': 'set',
'hash': 'hmset'
};
_.extend(RedisDb.prototype, Db.prototype, {
createClient: function() {
var self = this;
if (this.redis) {

@@ -38,3 +45,44 @@ return redis.createClient(this.redis.port, this.redis.host);

},
_getLoadFn: function(model, options) {
var type = model.redis_type || 'string';
return loadFnMap[type.toLowerCase()] || 'get';
},
_getSaveFn: function(model, options) {
var type = model.redis_type || 'string';
return saveFnMap[type.toLowerCase()] || 'set';
},
_getSaveArgs: function(model, options, fn) {
var args = [this.getIdKey(model, options)];
options = options || {};
if (fn === 'hmset') {
var data = model.toJSON();
var out = {};
Object.keys(data).forEach(function(attr) {
out[attr] = JSON.stringify(data[attr]);
});
args.push(out);
} else if (fn === 'set') {
args.push(JSON.stringify(model));
}
return args;
},
_getLoadArgs: function(model, options) {
var args = [this.getIdKey(model, options)];
options = options || {};
return args;
},
getFetchCommand: function(model, options) {
var fn = this._getLoadFn(model);
var res = {};
res.args = this._getLoadArgs(model, options, fn);
res.fn = fn;
return res;
},
getSaveCommand: function(model, options) {
var fn = this._getSaveFn(model);
var res = {};
res.args = this._getSaveArgs(model, options, fn);
res.fn = fn;
return res;
},
// get key for set where ids are stored

@@ -45,5 +93,5 @@ getIdKey: function(model, options) {

if (options.url) {
key = typeof options.url === "function" ? options.url() : options.url;
key = typeof options.url === 'function' ? options.url() : options.url;
} else if (model.url) {
key = typeof model.url === "function" ? model.url() : model.url;
key = typeof model.url === 'function' ? model.url() : model.url;
} else if (model.id) {

@@ -94,3 +142,3 @@ key = model.id;

var allIndexed = _.each(objectKeys, function(attr) {
if(indexedKeys.indexOf(attr) > -1) {
if (indexedKeys.indexOf(attr) > -1) {
searchAttrs[attr] = model.get(attr);

@@ -123,6 +171,14 @@ }

debug('find: ' + key);
this.redis.get(key, function(err, data) {
data = data && JSON.parse(data);
var cmd = this.getFetchCommand(model, options);
cmd.args.push(function(err, data) {
if (typeof data === 'string') {
data = data && JSON.parse(data);
} else if (data) {
_.each(data, function(v, k) {
data[k] = JSON.parse(v);
});
}
callback(err, data);
});
this.redis[cmd.fn].apply(this.redis, cmd.args);
},

@@ -161,8 +217,8 @@

}
this.redis.set(key, JSON.stringify(model), function(err, res) {
var cmd = this.getSaveCommand(model, options);
cmd.args.push(function(err, res) {
if (model.collection) {
var setKey = self.getIdKey(model.collection, {});
var modelKey = model.get(model.idAttribute);
debug('adding model ' + modelKey + " to " + setKey);
debug('adding model ' + modelKey + ' to ' + setKey);
self.redis.sadd(setKey, modelKey, function(err, res) {

@@ -175,2 +231,3 @@ self._updateIndexes(model, options, callback);

});
this.redis[cmd.fn].apply(this.redis, cmd.args);
},

@@ -202,3 +259,3 @@

var modelKey = model.get(model.idAttribute);
debug('removing model ' + modelKey + " from " + setKey);
debug('removing model ' + modelKey + ' from ' + setKey);
self.redis.srem(setKey, modelKey, function(err, res) {

@@ -343,3 +400,3 @@ if (err) return callback(err);

var prefix = options.prefix || (this.getIdKey(collection, {}));
if(prefix.indexOf(':', prefix.length - 1) === -1
if (prefix.indexOf(':', prefix.length - 1) === -1
&& options.keys.length) {

@@ -369,5 +426,4 @@ prefix += ':';

});
Backbone.RedisDb.Set = require('./lib/set');
Backbone.RedisDb.Hash = require('./lib/hash');
module.exports = Backbone.RedisDb;
RedisDb.sync = RedisDb.prototype.sync;
RedisDb.Hash = require('./lib/hash');
module.exports = RedisDb;

@@ -1,157 +0,7 @@

var Deferred = require('backbone-promises');
var Promises = require('backbone-promises');
var Backbone = require('backbone');
var debug = require('debug')('backbone-db-redis:hash');
var Hash = module.exports = Deferred.Model.extend({
constructor: function() {
Deferred.Model.apply(this, arguments);
},
hdel: function() {
},
hincrby: function(field, val, opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return this.defer('hincrby', opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hmget requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
var key = db.key(self.url());
db.redis.hincrby(key, field, val, function(err, res) {
cb(err, res);
});
});
},
hmget: function(fields, opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return this.defer('hmget', opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hmget requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
var key = db.key(self.url());
db.redis.hmget(key, fields, function(err, res) {
self.set(res);
cb(err, res);
});
});
},
hvals: function() {
},
hexists: function() {
},
hincrbyfloat: function() {
},
hmset: function(opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return this.defer("hmset", opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hset requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
var obj = {};
Object.keys(self.attributes).forEach(function(attr) {
if(typeof self.attributes[attr] === "object") {
obj[attr] = JSON.stringify(self.attributes[attr]);
} else {
obj[attr] = self.attributes[attr];
}
});
db.redis.hmset(db.key(self.url()), obj, cb);
});
},
hget: function(field, opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return this.defer('hget', opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hget requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
var key = db.key(self.url());
db.redis.hget(key, field, function(err, res) {
if(err) return cb(err);
if(res) {
self.set(field, res);
}
cb(null, res);
});
});
},
hkeys: function() {
},
hset: function(field, value, opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return this.defer('hset', opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hset requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
var key = db.key(self.url());
db.redis.hset(key, field, value, function(err, res) {
if(res) {
debug('set %s = %s',field, value);
self.set(field, value);
}
cb(err, value);
});
});
},
hgetall: function(opt) {
opt = opt || {};
var self = this,
db = this.db || this.collection.db;
return self.defer('hgetall', opt, function(cb) {
if(!self.url || !db) {
return cb(new Error("hgetall requires the collection to have an .url function and backbone-redis-db.RedisDb in .db"));
}
db.redis.hgetall(db.key(self.url()), function(err, res) {
if(err) return cb(err);
self.set(res);
cb(null, self);
});
});
},
hlen: function() {
},
hsetnx: function() {
},
fetch: function(keys) {
var self = this;
return this.defer("fetch", function(cb) {
if(keys) {
self.hmget(keys, cb);
} else {
self.hgetall(cb);
}
});
},
save: function(opt) {
opt = opt || {};
var self = this;
debug('saving hash %s', self.url());
return this.defer("save", function(cb) {
if(self.hasChanged()) {
self.hmset(cb);
} else {
cb(null, self);
}
});
}
var Hash = module.exports = Promises.Model.extend({
redis_type: 'hash'
});
var debug = require('debug')('backbone-db-redis:indexing');
var _ = require('underscore');
var _ = require('lodash');

@@ -4,0 +4,0 @@ function addToSet(set, member) {

@@ -1,64 +0,6 @@

var Deferred = require('backbone-promises');
var Promises = require('backbone-promises');
var Backbone = require('backbone');
var List = module.exports = Deferred.Collection.extend({
constructor: function() {
Deferred.Collection.apply(this, arguments);
},
blpop: function(value) {
var self = this;
return this.defer("blpop", function(cb) {
this.db.redis.blpop(this.url(), function(err, res) {
if(err) return cb(err);
var a = new self.model({id:res});
a.fetch();
this.add(a);
});
});
},
llen: function() {
},
rpush: function() {
},
brpop: function() {
},
lpop: function() {
},
lset: function() {
},
rpushx: function() {
},
brpoplpush: function() {
},
lpush: function() {
},
ltrim: function() {
},
lindex: function() {
},
lpushx: function() {
},
rpop: function() {
},
linsert: function() {
},
lrange: function() {
},
rpoplpush: function() {
}
var List = module.exports = Promises.Collection.extend({
redis_type: 'list'
});

@@ -1,2 +0,2 @@

var _ = require('underscore');
var _ = require('lodash');
var debug = require('debug')('backbone-db-redis:query');

@@ -135,3 +135,5 @@

this.multi.zrange(set, start, end);
} else this.multi.zrevrange(set, 0, -1);
} else {
this.multi.zrevrange(set, 0, -1);
}
},

@@ -143,3 +145,3 @@

var sortOrder = 1;
if (sortProp && sortProp[0] === "-") {
if (sortProp && sortProp[0] === '-') {
sortOrder = -1;

@@ -146,0 +148,0 @@ sortProp = sortProp.substr(1);

var redis = require('redis'),
debug = require('debug')('store'),
_ = require('underscore');
_ = require('lodash');

@@ -12,3 +12,3 @@ /**

this.client = client;
if(!this.client) {
if (!this.client) {
this.client = redis.createClient();

@@ -20,3 +20,3 @@ }

debug('getItem');
this.client.get(this.name+key, cb);
this.client.get(this.name + key, cb);
};

@@ -26,3 +26,3 @@

debug('setItem');
this.client.set(this.name+key, cb);
this.client.set(this.name + key, cb);
};

@@ -32,3 +32,3 @@

debug('removeItem');
this.client.del(this.name+key, cb);
this.client.del(this.name + key, cb);
};

@@ -35,0 +35,0 @@

@@ -1,215 +0,8 @@

var Deferred = require('backbone-promises');
var Backbone = require('backbone');
var Promises = require('backbone-promises');
var debug = require('debug')('backbone-db-redis:set');
var _ = require('underscore');
var _ = require('lodash');
module.exports = Deferred.Collection.extend({
constructor: function() {
Deferred.Collection.apply(this, arguments);
},
sadd: function(values, options) {
var self = this,
db = this.db || this.model.db;
options || (options = {});
if(values.pluck) {
values = values.pluck('id');
} else if(!Array.isArray(values)) {
values = [values];
}
return this.defer('sadd', options, function(cb) {
if(!self.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var key = db.key(self.url());
debug('Adding %s to redis set %s', JSON.stringify(values), key);
db.redis.sadd(key, values, function(err, res) {
return cb(err, res);
});
});
},
sinter: function(collection, opt) {
var self = this,
db = this.db || this.model.db;
opt = opt || {};
return this.defer('sinter', opt, function(cb) {
if(!self.url || !collection.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var src = db.key(self.url());
var dst = db.key(collection.url());
debug('redis sunion %s %s',src, dst);
db.redis.sinter(src, dst, function(err, ids) {
if(err || (!ids || ids.length === 0)) {
return cb(err, ids);
}
var done = _.after(ids.length, cb.success);
ids.forEach(function(id) {
var model = new self.model({id:id});
self.add(model);
debug('fetching %s',id);
model.fetch().done(done).fail(cb);
});
});
});
},
smove: function(collection, member, opt) {
var self = this,
db = this.db || this.model.db;
opt = opt || {};
return this.defer('smove', function(cb) {
if(!self.url || !collection.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var src = db.key(self.url());
var dst = db.key(collection.url());
debug('redis smove %s %s',src, dst);
if(!member) {
member = self.at(0);
}
db.redis.smove(src, dst, member.get('id'), function(err, ids) {
self.remove(member);
collection.add(member);
cb(err, self);
});
});
},
sunion: function(collection, opt) {
var self = this,
db = this.db || this.model.db;
opt = opt || {};
return this.defer('sunion', opt, function(cb) {
if(!self.url || !collection.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var src = db.key(self.url());
var dst = db.key(collection.url());
debug('redis sunion %s %s',src, dst);
db.redis.sunion(src, dst, function(err, ids) {
if(err || (!ids || ids.length === 0)) {
return cb(err, ids);
}
var done = _.after(ids.length, cb.success);
ids.forEach(function(id) {
var model = new self.model({id:id});
self.add(model);
debug('fetching %s',id);
model.fetch().done(done).fail(cb);
});
});
});
},
scard: function(opt) {
var self = this,
db = this.db || this.model.db;
var src = db.key(self.url());
opt = opt || {};
return this.defer('scard', opt, function(cb) {
debug('redis scard %s',src);
db.redis.scard(src, cb);
});
},
sinterstore: function() {
},
spop: function() {
},
sunionstore: function() {
},
sdiff: function(collection, opt) {
var self = this,
db = this.db || this.model.db;
return this.defer('sdiff', opt, function(cb) {
if(!self.url || !collection.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var src = db.key(self.url());
var dst = db.key(collection.url());
debug('redis sdiff %s %s',src, dst);
db.redis.sdiff(src, dst, function(err, ids) {
if(err || (!ids || ids.length === 0)) {
return cb(err, ids);
}
var done = _.after(ids.length, cb.success);
ids.forEach(function(id) {
var model = new self.model({id:id});
self.add(model);
debug('fetching %s',id);
model.fetch().done(done).fail(cb);
});
});
});
},
sismember: function() {
},
srandmember: function() {
},
sdiffstore: function() {
},
smembers: function() {
},
srem: function(ids, opt) {
var self = this,
db = this.db || this.model.db;
opt || (opt = {});
return this.defer('srem', opt, function(cb) {
if(!self.url) {
return cb(new Error("url function is required for source and destination collections"));
}
var key = db.key(self.url());
debug('Removing %s from redis set %s', JSON.stringify(ids), key);
db.redis.srem(key, ids, function(err, res) {
return cb(err, res);
});
});
},
save: function(opt) {
opt = opt || {};
var self = this;
var db = this.db || this.model.db;
debug('saving set %s',this.url());
return this.defer("save", opt, function(cb) {
if(!db || !self.url) {
return cb(new Error('.url or .db missing from set/set.model'));
}
debug('saving %s to %s', JSON.stringify(self.pluck(opt.idAttribute || 'id')), db.key(self.url()));
self.sadd(self.pluck('id'), function(err, res) {
return cb(err, res);
});
});
},
remove: function(models, opt) {
var self = this;
opt = opt || {};
if(models && models.models) {
models = models.models;
}
if(!Array.isArray(models)) {
models = [models];
}
var ids = models.map(function(model) {
return model.id || model.get(model.idAttribute);
});
return self.defer('remove', opt, function(cb) {
self.srem(ids, function(err, res) {
Deferred.Collection.prototype.remove.call(self, models);
cb(err, res);
});
});
}
module.exports = Promises.Collection.extend({
redis_type: 'set',
model: Promises.Model
});

@@ -1,59 +0,6 @@

var Deferred = require('backbone-promises');
var Backbone = require('backbone');
var Promises = require('backbone-promises');
var SortedSet = module.exports = Deferred.Collection.extend({
model: Deferred.Model,
constructor: function() {
Deferred.Collection.apply(this, arguments);
if(this.db) {
this.redis = this.db.redis;
} else {
this.redis = this.model.db.redis;
}
},
zadd: function(value) {
},
zinterstore: function() {
},
zrem: function() {
},
zrevrangebyscore: function() {
},
zcard: function() {
},
zrange: function() {
},
zremrangebyrank: function() {
},
zrevrank: function() {
},
zcount: function() {
},
zrangebyscore: function() {
},
zscore: function() {
},
zincrby: function() {
},
zrank: function() {
},
zrevrange: function() {
},
zunionstore: function() {
}
var SortedSet = module.exports = Promises.Collection.extend({
redis_type: 'zset',
model: Promises.Model
});
{
"name": "backbone-db-redis",
"version": "0.0.15",
"version": "0.0.16",
"description": "Redis driver for Backbone.Db",

@@ -13,9 +13,9 @@ "main": "index.js",

"dependencies": {
"lodash": "~2.4.1",
"backbone": "~1.1.0",
"underscore": "~1.4.4",
"redis": "~0.8.4",
"debug": "~0.7.2",
"backbone-db": "~0.4",
"backbone-promises": "~0.2",
"when": "~2.8.0"
"when": "~2.8.0",
"backbone-db": "~0.4.8"
},

@@ -26,4 +26,6 @@ "devDependencies": {

"chai": "~1.9.0",
"jshint": "~2.4.3"
"jshint": "~2.4.3",
"jscs": "~1.3.0",
"mocha-as-promised": "~2.0.0"
}
}

@@ -1,2 +0,2 @@

var _ = require('underscore');
var _ = require('lodash');
var RedisDb = require('../');

@@ -12,3 +12,3 @@ var Backbone = require('backbone');

db: store,
sync: RedisDb.sync,
sync: store.sync,
type: 'mymodel',

@@ -18,3 +18,3 @@ dbBaseKey: 'mymodels',

var key = this.dbBaseKey || this.type;
if(!this.isNew()) {
if (!this.isNew()) {
key += ':' + this.get(this.idAttribute);

@@ -28,3 +28,3 @@ }

db: store,
sync: RedisDb.sync,
sync: store.sync,
model: MyModel,

@@ -31,0 +31,0 @@ type: 'mymodels',

var assert = require('assert');
var _ = require('underscore');
var _ = require('lodash' );
var nodefn = require('when/node/function');

@@ -87,3 +87,3 @@ var Promises = require('backbone-promises');

options = options ? _.clone(options) : {};
if(!this.indexDb) {
if (!this.indexDb) {
throw new Error('indexDb must be defined');

@@ -93,3 +93,3 @@ }

var args = [this, options];
if(models) args.splice(1, 0, models);
if (models) args.splice(1, 0, models);
return nodefn.apply(_.bind(this.indexDb[fn], this.indexDb), args);

@@ -96,0 +96,0 @@ }

var assert = require('assert');
var _ = require('underscore');
var _ = require('lodash');
var Promises = require('backbone-promises');

@@ -4,0 +4,0 @@

@@ -8,7 +8,7 @@ var assert = require('assert');

it('should .save from store', function(t) {
var m = new MyModel({id:1, "asd":"das"});
var m = new MyModel({id: 1, 'asd': 'das'});
m.save().then(function() {
var m2 = new MyModel({id:1});
var m2 = new MyModel({id: 1});
m2.fetch().then(function() {
assert.equal(m2.get("asd"),"das");
assert.equal(m2.get('asd'),'das');
t();

@@ -20,3 +20,3 @@ });

it('should .destroy from store', function(t) {
var m = new MyModel({id:1, "asd":"das"});
var m = new MyModel({id: 1, 'asd': 'das'});
m.destroy()

@@ -23,0 +23,0 @@ .then(function() {

var assert = require('assert');
var _ = require('underscore');
var _ = require('lodash');
var Promises = require('backbone-promises');

@@ -4,0 +4,0 @@

Sorry, the diff of this file is not supported yet

SocketSocket SOC 2 Logo

Product

  • Package Alerts
  • Integrations
  • Docs
  • Pricing
  • FAQ
  • Roadmap
  • Changelog

Packages

npm

Stay in touch

Get open source security insights delivered straight into your inbox.


  • Terms
  • Privacy
  • Security

Made with ⚡️ by Socket Inc