strongloop/loopback-connector-mongodb — pull request #19 by pandaiolo (2014-02-23 18:08:27+00:00)
Title: Do not return MongoDB-specific _id to client API, except if specified in model definition

As discussed in the Google Group, here is a PR that removes mongodb-specific `_id` from API exposition.

The main issue about this, other than having client-side model having to deal with unexpected `_id` attribute, is that when making a PUT with the same object, hence with `_id` included, it triggers a MongoDB exception, because it does not accept modifications of `_id` property.

After this commit, only the ID specified in the model definition will be returned, beeing `id` or another specified name.

**Note** : As explained in the `README.md`, I added the possibility to still rely on MongoDB `_id` attribute in the loopback model, as I suspect that some might need it, but in this case, it needs to be explicitly defined in the model definition, with `ObjectID` type. Example :

var ds = app.dataSources.db;
MyModel = ds.createModel('mymodel', {_id: ds.ObjectID});

This possibility required to add a property setter for the property named `_id`, because the default behaviour of the property setter in the model builder is to use the type as a caster :

property = DataType(value);

The main problem is that MongoDB javascript BSON ObjectID type cannot cast itself ! It will throw an error.

The custom MongoDB connector setter will take care of this issue by calling ObjectID only if value is not already instance of it.

I implemented this setter in two model definition hooks : `Connector.prototype.define` and `Connector.prototype.defineProperty`. If there are additional ways to set the ID property, they also should be hooked to in order to enable the custom setter.

I also added a bunch of tests to double check that all behaviour work with models with `_id` or other type of IDs, and updated `README.md`