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

backbone-db

Package Overview
Dependencies
Maintainers
1
Versions
50
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-db - npm Package Compare versions

Comparing version 0.1.4 to 0.2.0

.npmignore

86

lib/db.js
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();

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