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

loopback-connector-mongodb

Package Overview
Dependencies
Maintainers
4
Versions
90
Alerts
File Explorer

Advanced tools

Socket logo

Install Socket

Detect and block malicious and high-risk dependencies

Install

loopback-connector-mongodb - npm Package Compare versions

Comparing version

to
1.4.0

example/model-inq.js

57

lib/mongodb.js

@@ -135,11 +135,15 @@ /*!

/**
* Access a MongoDB collection by name
* @param {String} name The collection name
* Access a MongoDB collection by model name
* @param {String} model The model name
* @returns {*}
*/
MongoDB.prototype.collection = function (name) {
MongoDB.prototype.collection = function (model) {
if (!this.db) {
throw new Error('MongoDB connection is not established');
}
return this.db.collection(name);
var modelClass = this._models[model];
if (modelClass.settings.mongodb) {
model = modelClass.settings.mongodb.collection || model;
}
return this.db.collection(model);
};

@@ -389,13 +393,19 @@

function buildWhere(where) {
MongoDB.prototype.buildWhere = function (model, where) {
var self = this;
var query = {};
if(where === null || (typeof where !== 'object')) {
if (where === null || (typeof where !== 'object')) {
return query;
}
var idName = self.idName(model);
Object.keys(where).forEach(function (k) {
var cond = where[k];
if (k === idName) {
k = '_id';
cond = ObjectID(cond);
}
if (k === 'and' || k === 'or' || k === 'nor') {
if (Array.isArray(cond)) {
cond = cond.map(function (c) {
return buildWhere(c);
return self.buildWhere(model, c);
});

@@ -441,2 +451,3 @@ }

}
/**

@@ -466,3 +477,3 @@ * Find matching model instances by the filter

}
query = buildWhere(filter.where);
query = self.buildWhere(model, filter.where);
}

@@ -552,3 +563,3 @@ var fields = filter.fields;

}
where = buildWhere(where);
where = self.buildWhere(model, where);
this.collection(model).remove(where || {}, function (err, result) {

@@ -575,3 +586,3 @@ if (self.debug) {

}
where = buildWhere(where);
where = self.buildWhere(model, where);
this.collection(model).count(where, function (err, count) {

@@ -616,2 +627,28 @@ if (self.debug) {

/**
* Update all matching instances
* @param {String} model The model name
* @param {Object} where The search criteria
* @param {Object} data The property/value pairs to be updated
* @callback {Function} cb Callback function
*/
MongoDB.prototype.update =
MongoDB.prototype.updateAll = function updateAll(model, where, data, cb) {
var self = this;
if (self.debug) {
debug('updateAll', model, where, data);
}
var idName = this.idName(model);
delete data[idName];
where = self.buildWhere(model, where);
this.collection(model).update(where, {$set: data}, {multi: true, upsert: false},
function (err, count) {
if (self.debug) {
debug('updateAll.callback', model, where, data, err, count);
}
cb && cb(err, count);
});
};
/**
* Disconnect from MongoDB

@@ -618,0 +655,0 @@ */

2

package.json
{
"name": "loopback-connector-mongodb",
"version": "1.3.0",
"version": "1.4.0",
"description": "LoopBack MongoDB Connector",

@@ -5,0 +5,0 @@ "keywords": [

@@ -27,3 +27,8 @@ // This test written in mocha+should.js

title: { type: String, length: 255, index: true },
content: { type: String }
content: { type: String },
comments: [String]
}, {
mongodb: {
collection: 'PostCollection' // Customize the collection name
}
});

@@ -43,3 +48,3 @@

PostWithNumberId = db.define('PostWithNumberId', {
PostWithNumberUnderscoreId = db.define('PostWithNumberUnderscoreId', {
_id: {type: Number, id: true},

@@ -50,2 +55,8 @@ title: { type: String, length: 255, index: true },

PostWithNumberId = db.define('PostWithNumberId', {
id: {type: Number, id: true},
title: { type: String, length: 255, index: true },
content: { type: String }
});
User.hasMany(Post);

@@ -60,4 +71,6 @@ Post.belongsTo(User);

PostWithNumberId.destroyAll(function () {
PostWithStringId.destroyAll(function () {
done();
PostWithNumberUnderscoreId.destroyAll(function () {
PostWithStringId.destroyAll(function () {
done();
});
});

@@ -90,4 +103,4 @@ });

should.not.exist(PostWithObjectId.definition.properties.id);
PostWithNumberId.definition.properties._id.type.should.be.equal(Number);
should.not.exist(PostWithNumberId.definition.properties.id);
PostWithNumberUnderscoreId.definition.properties._id.type.should.be.equal(Number);
should.not.exist(PostWithNumberUnderscoreId.definition.properties.id);

@@ -98,3 +111,3 @@ done();

it('should handle correctly type Number for id field _id', function (done) {
PostWithNumberId.create({_id: 3, content: "test"}, function (err, person) {
PostWithNumberUnderscoreId.create({_id: 3, content: "test"}, function (err, person) {
should.not.exist(err);

@@ -104,3 +117,3 @@ should.not.exist(person.id);

PostWithNumberId.findById(person._id, function (err, p) {
PostWithNumberUnderscoreId.findById(person._id, function (err, p) {
should.not.exist(err);

@@ -115,3 +128,3 @@ p.content.should.be.equal("test");

it('should handle correctly type Number for id field _id using string', function (done) {
PostWithNumberId.create({_id: 4, content: "test"}, function (err, person) {
PostWithNumberUnderscoreId.create({_id: 4, content: "test"}, function (err, person) {
should.not.exist(err);

@@ -121,3 +134,3 @@ should.not.exist(person.id);

PostWithNumberId.findById('4', function (err, p) {
PostWithNumberUnderscoreId.findById('4', function (err, p) {
should.not.exist(err);

@@ -240,6 +253,4 @@ p.content.should.be.equal("test");

it('create should return id field but not mongodb _id', function (done) {
Post.create(function (err, post) {
Post.create({title: 'Post1', content: 'Post content'}, function (err, post) {
//console.log('create should', err, post);

@@ -255,7 +266,16 @@ should.not.exist(err);

it('should allow to find by id string', function (done) {
Post.create(function (err, post) {
Post.create({title: 'Post1', content: 'Post content'}, function (err, post) {
Post.findById(post.id.toString(), function (err, p) {
should.not.exist(err);
should.exist(p);
done();
});
});
});
it('should allow custom collection name', function (done) {
Post.create({title: 'Post1', content: 'Post content'}, function (err, post) {
Post.dataSource.connector.db.collection('PostCollection').findOne({_id: post.id}, function (err, p) {
should.not.exist(err);
should.exist(p);
done();

@@ -266,5 +286,72 @@ });

it('should allow to find by id using where', function (done) {
Post.create({title: 'Post1', content: 'Post1 content'}, function (err, p1) {
Post.create({title: 'Post2', content: 'Post2 content'}, function (err, p2) {
Post.find({where: {id: p1.id}}, function (err, p) {
should.not.exist(err);
should.exist(p && p[0]);
p.length.should.be.equal(1);
// Not strict equal
p[0].id.should.be.eql(p1.id);
done();
});
});
});
});
it('should allow to find by id using where inq', function (done) {
Post.create({title: 'Post1', content: 'Post1 content'}, function (err, p1) {
Post.create({title: 'Post2', content: 'Post2 content'}, function (err, p2) {
Post.find({where: {id: {inq: [p1.id]}}}, function (err, p) {
should.not.exist(err);
should.exist(p && p[0]);
p.length.should.be.equal(1);
// Not strict equal
p[0].id.should.be.eql(p1.id);
done();
});
});
});
});
it('should allow to find by number id using where', function (done) {
PostWithNumberId.create({id: 1, title: 'Post1', content: 'Post1 content'}, function (err, p1) {
PostWithNumberId.create({id: 2, title: 'Post2', content: 'Post2 content'}, function (err, p2) {
PostWithNumberId.find({where: {id: p1.id}}, function (err, p) {
should.not.exist(err);
should.exist(p && p[0]);
p.length.should.be.equal(1);
p[0].id.should.be.eql(p1.id);
done();
});
});
});
});
it('should allow to find by number id using where inq', function (done) {
PostWithNumberId.create({id: 1, title: 'Post1', content: 'Post1 content'}, function (err, p1) {
PostWithNumberId.create({id: 2, title: 'Post2', content: 'Post2 content'}, function (err, p2) {
PostWithNumberId.find({where: {id: {inq: [1]}}}, function (err, p) {
should.not.exist(err);
should.exist(p && p[0]);
p.length.should.be.equal(1);
p[0].id.should.be.eql(p1.id);
PostWithNumberId.find({where: {id: {inq: [1, 2]}}}, function (err, p) {
should.not.exist(err);
p.length.should.be.equal(2);
p[0].id.should.be.eql(p1.id);
p[1].id.should.be.eql(p2.id);
PostWithNumberId.find({where: {id: {inq: [0]}}}, function (err, p) {
should.not.exist(err);
p.length.should.be.equal(0);
done();
});
});
});
});
});
});
it('save should not return mongodb _id', function (done) {
Post.create(function (err, post) {
Post.create({title: 'Post1', content: 'Post content'}, function (err, post) {
post.content = 'AAA';

@@ -283,3 +370,3 @@ post.save(function(err, p) {

it('find should return an object with an id, which is instanceof ObjectID, but not mongodb _id', function (done) {
Post.create(function (err, post) {
Post.create({title: 'Post1', content: 'Post content'}, function (err, post) {
Post.findById(post.id, function (err, post) {

@@ -319,5 +406,6 @@ should.not.exist(err);

it('updateOrCreate should update the instance without removing existing properties', function (done) {
Post.create({title: 'a', content: 'AAA'}, function (err, post) {
Post.create({title: 'a', content: 'AAA', comments: ['Comment1']}, function (err, post) {
post = post.toObject();
delete post.title;
delete post.comments;
Post.updateOrCreate(post, function (err, p) {

@@ -334,2 +422,3 @@ should.not.exist(err);

p.title.should.be.equal('a');
p.comments[0].should.be.equal('Comment1');

@@ -651,3 +740,5 @@ done();

PostWithObjectId.destroyAll(function () {
PostWithNumberId.destroyAll(done);
PostWithNumberId.destroyAll(function () {
PostWithNumberUnderscoreId.destroyAll(done);
});
});

@@ -654,0 +745,0 @@ });