backbone-db
Advanced tools
Comparing version 0.1.4 to 0.2.0
var Backbone = require('backbone'); | ||
var _ = require('underscore'); | ||
var debug = require('debug')('backbone-db'); | ||
var jsonquery = require('jsonquery'); | ||
@@ -47,21 +48,48 @@ var self = this; | ||
function filterModels(models, filterOptions) { | ||
_.each(filterOptions, function(filter, filterKey) { | ||
if(_.isObject(filter) && filter.hasOwnProperty('$in')) { | ||
models = _.filter(models, function(model) { | ||
var valuesToSearch = filter['$in']; | ||
return _.some(valuesToSearch, function(valueToSearch) { | ||
return model.hasOwnProperty(filterKey) | ||
&& _.contains(model[filterKey], valueToSearch); | ||
}); | ||
}); | ||
delete filterOptions[filterKey]; | ||
function filterModels(models, filterOptions, callback) { | ||
if(!filterOptions.where) return callback(null, models); | ||
filteredModels = []; | ||
var jq = jsonquery(filterOptions.where); | ||
jq.on('data', function(model) { | ||
filteredModels.push(model); | ||
}); | ||
jq.on('end', function() { | ||
callback(null, filteredModels); | ||
}); | ||
_.map(models, jq.write); | ||
jq.end(); | ||
} | ||
// mock sort, offset, after_id, before_id, limit & filtering | ||
function queryModels(models, options, callback) { | ||
var offset = options.offset ? options.offset : 0; | ||
var limit = options.limit ? options.limit : models.length; | ||
filterModels(models, options, function(err, models) { | ||
if(options.sort) models.sort(sort(options.sort)); | ||
if(options.after_id) { | ||
for (var i = 0; i < models.length; i++) { | ||
if(models[i].id === options.after_id) { | ||
offset = i + 1; | ||
break; | ||
} | ||
} | ||
} | ||
if(options.before_id) { | ||
for (var i = 0; i < models.length; i++) { | ||
if(models[i].id === options.before_id) { | ||
offset = i - limit; | ||
if(offset < 0) offset = 0; | ||
break; | ||
} | ||
} | ||
} | ||
models = models.splice(offset, limit); | ||
callback(err, models); | ||
}); | ||
if(!_.keys(filterOptions).length) { | ||
return models; | ||
} | ||
return _.where(models, filterOptions); | ||
} | ||
Backbone.Db = function Db(name) { | ||
@@ -115,3 +143,2 @@ var self = this; | ||
_.extend(Backbone.Db.prototype, Backbone.Events, { | ||
@@ -157,26 +184,3 @@ save: function(cb) { | ||
var done = _.after(this.records.length, function() { | ||
// mock sort, offset, after_id, before_id, limit | ||
var offset = options.offset ? options.offset : 0; | ||
var limit = options.limit ? options.limit : models.length; | ||
if(options.where) models = filterModels(models, options.where); | ||
if(options.sort) models.sort(sort(options.sort)); | ||
if(options.after_id) { | ||
for (var i = 0; i < models.length; i++) { | ||
if(models[i].id === options.after_id) { | ||
offset = i + 1; | ||
break; | ||
} | ||
} | ||
} | ||
if(options.before_id) { | ||
for (var i = 0; i < models.length; i++) { | ||
if(models[i].id === options.before_id) { | ||
offset = i - limit; | ||
if(offset < 0) offset = 0; | ||
break; | ||
} | ||
} | ||
} | ||
models = models.splice(offset, limit); | ||
return cb(null, models); | ||
queryModels(models, options, cb); | ||
}); | ||
@@ -183,0 +187,0 @@ |
{ | ||
"name": "backbone-db", | ||
"version": "0.1.4", | ||
"version": "0.2.0", | ||
"description": "Key-Value database storage interface, localStorage and in-process implementations", | ||
@@ -26,3 +26,4 @@ "main": "index.js", | ||
"underscore": "~1.4.4", | ||
"debug": "~0.7.2" | ||
"debug": "~0.7.2", | ||
"jsonquery": "~0.1.3" | ||
}, | ||
@@ -29,0 +30,0 @@ "devDependencies": { |
@@ -174,3 +174,3 @@ var Db = require('../'); | ||
where: {arr: { | ||
'$in': ['foo'] | ||
$in: ['foo'] | ||
}}, | ||
@@ -192,3 +192,3 @@ success: function() { | ||
where: {arr: { | ||
'$in': ['foo', 'bar'] | ||
$in: ['foo', 'bar'] | ||
}}, | ||
@@ -206,2 +206,19 @@ success: function() { | ||
it('should query models with $ne', function(t) { | ||
var collection = new MyCollection(); | ||
collection.fetch({ | ||
where: {test: { | ||
$ne: 1 | ||
}}, | ||
success: function() { | ||
assert.equal(collection.length, 1); | ||
assert.equal(collection.at(0).get('test'), 2); | ||
t(); | ||
}, | ||
error: function(err){ | ||
assert(err); | ||
} | ||
}); | ||
}); | ||
it('should remove a model from collection when destroyed', function(t) { | ||
@@ -208,0 +225,0 @@ var m = new MyCollection(); |
Major refactor
Supply chain riskPackage has recently undergone a major refactor. It may be unstable or indicate significant internal changes. Use caution when updating to versions that include significant changes.
Found 1 instance in 1 package
Native code
Supply chain riskContains native code (e.g., compiled binaries or shared libraries). Including native code can obscure malicious behavior.
Found 1 instance in 1 package
14774
4
9
496
+ Addedjsonquery@~0.1.3
+ Addedjsonquery@0.1.8(transitive)