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
3
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 1.1.7 to 1.1.8

19

lib/mongodb.js

@@ -162,3 +162,3 @@ /*!

data._id = oid; // Set it to _id
delete data[idName];
idName != '_id' && delete data[idName];
}

@@ -182,2 +182,4 @@ this.collection(model).insert(data, {safe: true}, function (err, m) {

}
} else if (idType === ObjectID) {
idValue = ObjectID(idValue);
}

@@ -204,3 +206,3 @@ callback(err, err ? null : idValue);

data._id = oid;
delete data[idName];
idName != '_id' && delete data[idName];

@@ -210,2 +212,3 @@ this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (err, result) {

self.setIdValue(model, data, idValue);
idName != '_id' && delete data._id;
}

@@ -251,2 +254,3 @@ if (self.debug) {

}
var idName = self.idName(model);
var oid = ObjectID(id);

@@ -257,2 +261,4 @@ this.collection(model).findOne({_id: oid}, function (err, data) {

}
data && idName != '_id' && delete data._id;
callback && callback(err, data);

@@ -276,2 +282,3 @@ });

var idValue = self.getIdValue(model, data);
var idName = self.idName(model);

@@ -294,2 +301,3 @@ if (idValue === null || idValue === undefined) {

self.setIdValue(model, data, id);
data && idName != '_id' && delete data._id;
callback(null, data);

@@ -454,3 +462,3 @@ } else {

// Don't pass back _id if the fields is set
if (fields) {
if (fields || idName != '_id') {
delete o._id;

@@ -525,4 +533,4 @@ }

var oid = ObjectID(id);
delete data[this.idName(model)];
var idName = this.idName(model);
delete data[idName];
this.collection(model).findAndModify({_id: oid}, [

@@ -539,2 +547,3 @@ ['_id', 'asc']

self.setIdValue(model, object, id);
object && idName != '_id' && delete object._id;
cb && cb(err, object);

@@ -541,0 +550,0 @@ });

{
"name": "loopback-connector-mongodb",
"version": "1.1.7",
"version": "1.1.8",
"description": "LoopBack MongoDB Connector",

@@ -5,0 +5,0 @@ "keywords": [ "StrongLoop", "LoopBack", "MongoDB", "DataSource", "Connector" ],

@@ -30,2 +30,25 @@ ## loopback-connector-mongodb

### About MongoDB _id field
MongoDB uses a specific ID field with BSON `ObjectID` type, named `_id`
This connector does not expose MongoDB `_id` by default, to keep consistency with other connectors. Instead, it is transparently mapped to the `id` field - which is declared by default in the model if you do not define any `id`.
If you wish to still be able to access `_id` property, you must define it explicitely as your model ID, along with its type.
*Example :*
var ds = app.dataSources.db;
MyModel = ds.createModel('mymodel', {
_id: { type: ds.ObjectID, id: true }
});
*Example with a Number _id :
MyModel = ds.createModel('mymodel', {
_id: { type: Number, id: true }
});
## Customizing MongoDB configuration for tests/examples

@@ -68,1 +91,5 @@

npm test
## Release notes
* 1.1.7 - Do not return MongoDB-specific _id to client API, except if specifically specified in the model definition

@@ -28,2 +28,14 @@ // This test written in mocha+should.js

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

@@ -36,2 +48,30 @@ Post.belongsTo(User);

Post.destroyAll(function () {
PostWithObjectId.destroyAll(function () {
PostWithNumberId.destroyAll(function () {
done();
});
});
});
});
});
it('should have created models with correct _id types', function (done) {
PostWithObjectId.definition.properties._id.type.should.be.equal(db.ObjectID);
should.not.exist(PostWithObjectId.definition.properties.id);
PostWithNumberId.definition.properties._id.type.should.be.equal(Number);
should.not.exist(PostWithNumberId.definition.properties.id);
done();
});
it('should handle correctly type Number for id field _id', function (done) {
PostWithNumberId.create({_id: 3, content: "test"}, function (err, person) {
should.not.exist(err);
should.not.exist(person.id);
person._id.should.be.equal(3);
PostWithNumberId.findById(person._id, function (err, p) {
should.not.exist(err);
p.content.should.be.equal("test");
done();

@@ -42,6 +82,95 @@ });

it('should allow to find post by id string if `_id` is defined id', function (done) {
PostWithObjectId.create(function (err, post) {
PostWithObjectId.find(post._id.toString(), function (err, p) {
should.not.exist(err);
post = p[0];
should.exist(post);
post._id.should.be.an.instanceOf(db.ObjectID);
done();
});
});
});
it('find with `_id` as defined id should return an object with _id instanceof ObjectID', function (done) {
PostWithObjectId.create(function (err, post) {
PostWithObjectId.findById(post._id, function (err, post) {
should.not.exist(err);
post._id.should.be.an.instanceOf(db.ObjectID);
done();
});
});
});
it('should update the instance with `_id` as defined id', function (done) {
PostWithObjectId.create({title: 'a', content: 'AAA'}, function (err, post) {
post.title = 'b';
PostWithObjectId.updateOrCreate(post, function (err, p) {
should.not.exist(err);
p._id.should.be.equal(post._id);
PostWithObjectId.findById(post._id, function (err, p) {
should.not.exist(err);
p._id.should.be.eql(post._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
});
PostWithObjectId.find({where: {title: 'b'}}, function (err, posts) {
should.not.exist(err);
p = posts[0];
p._id.should.be.eql(post._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
posts.should.have.lengthOf(1);
done();
});
});
});
});
it('all should return object (with `_id` as defined id) with an _id instanceof ObjectID', function (done) {
var post = new PostWithObjectId({title: 'a', content: 'AAA'})
post.save(function (err, post) {
PostWithObjectId.all({where: {title: 'a'}}, function (err, posts) {
should.not.exist(err);
posts.should.have.lengthOf(1);
post = posts[0];
post.should.have.property('title', 'a');
post.should.have.property('content', 'AAA');
post._id.should.be.an.instanceOf(db.ObjectID);
done();
});
});
});
it('all return should honor filter.fields, with `_id` as defined id', function (done) {
var post = new PostWithObjectId({title: 'a', content: 'AAA'})
post.save(function (err, post) {
PostWithObjectId.all({fields: ['title'], where: {title: 'a'}}, function (err, posts) {
should.not.exist(err);
posts.should.have.lengthOf(1);
post = posts[0];
post.should.have.property('title', 'a');
post.should.not.have.property('content');
should.not.exist(post._id);
done();
});
});
});
it('hasMany should support additional conditions', function (done) {
User.create(function (e, u) {
u.posts.create({}, function (e, p) {
u.posts({where: {_id: p.id}}, function (err, posts) {
u.posts({where: {id: p.id}}, function (err, posts) {
should.not.exist(err);

@@ -56,7 +185,20 @@ posts.should.have.lengthOf(1);

it('create should return id field but not mongodb _id', function (done) {
Post.create(function (err, post) {
//console.log('create should', err, post);
should.not.exist(err);
should.exist(post.id);
should.not.exist(post._id);
done();
});
});
it('should allow to find by id string', function (done) {
Post.create(function (err, post) {
Post.find(post.id.toString(), function (err, post) {
Post.find(post.id.toString(), function (err, p) {
should.not.exist(err);
should.exist(post);
should.exist(p);

@@ -68,8 +210,23 @@ done();

it('find should return an object with an id, which is instanceof ObjectId', function (done) {
it('save should not return mongodb _id', function (done) {
Post.create(function (err, post) {
post.content = 'AAA';
post.save(function(err, p) {
should.not.exist(err)
should.not.exist(p._id);
p.id.should.be.equal(post.id);
p.content.should.be.equal('AAA')
done();
});
});
});
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.findById(post.id, function (err, post) {
should.not.exist(err);
post.id.should.be.an.instanceOf(db.ObjectID);
post._id.should.be.an.instanceOf(db.ObjectID);
should.not.exist(post._id);

@@ -89,7 +246,10 @@ done();

p.content.should.be.equal(post.content);
should.not.exist(p._id);
Post.findById(post.id, function (err, p) {
p.id.should.be.equal(post.id);
should.not.exist(p._id);
p.content.should.be.equal(post.content);
p.title.should.be.equal('b');
done();

@@ -102,3 +262,3 @@ });

it('all should return object with an id, which is instanceof ObjectID', function (done) {
it('all should return object with an id, which is instanceof ObjectID, but not mongodb _id', function (done) {
var post = new Post({title: 'a', content: 'AAA'})

@@ -113,3 +273,3 @@ post.save(function (err, post) {

post.id.should.be.an.instanceOf(db.ObjectID);
post._id.should.be.an.instanceOf(db.ObjectID);
should.not.exist(post._id);

@@ -122,3 +282,3 @@ done();

it('all should return honor filter.fields', function (done) {
it('all return should honor filter.fields', function (done) {
var post = new Post({title: 'b', content: 'BBB'})

@@ -132,2 +292,5 @@ post.save(function (err, post) {

post.should.not.have.property('content');
should.not.exist(post._id);
should.not.exist(post.id);
done();

@@ -145,3 +308,5 @@ });

should.not.exist(err);
should.not.exist(post._id);
post.id.should.be.equal(oid);
done();

@@ -203,6 +368,9 @@ });

User.destroyAll(function () {
Post.destroyAll(done);
Post.destroyAll(function () {
PostWithObjectId.destroyAll(function () {
PostWithNumberId.destroyAll(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