backbone-db-redis
Advanced tools
Comparing version 0.0.14 to 0.0.15
39
index.js
@@ -39,2 +39,3 @@ var _ = require('underscore'), | ||
var key = ''; | ||
options = options || {}; | ||
if (options.url) { | ||
@@ -65,2 +66,5 @@ key = typeof options.url === "function" ? options.url() : options.url; | ||
var collectionKey = this.getIdKey(model, options); | ||
var modelKey; | ||
var dbOpts; | ||
debug('findAll ' + collectionKey); | ||
@@ -70,6 +74,7 @@ // if Collection | ||
var m = new model.model(); | ||
var modelKey = this.getIdKey(m, {}); | ||
var dbOpts = { | ||
modelKey = this.getIdKey(m, {}); | ||
dbOpts = { | ||
db: this, | ||
model: model, | ||
ModelClass: model.model, | ||
modelKey: modelKey, | ||
@@ -81,6 +86,30 @@ collectionKey: collectionKey, | ||
} else { | ||
this.redis.get(collectionKey, function(err, data) { | ||
data = data && JSON.parse(data); | ||
callback(err, data); | ||
// fetch a model with given attributes | ||
// check that initial attributes are indexed | ||
var indexedKeys = _.pluck(model.indexes, 'property'); | ||
var objectKeys = Object.keys(model.attributes); | ||
var searchAttrs = {}; | ||
var allIndexed = _.each(objectKeys, function(attr) { | ||
if(indexedKeys.indexOf(attr) > -1) { | ||
searchAttrs[attr] = model.get(attr); | ||
} | ||
}); | ||
if (!Object.keys(searchAttrs).length) { | ||
var err = new Error('Cannot fetch model with given attributes'); | ||
return callback(err); | ||
} | ||
options.where = searchAttrs; | ||
debug('fetch model', collectionKey, options.where); | ||
modelKey = this.getIdKey(model, {}); | ||
dbOpts = { | ||
db: this, | ||
model: model, | ||
ModelClass: model.constructor, | ||
modelKey: modelKey, | ||
collectionKey: collectionKey, | ||
indexes: model.indexes | ||
}; | ||
query.queryModels(options, dbOpts, function(err, results) { | ||
callback(err, results && results.length && results[0]); | ||
}); | ||
} | ||
@@ -87,0 +116,0 @@ }, |
@@ -23,2 +23,3 @@ var _ = require('underscore'); | ||
this.model = dbOptions.model; | ||
this.ModelClass = dbOptions.ModelClass; | ||
this.limit = this.filterOptions.limit ? this.filterOptions.limit : 50; | ||
@@ -105,3 +106,6 @@ this.offset = this.filterOptions.offset ? this.filterOptions.offset : 0; | ||
var keys = _.map(ids, function(id) { | ||
return this.dbOptions.modelKey + ':' + id; | ||
var attrs = {}; | ||
attrs[this.ModelClass.prototype.idAttribute] = id; | ||
var model = new this.ModelClass(attrs); | ||
return this.db.getIdKey(model); | ||
}, this); | ||
@@ -108,0 +112,0 @@ if (!keys.length) { |
{ | ||
"name": "backbone-db-redis", | ||
"version": "0.0.14", | ||
"version": "0.0.15", | ||
"description": "Redis driver for Backbone.Db", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -42,2 +42,24 @@ var assert = require('assert'); | ||
it('should fetch model with non-id attributes', function(done) { | ||
var model = new IndexedModel({value: 1}); | ||
model | ||
.fetch() | ||
.then(function() { | ||
assert.equal(model.id, 1); | ||
assert.equal(model.get('value'), 1); | ||
done(); | ||
}, done); | ||
}); | ||
it('should result an error if trying to fetch model with non-indexed attributes', function(done) { | ||
var model = new IndexedModel({foo: 'bar'}); | ||
model | ||
.fetch() | ||
.then(function() { | ||
done(new Error('model should not be fetched')); | ||
}, function(err) { | ||
done(); | ||
}); | ||
}); | ||
it('should fetch all models', function(done) { | ||
@@ -48,3 +70,3 @@ collection | ||
done(); | ||
}); | ||
}, done); | ||
}); | ||
@@ -51,0 +73,0 @@ |
62385
1856