loopback-connector-mongodb
Advanced tools
@@ -145,2 +145,30 @@ /*! | ||
/*! | ||
* Convert the data from database to JSON | ||
* | ||
* @param {String} model The model name | ||
* @param {Object} data The data from DB | ||
*/ | ||
MongoDB.prototype.fromDatabase = function (model, data) { | ||
if (!data) { | ||
return null; | ||
} | ||
var props = this._models[model].properties; | ||
for (var p in props) { | ||
var prop = props[p]; | ||
if (prop && prop.type === Buffer) { | ||
if(data[p] instanceof mongodb.Binary) { | ||
// Convert the Binary into Buffer | ||
data[p] = data[p].read(0, data[p].length()); | ||
} | ||
} else if(prop && prop.type === String) { | ||
if(data[p] instanceof mongodb.Binary) { | ||
// Convert the Binary into String | ||
data[p] = data[p].toString(); | ||
} | ||
} | ||
} | ||
return data; | ||
}; | ||
/** | ||
@@ -260,2 +288,3 @@ * Create a new model instance for the given data | ||
data = self.fromDatabase(model, data); | ||
data && idName != '_id' && delete data._id; | ||
@@ -461,2 +490,4 @@ callback && callback(err, data); | ||
} | ||
o = self.fromDatabase(model, o); | ||
return o; | ||
@@ -463,0 +494,0 @@ }); |
{ | ||
"name": "loopback-connector-mongodb", | ||
"version": "1.2.0", | ||
"version": "1.2.1", | ||
"description": "LoopBack MongoDB Connector", | ||
@@ -5,0 +5,0 @@ "keywords": [ |
@@ -5,50 +5,4 @@ ## loopback-connector-mongodb | ||
## Usage | ||
Please see the full documentation at [docs.strongloop.com](http://docs.strongloop.com/display/DOC/MongoDB+connector). | ||
To use it you need `loopback-datasource-juggler@1.0.x`. | ||
1. Setup dependencies in `package.json`: | ||
```json | ||
{ | ||
... | ||
"dependencies": { | ||
"loopback-datasource-juggler": "1.0.x", | ||
"loopback-connector-mongodb": "1.0.x" | ||
}, | ||
... | ||
} | ||
``` | ||
2. Use: | ||
```javascript | ||
var DataSource = require('loopback-datasource-juggler').DataSource; | ||
var ds = new DataSource('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 | ||
@@ -62,4 +16,8 @@ | ||
The .loopbackrc file should be in JSON format, for example: | ||
**Note**: Tests and examples in this project configure the data source using the deprecated '.loopbackrc' file method, | ||
which is not suppored in general. | ||
For information on configuring the connector in a LoopBack application, please refer to [LoopBack documentation](http://docs.strongloop.com/display/DOC/MongoDB+connector). | ||
The .loopbackrc file is in JSON format, for example: | ||
{ | ||
@@ -66,0 +24,0 @@ "dev": { |
@@ -14,3 +14,4 @@ // This test written in mocha+should.js | ||
email: { type: String, index: true, unique: true }, | ||
age: Number | ||
age: Number, | ||
icon: Buffer | ||
}, { | ||
@@ -192,4 +193,11 @@ indexes: { | ||
it('should support Buffer type', function (done) { | ||
User.create({name: 'John', icon: new Buffer('1a2')}, function (e, u) { | ||
User.findById(u.id, function (e, user) { | ||
user.icon.should.be.an.instanceOf(Buffer); | ||
done(); | ||
}); | ||
}); | ||
}); | ||
it('hasMany should support additional conditions', function (done) { | ||
@@ -196,0 +204,0 @@ User.create(function (e, u) { |
63200
2.22%16
6.67%1237
6%52
-44.68%