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
1
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.1.1 to 0.2.2

.jshintrc

149

index.js

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

var _ = require('underscore')
, Db = require('backbone-db')
, debug = require('debug')('backbone-db-mongodb')
, ObjectId = require('mongodb').BSONPure.ObjectID;
var _ = require('underscore'),
Db = require('backbone-db'),
debug = require('debug')('backbone-db-mongodb'),
ObjectId = require('mongodb').BSONPure.ObjectID;
function MongoDB (client) {
if(!client) throw new Error('Db.MongoDB requires a connected mongo client');
function MongoDB(client) {
if (!client) throw new Error('Db.MongoDB requires a connected mongo client');
this.client = client;

@@ -13,3 +13,3 @@ }

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

@@ -23,10 +23,30 @@ sortProp = sortProp.substr(1);

function toMongoDoc(doc) {
if (doc._id) {
if (typeof doc._id === "string" && doc._id.length === "24") {
debug('converting _id to MongoID');
doc._id = new ObjectId(doc._id);
}
}
return doc;
}
function fromMongoDoc(doc) {
if (doc._id) {
if (typeof doc._id === "object" && doc._id.toString && doc._id.toString().length === "24") {
debug('converting _id to MongoID');
doc._id = doc._id.toString();
}
}
return doc;
}
MongoDB.sync = Db.sync;
_.extend(MongoDB.prototype, Db.prototype, {
_getCollection: function(model, options, callback) {
if(options && options.mongo_collection) {
_getCollection: function (model, options, callback) {
if (options && options.mongo_collection) {
this.client.collection(options.mongo_collection, callback);
} else if (model && model.mongo_collection) {
this.client.collection(model.mongo_collection, callback);
} else if(model && model.model && model.model.mongo_collection) {
} else if (model && model.model && model.model.mongo_collection) {
this.client.collection(model.model.mongo_collection, callback);

@@ -38,13 +58,15 @@ } else {

findAll: function(model, options, callback) {
findAll: function (model, options, callback) {
options = options || {};
var query = options.where || {};
var offset = options.offset || 0;
var query = options.where ||  {};
var offset = options.offset ||  0;
var limit = options.limit || this.limit || 50;
var sort = options.sort ? convertSort(options.sort) : {$natural: 1};
if(options.after_id) {
var sort = options.sort ? convertSort(options.sort) : {
$natural: 1
};
if (options.after_id) {
query._id = {
$gt: options.after_id
};
} else if(options.before_id) {
} else if (options.before_id) {
query._id = {

@@ -55,4 +77,4 @@ $lt: options.before_id

debug('findAll', query, 'limit:', limit, 'offset:', offset, 'sort:', sort);
this._getCollection(model, options, function(err, collection) {
if(err) return callback(err);
this._getCollection(model, options, function (err, collection) {
if (err) return callback(err);
collection

@@ -63,17 +85,26 @@ .find(query)

.limit(limit)
.toArray(callback);
.toArray(function (err, res) {
if (res && res.length > 0) {
res = _.map(res, fromMongoDoc);
}
callback(err, res);
});
});
},
find: function(model, options, callback) {
find: function (model, options, callback) {
options = options || {};
var query = options.where || {_id:model.get(model.idAttribute)};
var query = options.where ||  {
_id: model.get(model.idAttribute)
};
debug('find', query);
this._getCollection(model, options, function(err, col) {
if(err) return callback(err);
col.findOne(query, callback);
this._getCollection(model, options, function (err, col) {
if (err) return callback(err);
col.findOne(query, function (err, res) {
return callback(err, fromMongoDoc(res));
});
});
},
create: function(model, options, callback) {
create: function (model, options, callback) {
var self = this;

@@ -84,4 +115,4 @@ var key = this._getCollection(model, options);

if (model.isNew()) {
this.createId(model, options, function(err) {
if(err) callback(err);
this.createId(model, options, function (err) {
if (err) callback(err);
self.update(model, options, callback);

@@ -94,7 +125,7 @@ });

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

@@ -104,33 +135,37 @@ });

_createDefaultId: function(callback) {
_createDefaultId: function (callback) {
callback(null, new ObjectId());
},
update: function(model, options, callback) {
update: function (model, options, callback) {
var self = this;
debug('update:' + model.id);
if(model.isNew()) {
debug('update:' + model.get(model.idAttribute));
if (model.isNew()) {
return this.create(model, options, callback);
}
if(!model.has('_id') && model.has(model.idAttribute)) {
if (!model.has('_id') && model.has(model.idAttribute)) {
model.set('_id', model.get(model.idAttribute));
}
if(options.inc) {
if (options.inc) {
return this.inc(model, options, callback);
}
this._getCollection(model, options, function(err, collection) {
if(err) return callback(err);
collection.save(model.toJSON(), callback);
this._getCollection(model, options, function (err, collection) {
if (err) return callback(err);
collection.save(toMongoDoc(model.toJSON()), callback);
});
},
destroy: function(model, options, callback) {
destroy: function (model, options, callback) {
var self = this;
debug("destroy: " + model.id);
debug("destroy: " + model.get(model.idAttribute));
if (model.isNew()) {
return false;
}
this._getCollection(model, options, function(err, collection) {
if(err) return callback(err);
collection.remove({_id: model.id}, function(err, res) {
this._getCollection(model, options, function (err, collection) {
if (err) return callback(err);
var data = toMongoDoc(model.toJSON());
collection.remove({
_id: data._id
}, function (err, res) {
callback(err, res || options.ignoreFailures ? 1 : res);

@@ -141,4 +176,4 @@ });

inc: function(model, options, callback) {
if(!options || !options.inc || !options.inc.attribute) {
inc: function (model, options, callback) {
if (!options || !options.inc || !options.inc.attribute) {
throw new Error('inc settings must be defined');

@@ -151,8 +186,12 @@ }

debug('inc:' + JSON.stringify(inc));
this._getCollection(model, options, function(err, col) {
if(err) return callback(err);
col.update(
{_id: model.id},
{$inc: inc},
{upsert: options.upsert || false},
this._getCollection(model, options, function (err, col) {
if (err) return callback(err);
var data = toMongoDoc(model.toJSON());
col.update({
_id: data._id
}, {
$inc: inc
}, {
upsert: options.upsert || false
},
callback

@@ -164,4 +203,2 @@ );

module.exports = Db.MongoDB = MongoDB;
module.exports = Db.MongoDB = MongoDB;
{
"name": "backbone-db-mongodb",
"version": "0.1.1",
"version": "0.2.2",
"description": "MongoDB driver for Backbone.Db",

@@ -11,3 +11,3 @@ "main": "index.js",

"type": "git",
"url": "git://github.com/Nomon/backbone-db-mongodb.git"
"url": "git://github.com/Everyplay/backbone-db-mongodb.git"
},

@@ -22,4 +22,4 @@ "author": {

"backbone": "~1.1.0",
"backbone-db": "~0.3.0",
"backbone-promises": "~0.1.6",
"backbone-db": "~0.4.0",
"backbone-promises": "~0.2.0",
"debug": "~0.7.2",

@@ -29,4 +29,7 @@ "mongodb": "~1.3.19"

"devDependencies": {
"mocha": "~1.13.0"
"mocha": "~1.13.0",
"istanbul": "~0.2.4",
"jshint": "~2.4.3",
"chai": "~1.9.0"
}
}
## Usage
[![Build Status](https://travis-ci.org/Everyplay/backbone-db-mongodb.png?branch=master)](https://travis-ci.org/Everyplay/backbone-db-mongodb)

@@ -3,0 +4,0 @@ ```js

@@ -9,3 +9,3 @@ var assert = require('assert');

var format = require('util').format;
var MongoClient = require('mongodb').MongoClient;
var MongoClient = require('mongodb').MongoClient;
var db;

@@ -28,8 +28,8 @@ var store;

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

@@ -40,22 +40,36 @@ cb(err);

store = new MongoDB(db);
MyModel.prototype.db = store;
MyCollection.prototype.db = store;
cb(err);
this.Collection = MyCollection;
this.Model = MyModel;
this.Model.prototype.db = store;
this.Collection.prototype.db = store;
this.db = store;
cb(err, store);
});
};
exports.clearDb = function(done) {
exports.clearDb = function (done) {
db.collection(type).remove(done);
};
var fixtures = [
{id: 1, value: 1, name: 'a'},
{id: 2, value: 2, name: 'b'},
{id: 3, value: 3, name: 'c'},
{id: 4, value: 2, name: 'c'},
];
var fixtures = [{
id: 1,
value: 1,
name: 'a'
}, {
id: 2,
value: 2,
name: 'b'
}, {
id: 3,
value: 3,
name: 'c'
}, {
id: 4,
value: 2,
name: 'c'
}, ];
exports.insertFixtureData = function(collection, cb) {
exports.insertFixtureData = function (collection, cb) {
var fns = [];
_.each(fixtures, function(row) {
_.each(fixtures, function (row) {
fns.push(collection.create(row));

@@ -65,8 +79,8 @@ });

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

@@ -1,5 +0,26 @@

describe('MongoDB', function() {
it('should pass the backbone-db test suite', function() {
var assert = require('assert');
var setup = require('./setup');
var MyModel = setup.MyModel;
var MyCollection = setup.MyCollection;
var shared = require('backbone-db/test');
describe('backbone-db-mongodb', function () {
before(function (next) {
var self = this;
setup.setupDb(function () {
self.Model = this.Model;
self.Collection = this.Collection;
self.db = this.db;
next();
});
});
after(function (next) {
setup.clearDb(next);
});
shared.shouldImplementDb(function () {
});
});

@@ -6,3 +6,3 @@ var assert = require('assert');

describe('Collection tests', function() {
describe('Collection tests', function () {
var collection;

@@ -12,15 +12,19 @@ var model;

before(function(done) {
before(function (done) {
setup.setupDb(done);
});
after(function(done) {
after(function (done) {
setup.clearDb(done);
});
it('should .create a model', function(done) {
collection = new MyCollection();
collection
.create({'id_check': 1}, { wait: true })
.then(function(m) {
it('should .create a model', function (done) {
collection = new this.Collection();
collection
.create({
'id_check': 1
}, {
wait: true
})
.then(function (m) {
assert(m.get('id_check') === collection.at(0).get('id_check'));

@@ -32,5 +36,7 @@ model = m;

it('should fetch created model', function(done) {
var m2 = new MyModel({id: model.id});
m2.fetch().then(function(m) {
it('should fetch created model', function (done) {
var m2 = new this.Model({
id: model.id
});
m2.fetch().then(function (m) {
assert(m.get('id_check') === m2.get('id_check'));

@@ -41,7 +47,7 @@ done();

it('should fetch collection models', function(done) {
collection = new MyCollection();
it('should fetch collection models', function (done) {
collection = new this.Collection();
collection
.fetch()
.then(function(c) {
.then(function (c) {
assert(collection.length === 1);

@@ -53,17 +59,19 @@ assert(c.at(0));

it('should remove model from collection', function(done) {
it('should remove model from collection', function (done) {
testId = model.id;
model
.destroy()
.then(function() {
.then(function () {
done();
}).otherwise(done);
}).otherwise(done);
});
it('should check that model was removed', function(done) {
collection = new MyCollection();
it('should check that model was removed', function (done) {
collection = new this.Collection();
collection
.fetch()
.then(function() {
var removedModel = collection.where({id: testId});
.then(function () {
var removedModel = collection.where({
id: testId
});
assert(removedModel.length === 0);

@@ -73,2 +81,2 @@ done();

});
});
});

@@ -5,17 +5,18 @@ var assert = require('assert');

describe('MongoDB', function() {
describe('MongoDB', function () {
var id;
var model;
before(function(done) {
before(function (done) {
var self = this;
setup.setupDb(done);
});
after(function(done) {
after(function (done) {
setup.clearDb(done);
});
describe('#Model', function() {
it('should .save model with given id', function(done) {
var m = new MyModel({
describe('#Model', function () {
it('should .save model with given id', function (done) {
var m = new this.Model({
id: 1,

@@ -25,3 +26,3 @@ asd: 'das',

});
m.save().then(function() {
m.save().then(function () {
done();

@@ -31,8 +32,10 @@ });

it('should fetch saved model', function(done) {
var m2 = new MyModel({id:1});
it('should fetch saved model', function (done) {
var m2 = new this.Model({
id: 1
});
m2
.fetch()
.then(function() {
assert.equal(m2.get('asd'),'das');
.then(function () {
assert.equal(m2.get('asd'), 'das');
assert.equal(m2.get('counter'), 2);

@@ -43,5 +46,8 @@ done();

it('should .save model without id', function(done) {
var m = new MyModel({data: 'foo', counter: 5});
m.save().then(function(m) {
it('should .save model without id', function (done) {
var m = new this.Model({
data: 'foo',
counter: 5
});
m.save().then(function (m) {
id = m.get('id');

@@ -52,8 +58,10 @@ done();

it('should fetch saved model', function(done) {
model = new MyModel({id: id});
it('should fetch saved model', function (done) {
model = new this.Model({
id: id
});
model
.fetch()
.then(function() {
assert.equal(model.get('data'),'foo');
.then(function () {
assert.equal(model.get('data'), 'foo');
assert.equal(model.get('counter'), 5);

@@ -64,7 +72,7 @@ done();

it('should update model', function(done) {
it('should update model', function (done) {
model.set('data', 'new');
model
.save()
.then(function(m) {
.then(function (m) {
done();

@@ -74,8 +82,10 @@ }).otherwise(done);

it('should fetch updated model', function(done) {
model = new MyModel({id: id});
it('should fetch updated model', function (done) {
model = new this.Model({
id: id
});
model
.fetch()
.then(function() {
assert.equal(model.get('data'),'new');
.then(function () {
assert.equal(model.get('data'), 'new');
done();

@@ -85,4 +95,6 @@ }, assert).otherwise(done);

it('should inc model attribute', function(done) {
model = new MyModel({id: id});
it('should inc model attribute', function (done) {
model = new this.Model({
id: id
});
model

@@ -95,3 +107,3 @@ .save(null, {

})
.then(function(m) {
.then(function (m) {
done();

@@ -101,9 +113,11 @@ }).otherwise(done);

it('should check that attribute was increased', function(done) {
model = new MyModel({id: id});
it('should check that attribute was increased', function (done) {
model = new this.Model({
id: id
});
model
.fetch()
.then(function() {
.then(function () {
assert.equal(model.get('counter'), 6);
assert.equal(model.get('data'),'new');
assert.equal(model.get('data'), 'new');
done();

@@ -110,0 +124,0 @@ }, assert).otherwise(done);

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