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

backbone-db-mongodb

Package Overview
Dependencies
Maintainers
2
Versions
20
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

backbone-db-mongodb - npm Package Compare versions

Comparing version 0.2.6 to 0.2.7

coverage/coverage.json

65

index.js

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

var _ = require('underscore'),
var _ = require('lodash'),
Db = require('backbone-db'),

@@ -13,3 +13,3 @@ debug = require('debug')('backbone-db-mongodb'),

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

@@ -38,11 +38,34 @@ sortProp = sortProp.substr(1);

_filter: function(res, model) {
var self = this;
if(Array.isArray(res)) {
return _.map(res, function(item) {
return self._filter(item, model);
});
}
var m = model.model || model;
var idAttr = m.idAttribute || m.prototype.idAttribute;
if(res && res._id) {
if(!res[idAttr]) {
res[idAttr] = res._id;
}
delete res._id;
}
return res;
},
_getId: function(model) {
var id;
if(model && model.get) {
id = model.get('_id');
id = model.get(model.idAttribute);
}
if(!id) {
id = model.get(model.idAttribute);
id = model.get('_id');
}
if(typeof id === "string" && id.length === 24) {
if(!id && model) {
id = model.id || model._id;
}
if(typeof id === 'string' && id.length === 24) {
id = new ObjectId(id);

@@ -58,2 +81,3 @@ }

var limit = options.limit || this.limit || 50;
var self = this;
var sort = options.sort ? convertSort(options.sort) : {

@@ -80,2 +104,4 @@ $natural: 1

.toArray(function (err, res) {
if(err || !res) return callback(err, res);
res = self._filter(res, model);
callback(err, res);

@@ -88,5 +114,7 @@ });

options = options || {};
var query = options.where ||  {
var self = this;
var query = options.where || {
_id: this._getId(model)
};
debug('find %s', JSON.stringify(query));

@@ -96,2 +124,4 @@ this._getCollection(model, options, function (err, col) {

col.findOne(query, function (err, res) {
if(err) return callback(err);
res = self._filter(res, model);
return callback(err, res);

@@ -106,3 +136,3 @@ });

debug('create: ' + key);
debug('create: %s', key);
if (model.isNew()) {

@@ -119,6 +149,6 @@ this.createId(model, options, function (err) {

createId: function (model, options, callback) {
debug('createId');
var createIdFn = model.createId ? model.createId : this._createDefaultId;
createIdFn(function (err, id) {
model.set(model.idAttribute, id);
model.set('_id', id);
callback(err);

@@ -134,3 +164,2 @@ });

var self = this;
debug('update:' + model.get(model.idAttribute));
if (model.isNew()) {

@@ -142,7 +171,16 @@ return this.create(model, options, callback);

}
debug('update: %s', model.get(model.idAttribute));
this._getCollection(model, options, function (err, collection) {
if (err) return callback(err);
model.set('_id', self._getId(model));
collection.save(model.toJSON(), function(err, res) {
callback(err, model.toJSON());
var data = model.toJSON(options);
var idAdded = false;
if(!data._id) {
data._id = self._getId(model);
idAdded = true;
}
collection.save(data, function(err, res) {
if (idAdded && model.idAttribute !== '_id') {
delete data._id;
}
callback(err, data, res);
});

@@ -154,3 +192,3 @@ });

var self = this;
debug("destroy : " + model.get(model.idAttribute));
debug('destroy %s', model.get(model.idAttribute));
if (model.isNew()) {

@@ -190,2 +228,3 @@ return false;

function (err, res) {
callback(err, res || options.ignoreFailures ? 1 : res);

@@ -192,0 +231,0 @@ }

9

package.json
{
"name": "backbone-db-mongodb",
"version": "0.2.6",
"version": "0.2.7",
"description": "MongoDB driver for Backbone.Db",

@@ -19,3 +19,2 @@ "main": "index.js",

"dependencies": {
"underscore": "~1.4.4",
"backbone": "~1.1.0",

@@ -25,3 +24,4 @@ "backbone-db": "~0.4",

"debug": "~0.7.2",
"mongodb": "~1.3.19"
"mongodb": "~1.3.19",
"lodash": "~2.4.1"
},

@@ -32,4 +32,5 @@ "devDependencies": {

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

@@ -1,5 +0,4 @@

var assert = require('assert');
var _ = require('underscore');
require('mocha-as-promised')();
var _ = require('lodash');
var MongoDB = require('../');
var Backbone = require('backbone');
var Promises = require('backbone-promises');

@@ -30,6 +29,6 @@ var Model = Promises.Model;

var mongoPort = process.env.MONGO_PORT || 27017;
var url = format("mongodb://localhost:%s/backbone-db-tests", mongoPort);
var url = format('mongodb://localhost:%s/backbone-db-tests', mongoPort);
MongoClient.connect(url, {}, function (err, database) {
if (err) {
console.error("Start mongoDB or tune settings in test.model.js", err);
console.error('Start mongoDB or tune settings in test.model.js', err);
cb(err);

@@ -44,3 +43,3 @@ }

this.db = store;
cb(err, store);
cb.call(this, err, store);
});

@@ -78,8 +77,5 @@ };

Promises.when.all(fns)
.then(function () {
.done(function () {
cb(null);
})
.otherwise(function (err) {
cb(err);
});
}, cb);
};

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

var assert = require('assert');
var setup = require('./setup');
var MyModel = setup.MyModel;
var MyCollection = setup.MyCollection;
var shared = require('backbone-db/test');

@@ -6,0 +3,0 @@

@@ -0,6 +1,5 @@

var setup = require('./setup');
var assert = require('assert');
var setup = require('./setup');
var MyModel = setup.MyModel;
var MyCollection = setup.MyCollection;
describe('Collection tests', function () {

@@ -21,49 +20,41 @@ var collection;

it('should .create a model', function (done) {
it('should .create a model', function () {
collection = new this.Collection();
collection
return collection
.create({
'id_check': 1
})
.done(function (m) {
.then(function (m) {
model = m;
assert(m.get('id_check') === collection.at(0).get('id_check'));
done();
}, done);
});
});
it('should fetch created model', function (done) {
it('should fetch created model', function () {
var m2 = new this.Model({
id: model.get(model.idAttribute)
});
m2.fetch().then(function (m) {
return m2.fetch()
.then(function (m) {
assert(m.get('id_check') === m2.get('id_check'));
done();
}).otherwise(done);
});
});
it('should fetch collection models', function (done) {
it('should fetch collection models', function () {
collection = new this.Collection();
collection
.fetch()
return collection.fetch()
.then(function (c) {
assert(collection.length === 1);
assert(c.at(0));
done();
}).otherwise(done);
});
});
it('should remove model from collection', function (done) {
it('should remove model from collection', function () {
testId = model.id;
model
.destroy()
.done(function () {
done();
}, done);
return model.destroy();
});
it('should check that model was removed', function (done) {
it('should check that model was removed', function () {
collection = new this.Collection();
collection
.fetch()
return collection.fetch()
.then(function () {

@@ -74,5 +65,4 @@ var removedModel = collection.where({

assert(removedModel.length === 0);
done();
}).otherwise(done);
});
});
});

@@ -0,14 +1,23 @@

var setup = require('./setup');
var assert = require('assert');
var setup = require('./setup');
var MyModel = setup.MyModel;
var Promised = require('backbone-promises');
describe('MongoDB', function () {
var id;
var model;
before(function (done) {
before(function (next) {
var self = this;
setup.setupDb(done);
setup.setupDb(function () {
self.Model = this.Model;
self.Collection = this.Collection;
self.db = this.db;
next();
});
});
after(function (next) {
setup.clearDb(next);
});
after(function (done) {

@@ -19,27 +28,30 @@ setup.clearDb(done);

describe('#Model', function () {
it('should .save model with given id', function (done) {
var m = new this.Model({
id: 1,
var model;
it('should .save model with given id', function () {
var deferred = Promised.when.defer();
model = new this.Model({
asd: 'das',
counter: 2
});
m.save().done(function () {
done();
}, done);
model.db.createId(model, {}, function(err) {
if(err) return deferred.reject(err);
model.save().then(deferred.resolve);
});
return deferred.promise;
});
it('should fetch saved model', function (done) {
it('should fetch saved model', function () {
var m2 = new this.Model({
id: 1
id: model.get(model.idAttribute)
});
m2
.fetch()
return m2.fetch()
.then(function () {
assert.equal(m2.get('asd'), 'das');
assert.equal(m2.get('counter'), 2);
done();
}, assert).otherwise(done);
});
});
it('should .save model without id', function (done) {
it('should .save model without id', function () {
var m = new this.Model({

@@ -49,47 +61,38 @@ data: 'foo',

});
m.save().then(function (m) {
id = m.get('id');
done();
}).otherwise(done);
return m.save().then(function (m) {
id = m.get(m.idAttribute);
});
});
it('should fetch saved model', function (done) {
it('should fetch saved model', function () {
model = new this.Model({
id: id
});
model
.fetch()
return model.fetch()
.then(function () {
assert.equal(model.get('data'), 'foo');
assert.equal(model.get('counter'), 5);
done();
}, assert).otherwise(done);
});
});
it('should update model', function (done) {
it('should update model', function () {
model.set('data', 'new');
model
.save()
.then(function (m) {
done();
}).otherwise(done);
return model.save();
});
it('should fetch updated model', function (done) {
it('should fetch updated model', function () {
model = new this.Model({
id: id
});
model
.fetch()
return model.fetch()
.then(function () {
assert.equal(model.get('data'), 'new');
done();
}, assert).otherwise(done);
});
});
it('should inc model attribute', function (done) {
it('should inc model attribute', function () {
model = new this.Model({
id: id
});
model
return model
.save(null, {

@@ -100,13 +103,10 @@ inc: {

}
})
.then(function (m) {
done();
}).otherwise(done);
});
});
it('should check that attribute was increased', function (done) {
it('should check that attribute was increased', function () {
model = new this.Model({
id: id
});
model
return model
.fetch()

@@ -116,11 +116,10 @@ .then(function () {

assert.equal(model.get('data'), 'new');
done();
}, assert).otherwise(done);
});
});
it('should fail inc operation gracefully with ignoreFailures options', function (done) {
model = new this.Model({
it('should fail inc operation gracefully with ignoreFailures options', function () {
var m = new this.Model({
id: 'foo'
});
model
return m
.save(null, {

@@ -132,9 +131,17 @@ inc: {

ignoreFailures: true
})
.then(function (m) {
done();
}).otherwise(done);
});
});
it('should use model.idAttribute as _id but not add it to attributes', function() {
console.log(model.attributes);
assert.ok(model.get('_id') === undefined);
});
it('should convert _id to ObjectId if it is ObjectId like', function() {
var m = new this.Model({
id: ''+model.get(model.idAttribute)
});
return m.fetch();
});
});
});

@@ -0,7 +1,5 @@

var setup = require('./setup');
var assert = require('assert');
var _ = require('underscore');
var Promises = require('backbone-promises');
var _ = require('lodash');
var setup = require('./setup');
var inAscendingOrder = function(arr) {

@@ -39,12 +37,11 @@ var inOrder = _.every(arr, function(value, index) {

it('should fetch all models', function(done) {
collection
it('should fetch all models', function() {
return collection
.fetch()
.then(function() {
assert(collection.length === 4);
done();
}).otherwise(done);
});
});
it('should fetch matching models filtered with where operator', function(done) {
it('should fetch matching models filtered with where operator', function() {
var opts = {

@@ -55,3 +52,3 @@ where: {

};
collection
return collection
.fetch(opts)

@@ -64,7 +61,6 @@ .then(function() {

assert(allHaveCorrectValue);
done();
}).otherwise(done);
});
});
it('should filter with multiple where options', function(done) {
it('should filter with multiple where options', function() {
var opts = {

@@ -76,3 +72,3 @@ where: {

};
collection
return collection
.fetch(opts)

@@ -85,7 +81,6 @@ .then(function() {

assert(allHaveCorrectValue);
done();
}).otherwise(done);
});
});
it('should fetch models with limit & offset', function(done) {
it('should fetch models with limit & offset', function() {
var opts = {

@@ -95,3 +90,3 @@ limit: 2,

};
collection
return collection
.fetch(opts)

@@ -102,11 +97,10 @@ .then(function() {

assert(m.get('id') === 2);
done();
}).otherwise(done);
});
});
it('should fetch models sorted with value in ascending order', function(done) {
it('should fetch models sorted with value in ascending order', function() {
var opts = {
sort: 'value'
};
collection
return collection
.fetch(opts)

@@ -117,11 +111,10 @@ .then(function() {

assert(inAscendingOrder(values));
done();
}).otherwise(done);
});
});
it('should fetch models sorted with value in descending order', function(done) {
it('should fetch models sorted with value in descending order', function() {
var opts = {
sort: '-value'
};
collection
return collection
.fetch(opts)

@@ -132,7 +125,6 @@ .then(function() {

assert(inDescendingOrder(values));
done();
}).otherwise(done);
});
});
it('should fetch collections first page sorted ascending', function(done) {
it('should fetch collections first page sorted ascending', function() {
var opts = {

@@ -142,3 +134,3 @@ sort: 'value',

};
collection
return collection
.fetch(opts)

@@ -150,7 +142,6 @@ .then(function() {

testId = collection.at(collection.length - 1).id;
done();
}).otherwise(done);
});
});
it('should page through models with after_id', function(done) {
it('should page through models with after_id', function() {
var opts = {

@@ -161,3 +152,3 @@ sort: 'value',

};
collection
return collection
.fetch(opts)

@@ -168,7 +159,6 @@ .then(function() {

assert(values[1] === 3);
done();
}).otherwise(done);
});
});
it('should fetch collections first page sorted descending', function(done) {
it('should fetch collections first page sorted descending', function() {
var opts = {

@@ -178,3 +168,3 @@ sort: '-value',

};
collection
return collection
.fetch(opts)

@@ -186,7 +176,6 @@ .then(function() {

testId = collection.at(0).id;
done();
}).otherwise(done);
});
});
it('should page through models with before_id', function(done) {
it('should page through models with before_id', function() {
var opts = {

@@ -197,3 +186,3 @@ sort: '-value',

};
collection
return collection
.fetch(opts)

@@ -204,7 +193,6 @@ .then(function() {

assert(values[1] === 1);
done();
}).otherwise(done);
});
});
it('should fetch models with combined options #1', function(done) {
it('should fetch models with combined options #1', function() {
var opts = {

@@ -216,3 +204,3 @@ where: {name: 'c'},

};
collection
return collection
.fetch(opts)

@@ -223,7 +211,6 @@ .then(function() {

assert(inAscendingOrder(values));
done();
}).otherwise(done);
});
});
it('should fetch models with combined options #2', function(done) {
it('should fetch models with combined options #2', function() {
var opts = {

@@ -235,3 +222,3 @@ where: {name: 'c'},

};
collection
return collection
.fetch(opts)

@@ -242,7 +229,6 @@ .then(function() {

assert(inDescendingOrder(values));
done();
}).otherwise(done);
});
});
it('should fetch models with combined options #3', function(done) {
it('should fetch models with combined options #3', function() {
var opts = {

@@ -254,3 +240,3 @@ where: {name: 'c'},

};
collection
return collection
.fetch(opts)

@@ -261,7 +247,6 @@ .then(function() {

assert(values[0] === 3);
done();
}).otherwise(done);
});
});
it('should fetch models with combined options #4', function(done) {
it('should fetch models with combined options #4', function() {
var opts = {

@@ -273,3 +258,3 @@ where: {name: 'c', value: 2},

};
collection
return collection
.fetch(opts)

@@ -280,5 +265,4 @@ .then(function() {

assert(values[0] === 2);
done();
}).otherwise(done);
});
});
});
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