Skip to content

Commit

Permalink
Merge branch 'release/1.1.8' into production
Browse files Browse the repository at this point in the history
  • Loading branch information
Raymond Feng committed Feb 27, 2014
2 parents 0e6233a + f0802c3 commit 2de839e
Show file tree
Hide file tree
Showing 4 changed files with 220 additions and 16 deletions.
27 changes: 27 additions & 0 deletions README.md
Expand Up @@ -27,6 +27,29 @@ To use it you need `loopback-datasource-juggler@1.0.x`.
...
```


### 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

By default, examples and tests from this module assume there is a MongoDB server
Expand Down Expand Up @@ -64,3 +87,7 @@ authentication enabled.
## Running tests

npm test

## Release notes

* 1.1.7 - Do not return MongoDB-specific _id to client API, except if specifically specified in the model definition
19 changes: 14 additions & 5 deletions lib/mongodb.js
Expand Up @@ -159,7 +159,7 @@ MongoDB.prototype.create = function (model, data, callback) {
} else {
var oid = ObjectID(idValue); // Is it an Object ID?
data._id = oid; // Set it to _id
delete data[idName];
idName != '_id' && delete data[idName];
}
this.collection(model).insert(data, {safe: true}, function (err, m) {
if (self.debug) {
Expand All @@ -178,6 +178,8 @@ MongoDB.prototype.create = function (model, data, callback) {
if (!isNaN(num)) {
idValue = num;
}
} else if (idType === ObjectID) {
idValue = ObjectID(idValue);
}
callback(err, err ? null : idValue);
});
Expand All @@ -199,11 +201,12 @@ MongoDB.prototype.save = function (model, data, callback) {

var oid = ObjectID(idValue);
data._id = oid;
delete data[idName];
idName != '_id' && delete data[idName];

this.collection(model).update({_id: oid}, data, {safe: true, upsert: true}, function (err, result) {
if (!err) {
self.setIdValue(model, data, idValue);
idName != '_id' && delete data._id;
}
if (self.debug) {
console.log('save.callback', model, err, result);
Expand Down Expand Up @@ -244,11 +247,14 @@ MongoDB.prototype.find = function find(model, id, callback) {
if (self.debug) {
console.log('find', model, id);
}
var idName = self.idName(model);
var oid = ObjectID(id);
this.collection(model).findOne({_id: oid}, function (err, data) {
if (self.debug) {
console.log('find.callback', model, id, err, data);
}

data && idName != '_id' && delete data._id;
callback && callback(err, data);
});
};
Expand All @@ -267,6 +273,7 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback
}

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

if (idValue === null || idValue === undefined) {
return this.create(data, callback);
Expand All @@ -284,6 +291,7 @@ MongoDB.prototype.updateOrCreate = function updateOrCreate(model, data, callback
}
if (id) {
self.setIdValue(model, data, id);
data && idName != '_id' && delete data._id;
callback(null, data);
} else {
callback(null, null); // wtf?
Expand Down Expand Up @@ -443,7 +451,7 @@ MongoDB.prototype.all = function all(model, filter, callback) {
self.setIdValue(model, o, o._id);
}
// Don't pass back _id if the fields is set
if (fields) {
if (fields || idName != '_id') {
delete o._id;
}
return o;
Expand Down Expand Up @@ -513,8 +521,8 @@ MongoDB.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
console.log('updateAttributes', model, id, data);
}
var oid = ObjectID(id);
delete data[this.idName(model)];

var idName = this.idName(model);
delete data[idName];
this.collection(model).findAndModify({_id: oid}, [
['_id', 'asc']
], {$set: data}, {}, function (err, object) {
Expand All @@ -526,6 +534,7 @@ MongoDB.prototype.updateAttributes = function updateAttrs(model, id, data, cb) {
err = 'No ' + model + ' found for id ' + id;
}
self.setIdValue(model, object, id);
object && idName != '_id' && delete object._id;
cb && cb(err, object);
});
};
Expand Down
2 changes: 1 addition & 1 deletion package.json
@@ -1,6 +1,6 @@
{
"name": "loopback-connector-mongodb",
"version": "1.1.7",
"version": "1.1.8",
"description": "LoopBack MongoDB Connector",
"keywords": [ "StrongLoop", "LoopBack", "MongoDB", "DataSource", "Connector" ],
"main": "index.js",
Expand Down

0 comments on commit 2de839e

Please sign in to comment.